Skip to content

Commit

Permalink
fix(reactivity-transform): apply transform for labelled variable decl…
Browse files Browse the repository at this point in the history
…arations

ref #5298 (comment)
  • Loading branch information
yyx990803 committed Jan 20, 2022
1 parent a81a992 commit a05b000
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
Expand Up @@ -10,6 +10,7 @@ exports[`$ unwrapping 1`] = `
}))
let c = () => {}
let d
label: var e = (ref())
"
`;

Expand All @@ -34,12 +35,13 @@ exports[`$ref & $shallowRef declarations 1`] = `
"import { ref as _ref, shallowRef as _shallowRef } from 'vue'
let foo = _ref()
let a = _ref(1)
export let a = _ref(1)
let b = _shallowRef({
count: 0
})
let c = () => {}
let d
label: var e = _ref()
"
`;

Expand Down
Expand Up @@ -25,6 +25,7 @@ test('$ unwrapping', () => {
}))
let c = () => {}
let d
label: var e = $(ref())
`)
expect(code).not.toMatch(`$(ref())`)
expect(code).not.toMatch(`$(ref(1))`)
Expand All @@ -39,19 +40,21 @@ test('$ unwrapping', () => {
// normal declarations left untouched
expect(code).toMatch(`let c = () => {}`)
expect(code).toMatch(`let d`)
expect(rootRefs).toStrictEqual(['foo', 'a', 'b'])
expect(code).toMatch(`label: var e = (ref())`)
expect(rootRefs).toStrictEqual(['foo', 'a', 'b', 'e'])
assertCode(code)
})

test('$ref & $shallowRef declarations', () => {
const { code, rootRefs, importedHelpers } = transform(`
let foo = $ref()
let a = $ref(1)
export let a = $ref(1)
let b = $shallowRef({
count: 0
})
let c = () => {}
let d
label: var e = $ref()
`)
expect(code).toMatch(
`import { ref as _ref, shallowRef as _shallowRef } from 'vue'`
Expand All @@ -69,7 +72,8 @@ test('$ref & $shallowRef declarations', () => {
// normal declarations left untouched
expect(code).toMatch(`let c = () => {}`)
expect(code).toMatch(`let d`)
expect(rootRefs).toStrictEqual(['foo', 'a', 'b'])
expect(code).toMatch(`label: var e = _ref()`)
expect(rootRefs).toStrictEqual(['foo', 'a', 'b', 'e'])
expect(importedHelpers).toStrictEqual(['ref', 'shallowRef'])
assertCode(code)
})
Expand Down
5 changes: 5 additions & 0 deletions packages/reactivity-transform/src/reactivityTransform.ts
Expand Up @@ -235,6 +235,11 @@ export function transformAST(
stmt.declaration.type === 'VariableDeclaration'
) {
walkVariableDeclaration(stmt.declaration, isRoot)
} else if (
stmt.type === 'LabeledStatement' &&
stmt.body.type === 'VariableDeclaration'
) {
walkVariableDeclaration(stmt.body, isRoot)
}
}
}
Expand Down

0 comments on commit a05b000

Please sign in to comment.