From: Evan You Date: Sat, 30 Dec 2023 10:13:15 +0000 (+0800) Subject: test: split import usage check tests into dedicated file X-Git-Tag: v3.4.2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=63c3e621ce6db8177bfdb4bf3990ea4cae803290;p=thirdparty%2Fvuejs%2Fcore.git test: split import usage check tests into dedicated file --- diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap index a2d24599cf..d63e6ec4d4 100644 --- a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap @@ -639,190 +639,6 @@ return { foo, bar, baz, y, z } }" `; -exports[`SFC compile - - `) - // FooBar: should not be matched by plain text or incorrect case - // FooBaz: used as PascalCase component - // FooQux: used as kebab-case component - // foo: lowercase component - expect(content).toMatch( - `return { fooBar, get FooBaz() { return FooBaz }, ` + - `get FooQux() { return FooQux }, get foo() { return foo } }`, - ) - assertCode(content) - }) - - test('directive', () => { - const { content } = compile(` - - - `) - expect(content).toMatch(`return { get vMyDir() { return vMyDir } }`) - assertCode(content) - }) - - test('dynamic arguments', () => { - const { content } = compile(` - - - `) - expect(content).toMatch( - `return { get FooBar() { return FooBar }, get foo() { return foo }, ` + - `get bar() { return bar }, get baz() { return baz } }`, - ) - assertCode(content) - }) - - // https://github.com/vuejs/core/issues/4599 - test('attribute expressions', () => { - const { content } = compile(` - - - `) - expect(content).toMatch( - `return { cond, get bar() { return bar }, get baz() { return baz } }`, - ) - 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 - expect(content).toMatch( - `return { get x() { return x }, get z() { return z }, get x$y() { return x$y } }`, - ) - assertCode(content) - }) - - // #4340 interpolations in template strings - test('js template string interpolations', () => { - const { content } = compile(` - - - `) - // VAR2 should not be matched - expect(content).toMatch( - `return { get VAR() { return VAR }, get VAR3() { return VAR3 } }`, - ) - assertCode(content) - }) - - // edge case: last tag in template - test('last tag', () => { - const { content } = compile(` - - - `) - expect(content).toMatch( - `return { get FooBaz() { return FooBaz }, get Last() { return Last } }`, - ) - assertCode(content) - }) - - test('TS annotations', () => { - const { content } = compile(` - - - `) - expect(content).toMatch(`return { a, b, get Baz() { return Baz } }`) - assertCode(content) - }) - - // vuejs/vue#12591 - test('v-on inline statement', () => { - // should not error - compile(` - - - `) - }) - - test('template ref', () => { - const { content } = compile(` - - - `) - expect(content).toMatch( - 'return { get foo() { return foo }, get bar() { return bar }, get Baz() { return Baz } }', - ) - assertCode(content) - }) - - // https://github.com/nuxt/nuxt/issues/22416 - test('property access', () => { - const { content } = compile(` - - - `) - expect(content).toMatch('return { get Foo() { return Foo } }') - assertCode(content) - }) - - test('spread operator', () => { - const { content } = compile(` - - - `) - expect(content).toMatch('return { get Foo() { return Foo } }') - assertCode(content) - }) - - test('property access (whitespace)', () => { - const { content } = compile(` - - - `) - expect(content).toMatch('return { get Foo() { return Foo } }') - assertCode(content) - }) - }) - describe('inlineTemplate mode', () => { test('should work', () => { const { content } = compile( diff --git a/packages/compiler-sfc/__tests__/compileScript/__snapshots__/importUsageCheck.spec.ts.snap b/packages/compiler-sfc/__tests__/compileScript/__snapshots__/importUsageCheck.spec.ts.snap new file mode 100644 index 0000000000..18946eb4b6 --- /dev/null +++ b/packages/compiler-sfc/__tests__/compileScript/__snapshots__/importUsageCheck.spec.ts.snap @@ -0,0 +1,185 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`TS annotations 1`] = ` +"import { defineComponent as _defineComponent } from 'vue' +import { Foo, Bar, Baz, Qux, Fred } from './x' + const a = 1 + +export default /*#__PURE__*/_defineComponent({ + setup(__props, { expose: __expose }) { + __expose(); + + function b() {} + +return { a, b, get Baz() { return Baz } } +} + +})" +`; + +exports[`attribute expressions 1`] = ` +"import { defineComponent as _defineComponent } from 'vue' +import { bar, baz } from './x' + const cond = true + +export default /*#__PURE__*/_defineComponent({ + setup(__props, { expose: __expose }) { + __expose(); + + +return { cond, get bar() { return bar }, get baz() { return baz } } +} + +})" +`; + +exports[`components 1`] = ` +"import { defineComponent as _defineComponent } from 'vue' +import { FooBar, FooBaz, FooQux, foo } from './x' + const fooBar: FooBar = 1 + +export default /*#__PURE__*/_defineComponent({ + setup(__props, { expose: __expose }) { + __expose(); + + +return { fooBar, get FooBaz() { return FooBaz }, get FooQux() { return FooQux }, get foo() { return foo } } +} + +})" +`; + +exports[`directive 1`] = ` +"import { defineComponent as _defineComponent } from 'vue' +import { vMyDir } from './x' + +export default /*#__PURE__*/_defineComponent({ + setup(__props, { expose: __expose }) { + __expose(); + + +return { get vMyDir() { return vMyDir } } +} + +})" +`; + +exports[`dynamic arguments 1`] = ` +"import { defineComponent as _defineComponent } from 'vue' +import { FooBar, foo, bar, unused, baz } from './x' + +export default /*#__PURE__*/_defineComponent({ + setup(__props, { expose: __expose }) { + __expose(); + + +return { get FooBar() { return FooBar }, get foo() { return foo }, get bar() { return bar }, get baz() { return baz } } +} + +})" +`; + +exports[`js template string interpolations 1`] = ` +"import { defineComponent as _defineComponent } from 'vue' +import { VAR, VAR2, VAR3 } from './x' + +export default /*#__PURE__*/_defineComponent({ + setup(__props, { expose: __expose }) { + __expose(); + + +return { get VAR() { return VAR }, get VAR3() { return VAR3 } } +} + +})" +`; + +exports[`last tag 1`] = ` +"import { defineComponent as _defineComponent } from 'vue' +import { FooBaz, Last } from './x' + +export default /*#__PURE__*/_defineComponent({ + setup(__props, { expose: __expose }) { + __expose(); + + +return { get FooBaz() { return FooBaz }, get Last() { return Last } } +} + +})" +`; + +exports[`property access (whitespace) 1`] = ` +"import { defineComponent as _defineComponent } from 'vue' +import { Foo, Bar, Baz } from './foo' + +export default /*#__PURE__*/_defineComponent({ + setup(__props, { expose: __expose }) { + __expose(); + + +return { get Foo() { return Foo } } +} + +})" +`; + +exports[`property access 1`] = ` +"import { defineComponent as _defineComponent } from 'vue' +import { Foo, Bar, Baz } from './foo' + +export default /*#__PURE__*/_defineComponent({ + setup(__props, { expose: __expose }) { + __expose(); + + +return { get Foo() { return Foo } } +} + +})" +`; + +exports[`spread operator 1`] = ` +"import { defineComponent as _defineComponent } from 'vue' +import { Foo, Bar, Baz } from './foo' + +export default /*#__PURE__*/_defineComponent({ + setup(__props, { expose: __expose }) { + __expose(); + + +return { get Foo() { return Foo } } +} + +})" +`; + +exports[`template ref 1`] = ` +"import { defineComponent as _defineComponent } from 'vue' +import { foo, bar, Baz } from './foo' + +export default /*#__PURE__*/_defineComponent({ + setup(__props, { expose: __expose }) { + __expose(); + + +return { get foo() { return foo }, get bar() { return bar }, get Baz() { return Baz } } +} + +})" +`; + +exports[`vue interpolations 1`] = ` +"import { defineComponent as _defineComponent } from 'vue' +import { x, y, z, x$y } from './x' + +export default /*#__PURE__*/_defineComponent({ + setup(__props, { expose: __expose }) { + __expose(); + + +return { get x() { return x }, get z() { return z }, get x$y() { return x$y } } +} + +})" +`; diff --git a/packages/compiler-sfc/__tests__/compileScript/importUsageCheck.spec.ts b/packages/compiler-sfc/__tests__/compileScript/importUsageCheck.spec.ts new file mode 100644 index 0000000000..06631b2ceb --- /dev/null +++ b/packages/compiler-sfc/__tests__/compileScript/importUsageCheck.spec.ts @@ -0,0 +1,221 @@ +import { assertCode, compileSFCScript as compile } from '../utils' + +// in dev mode, declared bindings are returned as an object from setup() +// when using TS, users may import types which should not be returned as +// values, so we need to check import usage in the template to determine +// what to be returned. + +test('components', () => { + const { content } = compile(` + + + `) + // FooBar: should not be matched by plain text or incorrect case + // FooBaz: used as PascalCase component + // FooQux: used as kebab-case component + // foo: lowercase component + expect(content).toMatch( + `return { fooBar, get FooBaz() { return FooBaz }, ` + + `get FooQux() { return FooQux }, get foo() { return foo } }`, + ) + assertCode(content) +}) + +test('directive', () => { + const { content } = compile(` + + + `) + expect(content).toMatch(`return { get vMyDir() { return vMyDir } }`) + assertCode(content) +}) + +test('dynamic arguments', () => { + const { content } = compile(` + + + `) + expect(content).toMatch( + `return { get FooBar() { return FooBar }, get foo() { return foo }, ` + + `get bar() { return bar }, get baz() { return baz } }`, + ) + assertCode(content) +}) + +// https://github.com/vuejs/core/issues/4599 +test('attribute expressions', () => { + const { content } = compile(` + + + `) + expect(content).toMatch( + `return { cond, get bar() { return bar }, get baz() { return baz } }`, + ) + 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 + expect(content).toMatch( + `return { get x() { return x }, get z() { return z }, get x$y() { return x$y } }`, + ) + assertCode(content) +}) + +// #4340 interpolations in template strings +test('js template string interpolations', () => { + const { content } = compile(` + + + `) + // VAR2 should not be matched + expect(content).toMatch( + `return { get VAR() { return VAR }, get VAR3() { return VAR3 } }`, + ) + assertCode(content) +}) + +// edge case: last tag in template +test('last tag', () => { + const { content } = compile(` + + + `) + expect(content).toMatch( + `return { get FooBaz() { return FooBaz }, get Last() { return Last } }`, + ) + assertCode(content) +}) + +test('TS annotations', () => { + const { content } = compile(` + + + `) + expect(content).toMatch(`return { a, b, get Baz() { return Baz } }`) + assertCode(content) +}) + +// vuejs/vue#12591 +test('v-on inline statement', () => { + // should not error + compile(` + + + `) +}) + +test('template ref', () => { + const { content } = compile(` + + + `) + expect(content).toMatch( + 'return { get foo() { return foo }, get bar() { return bar }, get Baz() { return Baz } }', + ) + assertCode(content) +}) + +// https://github.com/nuxt/nuxt/issues/22416 +test('property access', () => { + const { content } = compile(` + + + `) + expect(content).toMatch('return { get Foo() { return Foo } }') + assertCode(content) +}) + +test('spread operator', () => { + const { content } = compile(` + + + `) + expect(content).toMatch('return { get Foo() { return Foo } }') + assertCode(content) +}) + +test('property access (whitespace)', () => { + const { content } = compile(` + + + `) + expect(content).toMatch('return { get Foo() { return Foo } }') + assertCode(content) +})