An open source design system that's fully customizable and agent ready
TypeScript
fix(core): build Unicode property regexes at runtime (#7767)
Christopher Chedeau5fb597d93277
diff --git a/.changeset/core-runtime-unicode-property-regex.md b/.changeset/core-runtime-unicode-property-regex.mdnew file mode 100644index 0000000000..9f8cf38339--- /dev/null+++ b/.changeset/core-runtime-unicode-property-regex.md@@ -0,0 +1,6 @@+---+'@astryxdesign/core': patch+---++[fix] Source builds through Next.js's Babel no longer fail with `Unknown property: L` (or `Zs`, `Letter`) when the app sets no browserslist or targets a browser without Unicode property escapes. `Avatar`'s initials and the Markdown parser's emphasis and heading-link rules now build their `\p{…}` regular expressions at runtime, as `NumberInput` already does, so there is no literal for Babel to rewrite. Matching is unchanged.+@vjeuxdiff --git a/packages/core/src/Avatar/Avatar.tsx b/packages/core/src/Avatar/Avatar.tsxindex 01de6d4729..f7ac763c86 100644--- a/packages/core/src/Avatar/Avatar.tsx+++ b/packages/core/src/Avatar/Avatar.tsx@@ -387,9 +387,18 @@ export interface AvatarProps extends BaseProps<HTMLDivElement> {/*** A character that can stand for a word in initials: a letter, a digit, or an* emoji. Punctuation and other symbols, such as `(`, `"` or `-`, cannot.+ *+ * Built at runtime, not written as a literal: for targets without property+ * escapes, or when no browserslist is set, Babel's+ * transform-unicode-property-regex rewrites a `\p{…}` literal, and the copy+ * bundled with Next.js lacks the Unicode data it needs, so a source build fails+ * with "Unknown property". A literal is safe once Next's bundled Babel ships+ * that data.*/-const INITIAL_CHARACTER =- /[\p{L}\p{N}\p{Extended_Pictographic}\p{Regional_Indicator}]/u;+const INITIAL_CHARACTER = new RegExp(+ '[\\p{L}\\p{N}\\p{Extended_Pictographic}\\p{Regional_Indicator}]',+ 'u',+);/*** The first character of a word that can stand for it in initials, skippingdiff --git a/packages/core/src/Markdown/parser.ts b/packages/core/src/Markdown/parser.tsindex dc9886c9da..87dab10c58 100644--- a/packages/core/src/Markdown/parser.ts+++ b/packages/core/src/Markdown/parser.ts@@ -1718,8 +1718,14 @@ interface DelimiterRun {readonly canClose: boolean;}-const UNICODE_WHITESPACE = /^[\t\n\f\r\p{Zs}]$/u;-const UNICODE_PUNCTUATION = /^[\p{P}\p{S}]$/u;+// Built at runtime, not written as literals: for targets without property+// escapes, or when no browserslist is set, Babel's+// transform-unicode-property-regex rewrites a `\p{…}` literal, and the copy+// bundled with Next.js lacks the Unicode data it needs, so a source build fails+// with "Unknown property". Literals are safe once Next's bundled Babel ships+// that data.+const UNICODE_WHITESPACE = new RegExp('^[\\t\\n\\f\\r\\p{Zs}]$', 'u');+const UNICODE_PUNCTUATION = new RegExp('^[\\p{P}\\p{S}]$', 'u');/** The character before `index` (a whole code point), if any. */function characterBefore(text: string, index: number): string | undefined {diff --git a/packages/core/src/Markdown/plugins/headingLinks.ts b/packages/core/src/Markdown/plugins/headingLinks.tsindex 0a4a3abd06..40e02017bd 100644--- a/packages/core/src/Markdown/plugins/headingLinks.ts+++ b/packages/core/src/Markdown/plugins/headingLinks.ts@@ -59,13 +59,21 @@ function fail(message: string): never {throw new TypeError(`Markdown heading links: ${message}`);}+// Built at runtime, not written as a literal: for targets without property+// escapes, or when no browserslist is set, Babel's+// transform-unicode-property-regex rewrites a `\p{…}` literal, and the copy+// bundled with Next.js lacks the Unicode data it needs, so a source build fails+// with "Unknown property". A literal is safe once Next's bundled Babel ships+// that data.+const NON_SLUG_CHARACTERS = new RegExp('[^\\p{Letter}\\p{Number}]+', 'gu');+function headingSlug(value: string): string {return value.normalize('NFKC').trim().toLowerCase().replace(/['"]/gu, '')- .replace(/[^\p{Letter}\p{Number}]+/gu, '-')+ .replace(NON_SLUG_CHARACTERS, '-').replace(/^-+|-+$/gu, '');}diff --git a/packages/core/src/unicodePropertyEscapes.source-build.test.mjs b/packages/core/src/unicodePropertyEscapes.source-build.test.mjsnew file mode 100644index 0000000000..1cc997996e--- /dev/null+++ b/packages/core/src/unicodePropertyEscapes.source-build.test.mjs@@ -0,0 +1,69 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file unicodePropertyEscapes.source-build.test.mjs+ * @input Every Core source module a bundler compiles (tests and stories+ * excluded)+ * @output Fails when a module writes a Unicode property escape (`\p{…}` or+ * `\P{…}`) as a regular expression literal+ * @position Guard for source builds: for targets without property escapes, or+ * with no browserslist, Babel rewrites such a literal, and the copy bundled+ * with Next.js lacks the Unicode data to do it, so the build fails with+ * "Unknown property". Modules build these expressions at runtime instead,+ * as NumberInput/numberParser.ts does.+ */++import fs from 'node:fs';+import path from 'node:path';+import {fileURLToPath} from 'node:url';+import {parseSync} from '@babel/core';+import {describe, expect, it} from 'vitest';++const SRC = path.dirname(fileURLToPath(import.meta.url));+const SKIPPED = /\.(test|stories)\.tsx?$|\.d\.ts$/;++function sourceFiles(dir) {+ return fs.readdirSync(dir, {withFileTypes: true}).flatMap(entry => {+ const file = path.join(dir, entry.name);+ if (entry.isDirectory()) {+ return entry.name === '__tests__' ? [] : sourceFiles(file);+ }+ return /\.tsx?$/.test(entry.name) && !SKIPPED.test(entry.name)+ ? [file]+ : [];+ });+}++/** Every regular expression literal in a parsed tree. */+function regexLiterals(node, found = []) {+ if (node == null || typeof node !== 'object') return found;+ if (node.type === 'RegExpLiteral') found.push(node);+ for (const [key, value] of Object.entries(node)) {+ if (key !== 'loc' && key !== 'extra') regexLiterals(value, found);+ }+ return found;+}++describe('Core regular expressions in source builds', () => {+ it('writes no Unicode property escape as a regex literal', () => {+ const offenders = [];+ for (const file of sourceFiles(SRC)) {+ const source = fs.readFileSync(file, 'utf8');+ if (!/\\[pP]\{/.test(source)) continue;+ const ast = parseSync(source, {+ filename: file,+ babelrc: false,+ configFile: false,+ parserOpts: {plugins: ['typescript', 'jsx']},+ });+ for (const literal of regexLiterals(ast)) {+ if (/\\[pP]\{/.test(literal.pattern)) {+ offenders.push(+ `${path.relative(SRC, file)}:${literal.loc.start.line} /${literal.pattern}/${literal.flags}`,+ );+ }+ }+ }+ expect(offenders).toEqual([]);+ });+});