diff --git a/packages/babel-plugin-transform-inline-consecutive-adds/__tests__/transform-inline-consecutive-adds-test.js b/packages/babel-plugin-transform-inline-consecutive-adds/__tests__/transform-inline-consecutive-adds-test.js index b25b3894c..6ce2ce2b5 100644 --- a/packages/babel-plugin-transform-inline-consecutive-adds/__tests__/transform-inline-consecutive-adds-test.js +++ b/packages/babel-plugin-transform-inline-consecutive-adds/__tests__/transform-inline-consecutive-adds-test.js @@ -212,6 +212,16 @@ describe("transform-inline-consecutive-adds-plugin", () => { expect(transform(source)).toBe(source); }); + it("should not collapse computed properties with circular reference", () => { + const source = unpad( + ` + var foo = {}; + foo.bar = foo; + ` + ); + expect(transform(source)).toBe(source); + }); + it("should collapse statements with multiple assignments", () => { const source = unpad( ` @@ -291,6 +301,16 @@ describe("transform-inline-consecutive-adds-plugin", () => { expect(transform(source)).toBe(expected); }); + it("should not collapse statements for array-initialized sets with circular reference", () => { + const source = unpad( + ` + var foo = new Set([1, 2]); + foo.add(foo); + ` + ); + expect(transform(source)).toBe(source); + }); + it("should collapse array property assignments", () => { const source = unpad( ` @@ -373,4 +393,14 @@ describe("transform-inline-consecutive-adds-plugin", () => { ); expect(transform(source)).toBe(expected); }); + + it("should not collapse array property assignments if it is circular reference", () => { + const source = unpad( + ` + var foo = []; + foo[2] = foo; + ` + ); + expect(transform(source)).toBe(source); + }); }); diff --git a/packages/babel-plugin-transform-inline-consecutive-adds/src/index.js b/packages/babel-plugin-transform-inline-consecutive-adds/src/index.js index 345d67854..0f9a5989e 100644 --- a/packages/babel-plugin-transform-inline-consecutive-adds/src/index.js +++ b/packages/babel-plugin-transform-inline-consecutive-adds/src/index.js @@ -142,7 +142,7 @@ function getContiguousStatementsAndExpressions( function getReferenceChecker(references) { // returns a function s.t. given an expr, returns true iff expr is an ancestor of a reference - return expr => references.some(r => r.isDescendant(expr)); + return expr => references.some(r => r === expr || r.isDescendant(expr)); } function tryUseCollapser(t, collapser, varDecl, topLevel, checkReference) { diff --git a/packages/babel-plugin-transform-inline-consecutive-adds/src/set-collapser.js b/packages/babel-plugin-transform-inline-consecutive-adds/src/set-collapser.js index 313cad3ff..c23bfe471 100644 --- a/packages/babel-plugin-transform-inline-consecutive-adds/src/set-collapser.js +++ b/packages/babel-plugin-transform-inline-consecutive-adds/src/set-collapser.js @@ -44,7 +44,7 @@ class SetCollapser extends Collapser { if (args.length !== 1) { return false; } - if (checkReference(args)) { + if (checkReference(args[0])) { return false; } return true;