From 0d6c8f681d5e1b892b9c0e8b8a603adaea85acce Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Sun, 19 Feb 2023 11:15:06 +0800 Subject: [PATCH] Update typescript-eslint to v5.52 (#14358) --- package.json | 6 +- scripts/build/config.mjs | 12 +++ src/language-js/print/typescript.js | 27 +++--- tests/config/format-test.js | 10 ++- .../__snapshots__/jsfmt.spec.js.snap | 37 ++++++++ .../module-declaration/jsfmt.spec.js | 7 ++ .../__snapshots__/jsfmt.spec.js.snap | 14 +++ .../types/moduleDeclaration/kind-detection.ts | 1 + yarn.lock | 88 +++++++++---------- 9 files changed, 141 insertions(+), 61 deletions(-) create mode 100644 tests/format/misc/errors/typescript/module-declaration/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/format/misc/errors/typescript/module-declaration/jsfmt.spec.js create mode 100644 tests/format/typescript/conformance/types/moduleDeclaration/kind-detection.ts diff --git a/package.json b/package.json index d6e61f5ac..d4e16478a 100644 --- a/package.json +++ b/package.json @@ -37,8 +37,8 @@ "@iarna/toml": "2.2.5", "@prettier/is-es5-identifier-name": "0.1.0", "@prettier/parse-srcset": "2.0.2", - "@typescript-eslint/typescript-estree": "5.51.0", - "@typescript-eslint/visitor-keys": "5.51.0", + "@typescript-eslint/typescript-estree": "5.52.0", + "@typescript-eslint/visitor-keys": "5.52.0", "acorn": "8.8.2", "acorn-jsx": "5.3.2", "angular-estree-parser": "6.0.0", @@ -111,7 +111,7 @@ "@types/file-entry-cache": "5.0.2", "@types/find-cache-dir": "3.2.1", "@types/jest": "29.4.0", - "@typescript-eslint/eslint-plugin": "5.51.0", + "@typescript-eslint/eslint-plugin": "5.52.0", "benchmark": "2.1.4", "browserslist-to-esbuild": "1.2.0", "c8": "7.12.0", diff --git a/scripts/build/config.mjs b/scripts/build/config.mjs index 25544df26..c1a1fac1e 100644 --- a/scripts/build/config.mjs +++ b/scripts/build/config.mjs @@ -132,6 +132,18 @@ const pluginFiles = [ ), text: "exports.inferSingleRun = () => false;", }, + { + module: require.resolve( + "@typescript-eslint/typescript-estree/dist/parseSettings/ExpiringCache.js" + ), + text: "exports.ExpiringCache = class {};", + }, + { + module: require.resolve( + "@typescript-eslint/typescript-estree/dist/parseSettings/getProjectConfigFiles.js" + ), + text: "exports.resolveProjectList = () => [];", + }, { module: require.resolve( "@typescript-eslint/typescript-estree/dist/parseSettings/warnAboutTSVersion.js" diff --git a/src/language-js/print/typescript.js b/src/language-js/print/typescript.js index 30b185b3f..3aa60ba38 100644 --- a/src/language-js/print/typescript.js +++ b/src/language-js/print/typescript.js @@ -341,7 +341,6 @@ function printTypescript(path, options, print) { return ["require(", print("expression"), ")"]; case "TSModuleDeclaration": { const { parent } = path; - const isExternalModule = isStringLiteral(node.id); const parentIsDeclaration = parent.type === "TSModuleDeclaration"; const bodyIsDeclaration = node.body?.type === "TSModuleDeclaration"; @@ -352,15 +351,23 @@ function printTypescript(path, options, print) { // Global declaration looks like this: // (declare)? global { ... } - if (!node.global) { - parts.push( - isExternalModule || - /(?:^|\s)module(?:\s|$)/.test( - options.originalText.slice(locStart(node), locStart(node.id)) - ) - ? "module " - : "namespace " - ); + const isGlobal = + node.kind === "global" || + // TODO: Use `node.kind` when babel update AST + // https://github.com/typescript-eslint/typescript-eslint/pull/6443 + node.global; + + if (!isGlobal) { + const kind = + node.kind ?? + // TODO: Use `node.kind` when babel update AST + (isStringLiteral(node.id) || + /(?:^|\s)module(?:\s|$)/.test( + options.originalText.slice(locStart(node), locStart(node.id)) + ) + ? "module" + : "namespace"); + parts.push(kind, " "); } } diff --git a/tests/config/format-test.js b/tests/config/format-test.js index 6e6106484..5d08cc87b 100644 --- a/tests/config/format-test.js +++ b/tests/config/format-test.js @@ -82,7 +82,9 @@ const meriyahDisabledTests = new Set([ ].map((file) => path.join(__dirname, "../format", file)), ]); const babelTsDisabledTest = new Set( - [].map((directory) => path.join(__dirname, "../format/typescript", directory)) + ["conformance/types/moduleDeclaration/kind-detection.ts"].map((file) => + path.join(__dirname, "../format/typescript", file) + ) ); const isUnstable = (filename, options) => { @@ -236,8 +238,7 @@ function runSpec(fixtures, parsers, options) { if ( parsers.includes("typescript") && !parsers.includes("babel-ts") && - !IS_TYPESCRIPT_ONLY_TEST && - !babelTsDisabledTest.has(dirname) + !IS_TYPESCRIPT_ONLY_TEST ) { allParsers.push("babel-ts"); } @@ -285,7 +286,8 @@ function runSpec(fixtures, parsers, options) { if ( (currentParser === "espree" && espreeDisabledTests.has(filename)) || (currentParser === "meriyah" && meriyahDisabledTests.has(filename)) || - (currentParser === "acorn" && acornDisabledTests.has(filename)) + (currentParser === "acorn" && acornDisabledTests.has(filename)) || + (currentParser === "babel-ts" && babelTsDisabledTest.has(filename)) ) { continue; } diff --git a/tests/format/misc/errors/typescript/module-declaration/__snapshots__/jsfmt.spec.js.snap b/tests/format/misc/errors/typescript/module-declaration/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..2fb433337 --- /dev/null +++ b/tests/format/misc/errors/typescript/module-declaration/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,37 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`snippet: #0 [babel-ts] format 1`] = ` +"Missing semicolon. (1:10) +> 1 | namespace "a" {} + | ^" +`; + +exports[`snippet: #0 [typescript] format 1`] = ` +"Identifier expected. (1:11) +> 1 | namespace "a" {} + | ^" +`; + +exports[`snippet: #1 [babel-ts] format 1`] = ` +"Missing semicolon. (1:10) +> 1 | namespace "a"; + | ^" +`; + +exports[`snippet: #1 [typescript] format 1`] = ` +"Identifier expected. (1:11) +> 1 | namespace "a"; + | ^" +`; + +exports[`snippet: #2 [babel-ts] format 1`] = ` +"Unexpected token, expected "{" (1:12) +> 1 | namespace a; + | ^" +`; + +exports[`snippet: #2 [typescript] format 1`] = ` +"'{' expected. (1:12) +> 1 | namespace a; + | ^" +`; diff --git a/tests/format/misc/errors/typescript/module-declaration/jsfmt.spec.js b/tests/format/misc/errors/typescript/module-declaration/jsfmt.spec.js new file mode 100644 index 000000000..815a2e251 --- /dev/null +++ b/tests/format/misc/errors/typescript/module-declaration/jsfmt.spec.js @@ -0,0 +1,7 @@ +run_spec( + { + importMeta: import.meta, + snippets: ['namespace "a" {}', 'namespace "a";', "namespace a;"], + }, + ["babel-ts", "typescript"] +); diff --git a/tests/format/typescript/conformance/types/moduleDeclaration/__snapshots__/jsfmt.spec.js.snap b/tests/format/typescript/conformance/types/moduleDeclaration/__snapshots__/jsfmt.spec.js.snap index 2582451e8..2e0a30dff 100644 --- a/tests/format/typescript/conformance/types/moduleDeclaration/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/typescript/conformance/types/moduleDeclaration/__snapshots__/jsfmt.spec.js.snap @@ -1,5 +1,19 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`kind-detection.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +declare /* module */ namespace A {} + +=====================================output===================================== +declare namespace /* module */ A {} + +================================================================================ +`; + exports[`moduleDeclaration.ts format 1`] = ` ====================================options===================================== parsers: ["typescript"] diff --git a/tests/format/typescript/conformance/types/moduleDeclaration/kind-detection.ts b/tests/format/typescript/conformance/types/moduleDeclaration/kind-detection.ts new file mode 100644 index 000000000..4796ce701 --- /dev/null +++ b/tests/format/typescript/conformance/types/moduleDeclaration/kind-detection.ts @@ -0,0 +1 @@ +declare /* module */ namespace A {} diff --git a/yarn.lock b/yarn.lock index 26bf7db96..c154ba135 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1750,13 +1750,13 @@ __metadata: languageName: node linkType: hard -"@typescript-eslint/eslint-plugin@npm:5.51.0": - version: 5.51.0 - resolution: "@typescript-eslint/eslint-plugin@npm:5.51.0" +"@typescript-eslint/eslint-plugin@npm:5.52.0": + version: 5.52.0 + resolution: "@typescript-eslint/eslint-plugin@npm:5.52.0" dependencies: - "@typescript-eslint/scope-manager": 5.51.0 - "@typescript-eslint/type-utils": 5.51.0 - "@typescript-eslint/utils": 5.51.0 + "@typescript-eslint/scope-manager": 5.52.0 + "@typescript-eslint/type-utils": 5.52.0 + "@typescript-eslint/utils": 5.52.0 debug: ^4.3.4 grapheme-splitter: ^1.0.4 ignore: ^5.2.0 @@ -1770,26 +1770,26 @@ __metadata: peerDependenciesMeta: typescript: optional: true - checksum: 5351d8cec13bd9867ce4aaf7052aa31c9ca867fc89c620fc0fe5718ac2cbc165903275db59974324d98e45df0d33a73a4367d236668772912731031a672cfdcd + checksum: cff07ee94d8ab2a1b6c33b5c5bf641eff2bf2bebc0f35a9d8b3f128fd610e27a4aaf620bc2ad23608ad161b1810b7e32e5a2e0f746cc5094c3f506f7a14daa34 languageName: node linkType: hard -"@typescript-eslint/scope-manager@npm:5.51.0": - version: 5.51.0 - resolution: "@typescript-eslint/scope-manager@npm:5.51.0" +"@typescript-eslint/scope-manager@npm:5.52.0": + version: 5.52.0 + resolution: "@typescript-eslint/scope-manager@npm:5.52.0" dependencies: - "@typescript-eslint/types": 5.51.0 - "@typescript-eslint/visitor-keys": 5.51.0 - checksum: b3c9f48b6b7a7ae2ebcad4745ef91e4727776b2cf56d31be6456b1aa063aa649539e20f9fffa83cad9ccaaa9c492f2354a1c15526a2b789e235ec58b3a82d22c + "@typescript-eslint/types": 5.52.0 + "@typescript-eslint/visitor-keys": 5.52.0 + checksum: 9a03fe30f8e90a5106c482478f213eefdd09f2f74e24d9dc59b453885466a758fe6d1cd24d706aed6188fb03c84b16ca6491cf20da6b16b8fc53cad8b8c327f2 languageName: node linkType: hard -"@typescript-eslint/type-utils@npm:5.51.0": - version: 5.51.0 - resolution: "@typescript-eslint/type-utils@npm:5.51.0" +"@typescript-eslint/type-utils@npm:5.52.0": + version: 5.52.0 + resolution: "@typescript-eslint/type-utils@npm:5.52.0" dependencies: - "@typescript-eslint/typescript-estree": 5.51.0 - "@typescript-eslint/utils": 5.51.0 + "@typescript-eslint/typescript-estree": 5.52.0 + "@typescript-eslint/utils": 5.52.0 debug: ^4.3.4 tsutils: ^3.21.0 peerDependencies: @@ -1797,23 +1797,23 @@ __metadata: peerDependenciesMeta: typescript: optional: true - checksum: ab9747b0c629cfaaab903eed8ce1e39d34d69a402ce5faf2f1fff2bbb461bdbe034044b1368ba67ba8e5c1c512172e07d83c8a563635d8de811bf148d95c7dec + checksum: ac5422040461febab8a2eeec76d969024ccff76203dec357f7220c9b5e0dde96e3e3a76fd4118d42b50bd5bfb3a194aaceeb63417a2ac4e1ebf5e687558a9a10 languageName: node linkType: hard -"@typescript-eslint/types@npm:5.51.0": - version: 5.51.0 - resolution: "@typescript-eslint/types@npm:5.51.0" - checksum: b31021a0866f41ba5d71b6c4c7e20cc9b99d49c93bb7db63b55b2e51542fb75b4e27662ee86350da3c1318029e278a5a807facaf4cb5aeea724be8b0e021e836 +"@typescript-eslint/types@npm:5.52.0": + version: 5.52.0 + resolution: "@typescript-eslint/types@npm:5.52.0" + checksum: 018940d61aebf7cf3f7de1b9957446e2ea01f08fe950bef4788c716a3a88f7c42765fe7d80152b0d0428fcd4bd3ace2dfa8c459ba1c59d9a84e951642180f869 languageName: node linkType: hard -"@typescript-eslint/typescript-estree@npm:5.51.0": - version: 5.51.0 - resolution: "@typescript-eslint/typescript-estree@npm:5.51.0" +"@typescript-eslint/typescript-estree@npm:5.52.0": + version: 5.52.0 + resolution: "@typescript-eslint/typescript-estree@npm:5.52.0" dependencies: - "@typescript-eslint/types": 5.51.0 - "@typescript-eslint/visitor-keys": 5.51.0 + "@typescript-eslint/types": 5.52.0 + "@typescript-eslint/visitor-keys": 5.52.0 debug: ^4.3.4 globby: ^11.1.0 is-glob: ^4.0.3 @@ -1822,35 +1822,35 @@ __metadata: peerDependenciesMeta: typescript: optional: true - checksum: aec23e5cab48ee72fefa6d1ac266639ebabf6cebec1e0207ad47011d3a48186ac9a632c8e34c3bac896155f54895a497230c11d789fd81263b08eb267d7113ce + checksum: 67d396907fee3d6894e26411a5098a37f07e5d50343189e6361ff7db91c74a7ffe2abd630d11f14c2bda1f4af13edf52b80b11cbccb55b44079c7cec14c9e108 languageName: node linkType: hard -"@typescript-eslint/utils@npm:5.51.0, @typescript-eslint/utils@npm:^5.10.0": - version: 5.51.0 - resolution: "@typescript-eslint/utils@npm:5.51.0" +"@typescript-eslint/utils@npm:5.52.0, @typescript-eslint/utils@npm:^5.10.0": + version: 5.52.0 + resolution: "@typescript-eslint/utils@npm:5.52.0" dependencies: "@types/json-schema": ^7.0.9 "@types/semver": ^7.3.12 - "@typescript-eslint/scope-manager": 5.51.0 - "@typescript-eslint/types": 5.51.0 - "@typescript-eslint/typescript-estree": 5.51.0 + "@typescript-eslint/scope-manager": 5.52.0 + "@typescript-eslint/types": 5.52.0 + "@typescript-eslint/typescript-estree": 5.52.0 eslint-scope: ^5.1.1 eslint-utils: ^3.0.0 semver: ^7.3.7 peerDependencies: eslint: ^6.0.0 || ^7.0.0 || ^8.0.0 - checksum: c6e28c942fbac5500f0e8ed67ef304b484ba296486e55306f78fb090dc9d5bb1f25a0bedc065e14680041eadce5e95fa10aab618cb0c316599ec987e6ea72442 + checksum: 01906be5262ece36537e9d586e4d2d4791e05752a9354bcb42b1f5bf965f53daa13309c61c3dff5e201ea28c298e4e01cf0c93738afa0099fea0da3b1d8cb3a5 languageName: node linkType: hard -"@typescript-eslint/visitor-keys@npm:5.51.0": - version: 5.51.0 - resolution: "@typescript-eslint/visitor-keys@npm:5.51.0" +"@typescript-eslint/visitor-keys@npm:5.52.0": + version: 5.52.0 + resolution: "@typescript-eslint/visitor-keys@npm:5.52.0" dependencies: - "@typescript-eslint/types": 5.51.0 + "@typescript-eslint/types": 5.52.0 eslint-visitor-keys: ^3.3.0 - checksum: b49710f3c6b3b62a846a163afffd81be5eb2b1f44e25bec51ff3c9f4c3b579d74aa4cbd3753b4fc09ea3dbc64a7062f9c658c08d22bb2740a599cb703d876220 + checksum: 33b44f0cd35b7b47f34e89d52e47b8d8200f55af306b22db4de104d79f65907458ea022e548f50d966e32fea150432ac9c1ae65b3001b0ad2ac8a17c0211f370 languageName: node linkType: hard @@ -6716,9 +6716,9 @@ __metadata: "@types/file-entry-cache": 5.0.2 "@types/find-cache-dir": 3.2.1 "@types/jest": 29.4.0 - "@typescript-eslint/eslint-plugin": 5.51.0 - "@typescript-eslint/typescript-estree": 5.51.0 - "@typescript-eslint/visitor-keys": 5.51.0 + "@typescript-eslint/eslint-plugin": 5.52.0 + "@typescript-eslint/typescript-estree": 5.52.0 + "@typescript-eslint/visitor-keys": 5.52.0 acorn: 8.8.2 acorn-jsx: 5.3.2 angular-estree-parser: 6.0.0