Skip to content

Commit 86af287

Browse files
committed
Improve local styled factory parsing
1 parent 1c8a658 commit 86af287

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

plugin/src/utils/helpers.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { RuleContext } from '@typescript-eslint/utils/ts-eslint'
22
import type { TSESTree } from '@typescript-eslint/utils'
3+
import { analyze } from '@typescript-eslint/scope-manager'
34
import { type ImportResult, syncAction } from '.'
45
import {
56
isCallExpression,
@@ -12,7 +13,7 @@ import {
1213
isJSXMemberExpression,
1314
isJSXOpeningElement,
1415
isMemberExpression,
15-
isVariableDeclaration,
16+
isVariableDeclarator,
1617
type Node,
1718
} from './nodes'
1819

@@ -74,12 +75,13 @@ const isPandaIsh = (name: string, context: RuleContext<any, any>) => {
7475
}
7576

7677
const findDeclaration = (name: string, context: RuleContext<any, any>) => {
77-
let decl: TSESTree.VariableDeclarator | undefined
78-
context.sourceCode.ast.body.forEach((node) => {
79-
if (!isVariableDeclaration(node)) return
80-
decl = node.declarations.find((decl) => isIdentifier(decl.id) && decl.id.name === name)
78+
const scope = analyze(context.sourceCode.ast, {
79+
sourceType: 'module',
8180
})
82-
return decl
81+
const decl = scope.variables
82+
.find((v) => v.name === name)
83+
?.defs.find((d) => isIdentifier(d.name) && d.name.name === name)?.node
84+
if (isVariableDeclarator(decl)) return decl
8385
}
8486

8587
const isLocalStyledFactory = (node: TSESTree.JSXOpeningElement, context: RuleContext<any, any>) => {

plugin/src/utils/nodes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export const isTemplateLiteral = isNodeOfType(AST_NODE_TYPES.TemplateLiteral)
1313

1414
export const isMemberExpression = isNodeOfType(AST_NODE_TYPES.MemberExpression)
1515

16+
export const isVariableDeclarator = isNodeOfType(AST_NODE_TYPES.VariableDeclarator)
17+
1618
export const isVariableDeclaration = isNodeOfType(AST_NODE_TYPES.VariableDeclaration)
1719

1820
export const isJSXMemberExpression = isNodeOfType(AST_NODE_TYPES.JSXMemberExpression)

plugin/test/no-debug.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const valids = [
1111
'const randomFunc = f({ debug: true })',
1212
'<NonPandaComponent debug={true} />',
1313
'<NonPandaComponent debug={true}>content</NonPandaComponent>',
14-
`const a = 1; const PandaComp = styled(div); <PandaComp someProp={{ debug: true }} />`,
14+
`const PandaComp = styled(div); function App(){ const a = 1; return (<PandaComp someProp={{ debug: true }} />)}`,
1515
]
1616

1717
const invalids = [
@@ -37,6 +37,10 @@ const invalids = [
3737
code: `const PandaComp = styled(div); <PandaComp css={{ debug: true }} />`,
3838
output: 'const PandaComp = styled(div); <PandaComp css={{ }} />',
3939
},
40+
{
41+
code: `function App(){ const PandaComp = styled(div); return (<PandaComp css={{ debug: true }} />)}`,
42+
output: `function App(){ const PandaComp = styled(div); return (<PandaComp css={{ }} />)}`,
43+
},
4044
]
4145

4246
tester.run(RULE_NAME, rule as any, {

0 commit comments

Comments
 (0)