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

Prevent labels from being renamed due to let/const transpilation. #236

Merged
merged 1 commit into from Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/program/types/Identifier.js
Expand Up @@ -16,6 +16,10 @@ export default class Identifier extends Node {
}

initialise(transforms) {
if (this.isLabel()) {
return;
}

if (isReference(this, this.parent)) {
if (
transforms.arrow &&
Expand Down Expand Up @@ -43,6 +47,15 @@ export default class Identifier extends Node {
}
}

isLabel() {
switch (this.parent.type) {
case 'BreakStatement': return true;
case 'ContinueStatement': return true;
case 'LabeledStatement': return true;
default: return false;
}
}

transpile(code) {
if (this.alias) {
code.overwrite(this.start, this.end, this.alias, {
Expand Down
52 changes: 52 additions & 0 deletions test/samples/loops.js
Expand Up @@ -855,5 +855,57 @@ module.exports = [
var b = ref[1]; if ( b === void 0 ) b = "_";

f(a, b) }`
},

{
description: 'labelled breaks/continues are not renamed',

input: `
function f(x) {
x:
for (let x = 0;;) {
if (x > 1) { break x; }
else { continue x; }
}
}
`,

output: `
function f(x) {
x:
for (var x$1 = 0;;) {
if (x$1 > 1) { break x; }
else { continue x; }
}
}
`
},

{
description: 'Labels are not renamed',

input: `
function f() {
let x = 1;
{
let x = 2;
x:
for (;;) {
}
}
}
`,

output: `
function f() {
var x = 1;
{
var x$1 = 2;
x:
for (;;) {
}
}
}
`
}
];