Skip to content

Commit

Permalink
Enable require-unicode-regexp and regexp/strict rule (#16408)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jun 17, 2024
1 parent 22dad9c commit c03a12a
Show file tree
Hide file tree
Showing 127 changed files with 371 additions and 358 deletions.
19 changes: 10 additions & 9 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export default [
"prefer-rest-params": "error",
"prefer-spread": "error",
"require-await": "error",
"require-unicode-regexp": "error",
"symbol-description": "error",
yoda: [
"error",
Expand All @@ -149,9 +150,7 @@ export default [
"prettier-internal-rules/no-identifier-n": "error",
"prettier-internal-rules/prefer-fs-promises-submodule": "error",

// @typescript-eslint/eslint-plugin
"@typescript-eslint/prefer-ts-expect-error": "error",

/* @stylistic/eslint-plugin-js */
"@stylistic/js/quotes": [
"error",
"double",
Expand All @@ -160,7 +159,10 @@ export default [
},
],

// eslint-plugin-import
/* @typescript-eslint/eslint-plugin */
"@typescript-eslint/prefer-ts-expect-error": "error",

/* eslint-plugin-import */
"import/no-extraneous-dependencies": [
"error",
{
Expand All @@ -174,10 +176,10 @@ export default [
},
],

// eslint-plugin-n
/* eslint-plugin-n */
"n/no-path-concat": "error",

// eslint-plugin-regexp
/* eslint-plugin-regexp */
"regexp/match-any": [
"error",
{
Expand All @@ -200,8 +202,6 @@ export default [
strictTypes: false,
},
],
// Conflicting with `unicorn/better-regex`
"regexp/strict": "off",
"regexp/no-super-linear-backtracking": "off",
"regexp/unicode-property": [
"error",
Expand All @@ -216,10 +216,11 @@ export default [
},
],

/* eslint-plugin-simple-import-sort */
"simple-import-sort/imports": "error",
"simple-import-sort/exports": "error",

// eslint-plugin-unicorn
/* eslint-plugin-unicorn */
"unicorn/escape-case": "off",
"unicorn/catch-error-name": "off",
"unicorn/consistent-destructuring": "off",
Expand Down
4 changes: 2 additions & 2 deletions scripts/build/build-javascript-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function getEsbuildOptions({ file, files, shouldCollectLicenses, cliOptions }) {
process(text) {
const exports = [
...text.matchAll(
/(?<=\n)exports\.(?<specifier>\w+) = \k<specifier>;/g,
/(?<=\n)exports\.(?<specifier>\w+) = \k<specifier>;/gu,
),
].map((match) => match.groups.specifier);

Expand Down Expand Up @@ -112,7 +112,7 @@ function getEsbuildOptions({ file, files, shouldCollectLicenses, cliOptions }) {
// #12493, not sure what the problem is, but replace the cjs version with esm version seems fix it
{
module: require.resolve("tslib"),
path: require.resolve("tslib").replace(/tslib\.js$/, "tslib.es6.js"),
path: require.resolve("tslib").replace(/tslib\.js$/u, "tslib.es6.js"),
},
// https://github.com/evanw/esbuild/issues/2103
{
Expand Down
6 changes: 3 additions & 3 deletions scripts/build/build-package-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ async function buildPackageJson({ file, files }) {
return [
file.isPlugin ? `./plugins/${basename}` : `./${basename}`,
{
types: `./${file.output.file.replace(/\.js$/, ".d.ts")}`,
types: `./${file.output.file.replace(/\.js$/u, ".d.ts")}`,
require: `./${file.output.file}`,
default: `./${file.output.file.replace(/\.js$/, ".mjs")}`,
default: `./${file.output.file.replace(/\.js$/u, ".mjs")}`,
},
];
}),
Expand All @@ -86,7 +86,7 @@ async function buildPackageJson({ file, files }) {
[`./parser-${basename}.js`, `./${file.output.file}`],
[
`./esm/parser-${basename}.mjs`,
`./${file.output.file.replace(/\.js$/, ".mjs")}`,
`./${file.output.file.replace(/\.js$/u, ".mjs")}`,
],
];
}),
Expand Down
5 changes: 1 addition & 4 deletions scripts/build/build-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ async function typesFileBuilder({ file }) {
const replacements = pathReplacementMap[file.input] ?? [];
let text = await fs.promises.readFile(file.input, "utf8");
for (const { from, to } of replacements) {
text = text.replaceAll(
new RegExp(` from "${from}";`, "g"),
` from "${to}";`,
);
text = text.replaceAll(` from "${from}";`, ` from "${to}";`);
}
await writeFile(path.join(DIST_DIR, file.output.file), text);
}
Expand Down
16 changes: 8 additions & 8 deletions scripts/build/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const copyFileBuilder = ({ file }) =>
function getTypesFileConfig({ input: jsFileInput, outputBaseName, isPlugin }) {
let input = jsFileInput;
if (!isPlugin) {
input = jsFileInput.replace(/\.[cm]?js$/, ".d.ts");
input = jsFileInput.replace(/\.[cm]?js$/u, ".d.ts");

if (!fs.existsSync(path.join(PROJECT_ROOT, input))) {
return;
Expand Down Expand Up @@ -159,7 +159,7 @@ const pluginFiles = [
{
module: "*",
process(text, file) {
if (/require\(["'](?:typescript|ts-api-utils)["']\)/.test(text)) {
if (/require\(["'](?:typescript|ts-api-utils)["']\)/u.test(text)) {
throw new Error(`Unexpected \`require("typescript")\` in ${file}.`);
}

Expand Down Expand Up @@ -274,21 +274,21 @@ const pluginFiles = [
process(text) {
const typescriptVariables = [
...text.matchAll(
/import (?<variable>\w+) from ["']typescript["']/g,
/import (?<variable>\w+) from ["']typescript["']/gu,
),
].map((match) => match.groups.variable);

// Remove `'property' in typescript` check
text = text.replaceAll(
new RegExp(
`".*?" in (?:${typescriptVariables.join("|")})(?=\\W)`,
"g",
"gu",
),
"true",
);

text = text.replaceAll(
/(?<=import )(?=\w+ from ["']typescript["'])/g,
/(?<=import )(?=\w+ from ["']typescript["'])/gu,
"* as ",
);
return text;
Expand Down Expand Up @@ -373,7 +373,7 @@ const pluginFiles = [
].map((file) => ({
module: getPackageFile(`@angular/compiler/esm2022/src/${file}`),
process: (text) =>
text.replaceAll(/(?<=import .*? from )'(.{1,2}\/.*)'/g, "'$1.mjs'"),
text.replaceAll(/(?<=import .*? from )'(.{1,2}\/.*)'/gu, "'$1.mjs'"),
})),
],
},
Expand Down Expand Up @@ -468,7 +468,7 @@ const pluginFiles = [
"const LOCAL_DEBUG = false &&",
);

text = text.replace(/(?<=\n)export .*?;/, "export { preprocess };");
text = text.replace(/(?<=\n)export .*?;/u, "export { preprocess };");

return text;
},
Expand All @@ -488,7 +488,7 @@ const pluginFiles = [

let { input, umdPropertyName, outputBaseName, ...buildOptions } = file;

outputBaseName ??= input.match(/\/plugins\/(?<outputBaseName>.*?)\.js$/)
outputBaseName ??= input.match(/\/plugins\/(?<outputBaseName>.*?)\.js$/u)
.groups.outputBaseName;

const umdVariableName = `prettierPlugins.${
Expand Down
4 changes: 2 additions & 2 deletions scripts/build/esbuild-plugins/add-default-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default function esbuildPluginAddDefaultExport() {

let entry;

build.onResolve({ filter: /./ }, (module) => {
build.onResolve({ filter: /./u }, (module) => {
if (module.kind === "entry-point") {
const relativePath = module.path
.slice(module.resolveDir.length + 1)
Expand All @@ -23,7 +23,7 @@ export default function esbuildPluginAddDefaultExport() {
}
});

build.onLoad({ filter: /./, namespace }, () => {
build.onLoad({ filter: /./u, namespace }, () => {
const directory = path.dirname(entry);
const source = `./${path.basename(entry)}`;

Expand Down
2 changes: 1 addition & 1 deletion scripts/build/esbuild-plugins/evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default function esbuildPluginEvaluate() {
setup(build) {
const { format } = build.initialOptions;

build.onLoad({ filter: /\.evaluate\.c?js$/ }, async ({ path }) => {
build.onLoad({ filter: /\.evaluate\.c?js$/u }, async ({ path }) => {
const module = await importModule(path);
const text = Object.entries(module)
.map(([specifier, value]) => {
Expand Down
4 changes: 2 additions & 2 deletions scripts/build/esbuild-plugins/replace-module.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fs from "node:fs/promises";

const DEFAULT_ON_RESOLVE_CONCEPTS = { filter: /./, namespace: "file" };
const DEFAULT_ON_RESOLVE_CONCEPTS = { filter: /./u, namespace: "file" };
const DEFAULT_ON_LOAD_CONCEPTS = {
filter: /\.(?:js|json|mjs|cjs)$/,
filter: /\.(?:js|json|mjs|cjs)$/u,
namespace: "file",
};

Expand Down
2 changes: 1 addition & 1 deletion scripts/build/esbuild-plugins/strip-node-protocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export default function esbuildPluginStripNodeProtocol() {
return {
name: "strip-node-protocol",
setup(build) {
build.onResolve({ filter: /^node:/ }, ({ path, kind }) =>
build.onResolve({ filter: /^node:/u }, ({ path, kind }) =>
build.resolve(path.slice(5), { kind }),
);
},
Expand Down
16 changes: 8 additions & 8 deletions scripts/build/esmify-typescript-eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function esmifyTypescriptEslint(text) {
*/
text = text.replaceAll(
// TODO: Use duplicate capture group name when eslint supports
/(?<=\n)(?:const|let|var) (?<variable>\w+) = (?:__importStar\(require\(["'](?<moduleName1>.*?)["']\)\)|require\(["'](?<moduleName2>.*?)["']\));/g,
/(?<=\n)(?:const|let|var) (?<variable>\w+) = (?:__importStar\(require\(["'](?<moduleName1>.*?)["']\)\)|require\(["'](?<moduleName2>.*?)["']\));/gu,
(...args) => {
const groups = args.at(-1);
return `import * as ${groups.variable} from "${groups.moduleName1 || groups.moduleName2}";`;
Expand All @@ -37,7 +37,7 @@ function esmifyTypescriptEslint(text) {
````
*/
text = text.replaceAll(
/(?<=\n)(?:const|let|var) (?<variable>\w+) = __importDefault\(require\(["'](?<moduleName>.*?)["']\)\);/g,
/(?<=\n)(?:const|let|var) (?<variable>\w+) = __importDefault\(require\(["'](?<moduleName>.*?)["']\)\);/gu,
outdent`
import $<variable>_default_export from "$<moduleName>";
const $<variable> = {default: $<variable>_default_export};
Expand All @@ -49,9 +49,9 @@ function esmifyTypescriptEslint(text) {
'Object.defineProperty(exports, "__esModule", { value: true });',
"",
);
text = text.replaceAll(/(?<=\n)(?:exports\.\w+ = )+void 0;/g, "");
text = text.replaceAll(/(?<=\n)(?:exports\.\w+ = )+void 0;/gu, "");
text = text.replaceAll(
/(?<=\n)exports\.(?<specifier>\w+) = (?<variable>\w+);/g,
/(?<=\n)exports\.(?<specifier>\w+) = (?<variable>\w+);/gu,
(...args) => {
const { variable, specifier } = args.at(-1);
if (specifier === variable) {
Expand All @@ -63,7 +63,7 @@ function esmifyTypescriptEslint(text) {
);

text = text.replaceAll(
/(?<=\n)exports\.(?<specifier>\w+)(?= = `)/g,
/(?<=\n)exports\.(?<specifier>\w+)(?= = `)/gu,
"export const $<specifier>",
);

Expand All @@ -77,7 +77,7 @@ function esmifyTypescriptEslint(text) {
````
*/
text = text.replaceAll(
/(?<=\n)__exportStar\(require\(["'](?<moduleName>.*?)["']\), exports\);/g,
/(?<=\n)__exportStar\(require\(["'](?<moduleName>.*?)["']\), exports\);/gu,
'export * from "$<moduleName>";',
);

Expand All @@ -91,7 +91,7 @@ function esmifyTypescriptEslint(text) {
````
*/
text = text.replaceAll(
/(?<=\n)Object\.defineProperty\(exports, "(?<specifier>\w+)", { enumerable: true, get: function \(\) { return (?<value>.*?); } }\);/g,
/(?<=\n)Object\.defineProperty\(exports, "(?<specifier>\w+)", \{ enumerable: true, get: function \(\) \{ return (?<value>.*?); \} \}\);/gu,
"export const $<specifier> = $<value>;",
);

Expand All @@ -106,7 +106,7 @@ function esmifyTypescriptEslint(text) {
````
*/
text = text.replaceAll(
/(?<=\n)exports\.(?<specifier>\w+) = __importStar\(require\(["'](?<moduleName>.*?)["']\)\);/g,
/(?<=\n)exports\.(?<specifier>\w+) = __importStar\(require\(["'](?<moduleName>.*?)["']\)\);/gu,
outdent`
import * as $<specifier>_namespace_export from "$<moduleName>";
export {$<specifier>_namespace_export as $<specifier>};
Expand Down
4 changes: 2 additions & 2 deletions scripts/build/modify-typescript-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { PROJECT_ROOT, writeFile } from "../utils/index.js";
import UNUSED_SPECIFIERS from "./typescript-unused-specifiers.js";

function* getModules(text) {
const parts = text.split(/(?<=\n)(\/\/ src\/\S+\n)/);
const parts = text.split(/(?<=\n)(\/\/ src\/\S+\n)/u);

let start = parts[0].length;

Expand Down Expand Up @@ -152,7 +152,7 @@ function getExports(entry) {
const exports = lines
.map((line) => {
const match = line.match(
/^\s*(?<specifier>.*?): \(\) => (?<variable>.*?),?$/,
/^\s*(?<specifier>.*?): \(\) => (?<variable>.*?),?$/u,
);

if (!match) {
Expand Down
2 changes: 1 addition & 1 deletion scripts/draft-blog-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const nextVersion = `${semver.major(version)}.${semver.minor(
const postGlob = path.join(blogDir, `????-??-??-${nextVersion}.md`);
const postFile = path.join(
blogDir,
`${new Date().toISOString().replace(/T.+/, "")}-${nextVersion}.md`,
`${new Date().toISOString().replace(/T.+/u, "")}-${nextVersion}.md`,
);

const categories = [
Expand Down
2 changes: 1 addition & 1 deletion scripts/generate-unused-typescript-specifiers.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async function getRemovedSpecifiers(code, exports) {
const specifiers = [];
for (const { text } of errors) {
const match = text.match(
/^"(?<variable>.*?)" is not declared in this file$/,
/^"(?<variable>.*?)" is not declared in this file$/u,
);

if (match) {
Expand Down
10 changes: 5 additions & 5 deletions scripts/lint-changelog.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ for (const file of [
}
}

const authorRegex = /by @[\w-]+|by \[@([\w-]+)]\(https:\/\/github\.com\/\1\)/;
const titleRegex = /^#{4} (.*?)\((#\d{4,}|\[#\d{4,}])/;
const authorRegex = /by @[\w-]+|by \[@([\w-]+)\]\(https:\/\/github\.com\/\1\)/u;
const titleRegex = /^#{4} (.*?)\((#\d{4,}|\[#\d{4,}\])/u;

const template = fs.readFileSync(
path.join(CHANGELOG_ROOT, TEMPLATE_FILE),
"utf8",
);
const templateComments = template.match(/<!--.*?-->/gs);
const templateComments = template.match(/<!--.*?-->/gsu);
const [templateAuthorLink] = template.match(authorRegex);
const checkedFiles = new Map();

Expand All @@ -65,7 +65,7 @@ for (const category of CHANGELOG_CATEGORIES) {
continue;
}

const match = prFile.match(/^(\d{4,})(-\d+)?\.md$/);
const match = prFile.match(/^(\d{4,})(-\d+)?\.md$/u);
const displayPath = `${CHANGELOG_DIR}/${category}/${prFile}`;

if (!match) {
Expand Down Expand Up @@ -136,7 +136,7 @@ for (const category of CHANGELOG_CATEGORIES) {
);
}

if (/prettier master/i.test(content)) {
if (/prettier master/iu.test(content)) {
showErrorMessage(
`[${displayPath}]: Please use "main" instead of "master".`,
);
Expand Down
Loading

0 comments on commit c03a12a

Please sign in to comment.