Skip to content

Commit 402bdd4

Browse files
committed
fix(prefer-to-have-style): remove autofix for computed access of el.style
1 parent 99f044c commit 402bdd4

File tree

2 files changed

+0
-88
lines changed

2 files changed

+0
-88
lines changed

src/__tests__/lib/rules/prefer-to-have-style.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,14 @@ ruleTester.run("prefer-to-have-style", rule, {
7474
{
7575
code: `expect(screen.getByTestId("foo").style["scroll-snap-type"]).toBe("x mandatory")`,
7676
errors,
77-
output: `expect(screen.getByTestId("foo")).toHaveStyle({scrollSnapType: "x mandatory"})`,
7877
},
7978
{
8079
code: 'expect(el.style["scroll-snap-type"]).toBe(`${x} mandatory`)',
8180
errors,
82-
output: "expect(el).toHaveStyle({scrollSnapType: `${x} mandatory`})",
8381
},
8482
{
8583
code: `expect(el.style["scroll-snap-type"]).not.toBe("x mandatory")`,
8684
errors,
87-
output: `expect(el).not.toHaveStyle({scrollSnapType: "x mandatory"})`,
8885
},
8986
{
9087
code: `expect(el.style).toContain("background-color")`,
@@ -114,37 +111,30 @@ ruleTester.run("prefer-to-have-style", rule, {
114111
{
115112
code: `expect(imageElement.style[\`box-shadow\`]).toBe(\`inset 0px 0px 0px 400px \${c}\`)`,
116113
errors,
117-
output: `expect(imageElement).toHaveStyle(\`box-shadow: inset 0px 0px 0px 400px \${c}\`)`,
118114
},
119115
{
120116
code: `expect(imageElement.style[\`box-shadow\` ]).toBe( \`inset 0px 0px 0px 400px \${c}\`)`,
121117
errors,
122-
output: `expect(imageElement).toHaveStyle( \`box-shadow: inset 0px 0px 0px 400px \${c}\`)`,
123118
},
124119
{
125120
code: `expect(imageElement.style[\`box-\${shadow}\`]).toBe("inset 0px 0px 0px 400px 40px")`,
126121
errors,
127-
output: `expect(imageElement).toHaveStyle(\`box-\${shadow}: inset 0px 0px 0px 400px 40px\`)`,
128122
},
129123
{
130124
code: `expect(imageElement.style[\`box-shadow\`]).not.toBe(\`inset 0px 0px 0px 400px \${c}\`)`,
131125
errors,
132-
output: `expect(imageElement).not.toHaveStyle(\`box-shadow: inset 0px 0px 0px 400px \${c}\`)`,
133126
},
134127
{
135128
code: `expect(imageElement.style[\`box-shadow\`]).not.toBe("inset 0px 0px 0px 400px 40px")`,
136129
errors,
137-
output: `expect(imageElement).not.toHaveStyle(\`box-shadow: inset 0px 0px 0px 400px 40px\`)`,
138130
},
139131
{
140132
code: `expect(element.style[1]).toEqual('padding');`,
141133
errors,
142-
output: `expect(element).toHaveStyle({padding: expect.anything()});`,
143134
},
144135
{
145136
code: `expect(element.style[1]).toBe(\`padding\`);`,
146137
errors,
147-
output: `expect(element).toHaveStyle({[\`padding\`]: expect.anything()});`,
148138
},
149139
{
150140
code: `expect(element.style[1]).not.toEqual('padding');`,
@@ -157,7 +147,6 @@ ruleTester.run("prefer-to-have-style", rule, {
157147
{
158148
code: `expect(element.style[1]).toBe(x);`,
159149
errors,
160-
output: `expect(element).toHaveStyle({[x]: expect.anything()});`,
161150
},
162151
{
163152
code: `expect(element.style[0]).toBe(1);`,

src/rules/prefer-to-have-style.js

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,6 @@ export const meta = {
1818
};
1919

2020
export const create = (context) => {
21-
function getReplacementObjectProperty(styleName) {
22-
if (styleName.type === "Literal") {
23-
return camelCase(styleName.value);
24-
}
25-
26-
return `[${context.getSourceCode().getText(styleName)}]`;
27-
}
28-
function getReplacementStyleParam(styleName, styleValue) {
29-
return styleName.type === "Literal"
30-
? `{${camelCase(styleName.value)}: ${context
31-
.getSourceCode()
32-
.getText(styleValue)}}`
33-
: `${context.getSourceCode().getText(styleName).slice(0, -1)}: ${
34-
styleValue.type === "TemplateLiteral"
35-
? context.getSourceCode().getText(styleValue).substring(1)
36-
: `${styleValue.value}\``
37-
}`;
38-
}
39-
4021
return {
4122
//expect(el.style.foo).toBe("bar");
4223
[`MemberExpression[property.name=style][parent.computed=false][parent.parent.parent.property.name=/toBe$|to(Strict)?Equal/][parent.parent.parent.parent.arguments.0.type=/(Template)?Literal/][parent.parent.callee.name=expect]`](
@@ -158,76 +139,18 @@ export const create = (context) => {
158139
[`MemberExpression[property.name=style][parent.computed=true][parent.parent.parent.property.name=/toBe$|to(Strict)?Equal/][parent.parent.parent.parent.arguments.0.type=/((Template)?Literal|Identifier)/][parent.parent.callee.name=expect]`](
159140
node
160141
) {
161-
const styleName = node.parent.property;
162-
const [styleValue] = node.parent.parent.parent.parent.arguments;
163-
const matcher = node.parent.parent.parent.property;
164-
const startOfStyleMemberExpression = node.object.range[1];
165-
const endOfStyleMemberExpression =
166-
node.parent.parent.arguments[0].range[1];
167-
168-
let fix = null;
169-
170-
if (
171-
typeof styleValue.value !== "number" &&
172-
!(styleValue.value instanceof RegExp)
173-
) {
174-
fix = (fixer) => {
175-
return [
176-
fixer.removeRange([
177-
startOfStyleMemberExpression,
178-
endOfStyleMemberExpression,
179-
]),
180-
fixer.replaceText(matcher, "toHaveStyle"),
181-
fixer.replaceText(
182-
styleValue,
183-
typeof styleName.value === "number"
184-
? `{${getReplacementObjectProperty(
185-
styleValue
186-
)}: expect.anything()}`
187-
: getReplacementStyleParam(styleName, styleValue)
188-
),
189-
];
190-
};
191-
}
192-
193142
context.report({
194143
node: node.property,
195144
message: "Use toHaveStyle instead of asserting on element style",
196-
fix,
197145
});
198146
},
199147
//expect(el.style["foo-bar"]).not.toBe("baz")
200148
[`MemberExpression[property.name=style][parent.computed=true][parent.parent.parent.property.name=not][parent.parent.parent.parent.parent.callee.property.name=/toBe$|to(Strict)?Equal/][parent.parent.parent.parent.parent.arguments.0.type=/(Template)?Literal/][parent.parent.callee.name=expect]`](
201149
node
202150
) {
203-
const styleName = node.parent.property;
204-
const [styleValue] = node.parent.parent.parent.parent.parent.arguments;
205-
const matcher = node.parent.parent.parent.parent.property;
206-
const endOfStyleMemberExpression =
207-
node.parent.parent.arguments[0].range[1];
208-
209-
let fix = null;
210-
211-
if (typeof styleName.value !== "number") {
212-
fix = (fixer) => {
213-
return [
214-
fixer.removeRange([
215-
node.object.range[1],
216-
endOfStyleMemberExpression,
217-
]),
218-
fixer.replaceText(matcher, "toHaveStyle"),
219-
fixer.replaceText(
220-
styleValue,
221-
getReplacementStyleParam(styleName, styleValue)
222-
),
223-
];
224-
};
225-
}
226-
227151
context.report({
228152
node: node.property,
229153
message: "Use toHaveStyle instead of asserting on element style",
230-
fix,
231154
});
232155
},
233156
//expect(foo.style).toHaveProperty("foo", "bar")

0 commit comments

Comments
 (0)