Skip to content

Commit

Permalink
Fix folding Array literals (close #608) (close #609)
Browse files Browse the repository at this point in the history
  • Loading branch information
boopathi committed Jun 30, 2017
1 parent fd79492 commit ca943e7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,16 @@ describe("constant-folding-plugin", () => {
({}).toString;
`
);

thePlugin(
"should bail for spread element in array",
`
function foo() {
return [...iter].length;
}
function bar() {
return [...iter][0];
}
`
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@ module.exports = ({ types: t }) => {
ArrayExpression: {
members: {
length() {
if (this.elements.some(el => t.isSpreadElement(el))) {
return;
}
return t.numericLiteral(this.elements.length);
},
[FALLBACK_HANDLER](i) {
if (this.elements.some(el => t.isSpreadElement(el))) {
return;
}
if (typeof i === "number" || i.match(/^\d+$/)) {
return this.elements[i] || undef;
}
Expand Down

0 comments on commit ca943e7

Please sign in to comment.