From 4cc149248a78491eecacccce7e04b82e751afb64 Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Wed, 18 Nov 2020 10:59:58 +0900 Subject: [PATCH] Support TypeScript 4.1 via typescript-estree (#9636) * Update typescript and typescript-estree * Add AST transformation for TSTemplateLiteralType * Update tests for template-literal-types * Fix transformChainExpression * Update tests for key-remapping-in-mapped-types * Implement recoverInvalidDecorators * Update changelog * Avoid typecheck error * Add os.platform() shim * Update config.js * Update config.js * Shim os.cpus * Update `process.versions.node` * Attempt to fix build * Update ts parserOptions * Fix code style * Remove `errorOnUnknownASTType`, fix code style * Use `[] ||` instead of `[] //` * Fix hack * Add invalid tests * checkMissingDecorators * Update tests * Fix tests * Add comment * Revert "Implement recoverInvalidDecorators" This reverts commit 27e7b46ed41df36fb585791117c85e1b2dab932c. * Add ref pr link * Simplify logic * Don't transform `TSTemplateLiteralType` * Add `TSTemplateLiteralType` as simple type * Fix tests * Update comments * Add comment * Fix logic * Minor tweak * use multiple parsers * Update @typescript-eslint/typescript-estree to v4.8.0 * Switch to `parseWithNodeMaps` * Rename `esNode` -> `esTreeNode` * Update @typescript-eslint/typescript-estree to 4.8.1-alpha.1 * Use @typescript-eslint/typescript-estree 4.8.1 Co-authored-by: fisker Cheung --- changelog_unreleased/typescript/pr-9473.md | 4 +- package.json | 4 +- scripts/build/config.js | 5 + scripts/build/shims/os.mjs | 2 + src/language-graphql/printer-graphql.js | 11 +-- src/language-handlebars/printer-glimmer.js | 11 +-- src/language-js/parse-postprocess.js | 59 +++++++++++- src/language-js/parser-typescript.js | 17 ++-- src/language-js/print/template-literal.js | 15 ++- src/language-js/printer-estree-json.js | 4 +- src/language-js/printer-estree.js | 1 + src/language-js/utils.js | 11 ++- src/main/comments.js | 20 ++-- .../__snapshots__/jsfmt.spec.js.snap | 96 +++++++++++++++++++ .../decorator.ts | 0 .../invalid-typescript-decorators/enums.ts | 9 ++ .../invalid-typescript-decorators/function.ts | 4 + .../interface.ts | 4 +- .../jsfmt.spec.js | 1 + .../__snapshots__/jsfmt.spec.js.snap | 30 ------ .../conformance/types/decorator/jsfmt.spec.js | 1 - .../__snapshots__/jsfmt.spec.js.snap | 35 ------- tests/typescript/decorators/jsfmt.spec.js | 4 +- .../__snapshots__/jsfmt.spec.js.snap | 10 -- .../jsfmt.spec.js | 4 +- .../__snapshots__/jsfmt.spec.js.snap | 9 -- .../template-literal-types/jsfmt.spec.js | 4 +- .../__snapshots__/jsfmt.spec.js.snap | 36 +++++++ .../typeparams/consistent/jsfmt.spec.js | 8 +- .../consistent/template-literal-types.ts | 1 + .../typeparams/consistent/typescript-only.ts | 2 +- yarn.lock | 50 +++++----- 32 files changed, 304 insertions(+), 168 deletions(-) create mode 100644 tests/misc/errors/invalid-typescript-decorators/__snapshots__/jsfmt.spec.js.snap rename tests/{typescript/conformance/types/decorator => misc/errors/invalid-typescript-decorators}/decorator.ts (100%) create mode 100644 tests/misc/errors/invalid-typescript-decorators/enums.ts create mode 100644 tests/misc/errors/invalid-typescript-decorators/function.ts rename tests/{typescript/decorators => misc/errors/invalid-typescript-decorators}/interface.ts (55%) create mode 100644 tests/misc/errors/invalid-typescript-decorators/jsfmt.spec.js delete mode 100644 tests/typescript/conformance/types/decorator/__snapshots__/jsfmt.spec.js.snap delete mode 100644 tests/typescript/conformance/types/decorator/jsfmt.spec.js create mode 100644 tests/typescript/typeparams/consistent/template-literal-types.ts diff --git a/changelog_unreleased/typescript/pr-9473.md b/changelog_unreleased/typescript/pr-9473.md index ed18101ee85b..1c0f69efdbe0 100644 --- a/changelog_unreleased/typescript/pr-9473.md +++ b/changelog_unreleased/typescript/pr-9473.md @@ -1,6 +1,4 @@ -#### [HIGHLIGHT]Support TypeScript 4.1 via Babel (#9473 by @sosukesuzuki) - -`--parser=babel-ts` is required. +#### [HIGHLIGHT]Support TypeScript 4.1 (#9473, #9636 by @sosukesuzuki) ##### Key Remapping In Mapped Types diff --git a/package.json b/package.json index e02482f24ed6..fd59770159e6 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "@babel/parser": "7.12.5", "@glimmer/syntax": "0.66.0", "@iarna/toml": "2.2.5", - "@typescript-eslint/typescript-estree": "3.10.1", + "@typescript-eslint/typescript-estree": "4.8.1", "angular-estree-parser": "2.2.1", "angular-html-parser": "1.7.1", "camelcase": "6.2.0", @@ -78,7 +78,7 @@ "resolve": "1.19.0", "semver": "7.3.2", "string-width": "4.2.0", - "typescript": "4.0.5", + "typescript": "4.1.1-rc", "unicode-regex": "3.0.0", "unified": "9.2.0", "vnopts": "1.0.2", diff --git a/scripts/build/config.js b/scripts/build/config.js index 5e527e6a4932..c154f53d5721 100644 --- a/scripts/build/config.js +++ b/scripts/build/config.js @@ -40,6 +40,11 @@ const parsers = [ // `TypeScript`, `toolsVersion`, `globalThis` 'typeof process === "undefined" || process.browser': "false", 'typeof globalThis === "object"': "true", + // `@typescript-eslint/typescript-estree` v4 + 'require("globby")': "{}", + "extra.projects = prepareAndTransformProjects(": + "extra.projects = [] || prepareAndTransformProjects(", + "process.versions.node": "'999.999.999'", }, }, { diff --git a/scripts/build/shims/os.mjs b/scripts/build/shims/os.mjs index 17621df9f66d..27e543d96026 100644 --- a/scripts/build/shims/os.mjs +++ b/scripts/build/shims/os.mjs @@ -1,3 +1,5 @@ export default { EOL: "\n", + platform: () => "browser", + cpus: () => [{ model: "Prettier" }], }; diff --git a/src/language-graphql/printer-graphql.js b/src/language-graphql/printer-graphql.js index 0101aef7e7f8..28cc63a8f270 100644 --- a/src/language-graphql/printer-graphql.js +++ b/src/language-graphql/printer-graphql.js @@ -1,15 +1,8 @@ "use strict"; const { - concat, - join, - hardline, - line, - softline, - group, - indent, - ifBreak, -} = require("../document").builders; + builders: { concat, join, hardline, line, softline, group, indent, ifBreak }, +} = require("../document"); const { isNextLineEmpty } = require("../common/util"); const { insertPragma } = require("./pragma"); const { locStart, locEnd } = require("./loc"); diff --git a/src/language-handlebars/printer-glimmer.js b/src/language-handlebars/printer-glimmer.js index 2876a2ac7f2b..ff63ca3257ff 100644 --- a/src/language-handlebars/printer-glimmer.js +++ b/src/language-handlebars/printer-glimmer.js @@ -1,15 +1,8 @@ "use strict"; const { - concat, - group, - hardline, - ifBreak, - indent, - join, - line, - softline, -} = require("../document").builders; + builders: { concat, group, hardline, ifBreak, indent, join, line, softline }, +} = require("../document"); const { locStart, locEnd } = require("./loc"); const clean = require("./clean"); const { diff --git a/src/language-js/parse-postprocess.js b/src/language-js/parse-postprocess.js index 3efece636707..9aa82f603a0e 100644 --- a/src/language-js/parse-postprocess.js +++ b/src/language-js/parse-postprocess.js @@ -5,6 +5,7 @@ const { getNextNonSpaceNonCommentCharacter, getShebang, } = require("../common/util"); +const createError = require("../common/parser-create-error"); const { composeLoc, locStart, locEnd } = require("./loc"); const { isTypeCastComment } = require("./comments"); @@ -17,6 +18,50 @@ function postprocess(ast, options) { includeShebang(ast, options); } + // Invalid decorators are removed since `@typescript-eslint/typescript-estree` v4 + // https://github.com/typescript-eslint/typescript-eslint/pull/2375 + if (options.parser === "typescript" && options.originalText.includes("@")) { + const { + esTreeNodeToTSNodeMap, + tsNodeToESTreeNodeMap, + } = options.tsParseResult; + ast = visitNode(ast, (node) => { + const tsNode = esTreeNodeToTSNodeMap.get(node); + if (!tsNode) { + return; + } + const tsDecorators = tsNode.decorators; + if (!Array.isArray(tsDecorators)) { + return; + } + // `esTreeNodeToTSNodeMap.get(ClassBody)` and `esTreeNodeToTSNodeMap.get(ClassDeclaration)` has the same tsNode + const esTreeNode = tsNodeToESTreeNodeMap.get(tsNode); + if (esTreeNode !== node) { + 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) + ); + }) + ) { + const { start, end } = esTreeNode.loc; + throw createError( + "Leading decorators must be attached to a class declaration", + { + start: { line: start.line, column: start.column + 1 }, + end: { line: end.line, column: end.column + 1 }, + } + ); + } + }); + } + // Keep Babel's non-standard ParenthesizedExpression nodes only if they have Closure-style type cast comments. if ( options.parser !== "typescript" && @@ -146,9 +191,9 @@ function postprocess(ast, options) { } } -// This is a workaround to transform `ChainExpression` from `espree` into -// `babel` shape AST, we should do the opposite, since `ChainExpression` is the -// standard `estree` AST for `optional chaining` +// This is a workaround to transform `ChainExpression` from `espree`, `meriyah`, +// and `typescript` into `babel` shape AST, we should do the opposite, +// since `ChainExpression` is the standard `estree` AST for `optional chaining` // https://github.com/estree/estree/blob/master/es2020.md function transformChainExpression(node) { if (node.type === "CallExpression") { @@ -158,6 +203,10 @@ function transformChainExpression(node) { node.type = "OptionalMemberExpression"; node.object = transformChainExpression(node.object); } + // typescript + else if (node.type === "TSNonNullExpression") { + node.expression = transformChainExpression(node.expression); + } return node; } @@ -180,6 +229,10 @@ function visitNode(node, fn) { node[key] = visitNode(child, fn); } + if (Array.isArray(node)) { + return node; + } + return fn(node) || node; } diff --git a/src/language-js/parser-typescript.js b/src/language-js/parser-typescript.js index 1c3221f692d6..bf60d724ae4f 100644 --- a/src/language-js/parser-typescript.js +++ b/src/language-js/parser-typescript.js @@ -7,14 +7,14 @@ const postprocess = require("./parse-postprocess"); function parse(text, parsers, opts) { const jsx = isProbablyJsx(text); - let ast; + let result; try { // Try passing with our best guess first. - ast = tryParseTypeScript(text, jsx); + result = tryParseTypeScript(text, jsx); } catch (firstError) { try { // But if we get it wrong, try the opposite. - ast = tryParseTypeScript(text, !jsx); + result = tryParseTypeScript(text, !jsx); } catch (secondError) { // Suppose our guess is correct, throw the first error const { message, lineNumber, column } = firstError; @@ -30,12 +30,16 @@ function parse(text, parsers, opts) { } } - return postprocess(ast, { ...opts, originalText: text }); + return postprocess(result.ast, { + ...opts, + originalText: text, + tsParseResult: result, + }); } function tryParseTypeScript(text, jsx) { - const parser = require("@typescript-eslint/typescript-estree"); - return parser.parse(text, { + const { parseWithNodeMaps } = require("@typescript-eslint/typescript-estree"); + return parseWithNodeMaps(text, { // `jest@<=26.4.2` rely on `loc` // https://github.com/facebook/jest/issues/10444 loc: true, @@ -45,6 +49,7 @@ function tryParseTypeScript(text, jsx) { jsx, tokens: true, loggerFn: false, + project: [], }); } diff --git a/src/language-js/print/template-literal.js b/src/language-js/print/template-literal.js index 2ef4d77f991a..157ddc018bc2 100644 --- a/src/language-js/print/template-literal.js +++ b/src/language-js/print/template-literal.js @@ -24,17 +24,24 @@ const { function printTemplateLiteral(path, print, options) { const node = path.getValue(); - const parentNode = path.getParentNode(); + const isTemplateLiteral = node.type === "TemplateLiteral"; - if (isJestEachTemplateLiteral(node, parentNode)) { + if ( + isTemplateLiteral && + isJestEachTemplateLiteral(node, path.getParentNode()) + ) { const printed = printJestEachTemplateLiteral(path, options, print); if (printed) { return printed; } } + let expressionsKey = "expressions"; + if (node.type === "TSTemplateLiteralType") { + expressionsKey = "types"; + } const parts = []; - let expressions = path.map(print, "expressions"); + let expressions = path.map(print, expressionsKey); const isSimple = isSimpleTemplateLiteral(node); if (isSimple) { @@ -70,7 +77,7 @@ function printTemplateLiteral(path, print, options) { let printed = expressions[i]; if (!isSimple) { - const expression = node.expressions[i]; + const expression = node[expressionsKey][i]; // Breaks at the template element boundaries (${ and }) are preferred to breaking // in the middle of a MemberExpression if ( diff --git a/src/language-js/printer-estree-json.js b/src/language-js/printer-estree-json.js index 61d90c19953c..4ba8849ba88a 100644 --- a/src/language-js/printer-estree-json.js +++ b/src/language-js/printer-estree-json.js @@ -1,6 +1,8 @@ "use strict"; -const { concat, hardline, indent, join } = require("../document").builders; +const { + builders: { concat, hardline, indent, join }, +} = require("../document"); const preprocess = require("./print-preprocess"); function genericPrint(path, options, print) { diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index a6b8c0bc34a1..4657fa348fd3 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -2238,6 +2238,7 @@ function printPathNoParens(path, options, print, args) { return concat(parts); case "TemplateElement": return join(literalline, n.value.raw.split(/\r?\n/g)); + case "TSTemplateLiteralType": case "TemplateLiteral": { return printTemplateLiteral(path, print, options); } diff --git a/src/language-js/utils.js b/src/language-js/utils.js index 412c5d09e96f..a98e8c8b6212 100644 --- a/src/language-js/utils.js +++ b/src/language-js/utils.js @@ -464,6 +464,7 @@ const simpleTypeAnnotations = new Set([ "BigIntLiteralTypeAnnotation", "NumberLiteralTypeAnnotation", "TSLiteralType", + "TSTemplateLiteralType", // flow only, `empty`, `mixed` "EmptyTypeAnnotation", "MixedTypeAnnotation", @@ -647,11 +648,17 @@ function isNgForOf(node, index, parentNode) { * @returns {boolean} */ function isSimpleTemplateLiteral(node) { - if (node.expressions.length === 0) { + let expressionsKey = "expressions"; + if (node.type === "TSTemplateLiteralType") { + expressionsKey = "types"; + } + const expressions = node[expressionsKey]; + + if (expressions.length === 0) { return false; } - return node.expressions.every((expr) => { + return expressions.every((expr) => { // Disallow comments since printDocToString can't print them here if (expr.comments) { return false; diff --git a/src/main/comments.js b/src/main/comments.js index acb8343602bc..98209cc3bf80 100644 --- a/src/main/comments.js +++ b/src/main/comments.js @@ -4,15 +4,17 @@ const assert = require("assert"); const { - concat, - line, - hardline, - breakParent, - indent, - lineSuffix, - join, - cursor, -} = require("../document").builders; + builders: { + concat, + line, + hardline, + breakParent, + indent, + lineSuffix, + join, + cursor, + }, +} = require("../document"); const { hasNewline, diff --git a/tests/misc/errors/invalid-typescript-decorators/__snapshots__/jsfmt.spec.js.snap b/tests/misc/errors/invalid-typescript-decorators/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000000..3d93c72049b3 --- /dev/null +++ b/tests/misc/errors/invalid-typescript-decorators/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,96 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`decorator.ts [babel-ts] format 1`] = ` +"Leading decorators must be attached to a class declaration (4:1) + 2 | + 3 | @dec +> 4 | enum E {} + | ^ + 5 | " +`; + +exports[`decorator.ts [typescript] format 1`] = ` +"Leading decorators must be attached to a class declaration (3:1) + 1 | declare function dec(target: T): T; + 2 | +> 3 | @dec + | ^^^^ +> 4 | enum E {} + | ^^^^^^^^^^ + 5 | " +`; + +exports[`enums.ts [babel-ts] format 1`] = ` +"Leading decorators must be attached to a class declaration (4:1) + 2 | + 3 | @decorator() +> 4 | enum Direction { + | ^ + 5 | Up = 1, + 6 | Down, + 7 | Left," +`; + +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 | " +`; + +exports[`function.ts [babel-ts] format 1`] = ` +"Leading decorators must be attached to a class declaration (4:1) + 2 | + 3 | @decorator() +> 4 | function foo( ){} + | ^ + 5 | " +`; + +exports[`function.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 | function foo( ){} + | ^^^^^^^^^^^^^^^^^^^^^^^ + 5 | " +`; + +exports[`interface.ts [babel-ts] format 1`] = ` +"Leading decorators must be attached to a class declaration (5:1) + 3 | + 4 | @hello() +> 5 | interface MyInterface {id: string; + | ^ + 6 | } + 7 | " +`; + +exports[`interface.ts [typescript] format 1`] = ` +"Leading decorators must be attached to a class declaration (4:1) + 2 | // #4632 + 3 | +> 4 | @hello() + | ^^^^^^^^ +> 5 | interface MyInterface {id: string; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +> 6 | } + | ^^ + 7 | " +`; diff --git a/tests/typescript/conformance/types/decorator/decorator.ts b/tests/misc/errors/invalid-typescript-decorators/decorator.ts similarity index 100% rename from tests/typescript/conformance/types/decorator/decorator.ts rename to tests/misc/errors/invalid-typescript-decorators/decorator.ts diff --git a/tests/misc/errors/invalid-typescript-decorators/enums.ts b/tests/misc/errors/invalid-typescript-decorators/enums.ts new file mode 100644 index 000000000000..6db307060744 --- /dev/null +++ b/tests/misc/errors/invalid-typescript-decorators/enums.ts @@ -0,0 +1,9 @@ +// https://github.com/typescript-eslint/typescript-eslint/pull/2375 + +@decorator() +enum Direction { + Up = 1, + Down, + Left, + Right +} diff --git a/tests/misc/errors/invalid-typescript-decorators/function.ts b/tests/misc/errors/invalid-typescript-decorators/function.ts new file mode 100644 index 000000000000..038ebafbcb15 --- /dev/null +++ b/tests/misc/errors/invalid-typescript-decorators/function.ts @@ -0,0 +1,4 @@ +// https://github.com/typescript-eslint/typescript-eslint/pull/2375 + +@decorator() +function foo( ){} diff --git a/tests/typescript/decorators/interface.ts b/tests/misc/errors/invalid-typescript-decorators/interface.ts similarity index 55% rename from tests/typescript/decorators/interface.ts rename to tests/misc/errors/invalid-typescript-decorators/interface.ts index de7bb11c034e..ffd85a50a662 100644 --- a/tests/typescript/decorators/interface.ts +++ b/tests/misc/errors/invalid-typescript-decorators/interface.ts @@ -1,6 +1,6 @@ // invalid place but shouldn't be removed +// #4632 @hello() -interface MyInterface { - id: string; +interface MyInterface {id: string; } diff --git a/tests/misc/errors/invalid-typescript-decorators/jsfmt.spec.js b/tests/misc/errors/invalid-typescript-decorators/jsfmt.spec.js new file mode 100644 index 000000000000..aff59cd2fbce --- /dev/null +++ b/tests/misc/errors/invalid-typescript-decorators/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, ["typescript", "babel-ts"]); diff --git a/tests/typescript/conformance/types/decorator/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/conformance/types/decorator/__snapshots__/jsfmt.spec.js.snap deleted file mode 100644 index 38c86cce315c..000000000000 --- a/tests/typescript/conformance/types/decorator/__snapshots__/jsfmt.spec.js.snap +++ /dev/null @@ -1,30 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`decorator.ts [babel-ts] format 1`] = ` -"Leading decorators must be attached to a class declaration (4:1) - 2 | - 3 | @dec -> 4 | enum E {} - | ^ - 5 | " -`; - -exports[`decorator.ts format 1`] = ` -====================================options===================================== -parsers: ["typescript"] -printWidth: 80 - | printWidth -=====================================input====================================== -declare function dec(target: T): T; - -@dec -enum E {} - -=====================================output===================================== -declare function dec(target: T): T; - -@dec -enum E {} - -================================================================================ -`; diff --git a/tests/typescript/conformance/types/decorator/jsfmt.spec.js b/tests/typescript/conformance/types/decorator/jsfmt.spec.js deleted file mode 100644 index 4c48f7a811fd..000000000000 --- a/tests/typescript/conformance/types/decorator/jsfmt.spec.js +++ /dev/null @@ -1 +0,0 @@ -run_spec(__dirname, ["typescript"], { errors: { "babel-ts": true } }); diff --git a/tests/typescript/decorators/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/decorators/__snapshots__/jsfmt.spec.js.snap index e0d418a9854b..2d5808521bf7 100644 --- a/tests/typescript/decorators/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/decorators/__snapshots__/jsfmt.spec.js.snap @@ -395,38 +395,3 @@ class MyContainerComponent { ================================================================================ `; - -exports[`interface.ts [babel-ts] format 1`] = ` -"Leading decorators must be attached to a class declaration (4:1) - 2 | - 3 | @hello() -> 4 | interface MyInterface { - | ^ - 5 | id: string; - 6 | } - 7 | " -`; - -exports[`interface.ts format 1`] = ` -====================================options===================================== -parsers: ["typescript"] -printWidth: 80 - | printWidth -=====================================input====================================== -// invalid place but shouldn't be removed - -@hello() -interface MyInterface { - id: string; -} - -=====================================output===================================== -// invalid place but shouldn't be removed - -@hello() -interface MyInterface { - id: string; -} - -================================================================================ -`; diff --git a/tests/typescript/decorators/jsfmt.spec.js b/tests/typescript/decorators/jsfmt.spec.js index ac047e1b5a5c..2ea3bb6eb2e4 100644 --- a/tests/typescript/decorators/jsfmt.spec.js +++ b/tests/typescript/decorators/jsfmt.spec.js @@ -1,3 +1 @@ -run_spec(__dirname, ["typescript"], { - errors: { "babel-ts": ["interface.ts"] }, -}); +run_spec(__dirname, ["typescript"]); diff --git a/tests/typescript/key-remapping-in-mapped-types/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/key-remapping-in-mapped-types/__snapshots__/jsfmt.spec.js.snap index b46fb45264a4..6d6725530082 100644 --- a/tests/typescript/key-remapping-in-mapped-types/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/key-remapping-in-mapped-types/__snapshots__/jsfmt.spec.js.snap @@ -1,15 +1,5 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`key-remapping.ts [typescript] format 1`] = ` -"']' expected. (2:17) - 1 | type MappedTypeWithNewKeys = { -> 2 | [K in keyof T as NewKeyType]: T[K] - | ^ - 3 | }; - 4 | - 5 | type RemoveKindField = {" -`; - exports[`key-remapping.ts format 1`] = ` ====================================options===================================== parsers: ["babel-ts", "typescript"] diff --git a/tests/typescript/key-remapping-in-mapped-types/jsfmt.spec.js b/tests/typescript/key-remapping-in-mapped-types/jsfmt.spec.js index 1b122de11962..92b81c22bdc0 100644 --- a/tests/typescript/key-remapping-in-mapped-types/jsfmt.spec.js +++ b/tests/typescript/key-remapping-in-mapped-types/jsfmt.spec.js @@ -1,3 +1 @@ -run_spec(__dirname, ["babel-ts", "typescript"], { - errors: { typescript: true }, -}); +run_spec(__dirname, ["babel-ts", "typescript"]); diff --git a/tests/typescript/template-literal-types/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/template-literal-types/__snapshots__/jsfmt.spec.js.snap index de4070dd930e..8c44481543a2 100644 --- a/tests/typescript/template-literal-types/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/template-literal-types/__snapshots__/jsfmt.spec.js.snap @@ -1,14 +1,5 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`template-literal-types.ts [typescript] format 1`] = ` -"Type expected. (1:8) -> 1 | let x: \`foo-\${infer bar}\`; - | ^ - 2 | type HelloWorld = \`\${Hello}, \${World}\` - 3 | type SeussFish = \`\${Quantity | Color} fish\`; - 4 | declare function setAlignment(value: \`\${VerticalAlignment}-\${HorizontalAlignment}\`): void;" -`; - exports[`template-literal-types.ts format 1`] = ` ====================================options===================================== parsers: ["babel-ts", "typescript"] diff --git a/tests/typescript/template-literal-types/jsfmt.spec.js b/tests/typescript/template-literal-types/jsfmt.spec.js index 1b122de11962..92b81c22bdc0 100644 --- a/tests/typescript/template-literal-types/jsfmt.spec.js +++ b/tests/typescript/template-literal-types/jsfmt.spec.js @@ -1,3 +1 @@ -run_spec(__dirname, ["babel-ts", "typescript"], { - errors: { typescript: true }, -}); +run_spec(__dirname, ["babel-ts", "typescript"]); diff --git a/tests/typescript/typeparams/consistent/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/typeparams/consistent/__snapshots__/jsfmt.spec.js.snap index bc59c79cff51..7a227a5e08ee 100644 --- a/tests/typescript/typeparams/consistent/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/typeparams/consistent/__snapshots__/jsfmt.spec.js.snap @@ -71,6 +71,41 @@ const foo12: Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo ================================================================================ `; +exports[`template-literal-types.ts [babel] format 1`] = ` +"Unexpected token (1:84) +> 1 | const foo1: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo<\`Hello, \${keyof World}\`> = a; + | ^ + 2 | " +`; + +exports[`template-literal-types.ts [babel-flow] format 1`] = ` +"Unexpected token (1:84) +> 1 | const foo1: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo<\`Hello, \${keyof World}\`> = a; + | ^ + 2 | " +`; + +exports[`template-literal-types.ts [flow] format 1`] = ` +"Unexpected token \`\`\` (1:84) +> 1 | const foo1: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo<\`Hello, \${keyof World}\`> = a; + | ^ + 2 | " +`; + +exports[`template-literal-types.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript", "flow", "babel-flow", "babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +const foo1: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo<\`Hello, \${keyof World}\`> = a; + +=====================================output===================================== +const foo1: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo<\`Hello, \${keyof World}\`> = a; + +================================================================================ +`; + exports[`typescript-only.ts format 1`] = ` ====================================options===================================== parsers: ["typescript", "flow", "babel-flow", "babel"] @@ -81,6 +116,7 @@ const foo1: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo const foo2: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo = a; const foo3: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo = a; const foo4: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo = a; + =====================================output===================================== const foo1: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo = a; const foo2: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo = a; diff --git a/tests/typescript/typeparams/consistent/jsfmt.spec.js b/tests/typescript/typeparams/consistent/jsfmt.spec.js index 61cd597f9ee9..9990099dc84b 100644 --- a/tests/typescript/typeparams/consistent/jsfmt.spec.js +++ b/tests/typescript/typeparams/consistent/jsfmt.spec.js @@ -1 +1,7 @@ -run_spec(__dirname, ["typescript", "flow", "babel-flow", "babel"]); +run_spec(__dirname, ["typescript", "flow", "babel-flow", "babel"], { + errors: { + flow: ["template-literal-types.ts"], + "babel-flow": ["template-literal-types.ts"], + babel: ["template-literal-types.ts"], + }, +}); diff --git a/tests/typescript/typeparams/consistent/template-literal-types.ts b/tests/typescript/typeparams/consistent/template-literal-types.ts new file mode 100644 index 000000000000..06c888eeeda3 --- /dev/null +++ b/tests/typescript/typeparams/consistent/template-literal-types.ts @@ -0,0 +1 @@ +const foo1: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo<`Hello, ${keyof World}`> = a; diff --git a/tests/typescript/typeparams/consistent/typescript-only.ts b/tests/typescript/typeparams/consistent/typescript-only.ts index fcea89f2a2f9..68a5b36285b3 100644 --- a/tests/typescript/typeparams/consistent/typescript-only.ts +++ b/tests/typescript/typeparams/consistent/typescript-only.ts @@ -1,4 +1,4 @@ const foo1: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo = a; const foo2: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo = a; const foo3: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo = a; -const foo4: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo = a; \ No newline at end of file +const foo4: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo = a; diff --git a/yarn.lock b/yarn.lock index 192bbf37051f..34783333d25c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1465,19 +1465,10 @@ resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-4.0.1.tgz#1cf72582f764931f085cb8230ff215980fe467b2" integrity sha512-S+gD3fgbkZYW2rnbjugNMqibm9HpEjqZBZkTiI3PwbbNGWmAcxolWIUwZ0SKeG4Dy2ktpKKaI/6+HGYVH8Qrlg== -"@typescript-eslint/typescript-estree@3.10.1": - version "3.10.1" - resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-3.10.1.tgz#fd0061cc38add4fad45136d654408569f365b853" - integrity sha512-QbcXOuq6WYvnB3XPsZpIwztBoquEYLXh2MtwVU+kO8jgYCiv4G5xrSP/1wg4tkvrEE+esZVquIPX/dxPlePk1w== - dependencies: - "@typescript-eslint/types" "3.10.1" - "@typescript-eslint/visitor-keys" "3.10.1" - debug "^4.1.1" - glob "^7.1.6" - is-glob "^4.0.1" - lodash "^4.17.15" - semver "^7.3.2" - tsutils "^3.17.1" +"@typescript-eslint/types@4.8.1": + version "4.8.1" + resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-4.8.1.tgz#23829c73c5fc6f4fcd5346a7780b274f72fee222" + integrity sha512-ave2a18x2Y25q5K05K/U3JQIe2Av4+TNi/2YuzyaXLAsDx6UZkz1boZ7nR/N6Wwae2PpudTZmHFXqu7faXfHmA== "@typescript-eslint/typescript-estree@4.0.1": version "4.0.1" @@ -1493,12 +1484,19 @@ semver "^7.3.2" tsutils "^3.17.1" -"@typescript-eslint/visitor-keys@3.10.1": - version "3.10.1" - resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-3.10.1.tgz#cd4274773e3eb63b2e870ac602274487ecd1e931" - integrity sha512-9JgC82AaQeglebjZMgYR5wgmfUdUc+EitGUUMW8u2nDckaeimzW+VsoLV6FoimPv2id3VQzfjwBxEMVz08ameQ== +"@typescript-eslint/typescript-estree@4.8.1": + version "4.8.1" + resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-4.8.1.tgz#7307e3f2c9e95df7daa8dc0a34b8c43b7ec0dd32" + integrity sha512-bJ6Fn/6tW2g7WIkCWh3QRlaSU7CdUUK52shx36/J7T5oTQzANvi6raoTsbwGM11+7eBbeem8hCCKbyvAc0X3sQ== dependencies: - eslint-visitor-keys "^1.1.0" + "@typescript-eslint/types" "4.8.1" + "@typescript-eslint/visitor-keys" "4.8.1" + debug "^4.1.1" + globby "^11.0.1" + is-glob "^4.0.1" + lodash "^4.17.15" + semver "^7.3.2" + tsutils "^3.17.1" "@typescript-eslint/visitor-keys@4.0.1": version "4.0.1" @@ -1508,6 +1506,14 @@ "@typescript-eslint/types" "4.0.1" eslint-visitor-keys "^2.0.0" +"@typescript-eslint/visitor-keys@4.8.1": + version "4.8.1" + resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-4.8.1.tgz#794f68ee292d1b2e3aa9690ebedfcb3a8c90e3c3" + integrity sha512-3nrwXFdEYALQh/zW8rFwP4QltqsanCDz4CwWMPiIZmwlk9GlvBeueEIbq05SEq4ganqM0g9nh02xXgv5XI3PeQ== + dependencies: + "@typescript-eslint/types" "4.8.1" + eslint-visitor-keys "^2.0.0" + "@webassemblyjs/ast@1.9.0": version "1.9.0" resolved "https://registry.yarnpkg.com/@webassemblyjs/ast/-/ast-1.9.0.tgz#bd850604b4042459a5a41cd7d338cbed695ed964" @@ -7165,10 +7171,10 @@ typedarray-to-buffer@^3.1.5: dependencies: is-typedarray "^1.0.0" -typescript@4.0.5: - version "4.0.5" - resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.0.5.tgz#ae9dddfd1069f1cb5beb3ef3b2170dd7c1332389" - integrity sha512-ywmr/VrTVCmNTJ6iV2LwIrfG1P+lv6luD8sUJs+2eI9NLGigaN+nUQc13iHqisq7bra9lnmUSYqbJvegraBOPQ== +typescript@4.1.1-rc: + version "4.1.1-rc" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.1.1-rc.tgz#4a335244b9b2754995849ca4b607c74d6ee73719" + integrity sha512-tgNcFrLIjlaMWEc7bKC0bxLNIt8BIAauY/HLUOQDyTP75HGskETtXOt46x4EKAHRKhWVLMc7yM02puTHa/yhCA== unherit@^1.0.4: version "1.1.3"