Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(block-scoping): deconflicts blocks in switch-statement and parent… #240

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"acorn-dynamic-import": "^4.0.0",
"acorn-jsx": "^5.0.1",
"chalk": "^2.4.2",
"magic-string": "^0.25.3",
"magic-string": "0.25.3",
"minimist": "^1.2.0",
"regexpu-core": "^4.5.4"
}
Expand Down
1 change: 1 addition & 0 deletions src/program/BlockStatement.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default class BlockStatement extends Node {
this.scope = new Scope({
block: !this.isFunctionBlock,
parent: this.parent.findScope(false),
parentType: this.parent.type,
declare: id => this.createdDeclarations.push(id)
});

Expand Down
18 changes: 18 additions & 0 deletions src/program/Scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default function Scope(options) {
options = options || {};

this.parent = options.parent;
this.parentType = options.parentType;
this.isBlockScope = !!options.block;
this.createDeclarationCallback = options.declare;

Expand Down Expand Up @@ -108,6 +109,23 @@ Scope.prototype = {
);
},

findDeclarationScope(name, initialScope = this) {
if (
this.declarations[name] ||
(this.blockScopedDeclarations && this.blockScopedDeclarations[name]) ||
// Creation of new lexical environment for `catch` block: https://github.com/bublejs/buble/pull/240
this.parentType === 'CatchClause'
) {
return this;
}

if (this.parent) {
return this.parent.findDeclarationScope(name, initialScope);
}

return initialScope;
},

// Sometimes, block scope declarations change name during transpilation
resolveName(name) {
const declaration = this.findDeclaration(name);
Expand Down
1 change: 1 addition & 0 deletions src/program/types/ForInStatement.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default class ForInStatement extends LoopStatement {
this.scope = new Scope({
block: true,
parent: this.parent.findScope(false),
parentType: this.parent.type,
declare: id => this.createdDeclarations.push(id)
});

Expand Down
1 change: 1 addition & 0 deletions src/program/types/ForOfStatement.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default class ForOfStatement extends LoopStatement {
this.scope = new Scope({
block: true,
parent: this.parent.findScope(false),
parentType: this.parent.type,
declare: id => this.createdDeclarations.push(id)
});

Expand Down
1 change: 1 addition & 0 deletions src/program/types/ForStatement.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default class ForStatement extends LoopStatement {
this.scope = new Scope({
block: true,
parent: this.parent.findScope(false),
parentType: this.parent.type,
declare: id => this.createdDeclarations.push(id)
});

Expand Down
2 changes: 1 addition & 1 deletion src/program/types/Identifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default class Identifier extends Node {
return this.parent.body.scope;
}

return this.parent.findScope(functionScope);
return this.parent.findScope(functionScope).findDeclarationScope(this.name);
}

initialise(transforms) {
Expand Down
57 changes: 57 additions & 0 deletions test/samples/block-scoping.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,5 +642,62 @@ module.exports = [
var x = 1;
for (var x$1 in { x: x$1 }) {}
`
},

{
description: 'deconflicts blocks in switch-statement and parent function scope',
options: { transforms: { letConst: true } },
input: `
function foo() {
const n = 1;
for (let i = 0; i < n; i++) {
const o = n;
switch (n) {
case 1:
const n = 12;
console.log(n);
break;
}
}
}
`,
output:`
function foo() {
var n = 1;
for (var i = 0; i < n; i++) {
var o = n;
switch (n) {
case 1:
var n$1 = 12;
console.log(n$1);
break;
}
}
}
`
},
{
description: 'deconflicts blocks in class declaration and for statement',
options: { transforms: { letConst: true, classes: false } },
input: `
class e {
run() {
if(this instanceof e) {
for(let e = 0; e < 1; e++) console.log('here');
}
}
}
new e().run();
`,
output:`
class e {
run() {
if(this instanceof e) {
for(var e$1 = 0; e$1 < 1; e$1++) { console.log('here'); }
}
}
}
new e().run();
`
}
];