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

Commit 1e2db1b

Browse files
committed
fix: scan for template literal, close #37
1 parent c52503f commit 1e2db1b

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/core/identifiers.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PrivateName, Expression, Statement, SpreadElement, Node } from '@babel/types'
1+
import { PrivateName, Expression, Statement, SpreadElement, Node, TSType } from '@babel/types'
22

33
export function getIdentifierDeclarations(nodes: Statement[], identifiers = new Set<string>()) {
44
for (let node of nodes) {
@@ -54,7 +54,7 @@ export function getIdentifierDeclarations(nodes: Statement[], identifiers = new
5454
return identifiers
5555
}
5656

57-
export function getIdentifierUsages(node?: Expression | SpreadElement | PrivateName | Statement | null, identifiers = new Set<string>()) {
57+
export function getIdentifierUsages(node?: Expression | TSType | SpreadElement | PrivateName | Statement | null, identifiers = new Set<string>()) {
5858
if (!node)
5959
return identifiers
6060

@@ -114,6 +114,9 @@ export function getIdentifierUsages(node?: Expression | SpreadElement | PrivateN
114114
else if (node.type === 'ArrowFunctionExpression' || node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression') {
115115
getIdentifierUsages(node.body, identifiers)
116116
}
117+
else if (node.type === 'TemplateLiteral') {
118+
node.expressions.forEach(expr => getIdentifierUsages(expr, identifiers))
119+
}
117120
// else {
118121
// console.log(node)
119122
// }

test/identifiers.test.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-template-curly-in-string */
12
import { parse } from '@babel/parser'
23
import { getIdentifierDeclarations, getIdentifierUsages } from '../src/core/identifiers'
34

@@ -51,6 +52,8 @@ describe('identifiers', () => {
5152
['() => { foo() + bar; a }', ['foo', 'bar', 'a']],
5253
['(function () { foo() + bar })', ['foo', 'bar']],
5354
['function foobar() { return foo() + bar }', ['foo', 'bar']],
55+
['`${foo}bar`', ['foo']],
56+
['`${foo(zag)}` + bar', ['foo', 'zag', 'bar']],
5457
]
5558

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

0 commit comments

Comments
 (0)