diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScriptRefSugar.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScriptRefSugar.spec.ts.snap index 17b1c8edd34..df09c5afe95 100644 --- a/packages/compiler-sfc/__tests__/__snapshots__/compileScriptRefSugar.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/__snapshots__/compileScriptRefSugar.spec.ts.snap @@ -1,5 +1,59 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[``) expect(content).toMatch(`import { ref as _ref } from 'vue'`) - expect(content).not.toMatch(`ref: foo`) - expect(content).not.toMatch(`ref: a`) - expect(content).not.toMatch(`ref: b`) - expect(content).toMatch(`const foo = _ref()`) - expect(content).toMatch(`const a = _ref(1)`) + expect(content).not.toMatch(`$ref()`) + expect(content).not.toMatch(`$ref(1)`) + expect(content).not.toMatch(`$ref({`) + expect(content).toMatch(`let foo = _ref()`) + expect(content).toMatch(`let a = _ref(1)`) expect(content).toMatch(` - const b = _ref({ + let b = _ref({ count: 0 }) `) @@ -40,14 +40,14 @@ describe('`) expect(content).toMatch(` - const a = _ref(1), b = _ref(2), c = _ref({ + let a = _ref(1), b = _ref(2), c = _ref({ count: 0 }) `) @@ -60,19 +60,38 @@ describe('`) + expect(content).toMatch(` + const a = _computed(() => 1) + `) + expect(content).toMatch(`return { a }`) + assertCode(content) + expect(bindings).toStrictEqual({ + a: BindingTypes.SETUP_REF + }) + }) + + test('mixing $ref & $computed declarations', () => { + const { content, bindings } = compileWithRefSugar(``) - expect(content).toMatch(`foo: a = 1, b = 2`) + expect(content).toMatch(` + let a = _ref(1), b = _computed(() => a.value + 1) + `) + expect(content).toMatch(`return { a, b }`) assertCode(content) + expect(bindings).toStrictEqual({ + a: BindingTypes.SETUP_REF, + b: BindingTypes.SETUP_REF + }) }) test('accessing ref binding', () => { const { content } = compileWithRefSugar(``) expect(content).toMatch( - `const n = _ref(1), { a: __a, b: __c, d: __d = 1, e: __f = 2, ...__g } = useFoo()` + `let n = _ref(1), { a: __a, b: __c, d: __d = 1, e: __f = 2, ...__g } = (useFoo())` ) - expect(content).toMatch(`const { foo: __foo } = useSomthing(() => 1)`) + expect(content).toMatch(`let { foo: __foo } = (useSomthing(() => 1))`) expect(content).toMatch(`\nconst a = _ref(__a);`) expect(content).not.toMatch(`\nconst b = _ref(__b);`) expect(content).toMatch(`\nconst c = _ref(__c);`) @@ -194,11 +210,11 @@ describe('`) expect(content).toMatch( - `const n = _ref(1), [__a, __b = 1, ...__c] = useFoo()` + `let n = _ref(1), [__a, __b = 1, ...__c] = (useFoo())` ) expect(content).toMatch(`\nconst a = _ref(__a);`) expect(content).toMatch(`\nconst b = _ref(__b);`) @@ -216,12 +232,12 @@ describe('`) - expect(content).toMatch(`const [{ a: { b: __b }}] = useFoo()`) - expect(content).toMatch(`const { c: [__d, __e] } = useBar()`) + expect(content).toMatch(`let [{ a: { b: __b }}] = (useFoo())`) + expect(content).toMatch(`let { c: [__d, __e] } = (useBar())`) expect(content).not.toMatch(`\nconst a = _ref(__a);`) expect(content).not.toMatch(`\nconst c = _ref(__c);`) expect(content).toMatch(`\nconst b = _ref(__b);`) @@ -236,13 +252,26 @@ describe('`) + expect(content).toMatch(`const b = (a)`) + expect(content).toMatch(`const c = ({ a })`) + expect(content).toMatch(`callExternal((a))`) + assertCode(content) + }) + //#4062 test('should not rewrite type identifiers', () => { const { content } = compile( ` `, { refSugar: true @@ -253,22 +282,44 @@ describe('`, + { refSugar: true } + ) + ).toThrow(`$ref() bindings can only be declared with let`) + }) + + test('$ref w/ destructure', () => { + expect(() => + compile( + ``, + { refSugar: true } + ) + ).toThrow(`$ref() bindings cannot be used with destructuring`) + }) + + test('$computed w/ destructure', () => { expect(() => compile( ``, { refSugar: true } ) - ).toThrow(`ref: statements can only contain assignment expressions`) + ).toThrow(`$computed() bindings cannot be used with destructuring`) }) test('defineProps/Emit() referencing ref declarations', () => { expect(() => compile( `