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

Forward deopt node path #11832

Merged
merged 3 commits into from Aug 20, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 23 additions & 0 deletions packages/babel-core/test/evaluation.js
Expand Up @@ -21,6 +21,25 @@ describe("evaluation", function () {
});
}

function addDeoptTest(code, type, expectedType) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Can you move these tests to packages/babel-traverse/test/evaluation.js?

it(type + " deopt: " + code, function () {
const visitor = {};

visitor[type] = function (path) {
const evaluate = path.evaluate();
expect(evaluate.confident).toBeFalsy();
expect(evaluate.deopt.type).toEqual(expectedType);
};

traverse(
parse(code, {
plugins: ["*"],
}),
visitor,
);
});
}

addTest("void 0", "UnaryExpression", undefined);
addTest("!true", "UnaryExpression", false);
addTest("+'2'", "UnaryExpression", 2);
Expand Down Expand Up @@ -93,4 +112,8 @@ describe("evaluation", function () {
ab: 200,
z: [1, 2, 3],
});

addDeoptTest("({a:{b}})", "ObjectExpression", "Identifier");
addDeoptTest("({[a + 'b']: 1})", "ObjectExpression", "Identifier");
addDeoptTest("[{a}]", "ArrayExpression", "Identifier");
});
6 changes: 3 additions & 3 deletions packages/babel-traverse/src/path/evaluation.js
Expand Up @@ -219,7 +219,7 @@ function _evaluate(path, state) {
if (elemValue.confident) {
arr.push(elemValue.value);
} else {
return deopt(elem, state);
return deopt(elemValue.deopt, state);
}
}
return arr;
Expand All @@ -237,7 +237,7 @@ function _evaluate(path, state) {
if (prop.node.computed) {
key = key.evaluate();
if (!key.confident) {
return deopt(keyPath, state);
return deopt(key.deopt, state);
}
key = key.value;
} else if (key.isIdentifier()) {
Expand All @@ -248,7 +248,7 @@ function _evaluate(path, state) {
const valuePath = prop.get("value");
let value = valuePath.evaluate();
if (!value.confident) {
return deopt(valuePath, state);
return deopt(value.deopt, state);
}
value = value.value;
obj[key] = value;
Expand Down