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

Fix TaggedTemplateLiteral evaluation #15224

Merged
merged 3 commits into from Jan 3, 2023
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
4 changes: 3 additions & 1 deletion packages/babel-traverse/src/path/evaluation.ts
Expand Up @@ -430,7 +430,9 @@ function evaluateQuasis(
let str = "";

let i = 0;
const exprs = path.get("expressions");
const exprs: Array<NodePath<t.Node>> = path.isTemplateLiteral()
? path.get("expressions")
: path.get("quasi.expressions");

for (const elem of quasis) {
// not confident, evaluated an expression we don't like
Expand Down
27 changes: 21 additions & 6 deletions packages/babel-traverse/test/evaluation.js
Expand Up @@ -100,12 +100,27 @@ describe("evaluation", function () {
).toBe(false);
});

it("should evaluate template literals", function () {
expect(
getPath("var x = 8; var y = 1; var z = `value is ${x >>> y}`")
.get("body.2.declarations.0.init")
.evaluate().value,
).toBe("value is 4");
describe("template literals", function () {
it("should evaluate template literals", function () {
expect(
getPath("var x = 8; var y = 1; var z = `value is ${x >>> y}`")
.get("body.2.declarations.0.init")
.evaluate().value,
).toBe("value is 4");
});

it("shold evaluate String.raw tags", function () {
expect(
getPath("String.raw`a\\n${1}\\u`;").get("body.0.expression").evaluate()
.value,
).toBe("a\\n1\\u");
Comment on lines +113 to +116
Copy link
Member

Choose a reason for hiding this comment

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

This test was failing on main, returning a\\n\\u.

});

addDeoptTest(
"a`x${b}y`",
"TaggedTemplateExpression",
"TaggedTemplateExpression",
);
});

it("should evaluate member expressions", function () {
Expand Down