diff --git a/src/language-js/utils.js b/src/language-js/utils.js index 2849d0298..841a21014 100644 --- a/src/language-js/utils.js +++ b/src/language-js/utils.js @@ -430,45 +430,59 @@ function isMemberish(node) { ); } -const simpleFlowTypeAnnotations = new Set([ +const simpleTypeAnnotations = new Set([ + // `any` "AnyTypeAnnotation", + "TSAnyKeyword", + // `null` "NullLiteralTypeAnnotation", - "GenericTypeAnnotation", + "TSNullKeyword", + // `this` "ThisTypeAnnotation", + "TSThisType", + // `number` "NumberTypeAnnotation", + "TSNumberKeyword", + // `void` "VoidTypeAnnotation", - "EmptyTypeAnnotation", - "MixedTypeAnnotation", + "TSVoidKeyword", + // `boolean` "BooleanTypeAnnotation", + "TSBooleanKeyword", + // `bigint` + "BigIntTypeAnnotation", + "TSBigIntKeyword", + // literals "BooleanLiteralTypeAnnotation", "StringTypeAnnotation", - "BigIntTypeAnnotation", "BigIntLiteralTypeAnnotation", -]); -const simpleTypeScriptTypeAnnotations = new Set([ - "TSAnyKeyword", - "TSNullKeyword", - "TSTypeReference", - "TSThisType", - "TSNumberKeyword", - "TSVoidKeyword", - "TSBooleanKeyword", - "TSBigIntKeyword", "TSLiteralType", + // flow only, `empty`, `mixed` + "EmptyTypeAnnotation", + "MixedTypeAnnotation", ]); - /** * @param {Node} node * @returns {boolean} */ function isSimpleType(node) { - return ( - node && - ((simpleFlowTypeAnnotations.has(node.type) && - !(node.type === "GenericTypeAnnotation" && node.typeParameters)) || - (simpleTypeScriptTypeAnnotations.has(node.type) && - !(node.type === "TSTypeReference" && node.typeParameters))) - ); + if (!node) { + return false; + } + + if ( + (node.type === "GenericTypeAnnotation" || + node.type === "TSTypeReference") && + !node.typeParameters + ) { + return true; + } + + if (simpleTypeAnnotations.has(node.type)) { + return true; + } + + return false; } const unitTestRe = /^(skip|[fx]?(it|describe|test))$/; diff --git a/tests/typescript/typeparams/consistent/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/typeparams/consistent/__snapshots__/jsfmt.spec.js.snap index 9d867e023..b977e2974 100644 --- a/tests/typescript/typeparams/consistent/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/typeparams/consistent/__snapshots__/jsfmt.spec.js.snap @@ -1,5 +1,22 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`flow-only.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript", "flow", "babel-flow", "babel"] +printWidth: 80 + | printWidth +=====================================input====================================== + +const foo6: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo = a; +const foo7: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo = a; + +=====================================output===================================== +const foo6: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo = a; +const foo7: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo = a; + +================================================================================ +`; + exports[`issue-9501.ts format 1`] = ` ====================================options===================================== parsers: ["typescript", "flow", "babel-flow", "babel"] diff --git a/tests/typescript/typeparams/consistent/flow-only.ts b/tests/typescript/typeparams/consistent/flow-only.ts new file mode 100644 index 000000000..c4f7f6e5f --- /dev/null +++ b/tests/typescript/typeparams/consistent/flow-only.ts @@ -0,0 +1,3 @@ + +const foo6: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo = a; +const foo7: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo = a;