Skip to content

Commit

Permalink
Merge pull request #15 from Conduitry/gh-14
Browse files Browse the repository at this point in the history
fix/tidy handling of object literal shorthand syntax
  • Loading branch information
Rich-Harris authored Nov 15, 2019
2 parents ffa35e1 + 178b0ba commit 7b01aa7
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/print/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1026,12 +1026,15 @@ const handlers: Record<string, Handler> = {
return value;
}

const key = handle(node.key, state);

if (key.length === 1 && value.length === 1 && node.key.type === 'Identifier' && key[0].content === value[0].content) {
if (node.value.type === 'Identifier' && (
(node.key.type === 'Identifier' && node.key.name === node.value.name) ||
(node.key.type === 'Literal' && node.key.value === node.value.name)
)) {
return value;
}

const key = handle(node.key, state);

if (node.method || (node.value.type === 'FunctionExpression' && !node.value.id)) {
state = {
...state,
Expand Down
2 changes: 2 additions & 0 deletions test/samples/object-expressions/expected.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
obj = { foo, bar, baz: qux };
obj = { "1": "1" };
obj = { true: true };
obj = { foo };
obj = { a: b };

obj = {
Expand Down
2 changes: 1 addition & 1 deletion test/samples/object-expressions/expected.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions test/samples/object-expressions/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ module.exports = ({ b, p }) => {
obj = { "1": "1" };
obj = { true: true };
obj = { "foo": foo };
obj = { ${x} }
obj = {
Expand Down

0 comments on commit 7b01aa7

Please sign in to comment.