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: make letConst transpilation add explicit keys when appropriate #229

Merged
merged 2 commits into from
Nov 15, 2019
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
10 changes: 10 additions & 0 deletions src/program/BlockStatement.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,19 +351,29 @@ export default class BlockStatement extends Node {
const alias = this.scope.createIdentifier(name);

if (name !== alias) {
const declarationParent = declaration.node.parent;
declaration.name = alias;
code.overwrite(
declaration.node.start,
declaration.node.end,
alias,
{ storeName: true }
);
if (declarationParent.type === 'Property' && declarationParent.shorthand) {
declarationParent.shorthand = false;
code.prependLeft(declaration.node.start, `${name}: `);
}

for (const identifier of declaration.instances) {
identifier.rewritten = true;
const identifierParent = identifier.parent;
code.overwrite(identifier.start, identifier.end, alias, {
storeName: true
});
if (identifierParent.type === 'Property' && identifierParent.shorthand) {
identifierParent.shorthand = false;
code.prependLeft(identifier.start, `${name}: `);
}
}
}
}
Expand Down
51 changes: 51 additions & 0 deletions test/samples/block-scoping.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,5 +544,56 @@ module.exports = [
input: 'if(0);const e=0',

output: 'if(0){ ; }var e=0'
},

{
description: 'properly replaces keys of renamed properties when conciseMethodProperty is false',
options: { transforms: { letConst: true, conciseMethodProperty: false } },
input: `
const x = 1;
if (true) {
const x = 2;
const y = { x };
}
`,
output: `
var x = 1;
if (true) {
var x$1 = 2;
var y = { x: x$1 };
}
`
},

{
description: 'properly replaces keys of renamed properties in destructuring when conciseMethodProperty is false',
options: { transforms: { letConst: true, destructuring: false } },
input: `
const x = 1;
if (true) {
const y = {};
const { x } = y;
}
`,
output: `
var x = 1;
if (true) {
var y = {};
var { x: x$1 } = y;
}
`
},

{
description: 'does not duplicate the key when removing shorthand properties',
options: { transforms: { letConst: true } },
input: `
const x = 1;
for (const x in { x }) {}
`,
output: `
var x = 1;
for (var x$1 in { x: x$1 }) {}
`
}
];