Skip to content

feat(compiler): Implement constant propagation for template literals #29716

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

Closed
wants to merge 1 commit into from

Conversation

nikeee
Copy link
Contributor

@nikeee nikeee commented Jun 2, 2024

Template literals consisting entirely of constant values will be inlined to a string literal, effectively replacing the backticks with a double quote.

This is done primarily to make the resulting instruction a string literal, so it can be processed further in constant propatation. So this is now correctly simplified to true:

`` === "" // now true
`a${1}` === "a1" // now true

If a template literal contains a non-constant value, the entire literal is left as-is. Transforming a template literal to one that has only partially inlined is something that can be improved upon in the future.

Copy link

vercel bot commented Jun 2, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-compiler-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 16, 2024 10:28am

@react-sizebot
Copy link

react-sizebot commented Jun 2, 2024

Comparing: d77dd31...cee1f11

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.js = 6.66 kB 6.66 kB = 1.82 kB 1.82 kB
oss-stable/react-dom/cjs/react-dom-client.production.js = 496.48 kB 496.48 kB = 88.87 kB 88.87 kB
oss-experimental/react-dom/cjs/react-dom.production.js = 6.67 kB 6.67 kB = 1.83 kB 1.83 kB
oss-experimental/react-dom/cjs/react-dom-client.production.js = 501.30 kB 501.30 kB = 89.56 kB 89.56 kB
facebook-www/ReactDOM-prod.classic.js = 593.97 kB 593.97 kB = 104.48 kB 104.48 kB
facebook-www/ReactDOM-prod.modern.js = 570.35 kB 570.35 kB = 100.89 kB 100.89 kB
test_utils/ReactAllWarnings.js Deleted 63.65 kB 0.00 kB Deleted 15.90 kB 0.00 kB

Significant size changes

Includes any change greater than 0.2%:

Expand to show
Name +/- Base Current +/- gzip Base gzip Current gzip
test_utils/ReactAllWarnings.js Deleted 63.65 kB 0.00 kB Deleted 15.90 kB 0.00 kB

Generated by 🚫 dangerJS against cee1f11

}

const suffix = value.quasis[quasiIndex].cooked;
resultString += subExprValue.value.toString() + suffix;
Copy link
Member

Choose a reason for hiding this comment

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

can you double-check the spec behavior here? is it calling toString() or something else? would be great to link to the appropriate point in the spec for posterity.

Copy link
Contributor Author

@nikeee nikeee Jun 5, 2024

Choose a reason for hiding this comment

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

This is a good concern. I've looked it up:
https://262.ecma-international.org/6.0/#sec-template-literals-runtime-semantics-evaluation

It uses string coercion (defined as ToString), wich is defined here: https://262.ecma-international.org/6.0/#sec-tostring
We only deal with string/number/boolean in the case above. So I think the only case that would be non-trivial is ToString(number), which is defined here. This is the definition of Number.prototype.toString(), which - in case of radix = 10 - just returns ToString(number).

If I'm not mistaking, it matches the spec as long as the values are number/string/booleans. It should be a different story when it is null/undefined (which is excluded via the return above).

In the note it says that the coercion basically behaves like when using String.prototype.concat, which is also the way the TS compiler implements this when lowering to ES3/ES5 (they seem to assume that concat operation is not monkey-patched).

Is this fine or should we just handle the cases where it's trivially always a string? More sophisticated stuff can always be added later. What do you think?

Copy link
Member

@josephsavona josephsavona left a comment

Choose a reason for hiding this comment

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

My apologies, I overlooked this one. Thanks for the PR! This makes sense and seems reasonable to include, see comments just to make sure the behavior is identical to the spec. We should also test more edge cases: where the interpolation is at the beginning or end of the string, where there are multiple interpolations without any intervening text, with odd values like NaN, empty string, etc.

@nikeee nikeee force-pushed the template-string-inlining branch from 8bfecfc to cee1f11 Compare June 5, 2024 00:04
@nikeee nikeee marked this pull request as draft June 5, 2024 00:04
@nikeee
Copy link
Contributor Author

nikeee commented Jun 5, 2024

We should also test more edge cases: where the interpolation is at the beginning or end of the string, where there are multiple interpolations without any intervening text, with odd values like NaN, empty string, etc.

I agree, will do!

Template literals consisting entirely of constant values will be inlined to a string literal,
effectively replacing the backticks with a double quote.

