From 57f10812cc7f1e9f6c92736c36aba577943996fd Mon Sep 17 00:00:00 2001 From: Evan You Date: Tue, 17 Aug 2021 10:10:26 -0400 Subject: [PATCH] fix(compiler-sfc): fix import usage check for lowercase imported components fix #4358 --- .../__snapshots__/compileScript.spec.ts.snap | 92 +++++++++++++++---- .../__tests__/compileScript.spec.ts | 82 ++++++++++++++--- packages/compiler-sfc/src/compileScript.ts | 2 +- 3 files changed, 147 insertions(+), 29 deletions(-) diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap index 3ab28dceb2d..c282b7eb4d6 100644 --- a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap @@ -126,6 +126,82 @@ return { props, a, emit } }" `; +exports[`SFC compile `) - // FooBar: should not be matched by plain text + // FooBar: should not be matched by plain text or incorrect case // FooBaz: used as PascalCase component // FooQux: used as kebab-case component - // vMyDir: used as directive v-my-dir + // foo: lowercase component + expect(content).toMatch(`return { fooBar, FooBaz, FooQux, foo }`) + assertCode(content) + }) + + test('directive', () => { + const { content } = compile(` + + + `) + expect(content).toMatch(`return { vMyDir }`) + assertCode(content) + }) + + test('vue interpolations', () => { + const { content } = compile(` + + + `) // x: used in interpolation // y: should not be matched by {{ yy }} or 'y' in binding exps // x$y: #4274 should escape special chars when creating Regex - // VAR & VAR3: #4340 interpolations in tempalte strings - expect(content).toMatch( - `return { fooBar, FooBaz, FooQux, vMyDir, x, z, x$y, VAR, VAR3, Last }` - ) + expect(content).toMatch(`return { x, z, x$y }`) + assertCode(content) + }) + + // #4340 interpolations in tempalte strings + test('js template string interpolations', () => { + const { content } = compile(` + + + `) + // VAR2 should not be matched + expect(content).toMatch(`return { VAR, VAR3 }`) + assertCode(content) + }) + + // edge case: last tag in template + test('last tag', () => { + const { content } = compile(` + + + `) + expect(content).toMatch(`return { FooBaz, Last }`) assertCode(content) }) }) diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index 031d20634cf..5289d110462 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -2212,7 +2212,7 @@ function resolveTemplateUsageCheckString(sfc: SFCDescriptor) { !parserOptions.isNativeTag!(node.tag) && !parserOptions.isBuiltInComponent!(node.tag) ) { - code += `,${capitalize(camelize(node.tag))}` + code += `,${camelize(node.tag)},${capitalize(camelize(node.tag))}` } for (let i = 0; i < node.props.length; i++) { const prop = node.props[i]