Skip to content

Commit

Permalink
fix(parser): hanlde more cases
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Aug 22, 2021
1 parent f81b6fb commit e1276bb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 23 deletions.
51 changes: 34 additions & 17 deletions src/parse.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Parser as HTMLParser } from 'htmlparser2'
import { parse } from '@babel/parser'
import { PrivateName, Expression, Statement, SpreadElement } from '@babel/types'
import { PrivateName, Expression, Statement, SpreadElement, Node } from '@babel/types'
import { camelize, capitalize, isHTMLTag, isSVGTag, isVoidTag } from '@vue/shared'
import { ParseResult, TagMeta } from './types'

Expand Down Expand Up @@ -133,32 +133,41 @@ export function getIdentifiersDeclaration(nodes: Statement[], identifiers = new
identifiers.add(specifier.local.name)
}
else if (node.type === 'VariableDeclaration') {
for (const declarator of node.declarations) {
if (declarator.id.type === 'Identifier') {
identifiers.add(declarator.id.name)
function handleVariableId(node: Node) {
if (node.type === 'Identifier') {
identifiers.add(node.name)
}
else if (declarator.id.type === 'ObjectPattern') {
for (const property of declarator.id.properties) {
if (property.type === 'ObjectProperty' && property.key.type === 'Identifier')
identifiers.add(property.key.name)
else if (node.type === 'ObjectPattern') {
for (const property of node.properties) {
if (property.type === 'ObjectProperty')
handleVariableId(property.value)
else if (property.type === 'RestElement' && property.argument.type === 'Identifier')
identifiers.add(property.argument.name)
}
}
else if (declarator.id.type === 'ArrayPattern') {
for (const element of declarator.id.elements) {
else if (node.type === 'ArrayPattern') {
for (const element of node.elements) {
if (element?.type === 'Identifier')
identifiers.add(element.name)
else if (element?.type === 'RestElement' && element.argument.type === 'Identifier')
identifiers.add(element.argument.name)
else if (element?.type === 'ObjectPattern' || element?.type === 'ArrayPattern')
handleVariableId(element)
}
}
}

for (const declarator of node.declarations)
handleVariableId(declarator.id)
}
else if (node.type === 'FunctionDeclaration') {
if (node.id)
identifiers.add(node.id.name)
}
else if (node.type === 'ClassDeclaration') {
if (node.id)
identifiers.add(node.id.name)
}
// else {
// console.log(node)
// }
Expand All @@ -180,11 +189,9 @@ export function getIdentifiersUsage(node?: Expression | SpreadElement | PrivateN
getIdentifiersUsage(node.object, identifiers)
}
else if (node.type === 'CallExpression') {
// @ts-expect-error
getIdentifiersUsage(node.callee, identifiers)
getIdentifiersUsage(node.callee as Expression, identifiers)
node.arguments.forEach((arg) => {
// @ts-expect-error
getIdentifiersUsage(arg, identifiers)
getIdentifiersUsage(arg as Expression, identifiers)
})
}
else if (node.type === 'BinaryExpression' || node.type === 'LogicalExpression') {
Expand All @@ -204,10 +211,14 @@ export function getIdentifiersUsage(node?: Expression | SpreadElement | PrivateN
}
else if (node.type === 'ObjectExpression') {
node.properties.forEach((prop) => {
if (prop.type === 'ObjectProperty')
getIdentifiersUsage(prop.key, identifiers)
else if (prop.type === 'SpreadElement')
if (prop.type === 'ObjectProperty') {
if (prop.computed)
getIdentifiersUsage(prop.key, identifiers)
getIdentifiersUsage(prop.value as Expression, identifiers)
}
else if (prop.type === 'SpreadElement') {
getIdentifiersUsage(prop, identifiers)
}
})
}
else if (node.type === 'ArrayExpression') {
Expand All @@ -218,6 +229,12 @@ export function getIdentifiersUsage(node?: Expression | SpreadElement | PrivateN
else if (node.type === 'SpreadElement') {
getIdentifiersUsage(node.argument, identifiers)
}
else if (node.type === 'NewExpression') {
getIdentifiersUsage(node.callee as Expression, identifiers)
node.arguments.forEach((arg) => {
getIdentifiersUsage(arg as Expression, identifiers)
})
}
// else {
// console.log(node)
// }
Expand Down
19 changes: 13 additions & 6 deletions test/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ describe('parse', () => {
['import * as foo from "z"', ['foo']],
['function foo(bar) {const a = z}', ['foo']],
['console.log(foo)', []],
['const { data } = toRefs(state)', ['data']],
['var { data } = toRefs(state)', ['data']],
['const { data, ...args } = bar', ['data', 'args']],
['const { foo: bar } = bar', ['foo']],
['const { foo: bar } = bar', ['bar']],
['const { foo: { a, b: c, d: { e: [f] } } } = { bar }', ['a', 'c', 'f']],
['let [a, b,, ...c] = bar', ['a', 'b', 'c']],
['let [a, b, [c, {d}],, ...e] = bar', ['a', 'b', 'c', 'd', 'e']],
['class A extends B {}', ['A']],
]

for (const [input, output] of cases) {
Expand All @@ -31,15 +34,19 @@ describe('parse', () => {
const cases: [string, string[]][] = [
['foo', ['foo']],
['foo.bar', ['foo']],
['foo(bar, console.log)', ['foo', 'bar', 'console']],
['foo(bar, console.log, ...args)', ['foo', 'bar', 'console', 'args']],
['foo(bar())', ['foo', 'bar']],
['for (let x in foo) {}', ['foo']],
['for (let [x, idx] of foo) {}', ['foo']],
['a + b', ['a', 'b']],
['a ? "" : b < c', ['a', 'b', 'c']],
['a == b && a === c', ['a', 'b', 'c']],
['({ a, b, ...args, [c]: 1 })', ['a', 'b', 'args', 'c']],
['a == b && a === c || d != e', ['a', 'b', 'c', 'd', 'e']],
['({ a, b, ...args, [c]: 1, d: e, f: { g } })', ['a', 'b', 'args', 'c', 'e', 'g']],
['!a', ['a']],
['[a,b,...args]', ['a', 'b', 'args']],
['!!c', ['c']],
['[a,b,[c,{d}],...args]', ['a', 'b', 'c', 'd', 'args']],
['new Foo(a,[b])', ['Foo', 'a', 'b']],
['new RC.Foo()', ['RC']],
]

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

0 comments on commit e1276bb

Please sign in to comment.