Skip to content

Commit 48ad452

Browse files
committed
fix(compiler-sfc): respect var hoisting in props destructure
1 parent 9d92dbd commit 48ad452

3 files changed

Lines changed: 235 additions & 12 deletions

File tree

‎packages/compiler-sfc/__tests__/compileScript/__snapshots__/definePropsDestructure.spec.ts.snap‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,108 @@ return { rest }
388388
}"
389389
`;
390390

391+
exports[`sfc reactive props destructure > var declaration shadowing before declaration in for loop 1`] = `
392+
"import { defineComponent as _defineComponent } from 'vue'
393+
394+
export default /*@__PURE__*/_defineComponent({
395+
props: {
396+
foo: { type: String, required: false, default: "a" }
397+
},
398+
setup(__props: any) {
399+
400+
401+
let bar: string | undefined;
402+
function init() {
403+
bar = foo;
404+
for (var foo = "b"; false;) {}
405+
}
406+
init();
407+
408+
return () => {}
409+
}
410+
411+
})"
412+
`;
413+
414+
exports[`sfc reactive props destructure > var declaration shadowing before declaration in nested block 1`] = `
415+
"import { defineComponent as _defineComponent } from 'vue'
416+
417+
export default /*@__PURE__*/_defineComponent({
418+
props: {
419+
foo: { type: String, required: false, default: "a" }
420+
},
421+
setup(__props: any) {
422+
423+
424+
let bar: string | undefined;
425+
function init() {
426+
bar = foo;
427+
{
428+
var foo = "b";
429+
}
430+
}
431+
init();
432+
433+
return () => {}
434+
}
435+
436+
})"
437+
`;
438+
439+
exports[`sfc reactive props destructure > var declaration shadowing does not cross function scope 1`] = `
440+
"import { defineComponent as _defineComponent } from 'vue'
441+
442+
export default /*@__PURE__*/_defineComponent({
443+
props: {
444+
foo: { type: String, required: false, default: "a" }
445+
},
446+
setup(__props: any) {
447+
448+
449+
let bar: string | undefined;
450+
function init() {
451+
function nested() {
452+
{
453+
var foo = "b";
454+
}
455+
return foo;
456+
}
457+
bar = __props.foo;
458+
nested();
459+
}
460+
init();
461+
462+
return () => {}
463+
}
464+
465+
})"
466+
`;
467+
468+
exports[`sfc reactive props destructure > var declaration shadowing in nested block 1`] = `
469+
"import { defineComponent as _defineComponent } from 'vue'
470+
471+
export default /*@__PURE__*/_defineComponent({
472+
props: {
473+
foo: { type: String, required: false, default: "a" }
474+
},
475+
setup(__props: any) {
476+
477+
478+
let bar: string | undefined;
479+
function init() {
480+
{
481+
var foo = "b";
482+
}
483+
bar = foo;
484+
}
485+
init();
486+
487+
return () => {}
488+
}
489+
490+
})"
491+
`;
492+
391493
exports[`sfc reactive props destructure > with TSInstantiationExpression 1`] = `
392494
"import { defineComponent as _defineComponent } from 'vue'
393495
type Foo = <T extends string | number>(data: T) => void

‎packages/compiler-sfc/__tests__/compileScript/definePropsDestructure.spec.ts‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,79 @@ describe('sfc reactive props destructure', () => {
108108
assertCode(content)
109109
})
110110

