Skip to content

Commit

Permalink
fix(compiler-sfc): fix template usage check false positives on types
Browse files Browse the repository at this point in the history
fix #5414
  • Loading branch information
yyx990803 committed May 12, 2022
1 parent ba17792 commit ccf9256
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,23 @@ return { props, a, emit }
}"
`;

exports[`SFC compile <script setup> dev mode import usage check TS annotations 1`] = `
"import { defineComponent as _defineComponent } from 'vue'
import { Foo, Bar, Baz } from './x'

export default /*#__PURE__*/_defineComponent({
setup(__props, { expose }) {
expose();

const a = 1
function b() {}

return { a, b, Baz }
}

})"
`;

exports[`SFC compile <script setup> dev mode import usage check attribute expressions 1`] = `
"import { defineComponent as _defineComponent } from 'vue'
import { bar, baz } from './x'
Expand Down
17 changes: 17 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,23 @@ defineExpose({ foo: 123 })
expect(content).toMatch(`return { FooBaz, Last }`)
assertCode(content)
})

test('TS annotations', () => {
const { content } = compile(`
<script setup lang="ts">
import { Foo, Bar, Baz } from './x'
const a = 1
function b() {}
</script>
<template>
{{ a as Foo }}
{{ b<Bar>() }}
{{ Baz }}
</template>
`)
expect(content).toMatch(`return { a, b, Baz }`)
assertCode(content)
})
})

describe('inlineTemplate mode', () => {
Expand Down
45 changes: 37 additions & 8 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ import {
walkIdentifiers
} from '@vue/compiler-dom'
import { DEFAULT_FILENAME, SFCDescriptor, SFCScriptBlock } from './parse'
import { parse as _parse, ParserOptions, ParserPlugin } from '@babel/parser'
import {
parse as _parse,
parseExpression,
ParserOptions,
ParserPlugin
} from '@babel/parser'
import { camelize, capitalize, generateCodeFrame, makeMap } from '@vue/shared'
import {
Node,
Expand Down Expand Up @@ -348,14 +353,23 @@ export function compileScript(
local: string,
imported: string | false,
isType: boolean,
isFromSetup: boolean
isFromSetup: boolean,
needTemplateUsageCheck: boolean
) {
if (source === 'vue' && imported) {
userImportAlias[imported] = local
}

let isUsedInTemplate = true
if (isTS && sfc.template && !sfc.template.src && !sfc.template.lang) {
// template usage check is only needed in non-inline mode, so we can skip
// the work if inlineTemplate is true.
let isUsedInTemplate = needTemplateUsageCheck
if (
needTemplateUsageCheck &&
isTS &&
sfc.template &&
!sfc.template.src &&
!sfc.template.lang
) {
isUsedInTemplate = isImportUsed(local, sfc)
}

Expand Down Expand Up @@ -813,7 +827,8 @@ export function compileScript(
node.importKind === 'type' ||
(specifier.type === 'ImportSpecifier' &&
specifier.importKind === 'type'),
false
false,
!options.inlineTemplate
)
}
} else if (node.type === 'ExportDefaultDeclaration') {
Expand Down Expand Up @@ -1027,7 +1042,8 @@ export function compileScript(
node.importKind === 'type' ||
(specifier.type === 'ImportSpecifier' &&
specifier.importKind === 'type'),
true
true,
!options.inlineTemplate
)
}
}
Expand Down Expand Up @@ -2051,14 +2067,14 @@ function resolveTemplateUsageCheckString(sfc: SFCDescriptor) {
code += `,v${capitalize(camelize(prop.name))}`
}
if (prop.exp) {
code += `,${stripStrings(
code += `,${processExp(
(prop.exp as SimpleExpressionNode).content
)}`
}
}
}
} else if (node.type === NodeTypes.INTERPOLATION) {
code += `,${stripStrings(
code += `,${processExp(
(node.content as SimpleExpressionNode).content
)}`
}
Expand All @@ -2071,6 +2087,19 @@ function resolveTemplateUsageCheckString(sfc: SFCDescriptor) {
return code
}

function processExp(exp: string) {
if (/ as \w|<.*>/.test(exp)) {
let ret = ''
// has potential type cast or generic arguments that uses types
const ast = parseExpression(exp, { plugins: ['typescript'] })
walkIdentifiers(ast, node => {
ret += `,` + node.name
})
return ret
}
return stripStrings(exp)
}

function stripStrings(exp: string) {
return exp
.replace(/'[^']*'|"[^"]*"/g, '')
Expand Down

0 comments on commit ccf9256

Please sign in to comment.