This is done primarily to make the resulting instruction a string literal, so it can be
processed further in constant propatation. So this is now correctly simplified to `true`:
```js
`` === "" // now true
`a${1}` === "a1"
```

If a template literal contains a non-constant value, the entire literal is left as-is.
Transforming a template literal to one that has only partially inlined is something
that can be improved upon in the future.
Copy link

This pull request has been automatically marked as stale. If this pull request is still relevant, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize reviewing it yet. Your contribution is very much appreciated.

@github-actions github-actions bot added the Resolution: Stale Automatically closed due to inactivity label Oct 14, 2024
Copy link

Closing this pull request after a prolonged period of inactivity. If this issue is still present in the latest release, please ask for this pull request to be reopened. Thank you!

@github-actions github-actions bot closed this Oct 21, 2024
josephsavona pushed a commit that referenced this pull request May 8, 2025
…33139)

New take on #29716

## Summary
Template literals consisting entirely of constant values will be inlined
to a string literal, effectively replacing the backticks with a double
quote.

This is done primarily to make the resulting instruction a string
literal, so it can be processed further in constant propatation. So this
is now correctly simplified to `true`:
```js
`` === "" // now true
`a${1}` === "a1" // now true
```

If a template string literal can only partially be comptime-evaluated,
it is not that useful for dead code elimination or further constant
folding steps and thus, is left as-is in that case. Same is true if the
literal contains an array, object, symbol or function.

## How did you test this change?

See added tests.
github-actions bot pushed a commit that referenced this pull request May 8, 2025
…33139)

New take on #29716

## Summary
Template literals consisting entirely of constant values will be inlined
to a string literal, effectively replacing the backticks with a double
quote.

This is done primarily to make the resulting instruction a string
literal, so it can be processed further in constant propatation. So this
is now correctly simplified to `true`:
```js
`` === "" // now true
`a${1}` === "a1" // now true
```

If a template string literal can only partially be comptime-evaluated,
it is not that useful for dead code elimination or further constant
folding steps and thus, is left as-is in that case. Same is true if the
literal contains an array, object, symbol or function.

## How did you test this change?

See added tests.

DiffTrain build for [ac06829](ac06829)
github-actions bot pushed a commit that referenced this pull request May 8, 2025
…33139)

New take on #29716

## Summary
Template literals consisting entirely of constant values will be inlined
to a string literal, effectively replacing the backticks with a double
quote.

This is done primarily to make the resulting instruction a string
literal, so it can be processed further in constant propatation. So this
is now correctly simplified to `true`:
```js
`` === "" // now true
`a${1}` === "a1" // now true
```

If a template string literal can only partially be comptime-evaluated,
it is not that useful for dead code elimination or further constant
folding steps and thus, is left as-is in that case. Same is true if the
literal contains an array, object, symbol or function.

## How did you test this change?

See added tests.

DiffTrain build for [ac06829](ac06829)
github-actions bot pushed a commit to code/lib-react that referenced this pull request May 8, 2025
…acebook#33139)

New take on facebook#29716

## Summary
Template literals consisting entirely of constant values will be inlined
to a string literal, effectively replacing the backticks with a double
quote.

This is done primarily to make the resulting instruction a string
literal, so it can be processed further in constant propatation. So this
is now correctly simplified to `true`:
```js
`` === "" // now true
`a${1}` === "a1" // now true
```

If a template string literal can only partially be comptime-evaluated,
it is not that useful for dead code elimination or further constant
folding steps and thus, is left as-is in that case. Same is true if the
literal contains an array, object, symbol or function.

## How did you test this change?

See added tests.

DiffTrain build for [ac06829](facebook@ac06829)
github-actions bot pushed a commit to code/lib-react that referenced this pull request May 8, 2025
…acebook#33139)

New take on facebook#29716

## Summary
Template literals consisting entirely of constant values will be inlined
to a string literal, effectively replacing the backticks with a double
quote.

This is done primarily to make the resulting instruction a string
literal, so it can be processed further in constant propatation. So this
is now correctly simplified to `true`:
```js
`` === "" // now true
`a${1}` === "a1" // now true
```

If a template string literal can only partially be comptime-evaluated,
it is not that useful for dead code elimination or further constant
folding steps and thus, is left as-is in that case. Same is true if the
literal contains an array, object, symbol or function.

## How did you test this change?

See added tests.

DiffTrain build for [ac06829](facebook@ac06829)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants