@@ -18,25 +18,6 @@ export const meta = {
1818} ;
1919
2020export 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