Skip to content

Commit

Permalink
Fix component search only finding first callExpr
Browse files Browse the repository at this point in the history
  • Loading branch information
Trinovantes committed Apr 23, 2022
1 parent 6892583 commit 4d3b906
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 50 deletions.
21 changes: 18 additions & 3 deletions example/src/components/VueCompositionSetup.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts" setup>
import { computed, defineProps } from 'vue'
import { computed, defineProps, ref } from 'vue'
import Counter from './Counter.vue'
const props = defineProps({
Expand All @@ -11,6 +11,7 @@ const props = defineProps({
const name = computed(() => `Composition Setup num:${props.num}`)
const childNum = computed(() => props.num + 1)
const message = ref<string | null>(null)
</script>

<template>
Expand All @@ -23,9 +24,23 @@ const childNum = computed(() => props.num + 1)
v-if="num < 5"
:num="childNum"
/>
<Counter
<template
v-else
/>
>
<q-form>
<q-input
v-model="message"
label="Message"
debounce="250"
outlined
clearable
:rules="[ (val: string) => val && val.length > 0 || 'Message cannot be empty']"
hide-bottom-space
/>
</q-form>

<Counter />
</template>
</article>
</template>

Expand Down
32 changes: 32 additions & 0 deletions src/utils/findCallExpressions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { CallExpression, Node } from 'estree'
import { visitChildren } from './visitChildren'

export function findCallExpressions(root: Node, calleeName: string): Array<CallExpression> {
const isTargetCallExpression = (node: Node): node is CallExpression => {
if (node.type !== 'CallExpression') {
return false
}

if (node.callee.type !== 'Identifier') {
return false
}

if (node.callee.name !== calleeName) {
return false
}

return true
}

const foundExpressions: Array<CallExpression> = []
const search = (node: Node) => {
if (isTargetCallExpression(node)) {
foundExpressions.push(node)
}

visitChildren(node, search)
}

search(root)
return foundExpressions
}
30 changes: 0 additions & 30 deletions src/utils/findExpressionNode.ts

This file was deleted.

32 changes: 15 additions & 17 deletions src/webpack/QuasarUnusedPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import path from 'path'
import { existsSync } from 'fs'
import type { QuasarUnusedLoaderOptions } from './QuasarUnusedLoaderOptions'
import type { QuasarUnusedPluginOptions } from './QuasarUnusedPluginOptions'
import { findCallExpression } from '../utils/findExpressionNode'
import { findImportLocalId } from '../utils/findImportLocalId'
import { findCallExpressions } from '../utils/findCallExpressions'

interface QuasarAutoImport {
importName: Record<string, string>
Expand Down Expand Up @@ -96,26 +96,24 @@ export class QuasarUnusedPlugin implements WebpackPluginInstance {
return
}

const callExpr = findCallExpression(ast, resolveComponentLocalId)
if (!callExpr) {
return
}
const callExprs = findCallExpressions(ast, resolveComponentLocalId)
for (const callExpr of callExprs) {
if (callExpr.arguments[0].type !== 'Literal') {
continue
}

if (callExpr.arguments[0].type !== 'Literal') {
return
}
const componentName = callExpr.arguments[0].raw?.replace(/['"]+/g, '') ?? ''
if (!componentRegex.test(componentName)) {
continue
}

const componentName = callExpr.arguments[0].raw?.replace(/['"]+/g, '') ?? ''
if (!componentRegex.test(componentName)) {
return
}
const canonicalName = quasarAutoImport.importName[componentName]
if (!canonicalName) {
continue
}

const canonicalName = quasarAutoImport.importName[componentName]
if (!canonicalName) {
return
this.#usedComponents.add(canonicalName)
}

this.#usedComponents.add(canonicalName)
})
}

Expand Down

0 comments on commit 4d3b906

Please sign in to comment.