Skip to content

Commit

Permalink
Fix collapse with circular reference (#523)
Browse files Browse the repository at this point in the history
* Add tests for collapse with circular reference

* Fix collapse with circular reference
  • Loading branch information
jhen0409 authored and boopathi committed May 8, 2017
1 parent 9a5bf2a commit 673c617
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
`
Expand Down Expand Up @@ -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(
`
Expand Down Expand Up @@ -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);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 673c617

Please sign in to comment.