From b26f56bd316a3ed11c33f50cd4a3dac44a4ee529 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Tue, 4 Jun 2024 09:39:19 +0800 Subject: [PATCH] Update eslint-plugin-unicorn to v53 (#16275) --- package.json | 2 +- scripts/build/build-javascript-module.js | 4 +-- scripts/build/modify-typescript-module.js | 12 +++---- scripts/bundle-eslint-config.js | 3 +- src/cli/expand-patterns.js | 4 +-- src/language-graphql/printer-graphql.js | 8 +++-- src/language-handlebars/printer-glimmer.js | 2 +- src/language-js/embed/html.js | 10 ++++-- src/language-js/print/template-literal.js | 2 +- src/language-markdown/mdx.js | 2 +- src/language-markdown/printer-markdown.js | 4 +-- src/language-yaml/printer-yaml.js | 2 +- src/main/core-options.evaluate.js | 8 ++--- src/utils/front-matter/parse.js | 10 +++--- tests/format/misc/errors/json/format.test.js | 2 +- tests/integration/__tests__/format.js | 4 ++- tests/integration/__tests__/patterns-dirs.js | 29 +++++++++++------ tests/integration/__tests__/util-shared.js | 12 ++++--- website/playground/codeSamples.mjs | 4 +-- website/static/worker.js | 1 + yarn.lock | 34 ++++++++++---------- 21 files changed, 91 insertions(+), 68 deletions(-) diff --git a/package.json b/package.json index 155543772..399c8b094 100644 --- a/package.json +++ b/package.json @@ -136,7 +136,7 @@ "eslint-plugin-react": "7.34.1", "eslint-plugin-regexp": "2.6.0", "eslint-plugin-simple-import-sort": "12.1.0", - "eslint-plugin-unicorn": "52.0.0", + "eslint-plugin-unicorn": "53.0.0", "esm-utils": "4.3.0", "execa": "7.2.0", "jest": "30.0.0-alpha.4", diff --git a/scripts/build/build-javascript-module.js b/scripts/build/build-javascript-module.js index d556053df..ade467174 100644 --- a/scripts/build/build-javascript-module.js +++ b/scripts/build/build-javascript-module.js @@ -84,11 +84,11 @@ function getEsbuildOptions({ file, files, shouldCollectLicenses, cliOptions }) { text .replace( "const line = (0, _detectNewline().default)(docblock) ?? _os().EOL;", - 'const line = "\\n"', + String.raw`const line = "\n"`, ) .replace( "const line = (0, _detectNewline().default)(comments) ?? _os().EOL;", - 'const line = "\\n"', + String.raw`const line = "\n"`, ) .replace(/\nfunction _os\(\).*?\n}/s, "") .replace(/\nfunction _detectNewline\(\).*?\n}/s, ""), diff --git a/scripts/build/modify-typescript-module.js b/scripts/build/modify-typescript-module.js index 72f8a898a..6032c9510 100644 --- a/scripts/build/modify-typescript-module.js +++ b/scripts/build/modify-typescript-module.js @@ -21,12 +21,12 @@ function* getModules(text) { if (/\S/.test(code)) { const esmRegExp = new RegExp( [ - "\\s*var (?\\w+) = __esm\\({", + String.raw`\s*var (?\w+) = __esm\({`, `\\s*"${escapeStringRegexp(path)}"\\(\\) {`, ".*", - "\\s*}", - "\\s*}\\);", - ].join("\\n"), + String.raw`\s*}`, + String.raw`\s*}\);`, + ].join(String.raw`\n`), "s", ); const match = code.match(esmRegExp); @@ -90,11 +90,11 @@ class TypeScriptModuleSource { const regexp = new RegExp( [ "(?<=\n)", - "(?\\s*)", + String.raw`(?\s*)`, escapeStringRegexp(start), ".*?", "(?<=\n)", - "\\k", + String.raw`\k`, escapeStringRegexp(end), "(?=\n)", ].join(""), diff --git a/scripts/bundle-eslint-config.js b/scripts/bundle-eslint-config.js index f4a204328..bea00bbe2 100644 --- a/scripts/bundle-eslint-config.js +++ b/scripts/bundle-eslint-config.js @@ -32,8 +32,7 @@ export default [ "error", // Forbid `require()` .mjs file { - selector: - 'CallExpression[callee.name="require"][arguments.0.value=/^\\..*?\\.mjs$/]', + selector: String.raw`CallExpression[callee.name="require"][arguments.0.value=/^\..*?\.mjs$/]`, message: ".mjs file can't be `require()`d", }, ], diff --git a/src/cli/expand-patterns.js b/src/cli/expand-patterns.js index 6c28afbf9..9fc9ffad9 100644 --- a/src/cli/expand-patterns.js +++ b/src/cli/expand-patterns.js @@ -178,8 +178,8 @@ function escapePathForGlob(path) { .escapePath( path.replaceAll("\\", "\0"), // Workaround for fast-glob#262 (part 1) ) - .replaceAll("\\!", "@(!)") // Workaround for fast-glob#261 - .replaceAll("\0", "@(\\\\)"); // Workaround for fast-glob#262 (part 2) + .replaceAll(String.raw`\!`, "@(!)") // Workaround for fast-glob#261 + .replaceAll("\0", String.raw`@(\\)`); // Workaround for fast-glob#262 (part 2) } /** diff --git a/src/language-graphql/printer-graphql.js b/src/language-graphql/printer-graphql.js index 4eb0bfef7..b2bad2331 100644 --- a/src/language-graphql/printer-graphql.js +++ b/src/language-graphql/printer-graphql.js @@ -91,7 +91,9 @@ function genericPrint(path, options, print) { case "StringValue": if (node.block) { - const lines = node.value.replaceAll('"""', '\\"""').split("\n"); + const lines = node.value + .replaceAll('"""', String.raw`\"""`) + .split("\n"); if (lines.length === 1) { lines[0] = lines[0].trim(); } @@ -104,7 +106,9 @@ function genericPrint(path, options, print) { } return [ '"', - node.value.replaceAll(/["\\]/g, "\\$&").replaceAll("\n", "\\n"), + node.value + .replaceAll(/["\\]/g, String.raw`\$&`) + .replaceAll("\n", String.raw`\n`), '"', ]; diff --git a/src/language-handlebars/printer-glimmer.js b/src/language-handlebars/printer-glimmer.js index 658d2e251..f7ab3a58f 100644 --- a/src/language-handlebars/printer-glimmer.js +++ b/src/language-handlebars/printer-glimmer.js @@ -159,7 +159,7 @@ function print(path, options, print) { /* if `{{my-component}}` (or any text containing "{{") * makes it to the TextNode, it means it was escaped, * so let's print it escaped, ie.; `\{{my-component}}` */ - let text = node.chars.replaceAll("{{", "\\{{"); + let text = node.chars.replaceAll("{{", String.raw`\{{`); const attrName = getCurrentAttributeName(path); diff --git a/src/language-js/embed/html.js b/src/language-js/embed/html.js index 338dd0ce7..de96b0e12 100644 --- a/src/language-js/embed/html.js +++ b/src/language-js/embed/html.js @@ -32,7 +32,10 @@ async function printEmbedHtmlLike(parser, textToDoc, print, path, options) { const expressionDocs = printTemplateExpressions(path, print); - const placeholderRegex = new RegExp(composePlaceholder("(\\d+)"), "g"); + const placeholderRegex = new RegExp( + composePlaceholder(String.raw`(\d+)`), + "g", + ); let topLevelCount = 0; const doc = await textToDoc(text, { parser, @@ -56,7 +59,10 @@ async function printEmbedHtmlLike(parser, textToDoc, print, path, options) { if (component) { component = uncookTemplateElementValue(component); if (options.__embeddedInHtml) { - component = component.replaceAll(/<\/(?=script\b)/gi, "<\\/"); + component = component.replaceAll( + /<\/(?=script\b)/gi, + String.raw`<\/`, + ); } parts.push(component); } diff --git a/src/language-js/print/template-literal.js b/src/language-js/print/template-literal.js index effab7f88..eddb9d289 100644 --- a/src/language-js/print/template-literal.js +++ b/src/language-js/print/template-literal.js @@ -250,7 +250,7 @@ function escapeTemplateCharacters(doc, raw) { } function uncookTemplateElementValue(cookedValue) { - return cookedValue.replaceAll(/([\\`]|\${)/g, "\\$1"); + return cookedValue.replaceAll(/([\\`]|\${)/g, String.raw`\$1`); } function isJestEachTemplateLiteral({ node, parent }) { diff --git a/src/language-markdown/mdx.js b/src/language-markdown/mdx.js index 0eeec43af..55370f5a6 100644 --- a/src/language-markdown/mdx.js +++ b/src/language-markdown/mdx.js @@ -24,7 +24,7 @@ const IMPORT_REGEX = /^import\s/; const EXPORT_REGEX = /^export\s/; -const BLOCKS_REGEX = "[a-z][a-z0-9]*(\\.[a-z][a-z0-9]*)*|"; +const BLOCKS_REGEX = String.raw`[a-z][a-z0-9]*(\.[a-z][a-z0-9]*)*|`; const COMMENT_REGEX = /|/; const ES_COMMENT_REGEX = /^{\s*\/\*(.*)\*\/\s*}/; const EMPTY_NEWLINE = "\n\n"; diff --git a/src/language-markdown/printer-markdown.js b/src/language-markdown/printer-markdown.js index 5db008ba5..22d2a4593 100644 --- a/src/language-markdown/printer-markdown.js +++ b/src/language-markdown/printer-markdown.js @@ -88,7 +88,7 @@ function genericPrint(path, options, print) { return printSentence(path, print); case "word": { let escapedValue = node.value - .replaceAll("*", "\\*") // escape all `*` + .replaceAll("*", String.raw`\*`) // escape all `*` .replaceAll( new RegExp( [ @@ -101,7 +101,7 @@ function genericPrint(path, options, print) { (underscore1 ? `${text1}${underscore1}` : `${underscore2}${text2}` - ).replaceAll("_", "\\_"), + ).replaceAll("_", String.raw`\_`), ); // escape all `_` except concating with non-punctuation, e.g. `1_2_3` is not considered emphasis const isFirstSentence = (node, name, index) => diff --git a/src/language-yaml/printer-yaml.js b/src/language-yaml/printer-yaml.js index 439065c33..2e972dfdd 100644 --- a/src/language-yaml/printer-yaml.js +++ b/src/language-yaml/printer-yaml.js @@ -276,7 +276,7 @@ function printNode(path, options, print) { node.type === "quoteDouble" ? raw // double quote needs to be escaped by backslash in quoteDouble - .replaceAll('\\"', doubleQuote) + .replaceAll(String.raw`\"`, doubleQuote) .replaceAll("'", singleQuote.repeat(2)) : raw, options, diff --git a/src/main/core-options.evaluate.js b/src/main/core-options.evaluate.js index 3855a8b6c..3f8c6f1bb 100644 --- a/src/main/core-options.evaluate.js +++ b/src/main/core-options.evaluate.js @@ -62,17 +62,15 @@ const options = { choices: [ { value: "lf", - description: - "Line Feed only (\\n), common on Linux and macOS as well as inside git repos", + description: String.raw`Line Feed only (\n), common on Linux and macOS as well as inside git repos`, }, { value: "crlf", - description: - "Carriage Return + Line Feed characters (\\r\\n), common on Windows", + description: String.raw`Carriage Return + Line Feed characters (\r\n), common on Windows`, }, { value: "cr", - description: "Carriage Return character only (\\r), used very rarely", + description: String.raw`Carriage Return character only (\r), used very rarely`, }, { value: "auto", diff --git a/src/utils/front-matter/parse.js b/src/utils/front-matter/parse.js index cf940800f..7bfdc0ca5 100644 --- a/src/utils/front-matter/parse.js +++ b/src/utils/front-matter/parse.js @@ -1,13 +1,13 @@ const frontMatterRegex = new RegExp( - "^(?-{3}|\\+{3})" + + String.raw`^(?-{3}|\+{3})` + // trailing spaces after delimiters are allowed - "(?[^\\n]*)" + - "\\n(?:|(?.*?)\\n)" + + String.raw`(?[^\n]*)` + + String.raw`\n(?:|(?.*?)\n)` + // In some markdown processors such as pandoc, // "..." can be used as the end delimiter for YAML front-matter. // Adding `\.{3}` make the regex matches `+++\n...`, but we'll exclude it later - "(?\\k|\\.{3})" + - "[^\\S\\n]*(?:\\n|$)", + String.raw`(?\k|\.{3})` + + String.raw`[^\S\n]*(?:\n|$)`, "s", ); diff --git a/tests/format/misc/errors/json/format.test.js b/tests/format/misc/errors/json/format.test.js index 9837ec1ff..131b43ff2 100644 --- a/tests/format/misc/errors/json/format.test.js +++ b/tests/format/misc/errors/json/format.test.js @@ -7,7 +7,7 @@ runFormatTest( '{"foo": ~1}', '{"foo": false || "bar"}', '{"foo": () => {}}', - "packages\\the-hub\\cypress\\fixtures\\gridConfiguration.json", + String.raw`packages\the-hub\cypress\fixtures\gridConfiguration.json`, "1+2", "{Infinity}", "{[key]: 1}", diff --git a/tests/integration/__tests__/format.js b/tests/integration/__tests__/format.js index b56249c81..2bbd0b63c 100644 --- a/tests/integration/__tests__/format.js +++ b/tests/integration/__tests__/format.js @@ -58,7 +58,9 @@ test("should work with foo plugin instance", async () => { plugins: [fooPlugin], }), ), - ).toMatchInlineSnapshot('""{\\"tabWidth\\":8,\\"bracketSpacing\\":false}""'); + ).toMatchInlineSnapshot( + String.raw`""{\"tabWidth\":8,\"bracketSpacing\":false}""`, + ); }); test("'Adjacent JSX' error should not be swallowed by Babel's error recovery", async () => { diff --git a/tests/integration/__tests__/patterns-dirs.js b/tests/integration/__tests__/patterns-dirs.js index d77b8633a..8d4594666 100644 --- a/tests/integration/__tests__/patterns-dirs.js +++ b/tests/integration/__tests__/patterns-dirs.js @@ -139,24 +139,33 @@ if (path.sep === "/") { beforeAll(() => { fs.mkdirSync(path.resolve(base, "test-a\\")); fs.writeFileSync(path.resolve(base, "test-a\\", "test.js"), "x"); - fs.mkdirSync(path.resolve(base, "test-b\\?")); - fs.writeFileSync(path.resolve(base, "test-b\\?", "test.js"), "x"); + fs.mkdirSync(path.resolve(base, String.raw`test-b\?`)); + fs.writeFileSync( + path.resolve(base, String.raw`test-b\?`, "test.js"), + "x", + ); }); afterAll(() => { fs.unlinkSync(path.resolve(base, "test-a\\", "test.js")); fs.rmdirSync(path.resolve(base, "test-a\\")); - fs.unlinkSync(path.resolve(base, "test-b\\?", "test.js")); - fs.rmdirSync(path.resolve(base, "test-b\\?")); + fs.unlinkSync(path.resolve(base, String.raw`test-b\?`, "test.js")); + fs.rmdirSync(path.resolve(base, String.raw`test-b\?`)); }); - testPatterns("", ["test-a\\/test.js"], { stdout: "test-a\\/test.js" }); - testPatterns("", ["test-a\\"], { stdout: "test-a\\/test.js" }); - testPatterns("", ["test-a*/*"], { stdout: "test-a\\/test.js" }); + testPatterns("", [String.raw`test-a\/test.js`], { + stdout: String.raw`test-a\/test.js`, + }); + testPatterns("", ["test-a\\"], { stdout: String.raw`test-a\/test.js` }); + testPatterns("", ["test-a*/*"], { stdout: String.raw`test-a\/test.js` }); - testPatterns("", ["test-b\\?/test.js"], { stdout: "test-b\\?/test.js" }); - testPatterns("", ["test-b\\?"], { stdout: "test-b\\?/test.js" }); - testPatterns("", ["test-b*/*"], { stdout: "test-b\\?/test.js" }); + testPatterns("", [String.raw`test-b\?/test.js`], { + stdout: String.raw`test-b\?/test.js`, + }); + testPatterns("", [String.raw`test-b\?`], { + stdout: String.raw`test-b\?/test.js`, + }); + testPatterns("", ["test-b*/*"], { stdout: String.raw`test-b\?/test.js` }); }); } diff --git a/tests/integration/__tests__/util-shared.js b/tests/integration/__tests__/util-shared.js index 16d1eb272..2140db107 100644 --- a/tests/integration/__tests__/util-shared.js +++ b/tests/integration/__tests__/util-shared.js @@ -43,9 +43,9 @@ test("sharedUtil.getMaxContinuousCount", () => { expect(getMaxContinuousCount(fixture, "([a-f])")).toBe(2); expect(getMaxContinuousCount(fixture, "[a-f]")).toBe(3); expect(getMaxContinuousCount(fixture, "a-f")).toBe(4); - expect(getMaxContinuousCount(fixture, "([a\\-f])")).toBe(0); - expect(getMaxContinuousCount(fixture, "[a\\-f]")).toBe(0); - expect(getMaxContinuousCount(fixture, "a\\-f")).toBe(0); + expect(getMaxContinuousCount(fixture, String.raw`([a\-f])`)).toBe(0); + expect(getMaxContinuousCount(fixture, String.raw`[a\-f]`)).toBe(0); + expect(getMaxContinuousCount(fixture, String.raw`a\-f`)).toBe(0); }); test("sharedUtil.getStringWidth", () => { @@ -222,6 +222,10 @@ test("sharedUtil.makeString", () => { `${SINGLE_QUOTE}a${DOUBLE_QUOTE}${SINGLE_QUOTE}`, ); expect( - makeString("\\a", SINGLE_QUOTE, /* unescapeUnnecessaryEscapes */ true), + makeString( + String.raw`\a`, + SINGLE_QUOTE, + /* unescapeUnnecessaryEscapes */ true, + ), ).toBe(`${SINGLE_QUOTE}a${SINGLE_QUOTE}`); }); diff --git a/website/playground/codeSamples.mjs b/website/playground/codeSamples.mjs index e194b4581..865474ac5 100644 --- a/website/playground/codeSamples.mjs +++ b/website/playground/codeSamples.mjs @@ -10,12 +10,12 @@ export default function getCodeSamples(parser) { " if(!greeting){return null};", "", " // TODO: Don't use random in render", - ' let num = Math.floor (Math.random() * 1E+7).toString().replace(/\\.\\d+/ig, "")', + String.raw` let num = Math.floor (Math.random() * 1E+7).toString().replace(/\.\d+/ig, "")`, "", " return
", "", " { greeting.slice( 0, 1 ).toUpperCase() + greeting.slice(1).toLowerCase() }", - ' {greeting.endsWith(",") ? " " : ", " }', + String.raw` {greeting.endsWith(",") ? " " : ", " }`, " ", "\t{ greeted }", "\t", diff --git a/website/static/worker.js b/website/static/worker.js index b56c5f2a2..0ce0ecacd 100644 --- a/website/static/worker.js +++ b/website/static/worker.js @@ -67,6 +67,7 @@ async function handleMetaMessage() { return { type: "meta", + // eslint-disable-next-line unicorn/prefer-structured-clone supportInfo: JSON.parse(JSON.stringify(supportInfo)), version: prettier.version, }; diff --git a/yarn.lock b/yarn.lock index b9ab2fe1a..8a38a918d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -192,7 +192,7 @@ __metadata: languageName: node linkType: hard -"@babel/helper-validator-identifier@npm:^7.22.20, @babel/helper-validator-identifier@npm:^7.24.6": +"@babel/helper-validator-identifier@npm:^7.22.20, @babel/helper-validator-identifier@npm:^7.24.5, @babel/helper-validator-identifier@npm:^7.24.6": version: 7.24.6 resolution: "@babel/helper-validator-identifier@npm:7.24.6" checksum: 10/7e725ef0684291ca3306d5174a5d1cd9072ad58ba444cfa50aaf92a5c59dd723fa15031733ac598bb6b066cb62c2472e14cd82325522348977a72e99aa21b97a @@ -1098,7 +1098,7 @@ __metadata: languageName: node linkType: hard -"@eslint/eslintrc@npm:3.1.0": +"@eslint/eslintrc@npm:3.1.0, @eslint/eslintrc@npm:^3.0.2": version: 3.1.0 resolution: "@eslint/eslintrc@npm:3.1.0" dependencies: @@ -2621,7 +2621,7 @@ __metadata: languageName: node linkType: hard -"browserslist@npm:4.23.0, browserslist@npm:^4.21.10, browserslist@npm:^4.21.9, browserslist@npm:^4.22.2": +"browserslist@npm:4.23.0, browserslist@npm:^4.21.10, browserslist@npm:^4.21.9, browserslist@npm:^4.23.0": version: 4.23.0 resolution: "browserslist@npm:4.23.0" dependencies: @@ -3025,12 +3025,12 @@ __metadata: languageName: node linkType: hard -"core-js-compat@npm:^3.34.0": - version: 3.35.0 - resolution: "core-js-compat@npm:3.35.0" +"core-js-compat@npm:^3.37.0": + version: 3.37.1 + resolution: "core-js-compat@npm:3.37.1" dependencies: - browserslist: "npm:^4.22.2" - checksum: 10/aa21ad2f0c946be7a8ecef92233bc003a38fa27e43a925fcd9b79e32ae49b879e0f5c23459ffc310df38ee547389b8e5e43a6a8be0b2369b9b9ebf3d04ae69b9 + browserslist: "npm:^4.23.0" + checksum: 10/30c6fdbd9ff179cc53951814689b8aabec106e5de6cddfa7a7feacc96b66d415b8eebcf5ec8f7c68ef35c552fe7d39edb8b15b1ce0f27379a272295b6e937061 languageName: node linkType: hard @@ -3971,16 +3971,16 @@ __metadata: languageName: node linkType: hard -"eslint-plugin-unicorn@npm:52.0.0": - version: 52.0.0 - resolution: "eslint-plugin-unicorn@npm:52.0.0" +"eslint-plugin-unicorn@npm:53.0.0": + version: 53.0.0 + resolution: "eslint-plugin-unicorn@npm:53.0.0" dependencies: - "@babel/helper-validator-identifier": "npm:^7.22.20" + "@babel/helper-validator-identifier": "npm:^7.24.5" "@eslint-community/eslint-utils": "npm:^4.4.0" - "@eslint/eslintrc": "npm:^2.1.4" + "@eslint/eslintrc": "npm:^3.0.2" ci-info: "npm:^4.0.0" clean-regexp: "npm:^1.0.0" - core-js-compat: "npm:^3.34.0" + core-js-compat: "npm:^3.37.0" esquery: "npm:^1.5.0" indent-string: "npm:^4.0.0" is-builtin-module: "npm:^3.2.1" @@ -3989,11 +3989,11 @@ __metadata: read-pkg-up: "npm:^7.0.1" regexp-tree: "npm:^0.1.27" regjsparser: "npm:^0.10.0" - semver: "npm:^7.5.4" + semver: "npm:^7.6.1" strip-indent: "npm:^3.0.0" peerDependencies: eslint: ">=8.56.0" - checksum: 10/69b8aeee04806b808a534fe5484ad75ee9feec6078aad90651d7ce7216e2bd14980fec5a2e05fd800b874406a54240af66f04ab83023a7f1fe67397b6dc4c032 + checksum: 10/988673e28a556ea31475dde8470b0d176fbf5089eeb6693fd56432e06b5295b2093aefc16e0a7b319133b75ec4f044f94b2b40f3cf01ff04f4a29adf30c3ac22 languageName: node linkType: hard @@ -7423,7 +7423,7 @@ __metadata: eslint-plugin-react: "npm:7.34.1" eslint-plugin-regexp: "npm:2.6.0" eslint-plugin-simple-import-sort: "npm:12.1.0" - eslint-plugin-unicorn: "npm:52.0.0" + eslint-plugin-unicorn: "npm:53.0.0" esm-utils: "npm:4.3.0" espree: "npm:10.0.1" execa: "npm:7.2.0"