Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

Commit bf3dfee

Browse files
committed
fix: detection usages in inline functions, close #15
1 parent e98500d commit bf3dfee

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/core/identifiers.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ export function getIdentifierUsages(node?: Expression | SpreadElement | PrivateN
5353
if (!node)
5454
return identifiers
5555

56-
if (node.type === 'ExpressionStatement') {
56+
if (node.type === 'BlockStatement') {
57+
node.body.forEach(child => getIdentifierUsages(child, identifiers))
58+
}
59+
else if (node.type === 'ExpressionStatement') {
5760
getIdentifierUsages(node.expression, identifiers)
5861
}
5962
else if (node.type === 'Identifier') {
@@ -109,6 +112,12 @@ export function getIdentifierUsages(node?: Expression | SpreadElement | PrivateN
109112
getIdentifierUsages(arg as Expression, identifiers)
110113
})
111114
}
115+
else if (node.type === 'ArrowFunctionExpression' || node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression') {
116+
getIdentifierUsages(node.body, identifiers)
117+
}
118+
else if (node.type === 'ReturnStatement') {
119+
getIdentifierUsages(node.argument, identifiers)
120+
}
112121
// else {
113122
// console.log(node)
114123
// }

test/identifiers.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ describe('identifiers', () => {
4747
['[a,b,[c,{d}],...args]', ['a', 'b', 'c', 'd', 'args']],
4848
['new Foo(a,[b])', ['Foo', 'a', 'b']],
4949
['new RC.Foo()', ['RC']],
50+
['() => foo(bar)', ['foo', 'bar']],
51+
['() => { foo() + bar; a }', ['foo', 'bar', 'a']],
52+
['(function () { foo() + bar })', ['foo', 'bar']],
53+
['function foobar() { return foo() + bar }', ['foo', 'bar']],
5054
]
5155

5256
for (const [input, output] of cases) {

0 commit comments

Comments
 (0)