Skip to content

feat(codegen)!: print StringLiteral raw if minify option disabled#10553

Merged
graphite-app[bot] merged 1 commit intomainfrom
04-22-feat_codegen_print_stringliteral_raw_if_minify_option_disabled
Apr 23, 2025
Merged

feat(codegen)!: print StringLiteral raw if minify option disabled#10553
graphite-app[bot] merged 1 commit intomainfrom
04-22-feat_codegen_print_stringliteral_raw_if_minify_option_disabled

Conversation

@overlookmotel
Copy link
Member

@overlookmotel overlookmotel commented Apr 22, 2025

Closes #10214.

When options.minify is false, print raw StringLiteral as unchanged as possible from how it appeared in source code. But still obey options.single_quotes.

  • If options.single_quotes == true and string was originally quoted with ', print raw string unchanged.
  • If options.single_quotes == false and string was originally quoted with ", print raw string unchanged.
  • Otherwise, alter the quotes to those requested, and escape any quotes as required within the raw string content. Do not escape or unescape any other characters.
  • If StringLiteral has no raw representation, encode from value, using the quote character requested and escape characters as required (like when options.minify is true).

Copy link
Member Author

overlookmotel commented Apr 22, 2025


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@github-actions github-actions bot added A-codegen Area - Code Generation C-enhancement Category - New feature or request labels Apr 22, 2025
@overlookmotel overlookmotel marked this pull request as ready for review April 22, 2025 20:50
@codspeed-hq
Copy link

codspeed-hq bot commented Apr 22, 2025

CodSpeed Instrumentation Performance Report

Merging #10553 will create unknown performance changes

Comparing 04-22-feat_codegen_print_stringliteral_raw_if_minify_option_disabled (10e1018) with main (14bb2be)

Summary

🆕 36 new benchmarks

Benchmarks breakdown

Benchmark BASE HEAD Change
🆕 codegen[checker.ts] N/A 22.1 ms N/A
🆕 codegen_sourcemap[checker.ts] N/A 65 ms N/A
🆕 formatter[antd.js] N/A 708.4 ms N/A
🆕 formatter[react.development.js] N/A 8 ms N/A
🆕 formatter[typescript.js] N/A 1.1 s N/A
🆕 isolated-declarations[vue-id.ts] N/A 58.4 ms N/A
🆕 lexer[RadixUIAdoptionSection.jsx] N/A 21.3 µs N/A
🆕 lexer[antd.js] N/A 24.8 ms N/A
🆕 lexer[cal.com.tsx] N/A 5.9 ms N/A
🆕 lexer[checker.ts] N/A 14.9 ms N/A
🆕 lexer[pdf.mjs] N/A 3.9 ms N/A
🆕 linter[RadixUIAdoptionSection.jsx] N/A 2.7 ms N/A
🆕 linter[cal.com.tsx] N/A 1.2 s N/A
🆕 linter[checker.ts] N/A 3 s N/A
🆕 mangler[antd.js] N/A 15.9 ms N/A
🆕 mangler[react.development.js] N/A 293.9 µs N/A
🆕 mangler[typescript.js] N/A 39.4 ms N/A
🆕 minifier[antd.js] N/A 163.6 ms N/A
🆕 minifier[react.development.js] N/A 1.8 ms N/A
🆕 minifier[typescript.js] N/A 287.9 ms N/A
... ... ... ... ...

ℹ️ Only the first 20 benchmarks are displayed. Go to the app to view all benchmarks.

@overlookmotel overlookmotel marked this pull request as draft April 22, 2025 20:51
@overlookmotel
Copy link
Member Author

overlookmotel commented Apr 22, 2025

The failing test is https://github.com/oxc-project/oxc/tree/7e712820e9d19d2b1293b7e8ffe9f4275a34323c/tasks/transform_conformance/tests/babel-preset-typescript/test/fixtures/removeImportExtensions

-import "./a";
-import "./a";
-import "./a";
-import "./react";
+import "./a.ts";
+import "./a.mts";
+import "./a.cts";
+import "./react.tsx";
-import "a-package/file";
+import "a-package/file.ts";

My guess is we are altering the value of StringLiterals, without setting raw to None.

@Dunqing Does that sound possible?

@overlookmotel overlookmotel force-pushed the 04-22-feat_codegen_print_stringliteral_raw_if_minify_option_disabled branch from 0ba9775 to bc921a4 Compare April 22, 2025 22:20
@overlookmotel overlookmotel force-pushed the 04-22-test_codegen_add_more_tests_for_strings branch from 940fa1f to efbdc0a Compare April 22, 2025 22:20
@overlookmotel
Copy link
Member Author

Yes, that was it. Fixed in #10554.

@graphite-app graphite-app bot added the 0-merge Merge with Graphite Merge Queue label Apr 23, 2025
@graphite-app
Copy link
Contributor

graphite-app bot commented Apr 23, 2025

Merge activity

…ed (#10553)

Closes #10214.

When `options.minify` is `false`, print raw `StringLiteral` as unchanged as possible from how it appeared in source code. But still obey `options.single_quotes`.

* If `options.single_quotes == true` and string was originally quoted with `'`, print raw string unchanged.
* If `options.single_quotes == false` and string was originally quoted with `"`, print raw string unchanged.
* Otherwise, alter the quotes to those requested, and escape any quotes as required within the raw string content. Do not escape or unescape any other characters.
* If `StringLiteral` has no `raw` representation, encode from `value`, using the quote character requested and escape characters as required (like when `options.minify` is `true`).
@graphite-app graphite-app bot force-pushed the 04-22-test_codegen_add_more_tests_for_strings branch from efbdc0a to 14bb2be Compare April 23, 2025 03:27
@graphite-app graphite-app bot force-pushed the 04-22-feat_codegen_print_stringliteral_raw_if_minify_option_disabled branch from bc921a4 to 10e1018 Compare April 23, 2025 03:27
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Apr 23, 2025
Base automatically changed from 04-22-test_codegen_add_more_tests_for_strings to main April 23, 2025 03:36
@graphite-app graphite-app bot merged commit 10e1018 into main Apr 23, 2025
27 checks passed
@graphite-app graphite-app bot deleted the 04-22-feat_codegen_print_stringliteral_raw_if_minify_option_disabled branch April 23, 2025 03:39
Boshen added a commit that referenced this pull request Apr 23, 2025
Boshen added a commit that referenced this pull request Apr 23, 2025
… `minify` option disabled (#10553) (#10569)

This reverts commit 10e1018.

After experimenting with downstream crates like Rolldown, I discovered
this introduces a footgun.

It's really easy to change `StringLiteral::value` without also changing
`StringLiteral::raw`, which results in incorrect codegen.

After some consideration, I've decided against introducing an extra
option for printing the raw value, it adds a significant amount of
maintenance burden. (This issue alone is already consuming a lot of our
time). We will need to three modes in the codegen: normal, minify and
raw.

For now, let's assume our codegen will not preserve the original source
text. If preserving the original source text is a requirement, please
use magic string or any other direct string manipulation methods.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-codegen Area - Code Generation C-enhancement Category - New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

oxc_codegen: support printing StringLiteral using StringLiteral#raw

2 participants