|
1 | | -import { getInvalidTokens, isPandaAttribute, isPandaIsh, isPandaProp } from '../utils/helpers' |
| 1 | +import { getInvalidTokens, getTaggedTemplateCaller, isPandaAttribute, isPandaIsh, isPandaProp } from '../utils/helpers' |
2 | 2 | import { type Rule, createRule } from '../utils' |
3 | 3 | import { AST_NODE_TYPES } from '@typescript-eslint/utils' |
4 | 4 | import { isNodeOfTypes } from '@typescript-eslint/utils/ast-utils' |
5 | | -import { |
6 | | - isCallExpression, |
7 | | - isIdentifier, |
8 | | - isJSXExpressionContainer, |
9 | | - isLiteral, |
10 | | - isMemberExpression, |
11 | | - isTemplateLiteral, |
12 | | - type Node, |
13 | | -} from '../utils/nodes' |
| 5 | +import { isIdentifier, isJSXExpressionContainer, isLiteral, isTemplateLiteral, type Node } from '../utils/nodes' |
14 | 6 |
|
15 | 7 | export const RULE_NAME = 'no-invalid-token-paths' |
16 | 8 |
|
@@ -78,51 +70,35 @@ const rule: Rule = createRule({ |
78 | 70 | }, |
79 | 71 |
|
80 | 72 | TaggedTemplateExpression(node) { |
81 | | - const handleExpression = (caller: string) => { |
82 | | - if (!isPandaIsh(caller, context)) return |
| 73 | + const caller = getTaggedTemplateCaller(node) |
| 74 | + if (!caller) return |
83 | 75 |
|
84 | | - const quasis = node.quasi.quasis[0] |
85 | | - const styles = quasis.value.raw |
86 | | - const tokens = getInvalidTokens(styles, context) |
| 76 | + if (!isPandaIsh(caller, context)) return |
87 | 77 |
|
88 | | - tokens.forEach((token, i, arr) => { |
89 | | - // Avoid duplicate reports on the same token |
90 | | - if (arr.indexOf(token) < i) return |
| 78 | + const quasis = node.quasi.quasis[0] |
| 79 | + const styles = quasis.value.raw |
| 80 | + const tokens = getInvalidTokens(styles, context) |
91 | 81 |
|
92 | | - let index = styles.indexOf(token) |
| 82 | + tokens.forEach((token, i, arr) => { |
| 83 | + // Avoid duplicate reports on the same token |
| 84 | + if (arr.indexOf(token) < i) return |
93 | 85 |
|
94 | | - while (index !== -1) { |
95 | | - const start = quasis.range[0] + 1 + index |
96 | | - const end = start + token.length |
| 86 | + let index = styles.indexOf(token) |
97 | 87 |
|
98 | | - context.report({ |
99 | | - loc: { start: context.sourceCode.getLocFromIndex(start), end: context.sourceCode.getLocFromIndex(end) }, |
100 | | - messageId: 'noInvalidTokenPaths', |
101 | | - data: { token }, |
102 | | - }) |
| 88 | + while (index !== -1) { |
| 89 | + const start = quasis.range[0] + 1 + index |
| 90 | + const end = start + token.length |
103 | 91 |
|
104 | | - // Check for other occurences of the invalid token |
105 | | - index = styles.indexOf(token, index + 1) |
106 | | - } |
107 | | - }) |
108 | | - } |
109 | | - |
110 | | - // css`` |
111 | | - if (isIdentifier(node.tag)) { |
112 | | - handleExpression(node.tag.name) |
113 | | - } |
114 | | - |
115 | | - // styled.h1`` |
116 | | - if (isMemberExpression(node.tag)) { |
117 | | - if (!isIdentifier(node.tag.object)) return |
118 | | - handleExpression(node.tag.object.name) |
119 | | - } |
120 | | - |
121 | | - // styled(Comp)`` |
122 | | - if (isCallExpression(node.tag)) { |
123 | | - if (!isIdentifier(node.tag.callee)) return |
124 | | - handleExpression(node.tag.callee.name) |
125 | | - } |
| 92 | + context.report({ |
| 93 | + loc: { start: context.sourceCode.getLocFromIndex(start), end: context.sourceCode.getLocFromIndex(end) }, |
| 94 | + messageId: 'noInvalidTokenPaths', |
| 95 | + data: { token }, |
| 96 | + }) |
| 97 | + |
| 98 | + // Check for other occurences of the invalid token |
| 99 | + index = styles.indexOf(token, index + 1) |
| 100 | + } |
| 101 | + }) |
126 | 102 | }, |
127 | 103 | } |
128 | 104 | }, |
|
0 commit comments