Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Avoid infinite loops in type inference logic #12390

Merged
merged 1 commit into from Nov 23, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 21 additions & 6 deletions packages/babel-traverse/src/path/inference/index.js
Expand Up @@ -14,6 +14,11 @@ export function getTypeAnnotation(): Object {
return (this.typeAnnotation = type);
}

// Used to avoid infinite recursion in cases like
// var b, c; if (0) { c = 1; b = c; } c = b;
// It also works with indirect recursion.
const typeAnnotationInferringNodes = new WeakSet();

/**
* todo: split up this method
*/
Expand Down Expand Up @@ -47,14 +52,24 @@ export function _getTypeAnnotation(): ?Object {
return node.typeAnnotation;
}

let inferer = inferers[node.type];
if (inferer) {
return inferer.call(this, node);
if (typeAnnotationInferringNodes.has(node)) {
// Bail out from type inference to avoid infinite loops
return;
}
typeAnnotationInferringNodes.add(node);

try {
let inferer = inferers[node.type];
if (inferer) {
return inferer.call(this, node);
}

inferer = inferers[this.parentPath.type];
if (inferer?.validParent) {
return this.parentPath.getTypeAnnotation();
inferer = inferers[this.parentPath.type];
if (inferer?.validParent) {
return this.parentPath.getTypeAnnotation();
}
} finally {
typeAnnotationInferringNodes.delete(node);
}
}

Expand Down
16 changes: 16 additions & 0 deletions packages/babel-traverse/test/inference.js
Expand Up @@ -289,5 +289,21 @@ describe("inference", function () {
const type = path.getTypeAnnotation();
expect(t.isAnyTypeAnnotation(type)).toBeTruthy();
});
it("should not cause a stack overflow when two variable depend on eachother", function () {
const path = getPath(`
var b, c;
while (0) {
c = 1;
b = c;
}
c = b;
`).get("body.2.expression");

expect(path.toString()).toBe("c = b");

// Note: this could technically be "number | void", but the cycle detection
// logic just bails out to "any" to avoid infinite loops.
expect(path.getTypeAnnotation()).toEqual({ type: "AnyTypeAnnotation" });
});
});
});