Skip to content

Commit

Permalink
fix(compiler-sfc): fix template usage check edge case for v-on statem…
Browse files Browse the repository at this point in the history
…ents

fix #12591
  • Loading branch information
yyx990803 committed Jul 4, 2022
1 parent 6f0d2e6 commit caceece
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
27 changes: 12 additions & 15 deletions packages/compiler-sfc/src/compileScript.ts
Expand Up @@ -39,7 +39,7 @@ import { walk } from 'estree-walker'
import { RawSourceMap } from 'source-map'
import { warnOnce } from './warn'
import { isReservedTag } from 'web/util'
import { dirRE } from 'compiler/parser'
import { bindRE, dirRE, onRE } from 'compiler/parser'
import { parseText } from 'compiler/parser/text-parser'
import { DEFAULT_FILENAME } from './parseComponent'
import {
Expand Down Expand Up @@ -278,23 +278,16 @@ export function compileScript(
local: string,
imported: string | false,
isType: boolean,
isFromSetup: boolean,
needTemplateUsageCheck: boolean
isFromSetup: boolean
) {
if (source === 'vue' && imported) {
userImportAlias[imported] = local
}

// 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
) {
let isUsedInTemplate = true
if (isTS && sfc.template && !sfc.template.src && !sfc.template.lang) {
isUsedInTemplate = isImportUsed(local, sfc)
}

Expand Down Expand Up @@ -658,8 +651,7 @@ export function compileScript(
node.importKind === 'type' ||
(specifier.type === 'ImportSpecifier' &&
specifier.importKind === 'type'),
false,
true
false
)
}
} else if (node.type === 'ExportDefaultDeclaration') {
Expand Down Expand Up @@ -872,7 +864,6 @@ export function compileScript(
node.importKind === 'type' ||
(specifier.type === 'ImportSpecifier' &&
specifier.importKind === 'type'),
true,
true
)
}
Expand Down Expand Up @@ -1809,7 +1800,11 @@ function resolveTemplateUsageCheckString(sfc: SFCDescriptor) {
for (let i = 0; i < attrs.length; i++) {
const { name, value } = attrs[i]
if (dirRE.test(name)) {
const baseName = name.replace(dirRE, '')
const baseName = onRE.test(name)
? 'on'
: bindRE.test(name)
? 'bind'
: name.replace(dirRE, '')
if (!isBuiltInDir(baseName)) {
code += `,v${capitalize(camelize(baseName))}`
}
Expand Down Expand Up @@ -1838,6 +1833,8 @@ function processExp(exp: string, dir?: string): string {
if (/ as\s+\w|<.*>|:/.test(exp)) {
if (dir === 'slot') {
exp = `(${exp})=>{}`
} else if (dir === 'on') {
exp = `()=>{${exp}}`
} else if (dir === 'for') {
const inMatch = exp.match(forAliasRE)
if (inMatch) {
Expand Down
12 changes: 12 additions & 0 deletions packages/compiler-sfc/test/compileScript.spec.ts
Expand Up @@ -1562,5 +1562,17 @@ describe('SFC analyze <script> bindings', () => {
expect(content).toMatch(`name: 'Baz'`)
assertCode(content)
})

// #12591
test('should not error when performing ts expression check for v-on inline statement', () => {
compile(`
<script setup lang="ts">
import { foo } from './foo'
</script>
<template>
<div @click="$emit('update:a');"></div>
</tempalte>
`)
})
})
})

0 comments on commit caceece

Please sign in to comment.