diff --git a/src/language-graphql/printer-graphql.js b/src/language-graphql/printer-graphql.js index 4e34eab8ff10..b4fe1f947560 100644 --- a/src/language-graphql/printer-graphql.js +++ b/src/language-graphql/printer-graphql.js @@ -7,8 +7,7 @@ const { isNextLineEmpty, isNonEmptyArray } = require("../common/util"); const { insertPragma } = require("./pragma"); const { locStart, locEnd } = require("./loc"); -function genericPrint(path, options, print) { - const node = path.getValue(); +function genericPrint(node, options, print) { if (!node) { return ""; } @@ -20,17 +19,17 @@ function genericPrint(path, options, print) { switch (node.kind) { case "Document": { const parts = []; - path.each((pathChild, index, definitions) => { - parts.push(print()); + const { definitions } = node; + for (const [index, definition] of definitions.entries()) { + parts.push(print(definition)); if (index !== definitions.length - 1) { parts.push(hardline); - if ( - isNextLineEmpty(options.originalText, pathChild.getValue(), locEnd) - ) { + if (isNextLineEmpty(options.originalText, definition, locEnd)) { parts.push(hardline); } } - }, "definitions"); + } + return [...parts, hardline]; } case "OperationDefinition": { @@ -38,7 +37,7 @@ function genericPrint(path, options, print) { const hasName = Boolean(node.name); return [ hasOperation ? node.operation : "", - hasOperation && hasName ? [" ", print("name")] : "", + hasOperation && hasName ? [" ", print(node.name)] : "", isNonEmptyArray(node.variableDefinitions) ? group([ "(", @@ -46,22 +45,24 @@ function genericPrint(path, options, print) { softline, join( [ifBreak("", ", "), softline], - path.map(print, "variableDefinitions") + node.variableDefinitions.map((variableDefinition) => + print(node[variableDefinition]) + ) ), ]), softline, ")", ]) : "", - printDirectives(path, print, node), + printDirectives(node, print), node.selectionSet ? (!hasOperation && !hasName ? "" : " ") : "", - print("selectionSet"), + print(node.selectionSet), ]; } case "FragmentDefinition": { return [ "fragment ", - print("name"), + print(node.name), isNonEmptyArray(node.variableDefinitions) ? group([ "(", @@ -69,7 +70,9 @@ function genericPrint(path, options, print) { softline, join( [ifBreak("", ", "), softline], - path.map(print, "variableDefinitions") + node.variableDefinitions.map((variableDefinition) => + print(node[variableDefinition]) + ) ), ]), softline, @@ -77,10 +80,10 @@ function genericPrint(path, options, print) { ]) : "", " on ", - print("typeCondition"), - printDirectives(path, print, node), + print(node.typeCondition), + printDirectives(node, print), " ", - print("selectionSet"), + print(node.selectionSet), ]; } case "SelectionSet": { @@ -88,13 +91,7 @@ function genericPrint(path, options, print) { "{", indent([ hardline, - join( - hardline, - path.call( - (selectionsPath) => printSequence(selectionsPath, options, print), - "selections" - ) - ), + join(hardline, printSequence(node.selections, options, print)), ]), hardline, "}", @@ -102,8 +99,8 @@ function genericPrint(path, options, print) { } case "Field": { return group([ - node.alias ? [print("alias"), ": "] : "", - print("name"), + node.alias ? [print(node.alias), ": "] : "", + print(node.name), node.arguments.length > 0 ? group([ "(", @@ -111,19 +108,17 @@ function genericPrint(path, options, print) { softline, join( [ifBreak("", ", "), softline], - path.call( - (argsPath) => printSequence(argsPath, options, print), - "arguments" - ) + + printSequence(node.arguments, options, print) ), ]), softline, ")", ]) : "", - printDirectives(path, print, node), + printDirectives(node, print), node.selectionSet ? " " : "", - print("selectionSet"), + print(node.selectionSet), ]); } case "Name": { @@ -157,14 +152,17 @@ function genericPrint(path, options, print) { return "null"; } case "Variable": { - return ["$", print("name")]; + return ["$", print(node.name)]; } case "ListValue": { return group([ "[", indent([ softline, - join([ifBreak("", ", "), softline], path.map(print, "values")), + join( + [ifBreak("", ", "), softline], + node.values.map((value) => print(node[value])) + ), ]), softline, "]", @@ -176,7 +174,10 @@ function genericPrint(path, options, print) { options.bracketSpacing && node.fields.length > 0 ? " " : "", indent([ softline, - join([ifBreak("", ", "), softline], path.map(print, "fields")), + join( + [ifBreak("", ", "), softline], + node.fields.map((field) => print(node[field])) + ), ]), softline, ifBreak( @@ -188,13 +189,13 @@ function genericPrint(path, options, print) { } case "ObjectField": case "Argument": { - return [print("name"), ": ", print("value")]; + return [print(node.name), ": ", print(node.value)]; } case "Directive": { return [ "@", - print("name"), + print(node.name), node.arguments.length > 0 ? group([ "(", @@ -202,10 +203,7 @@ function genericPrint(path, options, print) { softline, join( [ifBreak("", ", "), softline], - path.call( - (argsPath) => printSequence(argsPath, options, print), - "arguments" - ) + printSequence(node.arguments, options, print) ), ]), softline, @@ -216,31 +214,31 @@ function genericPrint(path, options, print) { } case "NamedType": { - return print("name"); + return print(node.name); } case "VariableDefinition": { return [ - print("variable"), + print(node.variable), ": ", - print("type"), - node.defaultValue ? [" = ", print("defaultValue")] : "", - printDirectives(path, print, node), + print(node.type), + node.defaultValue ? [" = ", print(node.defaultValue)] : "", + printDirectives(node, print), ]; } case "ObjectTypeExtension": case "ObjectTypeDefinition": { return [ - print("description"), + print(node.description), node.description ? hardline : "", node.kind === "ObjectTypeExtension" ? "extend " : "", "type ", - print("name"), + print(node.name), node.interfaces.length > 0 - ? [" implements ", ...printInterfaces(path, options, print)] + ? [" implements ", ...printInterfaces(node, options, print)] : "", - printDirectives(path, print, node), + printDirectives(node, print), node.fields.length > 0 ? [ " {", @@ -248,10 +246,8 @@ function genericPrint(path, options, print) { hardline, join( hardline, - path.call( - (fieldsPath) => printSequence(fieldsPath, options, print), - "fields" - ) + + printSequence(node.fields, options, print) ), ]), hardline, @@ -263,9 +259,9 @@ function genericPrint(path, options, print) { case "FieldDefinition": { return [ - print("description"), + print(node.description), node.description ? hardline : "", - print("name"), + print(node.name), node.arguments.length > 0 ? group([ "(", @@ -273,10 +269,8 @@ function genericPrint(path, options, print) { softline, join( [ifBreak("", ", "), softline], - path.call( - (argsPath) => printSequence(argsPath, options, print), - "arguments" - ) + + printSequence(node.arguments, options, print) ), ]), softline, @@ -284,18 +278,18 @@ function genericPrint(path, options, print) { ]) : "", ": ", - print("type"), - printDirectives(path, print, node), + print(node.type), + printDirectives(node, print), ]; } case "DirectiveDefinition": { return [ - print("description"), + print(node.description), node.description ? hardline : "", "directive ", "@", - print("name"), + print(node.name), node.arguments.length > 0 ? group([ "(", @@ -303,10 +297,8 @@ function genericPrint(path, options, print) { softline, join( [ifBreak("", ", "), softline], - path.call( - (argsPath) => printSequence(argsPath, options, print), - "arguments" - ) + + printSequence(node.arguments, options, print) ), ]), softline, @@ -315,32 +307,29 @@ function genericPrint(path, options, print) { : "", node.repeatable ? " repeatable" : "", " on ", - join(" | ", path.map(print, "locations")), + join( + " | ", + node.locations.map((location) => print(node[location])) + ), ]; } case "EnumTypeExtension": case "EnumTypeDefinition": { return [ - print("description"), + print(node.description), node.description ? hardline : "", node.kind === "EnumTypeExtension" ? "extend " : "", "enum ", - print("name"), - printDirectives(path, print, node), + print(node.name), + printDirectives(node, print), node.values.length > 0 ? [ " {", indent([ hardline, - join( - hardline, - path.call( - (valuesPath) => printSequence(valuesPath, options, print), - "values" - ) - ), + join(hardline, printSequence(node.values, options, print)), ]), hardline, "}", @@ -351,34 +340,34 @@ function genericPrint(path, options, print) { case "EnumValueDefinition": { return [ - print("description"), + print(node.description), node.description ? hardline : "", - print("name"), - printDirectives(path, print, node), + print(node.name), + printDirectives(node, print), ]; } case "InputValueDefinition": { return [ - print("description"), + print(node.description), node.description ? (node.description.block ? hardline : line) : "", - print("name"), + print(node.name), ": ", - print("type"), - node.defaultValue ? [" = ", print("defaultValue")] : "", - printDirectives(path, print, node), + print(node.type), + node.defaultValue ? [" = ", print(node.defaultValue)] : "", + printDirectives(node, print), ]; } case "InputObjectTypeExtension": case "InputObjectTypeDefinition": { return [ - print("description"), + print(node.description), node.description ? hardline : "", node.kind === "InputObjectTypeExtension" ? "extend " : "", "input ", - print("name"), - printDirectives(path, print, node), + print(node.name), + printDirectives(node, print), node.fields.length > 0 ? [ " {", @@ -386,10 +375,8 @@ function genericPrint(path, options, print) { hardline, join( hardline, - path.call( - (fieldsPath) => printSequence(fieldsPath, options, print), - "fields" - ) + + printSequence(node.fields, options, print) ), ]), hardline, @@ -402,17 +389,15 @@ function genericPrint(path, options, print) { case "SchemaDefinition": { return [ "schema", - printDirectives(path, print, node), + printDirectives(node, print), " {", node.operationTypes.length > 0 ? indent([ hardline, join( hardline, - path.call( - (opsPath) => printSequence(opsPath, options, print), - "operationTypes" - ) + + printSequence(node.operationTypes, options, print) ), ]) : "", @@ -422,21 +407,21 @@ function genericPrint(path, options, print) { } case "OperationTypeDefinition": { - return [print("operation"), ": ", print("type")]; + return [print(node.operation), ": ", print(node.type)]; } case "InterfaceTypeExtension": case "InterfaceTypeDefinition": { return [ - print("description"), + print(node.description), node.description ? hardline : "", node.kind === "InterfaceTypeExtension" ? "extend " : "", "interface ", - print("name"), + print(node.name), node.interfaces.length > 0 - ? [" implements ", ...printInterfaces(path, options, print)] + ? [" implements ", ...printInterfaces(node, options, print)] : "", - printDirectives(path, print, node), + printDirectives(node, print), node.fields.length > 0 ? [ " {", @@ -444,10 +429,8 @@ function genericPrint(path, options, print) { hardline, join( hardline, - path.call( - (fieldsPath) => printSequence(fieldsPath, options, print), - "fields" - ) + + printSequence(node.fields, options, print) ), ]), hardline, @@ -458,36 +441,39 @@ function genericPrint(path, options, print) { } case "FragmentSpread": { - return ["...", print("name"), printDirectives(path, print, node)]; + return ["...", print(node.name), printDirectives(node, print)]; } case "InlineFragment": { return [ "...", - node.typeCondition ? [" on ", print("typeCondition")] : "", - printDirectives(path, print, node), + node.typeCondition ? [" on ", print(node.typeCondition)] : "", + printDirectives(node, print), " ", - print("selectionSet"), + print(node.selectionSet), ]; } case "UnionTypeExtension": case "UnionTypeDefinition": { return group([ - print("description"), + print(node.description), node.description ? hardline : "", group([ node.kind === "UnionTypeExtension" ? "extend " : "", "union ", - print("name"), - printDirectives(path, print, node), + print(node.name), + printDirectives(node, print), node.types.length > 0 ? [ " =", ifBreak("", " "), indent([ ifBreak([line, " "]), - join([line, "| "], path.map(print, "types")), + join( + [line, "| "], + node.types.map((type) => print(node[type])) + ), ]), ] : "", @@ -498,21 +484,21 @@ function genericPrint(path, options, print) { case "ScalarTypeExtension": case "ScalarTypeDefinition": { return [ - print("description"), + print(node.description), node.description ? hardline : "", node.kind === "ScalarTypeExtension" ? "extend " : "", "scalar ", - print("name"), - printDirectives(path, print, node), + print(node.name), + printDirectives(node, print), ]; } case "NonNullType": { - return [print("type"), "!"]; + return [print(node.type), "!"]; } case "ListType": { - return ["[", print("type"), "]"]; + return ["[", print(node.type), "]"]; } default: @@ -521,12 +507,15 @@ function genericPrint(path, options, print) { } } -function printDirectives(path, print, node) { +function printDirectives(node, print) { if (node.directives.length === 0) { return ""; } - const printed = join(line, path.map(print, "directives")); + const printed = join( + line, + node.directives.map((directive) => print(node[directive])) + ); if ( node.kind === "FragmentDefinition" || @@ -538,16 +527,13 @@ function printDirectives(path, print, node) { return [" ", group(indent([softline, printed]))]; } -function printSequence(sequencePath, options, print) { - const count = sequencePath.getValue().length; +function printSequence(sequence, options, print) { + const count = sequence.length; - return sequencePath.map((path, i) => { - const printed = print(); + return sequence.map((node, i) => { + const printed = print(node); - if ( - isNextLineEmpty(options.originalText, path.getValue(), locEnd) && - i < count - 1 - ) { + if (isNextLineEmpty(options.originalText, node, locEnd) && i < count - 1) { return [printed, hardline]; } @@ -559,8 +545,7 @@ function canAttachComment(node) { return node.kind && node.kind !== "Comment"; } -function printComment(commentPath) { - const comment = commentPath.getValue(); +function printComment(comment) { if (comment.kind === "Comment") { return "#" + comment.value.trimEnd(); } @@ -569,11 +554,10 @@ function printComment(commentPath) { throw new Error("Not a comment: " + JSON.stringify(comment)); } -function printInterfaces(path, options, print) { - const node = path.getNode(); +function printInterfaces(node, options, print) { const parts = []; const { interfaces } = node; - const printed = path.map((node) => print(node), "interfaces"); + const printed = interfaces.map((node) => print(node[node])); for (let index = 0; index < interfaces.length; index++) { const interfaceNode = interfaces[index]; @@ -597,8 +581,7 @@ function printInterfaces(path, options, print) { function clean(/*node, newNode , parent*/) {} clean.ignoredProperties = new Set(["loc", "comments"]); -function hasPrettierIgnore(path) { - const node = path.getValue(); +function hasPrettierIgnore(node) { return ( node && Array.isArray(node.comments) && diff --git a/src/main/ast-to-doc.js b/src/main/ast-to-doc.js index eb41c581fc9f..fe8ad458a1dc 100644 --- a/src/main/ast-to-doc.js +++ b/src/main/ast-to-doc.js @@ -1,6 +1,5 @@ "use strict"; -const AstPath = require("../common/ast-path"); const { builders: { hardline, addAlignmentToDoc }, utils: { propagateBreaks }, @@ -37,9 +36,8 @@ function printAstToDoc(ast, options, alignmentSize = 0) { } const cache = new Map(); - const path = new AstPath(ast); - let doc = mainPrint(); + let doc = mainPrint(ast); if (alignmentSize > 0) { // Add a hardline to make the indents take effect @@ -51,21 +49,7 @@ function printAstToDoc(ast, options, alignmentSize = 0) { return doc; - function mainPrint(selector, args) { - if (selector === undefined || selector === path) { - return mainPrintInternal(args); - } - - if (Array.isArray(selector)) { - return path.call(() => mainPrintInternal(args), ...selector); - } - - return path.call(() => mainPrintInternal(args), selector); - } - - function mainPrintInternal(args) { - const value = path.getValue(); - + function mainPrint(value, args) { const shouldCache = value && typeof value === "object" && args === undefined; @@ -73,7 +57,7 @@ function printAstToDoc(ast, options, alignmentSize = 0) { return cache.get(value); } - const doc = callPluginPrintFunction(path, options, mainPrint, args); + const doc = callPluginPrintFunction(value, options, mainPrint, args); if (shouldCache) { cache.set(value, doc); @@ -103,20 +87,19 @@ function printPrettierIgnoredNode(node, options) { return originalText.slice(start, end); } -function callPluginPrintFunction(path, options, printPath, args) { - const node = path.getValue(); +function callPluginPrintFunction(node, options, printPath, args) { const { printer } = options; let doc; // Escape hatch - if (printer.hasPrettierIgnore && printer.hasPrettierIgnore(path)) { + if (printer.hasPrettierIgnore && printer.hasPrettierIgnore(node)) { doc = printPrettierIgnoredNode(node, options); } else { if (node) { try { // Potentially switch to a different parser - doc = multiparser.printSubtree(path, printPath, options, printAstToDoc); + doc = multiparser.printSubtree(node, printPath, options, printAstToDoc); } catch (error) { /* istanbul ignore if */ if (process.env.PRETTIER_DEBUG) { @@ -127,7 +110,7 @@ function callPluginPrintFunction(path, options, printPath, args) { } if (!doc) { - doc = printer.print(path, options, printPath, args); + doc = printer.print(node, options, printPath, args); } } @@ -135,11 +118,11 @@ function callPluginPrintFunction(path, options, printPath, args) { // UnionTypeAnnotation has to align the child without the comments if ( !printer.willPrintOwnComments || - !printer.willPrintOwnComments(path, options) + !printer.willPrintOwnComments(node, options) ) { // printComments will call the plugin print function and check for // comments to print - doc = printComments(path, doc, options); + doc = printComments(node, doc, options); } return doc; diff --git a/src/main/comments.js b/src/main/comments.js index 17e652396700..1a6f61b104bb 100644 --- a/src/main/comments.js +++ b/src/main/comments.js @@ -420,10 +420,9 @@ function breakTies(tiesToBreak, text, options) { tiesToBreak.length = 0; } -function printComment(commentPath, options) { - const comment = commentPath.getValue(); +function printComment(comment, options) { comment.printed = true; - return options.printer.printComment(commentPath, options); + return options.printer.printComment(comment, options); } function findExpressionIndexForComment(quasis, comment, options) { @@ -441,9 +440,8 @@ function findExpressionIndexForComment(quasis, comment, options) { return 0; } -function printLeadingComment(commentPath, options) { - const comment = commentPath.getValue(); - const contents = printComment(commentPath, options); +function printLeadingComment(comment, options) { + const contents = printComment(comment, options); /* istanbul ignore next */ if (!contents) { return ""; @@ -468,9 +466,8 @@ function printLeadingComment(commentPath, options) { return [contents, hardline]; } -function printTrailingComment(commentPath, options) { - const comment = commentPath.getValue(); - const contents = printComment(commentPath, options); +function printTrailingComment(comment, options) { + const contents = printComment(comment, options); /* istanbul ignore next */ if (!contents) { return ""; @@ -510,25 +507,22 @@ function printTrailingComment(commentPath, options) { return printed; } -function printDanglingComments(path, options, sameIndent, filter) { +function printDanglingComments(node, options, sameIndent, filter) { const parts = []; - const node = path.getValue(); - if (!node || !node.comments) { return ""; } - path.each((commentPath) => { - const comment = commentPath.getValue(); + for (const comment of node.comment) { if ( comment && !comment.leading && !comment.trailing && (!filter || filter(comment)) ) { - parts.push(printComment(commentPath, options)); + parts.push(printComment(comment, options)); } - }, "comments"); + } if (parts.length === 0) { return ""; @@ -540,8 +534,7 @@ function printDanglingComments(path, options, sameIndent, filter) { return indent([hardline, join(hardline, parts)]); } -function printCommentsSeparately(path, options) { - const value = path.getValue(); +function printCommentsSeparately(value, options) { const hasComments = isNonEmptyArray(value && value.comments); const isCursorNode = Boolean(value && value === options.cursorNode); @@ -553,12 +546,11 @@ function printCommentsSeparately(path, options) { const leadingParts = []; const trailingParts = []; - path.each((commentPath) => { - const comment = commentPath.getValue(); + for (const comment of value.comments) { const { leading, trailing } = comment; if (leading) { - const contents = printLeadingComment(commentPath, options); + const contents = printLeadingComment(comment, options); /* istanbul ignore next */ if (!contents) { return; @@ -574,9 +566,9 @@ function printCommentsSeparately(path, options) { leadingParts.push(hardline); } } else if (trailing) { - trailingParts.push(printTrailingComment(commentPath, options)); + trailingParts.push(printTrailingComment(comment, options)); } - }, "comments"); + } if (isCursorNode) { leadingParts.unshift(cursor); @@ -586,8 +578,8 @@ function printCommentsSeparately(path, options) { return { leading: leadingParts, trailing: trailingParts }; } -function printComments(path, doc, options) { - const { leading, trailing } = printCommentsSeparately(path, options); +function printComments(node, doc, options) { + const { leading, trailing } = printCommentsSeparately(node, options); if (!leading && !trailing) { return doc; } diff --git a/src/main/multiparser.js b/src/main/multiparser.js index 017b73447935..6d6100ae9810 100644 --- a/src/main/multiparser.js +++ b/src/main/multiparser.js @@ -6,10 +6,10 @@ const { const { normalize } = require("./options"); const comments = require("./comments"); -function printSubtree(path, print, options, printAstToDoc) { +function printSubtree(node, print, options, printAstToDoc) { if (options.printer.embed && options.embeddedLanguageFormatting === "auto") { return options.printer.embed( - path, + node, print, (text, partialNextOptions, textToDocOptions) => textToDoc(