Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(identifier): avoid double escaping slash#18075

Merged
JLHwung merged 1 commit into
babel:mainfrom
JLHwung:fix-validator-identifier-size
Jun 17, 2026
Merged

fix(identifier): avoid double escaping slash#18075
JLHwung merged 1 commit into
babel:mainfrom
JLHwung:fix-validator-identifier-size

Conversation

@JLHwung

@JLHwung JLHwung commented Jun 17, 2026

Copy link
Copy Markdown
Contributor
Q                       A
Fixed Issues? #18054 (comment)
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

In this PR we avoid double escaping slash \ when generating the JSON data. The overall file size will slightly increase by a few bytes because JSON does not support \x, but I think it is worthy of change as the JSON file frees us from manually copy-pasting JS source codes.

@babel-bot

babel-bot commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/61793

@pkg-pr-new

pkg-pr-new Bot commented Jun 17, 2026

Copy link
Copy Markdown

Open in StackBlitz

commit: abb2a7a

@fisker

fisker commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

I think it is worthy of change as the JSON file frees us from manually copy-pasting JS source codes.

Shouldn't it the same if we generate a js/ts file that only export a string?

Update: Like this fisker@afa88d7

@nicolo-ribaudo nicolo-ribaudo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe we can have a rollup or Babel plugin to "minify" these strings by generating at bundle time the smaller escapes.

@JLHwung

JLHwung commented Jun 17, 2026

Copy link
Copy Markdown
Contributor Author

I think it is worthy of change as the JSON file frees us from manually copy-pasting JS source codes.

Shouldn't it the same if we generate a js/ts file that only export a string?

Update: Like this fisker@afa88d7

Thank you. I tried on this branch, we have a Babel plugin to rewrite imports from data/index.ts to data/index.js, which will not be resolvable because we typically do not compile data/*.ts files to data/*.js files.

@JLHwung JLHwung force-pushed the fix-validator-identifier-size branch from 0053089 to abb2a7a Compare June 17, 2026 13:11
@JLHwung

JLHwung commented Jun 17, 2026

Copy link
Copy Markdown
Contributor Author

Now that Babel 8 requires Node.js >= 22.18 which comes with ICU 77.1 supporting Unicode 16, if we want to shrink the size of this big regex which is also rarely used. A potential approach is to replace it with the following format

new RegExp (
  "[\\p{Identifier_Start}<code points assigned Identifier_Start in Unicode 17.0+>]",
  "u"
)

The new format will not contain double-digit hex points in practical because anything < 256 is well-defined and stable prior to Unicode 16.

It should drastically reduce the regex size while maintaining conformance to the latest Unicode standard. The downsize of this approach is that if you run Babel parser in a Node.js --without-intl build with the non-ASCII identifier, the parser will fail because Node.js does not have the required ICU data to evaluate \p{Identifier_Start}. Note that as of now ESLint also does not work on such Node.js build either.

@nicolo-ribaudo

Copy link
Copy Markdown
Member

The downsize of this approach is that if you run Babel parser in a Node.js --without-intl build with the non-ASCII identifier, the parser will fail because Node.js does not have the required ICU data to evaluate \p{Identifier_Start}.

I would be fine with this. For @babel/standalone we would transpile \p{Identifier_Start} away, right?

@JLHwung

JLHwung commented Jun 17, 2026

Copy link
Copy Markdown
Contributor Author

The downsize of this approach is that if you run Babel parser in a Node.js --without-intl build with the non-ASCII identifier, the parser will fail because Node.js does not have the required ICU data to evaluate \p{Identifier_Start}.

I would be fine with this. For @babel/standalone we would transpile \p{Identifier_Start} away, right?

Good question, yes and ideally the transpiled result should be as same as the transpiled result of the \p{Identifier_Start} itself, but I will need to test and confirm that. Note that the transpiled result will be longer than the current regex because the current regex contains only BMP characters and the other characters are handled in the linear search.

@liuxingbaoyu

Copy link
Copy Markdown
Member

I'm concerned that when Unicode 18 is introduced, the parser will behave inconsistently across different engine versions.

@JLHwung

JLHwung commented Jun 17, 2026

Copy link
Copy Markdown
Contributor Author

I'm concerned that when Unicode 18 is introduced, the parser will behave inconsistently across different engine versions.

To clarify, what I mean in

new RegExp (
  "[\\p{Identifier_Start}<code points assigned Identifier_Start in Unicode 17.0+>]",
  "u"
)

is \p{Identifier_Start} and any other code points assigned Identifier_Start in Unicode 17 and above. Sorry that the + is a bit confusing in the regex context.

@JLHwung

JLHwung commented Jun 17, 2026

Copy link
Copy Markdown
Contributor Author

The downsize of this approach is that if you run Babel parser in a Node.js --without-intl build with the non-ASCII identifier, the parser will fail because Node.js does not have the required ICU data to evaluate \p{Identifier_Start}.

I would be fine with this. For @babel/standalone we would transpile \p{Identifier_Start} away, right?

Good question, yes and ideally the transpiled result should be as same as the transpiled result of the \p{Identifier_Start} itself, but I will need to test and confirm that. Note that the transpiled result will be longer than the current regex because the current regex contains only BMP characters and the other characters are handled in the linear search.

To keep this PR focused, I will keep this PR as-is and experiment a bit and open a new PR if this approach is applicable.

@JLHwung JLHwung merged commit 1f23b01 into babel:main Jun 17, 2026
57 checks passed
@JLHwung JLHwung deleted the fix-validator-identifier-size branch June 17, 2026 15:21
@nicolo-ribaudo

Copy link
Copy Markdown
Member

I'm finishing the preset-modules update, then let's release.

@nicolo-ribaudo nicolo-ribaudo added the PR: Bug Fix 🐛 A type of pull request used for our changelog categories label Jun 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: Bug Fix 🐛 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants