Skip to content

Commit 8f39db1

Browse files
committed
fix(prefer-to-have-style): handle regexp literals
1 parent 186ae50 commit 8f39db1

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ ruleTester.run("prefer-to-have-style", rule, {
1414
`expect(el.style).toEqual(foo)`,
1515
`expect(el.style[1]).toEqual([])`,
1616
`expect(el.style[1]).toEqual({})`,
17+
`expect(element.style[0]).toBe(new RegExp('reg'));`,
1718
`expect(el).toHaveAttribute("style")`,
1819
`React.useLayoutEffect(() => {
1920
if (foo) {
@@ -162,5 +163,9 @@ ruleTester.run("prefer-to-have-style", rule, {
162163
code: `expect(element.style[0]).toBe(1);`,
163164
errors,
164165
},
166+
{
167+
code: `expect(element.style[0]).toBe(/RegExp/);`,
168+
errors,
169+
},
165170
],
166171
});

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,10 @@ export const create = (context) => {
167167

168168
let fix = null;
169169

170-
if (typeof styleValue.value !== "number") {
170+
if (
171+
typeof styleValue.value !== "number" &&
172+
!(styleValue.value instanceof RegExp)
173+
) {
171174
fix = (fixer) => {
172175
return [
173176
fixer.removeRange([
@@ -249,9 +252,9 @@ export const create = (context) => {
249252
fixer.replaceText(matcher, "toHaveStyle"),
250253
fixer.replaceTextRange(
251254
[styleName.range[0], styleValue.range[1]],
252-
`{${camelCase(
253-
styleName.value
254-
)}: ${context.getSourceCode().getText(styleValue)}}`
255+
`{${camelCase(styleName.value)}: ${context
256+
.getSourceCode()
257+
.getText(styleValue)}}`
255258
),
256259
];
257260
},
@@ -262,10 +265,8 @@ export const create = (context) => {
262265
[`MemberExpression[property.name=style][parent.parent.property.name=not][parent.parent.parent.property.name=toHaveProperty][parent.callee.name=expect]`](
263266
node
264267
) {
265-
const [
266-
styleName,
267-
styleValue,
268-
] = node.parent.parent.parent.parent.arguments;
268+
const [styleName, styleValue] =
269+
node.parent.parent.parent.parent.arguments;
269270
const matcher = node.parent.parent.parent.property;
270271

271272
context.report({
@@ -283,9 +284,9 @@ export const create = (context) => {
283284
fixer.replaceText(matcher, "toHaveStyle"),
284285
fixer.replaceTextRange(
285286
[styleName.range[0], styleValue.range[1]],
286-
`{${camelCase(
287-
styleName.value
288-
)}: ${context.getSourceCode().getText(styleValue)}}`
287+
`{${camelCase(styleName.value)}: ${context
288+
.getSourceCode()
289+
.getText(styleValue)}}`
289290
),
290291
];
291292
},

0 commit comments

Comments
 (0)