111+
test('var declaration shadowing in nested block', () => {
112+
const { content } = compile(`<script setup lang="ts">
113+
const { foo = "a" } = defineProps<{ foo?: string }>();
114+
let bar: string | undefined;
115+
function init() {
116+
{
117+
var foo = "b";
118+
}
119+
bar = foo;
120+
}
121+
init();
122+
</script>`)
123+
expect(content).toMatch(`var foo = "b"`)
124+
expect(content).toMatch(`bar = foo`)
125+
expect(content).not.toMatch(`bar = __props.foo`)
126+
assertCode(content)
127+
})
128+
129+
test('var declaration shadowing before declaration in nested block', () => {
130+
const { content } = compile(`<script setup lang="ts">
131+
const { foo = "a" } = defineProps<{ foo?: string }>();
132+
let bar: string | undefined;
133+
function init() {
134+
bar = foo;
135+
{
136+
var foo = "b";
137+
}
138+
}
139+
init();
140+
</script>`)
141+
expect(content).toMatch(`bar = foo`)
142+
expect(content).toMatch(`var foo = "b"`)
143+
expect(content).not.toMatch(`bar = __props.foo`)
144+
assertCode(content)
145+
})
146+
147+
test('var declaration shadowing before declaration in for loop', () => {
148+
const { content } = compile(`<script setup lang="ts">
149+
const { foo = "a" } = defineProps<{ foo?: string }>();
150+
let bar: string | undefined;
151+
function init() {
152+
bar = foo;
153+
for (var foo = "b"; false;) {}
154+
}
155+
init();
156+
</script>`)
157+
expect(content).toMatch(`bar = foo`)
158+
expect(content).toMatch(`for (var foo = "b"; false;)`)
159+
expect(content).not.toMatch(`bar = __props.foo`)
160+
assertCode(content)
161+
})
162+
163+
test('var declaration shadowing does not cross function scope', () => {
164+
const { content } = compile(`<script setup lang="ts">
165+
const { foo = "a" } = defineProps<{ foo?: string }>();
166+
let bar: string | undefined;
167+
function init() {
168+
function nested() {
169+
{
170+
var foo = "b";
171+
}
172+
return foo;
173+
}
174+
bar = foo;
175+
nested();
176+
}
177+
init();
178+
</script>`)
179+
expect(content).toMatch(`return foo`)
180+
expect(content).toMatch(`bar = __props.foo`)
181+
assertCode(content)
182+
})
183+
111184
test('default values w/ array runtime declaration', () => {
112185
const { content } = compile(`
113186
<script setup>

‎packages/compiler-sfc/src/script/definePropsDestructure.ts‎

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ export function transformDestructuredProps(
105105

106106
const rootScope: Scope = Object.create(null)
107107
const scopeStack: Scope[] = [rootScope]
108+
const functionScopeStack: Scope[] = [rootScope]
108109
let currentScope: Scope = rootScope
109110
const excludedIds = new WeakSet<Identifier>()
110111
const parentStack: Node[] = []
@@ -116,19 +117,26 @@ export function transformDestructuredProps(
116117
propsLocalToPublicMap[local] = key
117118
}
118119

119-
function pushScope() {
120-
scopeStack.push((currentScope = Object.create(currentScope)))
120+
function pushScope(isFunctionScope = false) {
121+
const scope = (currentScope = Object.create(currentScope))
122+
scopeStack.push(scope)
123+
if (isFunctionScope) {
124+
functionScopeStack.push(scope)
125+
}
121126
}
122127

123-
function popScope() {
128+
function popScope(isFunctionScope = false) {
124129
scopeStack.pop()
130+
if (isFunctionScope) {
131+
functionScopeStack.pop()
132+
}
125133
currentScope = scopeStack[scopeStack.length - 1] || null
126134
}
127135

128-
function registerLocalBinding(id: Identifier) {
136+
function registerLocalBinding(id: Identifier, scope = currentScope) {
129137
excludedIds.add(id)
130-
if (currentScope) {
131-
currentScope[id.name] = false
138+
if (scope) {
139+
scope[id.name] = false
132140
} else {
133141
ctx.error(
134142
'registerBinding called without active scope, something is wrong.',
@@ -162,7 +170,13 @@ export function transformDestructuredProps(
162170
}
163171
}
164172

165-
function walkVariableDeclaration(stmt: VariableDeclaration, isRoot = false) {
173+
function walkVariableDeclaration(
174+
stmt: VariableDeclaration,
175+
isRoot = false,
176+
scope = stmt.kind === 'var'
177+
? functionScopeStack[functionScopeStack.length - 1]
178+
: currentScope,
179+
) {
166180
if (stmt.declare) {
167181
return
168182
}
@@ -175,12 +189,42 @@ export function transformDestructuredProps(
175189
// are already passed in as knownProps
176190
excludedIds.add(id)
177191
} else {
178-
registerLocalBinding(id)
192+
registerLocalBinding(id, scope)
179193
}
180194
}
181195
}
182196
}
183197

198+
function walkFunctionScopeVarDeclarations(
199+
scopeNode: Program | BlockStatement,
200+
isRoot = false,
201+
) {
202+
const scope = functionScopeStack[functionScopeStack.length - 1]
203+
walk(scopeNode, {
204+
enter(node: Node, parent: Node | null) {
205+
if (
206+
parent &&
207+
parent.type.startsWith('TS') &&
208+
!TS_NODE_TYPES.includes(parent.type)
209+
) {
210+
return this.skip()
211+
}
212+
213+
if (
214+
isFunctionType(node) ||
215+
node.type === 'ClassDeclaration' ||
216+
node.type === 'ClassExpression'
217+
) {
218+
return this.skip()
219+
}
220+
221+
if (node.type === 'VariableDeclaration' && node.kind === 'var') {
222+
walkVariableDeclaration(node, isRoot && parent === scopeNode, scope)
223+
}
224+
},
225+
})
226+
}
227+
184228
function rewriteId(id: Identifier, parent: Node, parentStack: Node[]) {
185229
if (
186230
(parent.type === 'AssignmentExpression' && id === parent.left) ||
@@ -227,6 +271,7 @@ export function transformDestructuredProps(
227271

228272
// check root scope first
229273
const ast = ctx.scriptSetupAst!
274+
walkFunctionScopeVarDeclarations(ast, true)
230275
walkScope(ast, true)
231276
walk(ast, {
232277
enter(node: Node, parent: Node | null) {
@@ -246,9 +291,10 @@ export function transformDestructuredProps(
246291

247292
// function scopes
248293
if (isFunctionType(node)) {
249-
pushScope()
294+
pushScope(true)
250295
walkFunctionParams(node, registerLocalBinding)
251296
if (node.body.type === 'BlockStatement') {
297+
walkFunctionScopeVarDeclarations(node.body)
252298
walkScope(node.body)
253299
}
254300
return
@@ -301,9 +347,11 @@ export function transformDestructuredProps(
301347
},
302348
leave(node: Node, parent: Node | null) {
303349
parent && parentStack.pop()
304-
if (
305-
(node.type === 'BlockStatement' && !isFunctionType(parent!)) ||
306-
isFunctionType(node) ||
350+
if (isFunctionType(node)) {
351+
popScope(true)
352+
} else if (node.type === 'BlockStatement' && !isFunctionType(parent!)) {
353+
popScope()
354+
} else if (
307355
node.type === 'CatchClause' ||
308356
node.type === 'ForOfStatement' ||
309357
node.type === 'ForInStatement' ||

0 commit comments

Comments
 (0)