Skip to content

Commit

Permalink
Refactor: Improve readability (#9539)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Oct 30, 2020
1 parent 7351f78 commit d2ce7d4
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 23 deletions.
60 changes: 37 additions & 23 deletions src/language-js/utils.js
Expand Up @@ -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))$/;
Expand Down
@@ -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<empty> = a;
const foo7: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo<mixed> = a;
=====================================output=====================================
const foo6: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo<empty> = a;
const foo7: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo<mixed> = a;
================================================================================
`;
exports[`issue-9501.ts format 1`] = `
====================================options=====================================
parsers: ["typescript", "flow", "babel-flow", "babel"]
Expand Down
3 changes: 3 additions & 0 deletions tests/typescript/typeparams/consistent/flow-only.ts
@@ -0,0 +1,3 @@

const foo6: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo<empty> = a;
const foo7: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo<mixed> = a;

0 comments on commit d2ce7d4

Please sign in to comment.