diff --git a/src/language-js/needs-parens.js b/src/language-js/needs-parens.js index 6deab2bf0..c629fa304 100644 --- a/src/language-js/needs-parens.js +++ b/src/language-js/needs-parens.js @@ -413,7 +413,6 @@ function needsParens(path, options) { return false; } - case "TSJSDocFunctionType": case "TSConditionalType": if (name === "extendsType" && parent.type === "TSConditionalType") { return true; diff --git a/src/language-js/print/flow.js b/src/language-js/print/flow.js index 22a9d2408..adefb649c 100644 --- a/src/language-js/print/flow.js +++ b/src/language-js/print/flow.js @@ -13,7 +13,10 @@ const { printIndexedAccessType, } = require("./type-annotation"); const { printInterface } = require("./interface"); -const { printTypeParameters } = require("./type-parameters"); +const { + printTypeParameter, + printTypeParameters, +} = require("./type-parameters"); const { printExportDeclaration, printExportAllDeclaration, @@ -89,6 +92,10 @@ function printFlow(path, options, print) { // transformed away before printing. case "TypeAnnotation": return print("typeAnnotation"); + case "TypeParameter": + return printTypeParameter(path, options, print); + case "TypeofTypeAnnotation": + return ["typeof ", print("argument")]; } } diff --git a/src/language-js/print/jsx.js b/src/language-js/print/jsx.js index 57150c9b2..3b1f6ae38 100644 --- a/src/language-js/print/jsx.js +++ b/src/language-js/print/jsx.js @@ -698,6 +698,12 @@ function printJsxSpreadAttribute(path, options, print) { function printJsx(path, options, print) { const node = path.getValue(); + + // JSX nodes always starts with `JSX` + if (!node.type.startsWith("JSX")) { + return; + } + switch (node.type) { case "JSXAttribute": return printJsxAttribute(path, options, print); @@ -731,6 +737,9 @@ function printJsx(path, options, print) { case "JSXText": /* istanbul ignore next */ throw new Error("JSXTest should be handled by JSXElement"); + default: + /* istanbul ignore next */ + throw new Error(`Unknown JSX node type: ${JSON.stringify(node.type)}.`); } } diff --git a/src/language-js/print/typescript.js b/src/language-js/print/typescript.js index 424818c77..fa1619a5f 100644 --- a/src/language-js/print/typescript.js +++ b/src/language-js/print/typescript.js @@ -52,9 +52,22 @@ const { function printTypescript(path, options, print) { const node = path.getValue(); + + // TypeScript nodes always starts with `TS` + if (!node.type.startsWith("TS")) { + return; + } + + if (node.type.endsWith("Keyword")) { + return node.type.slice(2, -7).toLowerCase(); + } + const semi = options.semi ? ";" : ""; const parts = []; + switch (node.type) { + case "TSThisType": + return "this"; case "TSTypeAssertion": { const shouldBreakAfterCast = !( node.expression.type === "ArrayExpression" || @@ -131,40 +144,8 @@ function printTypescript(path, options, print) { case "TSTypeParameterDeclaration": case "TSTypeParameterInstantiation": return printTypeParameters(path, options, print, "params"); - case "TSTypeParameter": - case "TypeParameter": return printTypeParameter(path, options, print); - case "TypeofTypeAnnotation": - return ["typeof ", print("argument")]; - case "TSAbstractKeyword": - return "abstract"; - case "TSAsyncKeyword": - return "async"; - case "TSDeclareKeyword": - return "declare"; - case "TSExportKeyword": - return "export"; - case "TSNeverKeyword": - return "never"; - case "TSObjectKeyword": - return "object"; - case "TSProtectedKeyword": - return "protected"; - case "TSPrivateKeyword": - return "private"; - case "TSPublicKeyword": - return "public"; - case "TSReadonlyKeyword": - return "readonly"; - case "TSStaticKeyword": - return "static"; - case "TSUndefinedKeyword": - return "undefined"; - case "TSUnknownKeyword": - return "unknown"; - case "TSIntrinsicKeyword": - return "intrinsic"; case "TSAsExpression": { parts.push(print("expression"), " as ", print("typeAnnotation")); const parent = path.getParentNode(); @@ -179,15 +160,6 @@ function printTypescript(path, options, print) { case "TSArrayType": return [print("elementType"), "[]"]; case "TSPropertySignature": { - if (node.export) { - parts.push("export "); - } - if (node.accessibility) { - parts.push(node.accessibility + " "); - } - if (node.static) { - parts.push("static "); - } if (node.readonly) { parts.push("readonly "); } @@ -547,13 +519,11 @@ function printTypescript(path, options, print) { return ["?", print("typeAnnotation")]; case "TSJSDocNonNullableType": return ["!", print("typeAnnotation")]; - case "TSJSDocFunctionType": - return [ - "function(", - // The parameters could be here, but typescript-estree doesn't convert them anyway (throws an error). - "): ", - print("typeAnnotation"), - ]; + default: + /* istanbul ignore next */ + throw new Error( + `Unknown TypeScript node type: ${JSON.stringify(node.type)}.` + ); } } diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index b82524171..b2fb41126 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -967,33 +967,22 @@ function printPathNoParens(path, options, print, args) { case "DeclaredPredicate": return ["%checks(", print("value"), ")"]; case "AnyTypeAnnotation": - case "TSAnyKeyword": return "any"; case "BooleanTypeAnnotation": - case "TSBooleanKeyword": return "boolean"; case "BigIntTypeAnnotation": - case "TSBigIntKeyword": return "bigint"; - case "TSConstKeyword": - return "const"; case "NullLiteralTypeAnnotation": - case "TSNullKeyword": return "null"; case "NumberTypeAnnotation": - case "TSNumberKeyword": return "number"; case "SymbolTypeAnnotation": - case "TSSymbolKeyword": return "symbol"; case "StringTypeAnnotation": - case "TSStringKeyword": return "string"; case "VoidTypeAnnotation": - case "TSVoidKeyword": return "void"; case "ThisTypeAnnotation": - case "TSThisType": return "this"; case "PrivateIdentifier":