From d6daeec0a382ffb9471075f839d12568a4e57100 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 14 Jun 2024 23:51:44 +0800 Subject: [PATCH] Switch `PUNCTUATION_REGEXP` to use RegExp with `u` directly (#15621) --- package.json | 1 - src/language-markdown/constants.evaluate.js | 81 ++++++++++++++------- src/language-markdown/printer-markdown.js | 2 +- yarn.lock | 3 +- 4 files changed, 57 insertions(+), 30 deletions(-) diff --git a/package.json b/package.json index f4828e722..9125f251d 100644 --- a/package.json +++ b/package.json @@ -90,7 +90,6 @@ "postcss-scss": "4.0.9", "postcss-selector-parser": "2.2.3", "postcss-values-parser": "2.0.1", - "regexp-util": "2.0.0", "remark-footnotes": "2.0.0", "remark-math": "3.0.1", "remark-parse": "8.0.3", diff --git a/src/language-markdown/constants.evaluate.js b/src/language-markdown/constants.evaluate.js index 83e46b34f..ff4879ecb 100644 --- a/src/language-markdown/constants.evaluate.js +++ b/src/language-markdown/constants.evaluate.js @@ -24,32 +24,61 @@ const CJK_REGEXP = new RegExp( `(?:${cjkCharset.toString()})(?:${variationSelectorsCharset.toString()})?`, ); -// http://spec.commonmark.org/0.25/#ascii-punctuation-character -const asciiPunctuationCharset = - /* prettier-ignore */ new Charset( - "!", '"', "#", "$", "%", "&", "'", "(", ")", "*", - "+", ",", "-", ".", "/", ":", ";", "<", "=", ">", - "?", "@", "[", "\\", "]", "^", "_", "`", "{", "|", - "}", "~" +const asciiPunctuationCharacters = [ + "!", + '"', + "#", + "$", + "%", + "&", + "'", + "(", + ")", + "*", + "+", + ",", + "-", + ".", + "/", + ":", + ";", + "<", + "=", + ">", + "?", + "@", + "[", + "\\", + "]", + "^", + "_", + "`", + "{", + "|", + "}", + "~", +]; + +// https://spec.commonmark.org/0.25/#punctuation-character +// https://unicode.org/Public/5.1.0/ucd/UCD.html#General_Category_Values +const unicodePunctuationClasses = [ + /* Pc */ "Connector_Punctuation", + /* Pd */ "Dash_Punctuation", + /* Pe */ "Close_Punctuation", + /* Pf */ "Final_Punctuation", + /* Pi */ "Initial_Punctuation", + /* Po */ "Other_Punctuation", + /* Ps */ "Open_Punctuation", +]; + +const PUNCTUATION_REGEXP = new RegExp( + `(?:${[ + new Charset(...asciiPunctuationCharacters).toRegExp().source, + ...unicodePunctuationClasses.map( + (charset) => `\\p{General_Category=${charset}}`, + ), + ].join("|")})`, + "u", ); -// http://spec.commonmark.org/0.25/#punctuation-character -const unicodePunctuationCharset = unicodeRegex({ - // http://unicode.org/Public/5.1.0/ucd/UCD.html#General_Category_Values - General_Category: [ - /* Pc */ "Connector_Punctuation", - /* Pd */ "Dash_Punctuation", - /* Pe */ "Close_Punctuation", - /* Pf */ "Final_Punctuation", - /* Pi */ "Initial_Punctuation", - /* Po */ "Other_Punctuation", - /* Ps */ "Open_Punctuation", - ], -}); - -const PUNCTUATION_REGEXP = new Charset( - asciiPunctuationCharset, - unicodePunctuationCharset, -).toRegExp(); - export { CJK_REGEXP, PUNCTUATION_REGEXP }; diff --git a/src/language-markdown/printer-markdown.js b/src/language-markdown/printer-markdown.js index 22d2a4593..d7ff0263f 100644 --- a/src/language-markdown/printer-markdown.js +++ b/src/language-markdown/printer-markdown.js @@ -95,7 +95,7 @@ function genericPrint(path, options, print) { `(^|${PUNCTUATION_REGEXP.source})(_+)`, `(_+)(${PUNCTUATION_REGEXP.source}|$)`, ].join("|"), - "g", + "gu", ), (_, text1, underscore1, underscore2, text2) => (underscore1 diff --git a/yarn.lock b/yarn.lock index 9e9569837..c77cffff3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7423,7 +7423,6 @@ __metadata: postcss-values-parser: "npm:2.0.1" prettier: "npm:3.3.2" pretty-bytes: "npm:6.1.1" - regexp-util: "npm:2.0.0" remark-footnotes: "npm:2.0.0" remark-math: "npm:3.0.1" remark-parse: "npm:8.0.3" @@ -7631,7 +7630,7 @@ __metadata: languageName: node linkType: hard -"regexp-util@npm:2.0.0, regexp-util@npm:^2.0.0": +"regexp-util@npm:^2.0.0": version: 2.0.0 resolution: "regexp-util@npm:2.0.0" dependencies: