diff --git a/package.json b/package.json index 52d84505f2b3..e4d5371a9e0f 100644 --- a/package.json +++ b/package.json @@ -28,8 +28,8 @@ "@babel/types": "7.18.13", "@glimmer/syntax": "0.84.2", "@iarna/toml": "2.2.5", - "@typescript-eslint/typescript-estree": "5.35.1", - "@typescript-eslint/visitor-keys": "5.35.1", + "@typescript-eslint/typescript-estree": "5.36.2", + "@typescript-eslint/visitor-keys": "5.36.2", "acorn": "8.8.0", "acorn-jsx": "5.3.2", "angular-estree-parser": "2.5.1", @@ -90,7 +90,7 @@ "string-width": "5.0.1", "strip-ansi": "7.0.1", "to-fast-properties": "4.0.0", - "typescript": "4.7.4", + "typescript": "4.8.2", "unicode-regex": "3.0.0", "unified": "9.2.2", "vnopts": "1.0.2", @@ -104,7 +104,7 @@ "@types/file-entry-cache": "5.0.2", "@types/find-cache-dir": "3.2.1", "@types/jest": "29.0.0", - "@typescript-eslint/eslint-plugin": "5.35.1", + "@typescript-eslint/eslint-plugin": "5.36.2", "benchmark": "2.1.4", "browserslist-to-esbuild": "1.2.0", "c8": "7.12.0", diff --git a/scripts/build/modify-typescript-module.mjs b/scripts/build/modify-typescript-module.mjs index da1b4760729c..815c7b5f266b 100644 --- a/scripts/build/modify-typescript-module.mjs +++ b/scripts/build/modify-typescript-module.mjs @@ -47,14 +47,12 @@ function removeSubmodule(text, testFunction) { function replaceSubmodule(text, testFunction, replacement) { const modules = getSubmodules(text, testFunction); if (modules.length !== 1) { - return text; - // TODO: Enable this check when merge to `next` branch - // throw Object.assign( - // new Error( - // `Expect exactly one submodule to be found, got ${modules.length} submodules.` - // ), - // { modules } - // ); + throw Object.assign( + new Error( + `Expect exactly one submodule to be found, got ${modules.length} submodules.` + ), + { modules } + ); } const [{ start, end, before, after }] = modules; diff --git a/src/language-js/parse/postprocess/typescript.js b/src/language-js/parse/postprocess/typescript.js index 6a1a7b2d5a82..2d9ffb3c2074 100644 --- a/src/language-js/parse/postprocess/typescript.js +++ b/src/language-js/parse/postprocess/typescript.js @@ -1,31 +1,34 @@ +import isNonEmptyArray from "../../../utils/is-non-empty-array.js"; import visitNode from "./visit-node.js"; import throwTsSyntaxError from "./throw-ts-syntax-error.js"; +// Copied from https://unpkg.com/typescript@4.8.2/lib/typescript.js +function getSourceFileOfNode(node) { + while (node && node.kind !== 305 /* SyntaxKind.SourceFile */) { + node = node.parent; + } + return node; +} + // Invalid decorators are removed since `@typescript-eslint/typescript-estree` v4 // https://github.com/typescript-eslint/typescript-eslint/pull/2375 -function throwErrorForInvalidDecorator( - tsNode, - esTreeNode, - tsNodeToESTreeNodeMap -) { - const tsDecorators = tsNode.decorators; - if (!Array.isArray(tsDecorators)) { +// There is a `checkGrammarDecorators` in `typescript` package, consider use it directly in future +function throwErrorForInvalidDecorator(tsNode) { + const { illegalDecorators } = tsNode; + if (!isNonEmptyArray(illegalDecorators)) { return; } - const esTreeDecorators = esTreeNode.decorators; - if ( - !Array.isArray(esTreeDecorators) || - esTreeDecorators.length !== tsDecorators.length || - tsDecorators.some((tsDecorator) => { - const esTreeDecorator = tsNodeToESTreeNodeMap.get(tsDecorator); - return !esTreeDecorator || !esTreeDecorators.includes(esTreeDecorator); - }) - ) { - throwTsSyntaxError( - esTreeNode, - "Leading decorators must be attached to a class declaration" - ); - } + + const [{ expression }] = illegalDecorators; + + const sourceFile = getSourceFileOfNode(expression); + const [start, end] = [expression.pos, expression.end].map((position) => { + const { line, character: column } = + sourceFile.getLineAndCharacterOfPosition(position); + return { line: line + 1, column }; + }); + + throwTsSyntaxError({ loc: { start, end } }, "Decorators are not valid here."); } // Values of abstract property is removed since `@typescript-eslint/typescript-estree` v5 @@ -59,12 +62,13 @@ function throwErrorForInvalidNodes(ast, options) { if (!tsNode) { return; } + const esTreeNode = tsNodeToESTreeNodeMap.get(tsNode); if (esTreeNode !== node) { return; } - throwErrorForInvalidDecorator(tsNode, esTreeNode, tsNodeToESTreeNodeMap); + throwErrorForInvalidDecorator(tsNode); throwErrorForInvalidAbstractProperty(tsNode, esTreeNode); }); } diff --git a/tests/config/format-test.js b/tests/config/format-test.js index 98151797d787..9802c444e3c3 100644 --- a/tests/config/format-test.js +++ b/tests/config/format-test.js @@ -78,10 +78,7 @@ const meriyahDisabledTests = new Set([ ), ]); const babelTsDisabledTest = new Set( - [ - // Disabled temporarily https://github.com/babel/babel/issues/14777#issuecomment-1191474632 - "instantiation-expression", - ].map((directory) => path.join(__dirname, "../format/typescript", directory)) + [].map((directory) => path.join(__dirname, "../format/typescript", directory)) ); const isUnstable = (filename, options) => { diff --git a/tests/format/misc/errors/invalid-typescript-decorators/__snapshots__/jsfmt.spec.js.snap b/tests/format/misc/errors/invalid-typescript-decorators/__snapshots__/jsfmt.spec.js.snap index 8d1286b597dd..26d9fed0966b 100644 --- a/tests/format/misc/errors/invalid-typescript-decorators/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/misc/errors/invalid-typescript-decorators/__snapshots__/jsfmt.spec.js.snap @@ -10,13 +10,12 @@ exports[`decorator.ts [babel-ts] format 1`] = ` `; exports[`decorator.ts [typescript] format 1`] = ` -"Leading decorators must be attached to a class declaration (3:1) +"Decorators are not valid here. (3:2) 1 | declare function dec(target: T): T; 2 | > 3 | @dec - | ^^^^ -> 4 | enum E {} - | ^^^^^^^^^^ + | ^^^ + 4 | enum E {} 5 |" `; @@ -32,24 +31,14 @@ exports[`enums.ts [babel-ts] format 1`] = ` `; exports[`enums.ts [typescript] format 1`] = ` -"Leading decorators must be attached to a class declaration (3:1) - 1 | // https://github.com/typescript-eslint/typescript-eslint/pull/2375 - 2 | -> 3 | @decorator() - | ^^^^^^^^^^^^ -> 4 | enum Direction { - | ^^^^^^^^^^^^^^^^ -> 5 | Up = 1, - | ^^^^^^^^^^^^^^^^ -> 6 | Down, - | ^^^^^^^^^^^^^^^^ -> 7 | Left, - | ^^^^^^^^^^^^^^^^ -> 8 | Right - | ^^^^^^^^^^^^^^^^ -> 9 | } - | ^^ - 10 |" +"Decorators are not valid here. (3:2) + 1 | // https://github.com/typescript-eslint/typescript-eslint/pull/2375 + 2 | +> 3 | @decorator() + | ^^^^^^^^^^^ + 4 | enum Direction { + 5 | Up = 1, + 6 | Down," `; exports[`function.ts [babel-ts] format 1`] = ` @@ -62,13 +51,12 @@ exports[`function.ts [babel-ts] format 1`] = ` `; exports[`function.ts [typescript] format 1`] = ` -"Leading decorators must be attached to a class declaration (3:1) +"Decorators are not valid here. (3:2) 1 | // https://github.com/typescript-eslint/typescript-eslint/pull/2375 2 | > 3 | @decorator() - | ^^^^^^^^^^^^ -> 4 | function foo( ){} - | ^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^ + 4 | function foo( ){} 5 |" `; @@ -83,15 +71,13 @@ exports[`interface.ts [babel-ts] format 1`] = ` `; exports[`interface.ts [typescript] format 1`] = ` -"Leading decorators must be attached to a class declaration (4:1) +"Decorators are not valid here. (4:2) 2 | // #4632 3 | > 4 | @hello() - | ^^^^^^^^ -> 5 | interface MyInterface {id: string; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -> 6 | } - | ^^ + | ^^^^^^^ + 5 | interface MyInterface {id: string; + 6 | } 7 |" `; @@ -105,11 +91,10 @@ exports[`issue-9102.ts [babel-ts] format 1`] = ` `; exports[`issue-9102.ts [typescript] format 1`] = ` -"Leading decorators must be attached to a class declaration (1:1) +"Decorators are not valid here. (1:2) > 1 | @Decorator() - | ^^^^^^^^^^^^ -> 2 | type T = 1; - | ^^^^^^^^^^^^ + | ^^^^^^^^^^^ + 2 | type T = 1; 3 | class C {} 4 |" `; diff --git a/tests/format/typescript/instantiation-expression/__snapshots__/jsfmt.spec.js.snap b/tests/format/typescript/instantiation-expression/__snapshots__/jsfmt.spec.js.snap index 974c9bdab73a..75e7a6721b94 100644 --- a/tests/format/typescript/instantiation-expression/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/typescript/instantiation-expression/__snapshots__/jsfmt.spec.js.snap @@ -26,7 +26,8 @@ new A < B > C =====================================output===================================== -new A() < B > C; +new A(); +C; ================================================================================ `; diff --git a/yarn.lock b/yarn.lock index d50aded68c41..8d0c403923ab 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1635,13 +1635,13 @@ __metadata: languageName: node linkType: hard -"@typescript-eslint/eslint-plugin@npm:5.35.1": - version: 5.35.1 - resolution: "@typescript-eslint/eslint-plugin@npm:5.35.1" +"@typescript-eslint/eslint-plugin@npm:5.36.2": + version: 5.36.2 + resolution: "@typescript-eslint/eslint-plugin@npm:5.36.2" dependencies: - "@typescript-eslint/scope-manager": 5.35.1 - "@typescript-eslint/type-utils": 5.35.1 - "@typescript-eslint/utils": 5.35.1 + "@typescript-eslint/scope-manager": 5.36.2 + "@typescript-eslint/type-utils": 5.36.2 + "@typescript-eslint/utils": 5.36.2 debug: ^4.3.4 functional-red-black-tree: ^1.0.1 ignore: ^5.2.0 @@ -1654,25 +1654,26 @@ __metadata: peerDependenciesMeta: typescript: optional: true - checksum: 073f4dffd863881f1c87e1c217ac13bda44aaa2db12ef260032b5e8eb6ffd6b9cf6f62c85132dbf84152f353c435c66dd4f75c3bcb86eb23e926737aa4fb66fa + checksum: edcd9fcecdeb22a689b421cafe3b7adc859bf2fd6227aecdd7412c319c808e7bab063c8f94af32116cfc971962f9780d181cb0a4aa999951c2d2be1f84c6c376 languageName: node linkType: hard -"@typescript-eslint/scope-manager@npm:5.35.1": - version: 5.35.1 - resolution: "@typescript-eslint/scope-manager@npm:5.35.1" +"@typescript-eslint/scope-manager@npm:5.36.2": + version: 5.36.2 + resolution: "@typescript-eslint/scope-manager@npm:5.36.2" dependencies: - "@typescript-eslint/types": 5.35.1 - "@typescript-eslint/visitor-keys": 5.35.1 - checksum: 5a969a081309bac5962f99ee6dfdfd9c68ea677bc79d9796592dce82a36217f67aa55c7bf421b2c97b46c5149d6a9401bb4c57829595e8c19f47cfa9e8c2dd86 + "@typescript-eslint/types": 5.36.2 + "@typescript-eslint/visitor-keys": 5.36.2 + checksum: 93ff655f7c237c88ec6dc5911202dd8f81bd8909b27f1a758a9d77e9791040f1ee6fe2891314bde75c808ce586246e98003a1b1396937b0312f2440016dea751 languageName: node linkType: hard -"@typescript-eslint/type-utils@npm:5.35.1": - version: 5.35.1 - resolution: "@typescript-eslint/type-utils@npm:5.35.1" +"@typescript-eslint/type-utils@npm:5.36.2": + version: 5.36.2 + resolution: "@typescript-eslint/type-utils@npm:5.36.2" dependencies: - "@typescript-eslint/utils": 5.35.1 + "@typescript-eslint/typescript-estree": 5.36.2 + "@typescript-eslint/utils": 5.36.2 debug: ^4.3.4 tsutils: ^3.21.0 peerDependencies: @@ -1680,23 +1681,23 @@ __metadata: peerDependenciesMeta: typescript: optional: true - checksum: af317ba156f2767f76a7f97193873a00468370e157fdcc6ac19f664bc6c4c0a6836bd25028d17fdd54d339b6842fda68b82f1ce4142a222de6953625ea6c0a9c + checksum: c202b7d2cd08ed7f7d1ad7e430e9e1596478e147f0d485d02babfda0211c55fa950de1dc4d1c950008a8a047a31c1e982e97fe5558f93d496830eb9d9532bc71 languageName: node linkType: hard -"@typescript-eslint/types@npm:5.35.1": - version: 5.35.1 - resolution: "@typescript-eslint/types@npm:5.35.1" - checksum: a4e1001867f43f3364b109fc5a07b91ae7a34b78ab191c6c5c4695dac9bb2b80b0a602651c0b807c1c7c1fc3656d2bbd47c637afa08a09e7b1c39eae3c489e00 +"@typescript-eslint/types@npm:5.36.2": + version: 5.36.2 + resolution: "@typescript-eslint/types@npm:5.36.2" + checksum: 736cb8a76b58f2f9a7d066933094c5510ffe31479ea8b804a829ec85942420f1b55e0eb2688fbdaaaa9c0e5b3b590fb8f14bbd745353696b4fd33fda620d417b languageName: node linkType: hard -"@typescript-eslint/typescript-estree@npm:5.35.1": - version: 5.35.1 - resolution: "@typescript-eslint/typescript-estree@npm:5.35.1" +"@typescript-eslint/typescript-estree@npm:5.36.2": + version: 5.36.2 + resolution: "@typescript-eslint/typescript-estree@npm:5.36.2" dependencies: - "@typescript-eslint/types": 5.35.1 - "@typescript-eslint/visitor-keys": 5.35.1 + "@typescript-eslint/types": 5.36.2 + "@typescript-eslint/visitor-keys": 5.36.2 debug: ^4.3.4 globby: ^11.1.0 is-glob: ^4.0.3 @@ -1705,33 +1706,33 @@ __metadata: peerDependenciesMeta: typescript: optional: true - checksum: a917ca4753a3f92c8d8555c96f5414383a9742761625476fa36a019401543aa74996159afa0f7fc7fae05fe0f904e3c6f4153a55412070c8a94e8171e81084c7 + checksum: 2827ff57a114b6107ea6d555f3855007133b08a7c2bafba0cfa0c935d8b99fd7b49e982d48cccc1c5ba550d95748d0239f5e2109893f12a165d76ed64a0d261b languageName: node linkType: hard -"@typescript-eslint/utils@npm:5.35.1, @typescript-eslint/utils@npm:^5.10.0": - version: 5.35.1 - resolution: "@typescript-eslint/utils@npm:5.35.1" +"@typescript-eslint/utils@npm:5.36.2, @typescript-eslint/utils@npm:^5.10.0": + version: 5.36.2 + resolution: "@typescript-eslint/utils@npm:5.36.2" dependencies: "@types/json-schema": ^7.0.9 - "@typescript-eslint/scope-manager": 5.35.1 - "@typescript-eslint/types": 5.35.1 - "@typescript-eslint/typescript-estree": 5.35.1 + "@typescript-eslint/scope-manager": 5.36.2 + "@typescript-eslint/types": 5.36.2 + "@typescript-eslint/typescript-estree": 5.36.2 eslint-scope: ^5.1.1 eslint-utils: ^3.0.0 peerDependencies: eslint: ^6.0.0 || ^7.0.0 || ^8.0.0 - checksum: 2b04092583c3139dd090727c24fb9d7fdb1fb9f20f2e3f0141cab5b98b6a1934b0fc8cab948f7faae55588385b0f1fb7bbf91f52c705ce4528036a527c3119c6 + checksum: 45356cf55a8733e3ab1f2c3c19cdaefdb79857e35eb1433c29b81f3df071e9cef8a286bc407abe243889a21d9e793e999f92f03b9c727a0fac1c17a48e64c42a languageName: node linkType: hard -"@typescript-eslint/visitor-keys@npm:5.35.1": - version: 5.35.1 - resolution: "@typescript-eslint/visitor-keys@npm:5.35.1" +"@typescript-eslint/visitor-keys@npm:5.36.2": + version: 5.36.2 + resolution: "@typescript-eslint/visitor-keys@npm:5.36.2" dependencies: - "@typescript-eslint/types": 5.35.1 + "@typescript-eslint/types": 5.36.2 eslint-visitor-keys: ^3.3.0 - checksum: ef3c8377aac89935b5cc2fcf37bb3e42aa5f98848e7c22bdcbe5bb06c0fe8a1373a6897fd21109be8929b4708ad06c8874d2ef7bba17ff64911964203457330d + checksum: 87ccdcfa5cdedaa3a1aac30d656969f4f5910b62bcaacdf80a514dbf0cbbd8e79b55f8e987eab34cc79ece8ce4b8c19d5caf8b0afb74e0b0d7ab39fb29aa8eba languageName: node linkType: hard @@ -6899,9 +6900,9 @@ __metadata: "@types/file-entry-cache": 5.0.2 "@types/find-cache-dir": 3.2.1 "@types/jest": 29.0.0 - "@typescript-eslint/eslint-plugin": 5.35.1 - "@typescript-eslint/typescript-estree": 5.35.1 - "@typescript-eslint/visitor-keys": 5.35.1 + "@typescript-eslint/eslint-plugin": 5.36.2 + "@typescript-eslint/typescript-estree": 5.36.2 + "@typescript-eslint/visitor-keys": 5.36.2 acorn: 8.8.0 acorn-jsx: 5.3.2 angular-estree-parser: 2.5.1 @@ -6998,7 +6999,7 @@ __metadata: strip-ansi: 7.0.1 tempy: 3.0.0 to-fast-properties: 4.0.0 - typescript: 4.7.4 + typescript: 4.8.2 unicode-regex: 3.0.0 unified: 9.2.2 vnopts: 1.0.2 @@ -8289,23 +8290,23 @@ __metadata: languageName: node linkType: hard -"typescript@npm:4.7.4": - version: 4.7.4 - resolution: "typescript@npm:4.7.4" +"typescript@npm:4.8.2": + version: 4.8.2 + resolution: "typescript@npm:4.8.2" bin: tsc: bin/tsc tsserver: bin/tsserver - checksum: 5750181b1cd7e6482c4195825547e70f944114fb47e58e4aa7553e62f11b3f3173766aef9c281783edfd881f7b8299cf35e3ca8caebe73d8464528c907a164df + checksum: 7f5b81d0d558c9067f952c7af52ab7f19c2e70a916817929e4a5b256c93990bf3178eccb1ac8a850bc75df35f6781b6f4cb3370ce20d8b1ded92ed462348f628 languageName: node linkType: hard -"typescript@patch:typescript@4.7.4#~builtin": - version: 4.7.4 - resolution: "typescript@patch:typescript@npm%3A4.7.4#~builtin::version=4.7.4&hash=a1c5e5" +"typescript@patch:typescript@4.8.2#~builtin": + version: 4.8.2 + resolution: "typescript@patch:typescript@npm%3A4.8.2#~builtin::version=4.8.2&hash=a1c5e5" bin: tsc: bin/tsc tsserver: bin/tsserver - checksum: 9096d8f6c16cb80ef3bf96fcbbd055bf1c4a43bd14f3b7be45a9fbe7ada46ec977f604d5feed3263b4f2aa7d4c7477ce5f9cd87de0d6feedec69a983f3a4f93e + checksum: 5cb0f02f414f5405f4b0e7ee1fd7fa9177b6a8783c9017b6cad85f56ce4c4f93e0e6f2ce37e863cb597d44227cd009474c9fbd85bf7a50004e5557426cb58079 languageName: node linkType: hard