Skip to content

Commit

Permalink
Deopt number like properties (fix #464)
Browse files Browse the repository at this point in the history
  • Loading branch information
boopathi committed Mar 8, 2017
1 parent ebb9f29 commit 703abe3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ describe("transform-member-expressions-literals-plugin", () => {
expect(transform(source)).toBe(expected);
});

it("should not strip necessaary quotes for numeric like things", () => {
const source = "data['00'] = 5;";
expect(transform(source)).toBe(source);
});

it("should not transform invalid identifiers", () => {
const source = unpad(`
foo["default"];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ module.exports = function({ types: t }) {
}

if (prop.value.match(/^\d+$/)) {
node.property = t.numericLiteral(parseInt(prop.value, 10));
node.computed = false;
const newProp = parseInt(prop.value, 10);
if (newProp.toString() === prop.value) {
node.property = t.numericLiteral(newProp);
node.computed = false;
}
} else if (t.isValidIdentifier(prop.value)) {
node.property = t.identifier(prop.value);
node.computed = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ describe("transform-property-literals-plugin", () => {
expect(transform(source)).toBe(expected);
});

it("should not strip necessaary quotes for numeric like things", () => {
const source = unpad(`
var data = {
"00": 1,
"01": 2
};
`);
expect(transform(source)).toBe(source);
});

it("should not transform invalid identifiers", () => {
const source = unpad(`
({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ module.exports = function({ types: t }) {
}

if (key.value.match(/^\d+$/)) {
node.key = t.numericLiteral(parseInt(node.key.value, 10));
node.computed = false;
const newProp = parseInt(node.key.value, 10);
if (newProp.toString() === node.key.value) {
node.key = t.numericLiteral(newProp);
node.computed = false;
}
} else if (t.isValidIdentifier(key.value)) {
node.key = t.identifier(key.value);
node.computed = false;
Expand Down

0 comments on commit 703abe3

Please # to comment.