Refactor: Use optional chaining in src/language-js/comments.js (#12747)

* Use optional chaining in `src/language-js/comments.js`

* Using sets

* Address review
This commit is contained in:
Sosuke Suzuki authored and GitHub committed 2022-04-30 21:16:43 +09:00
1 parent 504d751036
commit 9348049311
1 file changed
+85 -117
+85 -117
View File
@@ -177,11 +177,7 @@ function handleIfStatementComments({
followingNode,
text,
}) {
if (
!enclosingNode ||
enclosingNode.type !== "IfStatement" ||
!followingNode
) {
if (enclosingNode?.type !== "IfStatement" || !followingNode) {
return false;
}
@@ -262,11 +258,7 @@ function handleWhileComments({
followingNode,
text,
}) {
if (
!enclosingNode ||
enclosingNode.type !== "WhileStatement" ||
!followingNode
) {
if (enclosingNode?.type !== "WhileStatement" || !followingNode) {
return false;
}
@@ -306,9 +298,8 @@ function handleTryStatementComments({
followingNode,
}) {
if (
!enclosingNode ||
(enclosingNode.type !== "TryStatement" &&
enclosingNode.type !== "CatchClause") ||
(enclosingNode?.type !== "TryStatement" &&
enclosingNode?.type !== "CatchClause") ||
!followingNode
) {
return false;
@@ -344,8 +335,7 @@ function handleMemberExpressionComments({
}) {
if (
isMemberExpression(enclosingNode) &&
followingNode &&
followingNode.type === "Identifier"
followingNode?.type === "Identifier"
) {
addLeadingComment(enclosingNode, comment);
return true;
@@ -367,9 +357,8 @@ function handleConditionalExpressionComments({
if (
(!precedingNode || !isSameLineAsPrecedingNode) &&
enclosingNode &&
(enclosingNode.type === "ConditionalExpression" ||
enclosingNode.type === "TSConditionalType") &&
(enclosingNode?.type === "ConditionalExpression" ||
enclosingNode?.type === "TSConditionalType") &&
followingNode
) {
addLeadingComment(followingNode, comment);
@@ -395,21 +384,21 @@ function handleObjectPropertyAssignment({
return false;
}
const classLikeNodeTypes = new Set([
"ClassDeclaration",
"ClassExpression",
"DeclareClass",
"DeclareInterface",
"InterfaceDeclaration",
"TSInterfaceDeclaration",
]);
function handleClassComments({
comment,
precedingNode,
enclosingNode,
followingNode,
}) {
if (
enclosingNode &&
(enclosingNode.type === "ClassDeclaration" ||
enclosingNode.type === "ClassExpression" ||
enclosingNode.type === "DeclareClass" ||
enclosingNode.type === "DeclareInterface" ||
enclosingNode.type === "InterfaceDeclaration" ||
enclosingNode.type === "TSInterfaceDeclaration")
) {
if (classLikeNodeTypes.has(enclosingNode?.type)) {
if (
isNonEmptyArray(enclosingNode.decorators) &&
!(followingNode && followingNode.type === "Decorator")
@@ -457,6 +446,15 @@ function handleClassComments({
return false;
}
const propertyLikeNodeTypes = new Set([
"ClassMethod",
"ClassProperty",
"PropertyDefinition",
"TSAbstractPropertyDefinition",
"TSAbstractMethodDefinition",
"TSDeclareMethod",
"MethodDefinition",
]);
function handleMethodNameComments({
comment,
precedingNode,
@@ -487,16 +485,8 @@ function handleMethodNameComments({
// Print comments between decorators and class methods as a trailing comment
// on the decorator node instead of the method node
if (
precedingNode &&
enclosingNode &&
precedingNode.type === "Decorator" &&
(enclosingNode.type === "ClassMethod" ||
enclosingNode.type === "ClassProperty" ||
enclosingNode.type === "PropertyDefinition" ||
enclosingNode.type === "TSAbstractPropertyDefinition" ||
enclosingNode.type === "TSAbstractMethodDefinition" ||
enclosingNode.type === "TSDeclareMethod" ||
enclosingNode.type === "MethodDefinition")
precedingNode?.type === "Decorator" &&
propertyLikeNodeTypes.has(enclosingNode?.type)
) {
addTrailingComment(precedingNode, comment);
return true;
@@ -505,6 +495,13 @@ function handleMethodNameComments({
return false;
}
const functionLikeNodeTypes = new Set([
"FunctionDeclaration",
"FunctionExpression",
"ClassMethod",
"MethodDefinition",
"ObjectMethod",
]);
function handleFunctionNameComments({
comment,
precedingNode,
@@ -514,15 +511,7 @@ function handleFunctionNameComments({
if (getNextNonSpaceNonCommentCharacter(text, comment, locEnd) !== "(") {
return false;
}
if (
precedingNode &&
enclosingNode &&
(enclosingNode.type === "FunctionDeclaration" ||
enclosingNode.type === "FunctionExpression" ||
enclosingNode.type === "ClassMethod" ||
enclosingNode.type === "MethodDefinition" ||
enclosingNode.type === "ObjectMethod")
) {
if (precedingNode && functionLikeNodeTypes.has(enclosingNode?.type)) {
addTrailingComment(precedingNode, comment);
return true;
}
@@ -530,7 +519,7 @@ function handleFunctionNameComments({
}
function handleCommentAfterArrowParams({ comment, enclosingNode, text }) {
if (!(enclosingNode && enclosingNode.type === "ArrowFunctionExpression")) {
if (!(enclosingNode?.type === "ArrowFunctionExpression")) {
return false;
}
@@ -561,9 +550,8 @@ function handleCommentInEmptyParens({ comment, enclosingNode, text }) {
return true;
}
if (
enclosingNode &&
(enclosingNode.type === "MethodDefinition" ||
enclosingNode.type === "TSAbstractMethodDefinition") &&
(enclosingNode?.type === "MethodDefinition" ||
enclosingNode?.type === "TSAbstractMethodDefinition") &&
getFunctionParameters(enclosingNode.value).length === 0
) {
addDanglingComment(enclosingNode.value, comment);
@@ -581,12 +569,9 @@ function handleLastFunctionArgComments({
}) {
// Flow function type definitions
if (
precedingNode &&
precedingNode.type === "FunctionTypeParam" &&
enclosingNode &&
enclosingNode.type === "FunctionTypeAnnotation" &&
followingNode &&
followingNode.type !== "FunctionTypeParam"
precedingNode?.type === "FunctionTypeParam" &&
enclosingNode?.type === "FunctionTypeAnnotation" &&
followingNode?.type !== "FunctionTypeParam"
) {
addTrailingComment(precedingNode, comment);
return true;
@@ -594,9 +579,8 @@ function handleLastFunctionArgComments({
// Real functions and TypeScript function type definitions
if (
precedingNode &&
(precedingNode.type === "Identifier" ||
precedingNode.type === "AssignmentPattern") &&
(precedingNode?.type === "Identifier" ||
precedingNode?.type === "AssignmentPattern") &&
enclosingNode &&
isRealFunctionLikeNode(enclosingNode) &&
getNextNonSpaceNonCommentCharacter(text, comment, locEnd) === ")"
@@ -606,10 +590,8 @@ function handleLastFunctionArgComments({
}
if (
enclosingNode &&
enclosingNode.type === "FunctionDeclaration" &&
followingNode &&
followingNode.type === "BlockStatement"
enclosingNode?.type === "FunctionDeclaration" &&
followingNode?.type === "BlockStatement"
) {
const functionParamRightParenIndex = (() => {
const parameters = getFunctionParameters(enclosingNode);
@@ -642,7 +624,7 @@ function handleLastFunctionArgComments({
}
function handleImportSpecifierComments({ comment, enclosingNode }) {
if (enclosingNode && enclosingNode.type === "ImportSpecifier") {
if (enclosingNode?.type === "ImportSpecifier") {
addLeadingComment(enclosingNode, comment);
return true;
}
@@ -650,7 +632,7 @@ function handleImportSpecifierComments({ comment, enclosingNode }) {
}
function handleLabeledStatementComments({ comment, enclosingNode }) {
if (enclosingNode && enclosingNode.type === "LabeledStatement") {
if (enclosingNode?.type === "LabeledStatement") {
addLeadingComment(enclosingNode, comment);
return true;
}
@@ -659,9 +641,8 @@ function handleLabeledStatementComments({ comment, enclosingNode }) {
function handleBreakAndContinueStatementComments({ comment, enclosingNode }) {
if (
enclosingNode &&
(enclosingNode.type === "ContinueStatement" ||
enclosingNode.type === "BreakStatement") &&
(enclosingNode?.type === "ContinueStatement" ||
enclosingNode?.type === "BreakStatement") &&
!enclosingNode.label
) {
addTrailingComment(enclosingNode, comment);
@@ -694,9 +675,8 @@ function handleUnionTypeComments({
followingNode,
}) {
if (
enclosingNode &&
(enclosingNode.type === "UnionTypeAnnotation" ||
enclosingNode.type === "TSUnionType")
enclosingNode?.type === "UnionTypeAnnotation" ||
enclosingNode?.type === "TSUnionType"
) {
if (isPrettierIgnoreComment(comment)) {
followingNode.prettierIgnore = true;
@@ -710,9 +690,8 @@ function handleUnionTypeComments({
}
if (
followingNode &&
(followingNode.type === "UnionTypeAnnotation" ||
followingNode.type === "TSUnionType") &&
(followingNode?.type === "UnionTypeAnnotation" ||
followingNode?.type === "TSUnionType") &&
isPrettierIgnoreComment(comment)
) {
followingNode.types[0].prettierIgnore = true;
@@ -748,9 +727,8 @@ function handleOnlyComments({
}
if (
enclosingNode &&
enclosingNode.type === "Program" &&
enclosingNode.body.length === 0 &&
enclosingNode?.type === "Program" &&
enclosingNode?.body.length === 0 &&
!isNonEmptyArray(enclosingNode.directives)
) {
if (isLastComment) {
@@ -762,11 +740,9 @@ function handleOnlyComments({
}
if (
followingNode &&
followingNode.type === "Program" &&
followingNode.body.length === 0 &&
enclosingNode &&
enclosingNode.type === "ModuleExpression"
followingNode?.type === "Program" &&
followingNode?.body.length === 0 &&
enclosingNode?.type === "ModuleExpression"
) {
addDanglingComment(followingNode, comment);
return true;
@@ -777,9 +753,8 @@ function handleOnlyComments({
function handleForComments({ comment, enclosingNode }) {
if (
enclosingNode &&
(enclosingNode.type === "ForInStatement" ||
enclosingNode.type === "ForOfStatement")
enclosingNode?.type === "ForInStatement" ||
enclosingNode?.type === "ForOfStatement"
) {
addLeadingComment(enclosingNode, comment);
return true;
@@ -810,32 +785,36 @@ function handleModuleSpecifiersComments({
}
function handleAssignmentPatternComments({ comment, enclosingNode }) {
if (enclosingNode && enclosingNode.type === "AssignmentPattern") {
if (enclosingNode?.type === "AssignmentPattern") {
addLeadingComment(enclosingNode, comment);
return true;
}
return false;
}
const assignmentLikeNodeTypes = new Set([
"VariableDeclarator",
"AssignmentExpression",
"TypeAlias",
"TSTypeAliasDeclaration",
]);
const complexExprNodeTypes = new Set([
"ObjectExpression",
"ArrayExpression",
"TemplateLiteral",
"TaggedTemplateExpression",
"ObjectTypeAnnotation",
"TSTypeLiteral",
]);
function handleVariableDeclaratorComments({
comment,
enclosingNode,
followingNode,
}) {
if (
enclosingNode &&
(enclosingNode.type === "VariableDeclarator" ||
enclosingNode.type === "AssignmentExpression" ||
enclosingNode.type === "TypeAlias" ||
enclosingNode.type === "TSTypeAliasDeclaration") &&
assignmentLikeNodeTypes.has(enclosingNode?.type) &&
followingNode &&
(followingNode.type === "ObjectExpression" ||
followingNode.type === "ArrayExpression" ||
followingNode.type === "TemplateLiteral" ||
followingNode.type === "TaggedTemplateExpression" ||
followingNode.type === "ObjectTypeAnnotation" ||
followingNode.type === "TSTypeLiteral" ||
isBlockComment(comment))
(complexExprNodeTypes.has(followingNode.type) || isBlockComment(comment))
) {
addLeadingComment(followingNode, comment);
return true;
@@ -851,10 +830,9 @@ function handleTSFunctionTrailingComments({
}) {
if (
!followingNode &&
enclosingNode &&
(enclosingNode.type === "TSMethodSignature" ||
enclosingNode.type === "TSDeclareFunction" ||
enclosingNode.type === "TSAbstractMethodDefinition") &&
(enclosingNode?.type === "TSMethodSignature" ||
enclosingNode?.type === "TSDeclareFunction" ||
enclosingNode?.type === "TSAbstractMethodDefinition") &&
getNextNonSpaceNonCommentCharacter(text, comment, locEnd) === ";"
) {
addTrailingComment(enclosingNode, comment);
@@ -866,10 +844,8 @@ function handleTSFunctionTrailingComments({
function handleIgnoreComments({ comment, enclosingNode, followingNode }) {
if (
isPrettierIgnoreComment(comment) &&
enclosingNode &&
enclosingNode.type === "TSMappedType" &&
followingNode &&
followingNode.type === "TSTypeParameter" &&
enclosingNode?.type === "TSMappedType" &&
followingNode?.type === "TSTypeParameter" &&
followingNode.constraint
) {
enclosingNode.prettierIgnore = true;
@@ -884,24 +860,16 @@ function handleTSMappedTypeComments({
enclosingNode,
followingNode,
}) {
if (!enclosingNode || enclosingNode.type !== "TSMappedType") {
if (enclosingNode?.type !== "TSMappedType") {
return false;
}
if (
followingNode &&
followingNode.type === "TSTypeParameter" &&
followingNode.name
) {
if (followingNode?.type === "TSTypeParameter" && followingNode.name) {
addLeadingComment(followingNode.name, comment);
return true;
}
if (
precedingNode &&
precedingNode.type === "TSTypeParameter" &&
precedingNode.constraint
) {
if (precedingNode?.type === "TSTypeParameter" && precedingNode.constraint) {
addTrailingComment(precedingNode.constraint, comment);
return true;
}