@@ -4,7 +4,7 @@ import type { RuleListener } from "@typescript-eslint/utils/ts-eslint";
44import type { CamelCase } from "string-ts" ;
55
66import { AST_NODE_TYPES as T } from "@typescript-eslint/types" ;
7- import { createRule } from "../utils" ;
7+ import { createJsxElementResolver , createRule , resolveAttribute } from "../utils" ;
88
99export const RULE_NAME = "no-string-style-prop" ;
1010
@@ -30,33 +30,23 @@ export default createRule<[], MessageID>({
3030} ) ;
3131
3232export function create ( context : RuleContext < MessageID , [ ] > ) : RuleListener {
33+ const resolver = createJsxElementResolver ( context ) ;
3334 return {
34- JSXAttribute ( node ) {
35- if ( node . name . name !== "style" ) return ;
36- const styleText = getAttributeValueText ( context , node . value ) ;
37- if ( styleText == null ) return ;
35+ JSXElement ( node ) {
36+ const { attributes } = resolver . resolve ( node ) ;
37+ const {
38+ attribute,
39+ attributeName,
40+ attributeValue,
41+ attributeValueString,
42+ } = resolveAttribute ( context , attributes , node , "style" ) ;
43+ if ( attributeName !== "style" ) return ;
44+ if ( attribute == null || attributeValue ?. node == null ) return ;
45+ if ( attributeValueString == null ) return ;
3846 context . report ( {
3947 messageId : "noStringStyleProp" ,
40- node,
48+ node : attributeValue . node ,
4149 } ) ;
4250 } ,
4351 } ;
4452}
45-
46- function getAttributeValueText ( context : RuleContext , node : TSESTree . JSXAttribute [ "value" ] ) {
47- if ( node == null ) return null ;
48- switch ( true ) {
49- case node . type === T . Literal
50- && typeof node . value === "string" :
51- return context . sourceCode . getText ( node ) ;
52- case node . type === T . JSXExpressionContainer
53- && node . expression . type === T . Literal
54- && typeof node . expression . value === "string" :
55- return context . sourceCode . getText ( node . expression ) ;
56- case node . type === T . JSXExpressionContainer
57- && node . expression . type === T . TemplateLiteral :
58- return context . sourceCode . getText ( node . expression ) ;
59- default :
60- return null ;
61- }
62- }
0 commit comments