Refactor: Improve readability (#9539)

This commit is contained in:
fisker Cheung authored and GitHub committed 2020-10-30 16:52:17 +08:00
1 parent 7351f78aa2
commit d2ce7d44d5
3 files changed
+57 -23

No files matched your search

+37 -23
View File
@@ -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))$/;
@@ -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"]
@@ -0,0 +1,3 @@
const foo6: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo<empty> = a;
const foo7: Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo<mixed> = a;