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

Commit e1276bb

Browse files
committed
fix(parser): hanlde more cases
1 parent f81b6fb commit e1276bb

File tree

2 files changed

+47
-23
lines changed

2 files changed

+47
-23
lines changed

src/parse.ts

+34-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Parser as HTMLParser } from 'htmlparser2'
22
import { parse } from '@babel/parser'
3-
import { PrivateName, Expression, Statement, SpreadElement } from '@babel/types'
3+
import { PrivateName, Expression, Statement, SpreadElement, Node } from '@babel/types'
44
import { camelize, capitalize, isHTMLTag, isSVGTag, isVoidTag } from '@vue/shared'
55
import { ParseResult, TagMeta } from './types'
66

@@ -133,32 +133,41 @@ export function getIdentifiersDeclaration(nodes: Statement[], identifiers = new
133133
identifiers.add(specifier.local.name)
134134
}
135135
else if (node.type === 'VariableDeclaration') {
136-
for (const declarator of node.declarations) {
137-
if (declarator.id.type === 'Identifier') {
138-
identifiers.add(declarator.id.name)
136+
function handleVariableId(node: Node) {
137+
if (node.type === 'Identifier') {
138+
identifiers.add(node.name)
139139
}
140-
else if (declarator.id.type === 'ObjectPattern') {
141-
for (const property of declarator.id.properties) {
142-
if (property.type === 'ObjectProperty' && property.key.type === 'Identifier')
143-
identifiers.add(property.key.name)
140+
else if (node.type === 'ObjectPattern') {
141+
for (const property of node.properties) {
142+
if (property.type === 'ObjectProperty')
143+
handleVariableId(property.value)
144144
else if (property.type === 'RestElement' && property.argument.type === 'Identifier')
145145
identifiers.add(property.argument.name)
146146
}
147147
}
148-
else if (declarator.id.type === 'ArrayPattern') {
149-
for (const element of declarator.id.elements) {
148+
else if (node.type === 'ArrayPattern') {
149+
for (const element of node.elements) {
150150
if (element?.type === 'Identifier')
151151
identifiers.add(element.name)
152152
else if (element?.type === 'RestElement' && element.argument.type === 'Identifier')
153153
identifiers.add(element.argument.name)
154+
else if (element?.type === 'ObjectPattern' || element?.type === 'ArrayPattern')
155+
handleVariableId(element)
154156
}
155157
}
156158
}
159+
160+
for (const declarator of node.declarations)
161+
handleVariableId(declarator.id)
157162
}
158163
else if (node.type === 'FunctionDeclaration') {
159164
if (node.id)
160165
identifiers.add(node.id.name)
161166
}
167+
else if (node.type === 'ClassDeclaration') {
168+
if (node.id)
169+
identifiers.add(node.id.name)
170+
}
162171
// else {
163172
// console.log(node)
164173
// }
@@ -180,11 +189,9 @@ export function getIdentifiersUsage(node?: Expression | SpreadElement | PrivateN
180189
getIdentifiersUsage(node.object, identifiers)
181190
}
182191
else if (node.type === 'CallExpression') {
183-
// @ts-expect-error
184-
getIdentifiersUsage(node.callee, identifiers)
192+
getIdentifiersUsage(node.callee as Expression, identifiers)
185193
node.arguments.forEach((arg) => {
186-
// @ts-expect-error
187-
getIdentifiersUsage(arg, identifiers)
194+
getIdentifiersUsage(arg as Expression, identifiers)
188195
})
189196
}
190197
else if (node.type === 'BinaryExpression' || node.type === 'LogicalExpression') {
@@ -204,10 +211,14 @@ export function getIdentifiersUsage(node?: Expression | SpreadElement | PrivateN
204211
}
205212
else if (node.type === 'ObjectExpression') {
206213
node.properties.forEach((prop) => {
207-
if (prop.type === 'ObjectProperty')
208-
getIdentifiersUsage(prop.key, identifiers)
209-
else if (prop.type === 'SpreadElement')
214+
if (prop.type === 'ObjectProperty') {
215+
if (prop.computed)
216+
getIdentifiersUsage(prop.key, identifiers)
217+
getIdentifiersUsage(prop.value as Expression, identifiers)
218+
}
219+
else if (prop.type === 'SpreadElement') {
210220
getIdentifiersUsage(prop, identifiers)
221+
}
211222
})
212223
}
213224
else if (node.type === 'ArrayExpression') {
@@ -218,6 +229,12 @@ export function getIdentifiersUsage(node?: Expression | SpreadElement | PrivateN
218229
else if (node.type === 'SpreadElement') {
219230
getIdentifiersUsage(node.argument, identifiers)
220231
}
232+
else if (node.type === 'NewExpression') {
233+
getIdentifiersUsage(node.callee as Expression, identifiers)
234+
node.arguments.forEach((arg) => {
235+
getIdentifiersUsage(arg as Expression, identifiers)
236+
})
237+
}
221238
// else {
222239
// console.log(node)
223240
// }

test/parse.test.ts

+13-6
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ describe('parse', () => {
1010
['import * as foo from "z"', ['foo']],
1111
['function foo(bar) {const a = z}', ['foo']],
1212
['console.log(foo)', []],
13-
['const { data } = toRefs(state)', ['data']],
13+
['var { data } = toRefs(state)', ['data']],
1414
['const { data, ...args } = bar', ['data', 'args']],
15-
['const { foo: bar } = bar', ['foo']],
15+
['const { foo: bar } = bar', ['bar']],
16+
['const { foo: { a, b: c, d: { e: [f] } } } = { bar }', ['a', 'c', 'f']],
1617
['let [a, b,, ...c] = bar', ['a', 'b', 'c']],
18+
['let [a, b, [c, {d}],, ...e] = bar', ['a', 'b', 'c', 'd', 'e']],
19+
['class A extends B {}', ['A']],
1720
]
1821

1922
for (const [input, output] of cases) {
@@ -31,15 +34,19 @@ describe('parse', () => {
3134
const cases: [string, string[]][] = [
3235
['foo', ['foo']],
3336
['foo.bar', ['foo']],
34-
['foo(bar, console.log)', ['foo', 'bar', 'console']],
37+
['foo(bar, console.log, ...args)', ['foo', 'bar', 'console', 'args']],
38+
['foo(bar())', ['foo', 'bar']],
3539
['for (let x in foo) {}', ['foo']],
3640
['for (let [x, idx] of foo) {}', ['foo']],
3741
['a + b', ['a', 'b']],
3842
['a ? "" : b < c', ['a', 'b', 'c']],
39-
['a == b && a === c', ['a', 'b', 'c']],
40-
['({ a, b, ...args, [c]: 1 })', ['a', 'b', 'args', 'c']],
43+
['a == b && a === c || d != e', ['a', 'b', 'c', 'd', 'e']],
44+
['({ a, b, ...args, [c]: 1, d: e, f: { g } })', ['a', 'b', 'args', 'c', 'e', 'g']],
4145
['!a', ['a']],
42-
['[a,b,...args]', ['a', 'b', 'args']],
46+
['!!c', ['c']],
47+
['[a,b,[c,{d}],...args]', ['a', 'b', 'c', 'd', 'args']],
48+
['new Foo(a,[b])', ['Foo', 'a', 'b']],
49+
['new RC.Foo()', ['RC']],
4350
]
4451

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

0 commit comments

Comments
 (0)