Skip to content

refactor: Shrink validator identifier size#18084

Merged
nicolo-ribaudo merged 8 commits into
babel:mainfrom
JLHwung:shrink-validator-identifier-size
Jun 30, 2026
Merged

refactor: Shrink validator identifier size#18084
nicolo-ribaudo merged 8 commits into
babel:mainfrom
JLHwung:shrink-validator-identifier-size

Conversation

@JLHwung

@JLHwung JLHwung commented Jun 18, 2026

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

This PR can be reviewed by commits.

In this PR we simplify the big regex we used when validating the identifier name, based on the fact that Node.js 22.18.0, the minimum Node.js version supported by Babel 8, supports Unicode 16.0. The regex is now simplified to

/[\p{ID_Start}<...codepoints assigned ID_Start in Unicode 17 and above>]/u

Ditto for the ID_Continue binary property.

The previous implementation is still kept for Babel-standalone because the transpiled output of \p{ID_Start} will further bloat the bundle size, therefore we introduce an identifier-browser implementation for Babel standalone and also run the unit test against it.

/cc @fisker

@JLHwung JLHwung added the PR: Internal 🏠 A type of pull request used for our changelog categories label Jun 18, 2026
@babel-bot

babel-bot commented Jun 18, 2026

Copy link
Copy Markdown
Collaborator

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors @babel/helper-validator-identifier to reduce identifier validation data/regex size by leveraging Node’s built-in Unicode property escapes (Unicode 16 in the minimum supported Node for Babel 8), while introducing a separate browser implementation to avoid bundle bloat in Babel Standalone.

Changes:

  • Replace large generated identifier data JSON usage with generated identifier-regex.ts using \p{ID_Start} / \p{ID_Continue} plus a small “post-Unicode-16” delta.
  • Add a browser-specific implementation (identifier-browser.ts + identifier-regex-browser.ts) and run the shared identifier-name test suite against it.
  • Update packaging/config to route internal exports via package.json#imports (#identifier) and adjust ignore/prettier config accordingly.

Reviewed changes

Copilot reviewed 17 out of 18 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
yarn.lock Adds regenerate/typings and updates Unicode package versions used for regex generation.
scripts/generators/npm-ignore.ts Stops ignoring now-removed data/ for this package.
packages/babel-helper-validator-identifier/test/identifier.spec.js Refactors tests to reuse shared test helper.
packages/babel-helper-validator-identifier/test/identifier-browser.skip-bundled.js Adds test coverage for the browser implementation (skipped in publish-bundle runs).
packages/babel-helper-validator-identifier/test/helpers/describe-is-identifier-name-tests.js Introduces shared test suite used by both implementations.
packages/babel-helper-validator-identifier/src/index.ts Re-exports identifier helpers via #identifier import mapping.
packages/babel-helper-validator-identifier/src/identifier.ts Switches to new generated regex module and uses fromCodePoint for supplementary checks.
packages/babel-helper-validator-identifier/src/identifier-regex.ts New generated Node-oriented regex + supplementary delta data.
packages/babel-helper-validator-identifier/src/identifier-regex-browser.ts New generated browser-oriented regex/data (no \p{…} usage).
packages/babel-helper-validator-identifier/src/identifier-browser.ts New browser implementation mirroring the previous data-driven logic.
packages/babel-helper-validator-identifier/scripts/generate-identifier-regex.ts Reworks generator to use Unicode ranges + regenerate, and emit new generated modules.
packages/babel-helper-validator-identifier/package.json Adds imports mapping for #identifier and new devDependencies.
packages/babel-helper-validator-identifier/data/supplementary-identifier-start.json Removed (logic moved to generated TS modules).
packages/babel-helper-validator-identifier/data/supplementary-identifier-continue.json Removed (logic moved to generated TS modules).
packages/babel-helper-validator-identifier/data/bmp-identifier-start.json Removed (logic moved to generated TS modules).
packages/babel-helper-validator-identifier/data/bmp-identifier-continue.json Removed (logic moved to generated TS modules).
packages/babel-helper-validator-identifier/.npmignore Stops excluding data/ (directory removed).
.prettierignore Stops excluding this package’s removed data/ directory.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/babel-helper-validator-identifier/src/identifier.ts
Comment thread packages/babel-helper-validator-identifier/src/identifier.ts
Comment thread packages/babel-helper-validator-identifier/scripts/generate-identifier-regex.ts Outdated
@pkg-pr-new

pkg-pr-new Bot commented Jun 18, 2026

Copy link
Copy Markdown

Open in StackBlitz

commit: 6ab1210

@nicolo-ribaudo nicolo-ribaudo merged commit c14d078 into babel:main Jun 30, 2026
57 checks passed
@nicolo-ribaudo nicolo-ribaudo deleted the shrink-validator-identifier-size branch June 30, 2026 09:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: Internal 🏠 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