Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix collapse with circular reference #523

Merged
merged 2 commits into from
May 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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])) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should not expect an array (as Line 44), so I change to args[0] made Set works with this PR.

return false;
}
return true;
Expand Down