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

Handle string properties after computed properties correctly #91

Merged
merged 2 commits into from
Jan 12, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/program/types/ObjectExpression.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export default class ObjectExpression extends Node {
} else {
const propId =
(isSimpleAssignment ? `;\n${i0}${name}` : `, ${name}`) +
(prop.computed ? '' : '.');
(prop.key.type === 'Literal' || prop.computed ? '' : '.');

if (moveStart < prop.start) {
code.overwrite(moveStart, prop.start, propId);
Expand All @@ -175,7 +175,15 @@ export default class ObjectExpression extends Node {
while (code.original[c] !== ']') c += 1;
c += 1;
}
if (prop.shorthand) {
if (prop.key.type === 'Literal' && !prop.computed) {
console.log('' + code)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed that in a follow-up

code.overwrite(
prop.start,
prop.key.end + 1,
'[' + code.slice(prop.start, prop.key.end) + '] = '
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to remove the trailing whitespace, since hypothetical whitespace between the colon and the property value would be preserved, anyway.

);
console.log('' + code)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed that, too.

} else if (prop.shorthand) {
code.overwrite(
prop.start,
prop.key.end,
Expand Down
14 changes: 13 additions & 1 deletion test/samples/object-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,20 @@ module.exports = [
},

{
description: 'avoids shadowing free variables with method names (#166)',
description: 'transpiles string-keyed properties after computed properties',

input: `
fn({['computed']:1, 'some-var':2, a: 3});
`,
output: `
var obj;

fn(( obj = {}, obj['computed'] = 1, obj['some-var'] = 2, obj.a = 3, obj ));
`
},

{
description: 'avoids shadowing free variables with method names (#166)',
input: `
let x = {
foo() { return foo },
Expand Down