Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract printInterface and printCallExpression (#9715)
- Loading branch information
Showing
4 changed files
with
207 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
"use strict"; | ||
|
||
const { | ||
builders: { concat, join, group }, | ||
} = require("../../document"); | ||
const pathNeedsParens = require("../needs-parens"); | ||
const { | ||
getCallArguments, | ||
hasFlowAnnotationComment, | ||
isCallOrOptionalCallExpression, | ||
isMemberish, | ||
isTemplateOnItsOwnLine, | ||
isTestCall, | ||
iterateCallArgumentsPath, | ||
} = require("../utils"); | ||
const printMemberChain = require("./member-chain"); | ||
const printCallArguments = require("./call-arguments"); | ||
const { printOptionalToken, printFunctionTypeParameters } = require("./misc"); | ||
|
||
function printCallExpression(path, options, print) { | ||
const n = path.getValue(); | ||
const isNew = n.type === "NewExpression"; | ||
const isDynamicImport = n.type === "ImportExpression"; | ||
|
||
const optional = printOptionalToken(path); | ||
const args = getCallArguments(n); | ||
if ( | ||
// Dangling comments not handled, all these special cases should has argument #9668 | ||
args.length > 0 && | ||
// We want to keep CommonJS- and AMD-style require calls, and AMD-style | ||
// define calls, as a unit. | ||
// e.g. `define(["some/lib", (lib) => {` | ||
((!isDynamicImport && | ||
!isNew && | ||
n.callee.type === "Identifier" && | ||
(n.callee.name === "require" || n.callee.name === "define")) || | ||
// Template literals as single arguments | ||
(args.length === 1 && | ||
isTemplateOnItsOwnLine(args[0], options.originalText)) || | ||
// Keep test declarations on a single line | ||
// e.g. `it('long name', () => {` | ||
(!isNew && isTestCall(n, path.getParentNode()))) | ||
) { | ||
const printed = []; | ||
iterateCallArgumentsPath(path, (argPath) => { | ||
printed.push(print(argPath)); | ||
}); | ||
return concat([ | ||
isNew ? "new " : "", | ||
path.call(print, "callee"), | ||
optional, | ||
printFunctionTypeParameters(path, options, print), | ||
concat(["(", join(", ", printed), ")"]), | ||
]); | ||
} | ||
|
||
// Inline Flow annotation comments following Identifiers in Call nodes need to | ||
// stay with the Identifier. For example: | ||
// | ||
// foo /*:: <SomeGeneric> */(bar); | ||
// | ||
// Here, we ensure that such comments stay between the Identifier and the Callee. | ||
const isIdentifierWithFlowAnnotation = | ||
(options.parser === "babel" || options.parser === "babel-flow") && | ||
n.callee && | ||
n.callee.type === "Identifier" && | ||
hasFlowAnnotationComment(n.callee.trailingComments); | ||
if (isIdentifierWithFlowAnnotation) { | ||
n.callee.trailingComments[0].printed = true; | ||
} | ||
|
||
// We detect calls on member lookups and possibly print them in a | ||
// special chain format. See `printMemberChain` for more info. | ||
if ( | ||
!isDynamicImport && | ||
!isNew && | ||
isMemberish(n.callee) && | ||
!path.call((path) => pathNeedsParens(path, options), "callee") | ||
) { | ||
return printMemberChain(path, options, print); | ||
} | ||
|
||
const contents = concat([ | ||
isNew ? "new " : "", | ||
isDynamicImport ? "import" : path.call(print, "callee"), | ||
optional, | ||
isIdentifierWithFlowAnnotation | ||
? `/*:: ${n.callee.trailingComments[0].value.slice(2).trim()} */` | ||
: "", | ||
printFunctionTypeParameters(path, options, print), | ||
printCallArguments(path, options, print), | ||
]); | ||
|
||
// We group here when the callee is itself a call expression. | ||
// See `isLongCurriedCallExpression` for more info. | ||
if (isDynamicImport || isCallOrOptionalCallExpression(n.callee)) { | ||
return group(contents); | ||
} | ||
|
||
return contents; | ||
} | ||
|
||
module.exports = { printCallExpression }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
"use strict"; | ||
|
||
const { | ||
builders: { concat, join, line, group, indent, ifBreak }, | ||
} = require("../../document"); | ||
const { | ||
hasTrailingComment, | ||
hasTrailingLineComment, | ||
identity, | ||
} = require("../utils"); | ||
const { getTypeParametersGroupId } = require("./type-parameters"); | ||
const { printTypeScriptModifiers } = require("./misc"); | ||
|
||
function printInterface(path, options, print) { | ||
const n = path.getValue(); | ||
const parts = []; | ||
if (n.type === "DeclareInterface" || n.declare) { | ||
parts.push("declare "); | ||
} | ||
|
||
if (n.type === "TSInterfaceDeclaration") { | ||
parts.push( | ||
n.abstract ? "abstract " : "", | ||
printTypeScriptModifiers(path, options, print) | ||
); | ||
} | ||
|
||
parts.push("interface"); | ||
|
||
const partsGroup = []; | ||
const extendsParts = []; | ||
|
||
if (n.type !== "InterfaceTypeAnnotation") { | ||
partsGroup.push( | ||
" ", | ||
path.call(print, "id"), | ||
path.call(print, "typeParameters") | ||
); | ||
} | ||
|
||
const shouldIndentOnlyHeritageClauses = | ||
n.typeParameters && !hasTrailingLineComment(n.typeParameters); | ||
|
||
if (n.extends && n.extends.length !== 0) { | ||
extendsParts.push( | ||
shouldIndentOnlyHeritageClauses | ||
? ifBreak(" ", line, { | ||
groupId: getTypeParametersGroupId(n.typeParameters), | ||
}) | ||
: line, | ||
"extends ", | ||
(n.extends.length === 1 ? identity : indent)( | ||
join(concat([",", line]), path.map(print, "extends")) | ||
) | ||
); | ||
} | ||
|
||
if ( | ||
(n.id && hasTrailingComment(n.id)) || | ||
(n.extends && n.extends.length !== 0) | ||
) { | ||
const printedExtends = concat(extendsParts); | ||
if (shouldIndentOnlyHeritageClauses) { | ||
parts.push( | ||
group( | ||
concat( | ||
partsGroup.concat(ifBreak(indent(printedExtends), printedExtends)) | ||
) | ||
) | ||
); | ||
} else { | ||
parts.push(group(indent(concat(partsGroup.concat(printedExtends))))); | ||
} | ||
} else { | ||
parts.push(...partsGroup, ...extendsParts); | ||
} | ||
|
||
parts.push(" ", path.call(print, "body")); | ||
|
||
return group(concat(parts)); | ||
} | ||
|
||
module.exports = { printInterface }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.