Skip to content

Commit

Permalink
Move print pre-check into core (#13400)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Aug 30, 2022
1 parent f5610b8 commit f47a852
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 171 deletions.
6 changes: 0 additions & 6 deletions src/language-css/printer-postcss.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
} from "../document/builders.js";
import { removeLines, getDocParts } from "../document/utils.js";
import createGetVisitorKeys from "../utils/create-get-visitor-keys.js";
import createPrintPreCheckFunction from "../utils/create-print-pre-check-function.js";
import UnexpectedNodeError from "../utils/unexpected-node-error.js";
import clean from "./clean.js";
import embed from "./embed.js";
Expand Down Expand Up @@ -79,7 +78,6 @@ import { locStart, locEnd } from "./loc.js";
import printUnit from "./utils/print-unit.js";

const getVisitorKeys = createGetVisitorKeys(visitorKeys);
const ensurePrintingNode = createPrintPreCheckFunction(getVisitorKeys);

function shouldPrintComma(options) {
return options.trailingComma === "es5" || options.trailingComma === "all";
Expand All @@ -88,10 +86,6 @@ function shouldPrintComma(options) {
function genericPrint(path, options, print) {
const node = path.getValue();

if (process.env.NODE_ENV !== "production") {
ensurePrintingNode(path);
}

switch (node.type) {
case "front-matter":
return [node.raw, hardline];
Expand Down
131 changes: 47 additions & 84 deletions src/language-graphql/printer-graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,16 @@ import {
} from "../document/builders.js";
import { isNextLineEmpty, isNonEmptyArray } from "../common/util.js";
import createGetVisitorKeys from "../utils/create-get-visitor-keys.js";
import createPrintPreCheckFunction from "../utils/create-print-pre-check-function.js";
import UnexpectedNodeError from "../utils/unexpected-node-error.js";
import { insertPragma } from "./pragma.js";
import { locStart, locEnd } from "./loc.js";
import visitorKeys from "./visitor-keys.evaluate.js";

const getVisitorKeys = createGetVisitorKeys(visitorKeys, "kind");
const ensurePrintingNode = createPrintPreCheckFunction(getVisitorKeys);

function genericPrint(path, options, print) {
const node = path.getValue();

if (process.env.NODE_ENV !== "production") {
ensurePrintingNode(path);
}

switch (node.kind) {
case "Document": {
const parts = [];
Expand Down Expand Up @@ -232,29 +226,50 @@ function genericPrint(path, options, print) {
}

case "ObjectTypeExtension":
case "ObjectTypeDefinition": {
return [
print("description"),
node.description ? hardline : "",
node.kind === "ObjectTypeExtension" ? "extend " : "",
"type ",
print("name"),
node.interfaces.length > 0
? [" implements ", ...printInterfaces(path, options, print)]
: "",
printDirectives(path, print, node),
node.fields.length > 0
? [
" {",
indent([
hardline,
join(hardline, printSequence(path, options, print, "fields")),
]),
hardline,
"}",
]
: "",
];
case "ObjectTypeDefinition":
case "InputObjectTypeExtension":
case "InputObjectTypeDefinition":
case "InterfaceTypeExtension":
case "InterfaceTypeDefinition": {
const { kind } = node;
const parts = [];

if (kind.endsWith("TypeDefinition")) {
if (node.description) {
parts.push(print("description"), hardline);
}
} else {
parts.push("extend ");
}

if (kind.startsWith("ObjectType")) {
parts.push("type");
} else if (kind.startsWith("InputObjectType")) {
parts.push("input");
} else {
parts.push("interface");
}
parts.push(" ", print("name"));

if (!kind.startsWith("InputObjectType") && node.interfaces.length > 0) {
parts.push(" implements ", ...printInterfaces(path, options, print));
}

parts.push(printDirectives(path, print, node));

if (node.fields.length > 0) {
parts.push([
" {",
indent([
hardline,
join(hardline, printSequence(path, options, print, "fields")),
]),
hardline,
"}",
]);
}

return parts;
}

case "FieldDefinition": {
Expand Down Expand Up @@ -312,8 +327,7 @@ function genericPrint(path, options, print) {
case "EnumTypeExtension":
case "EnumTypeDefinition": {
return [
print("description"),
node.description ? hardline : "",
...(node.description ? [print("description"), hardline] : []),
node.kind === "EnumTypeExtension" ? "extend " : "",
"enum ",
print("name"),
Expand Down Expand Up @@ -353,29 +367,6 @@ function genericPrint(path, options, print) {
];
}

case "InputObjectTypeExtension":
case "InputObjectTypeDefinition": {
return [
print("description"),
node.description ? hardline : "",
node.kind === "InputObjectTypeExtension" ? "extend " : "",
"input ",
print("name"),
printDirectives(path, print, node),
node.fields.length > 0
? [
" {",
indent([
hardline,
join(hardline, printSequence(path, options, print, "fields")),
]),
hardline,
"}",
]
: "",
];
}

case "SchemaExtension": {
return [
"extend schema",
Expand Down Expand Up @@ -421,32 +412,6 @@ function genericPrint(path, options, print) {
return [node.operation, ": ", print("type")];
}

case "InterfaceTypeExtension":
case "InterfaceTypeDefinition": {
return [
print("description"),
node.description ? hardline : "",
node.kind === "InterfaceTypeExtension" ? "extend " : "",
"interface ",
print("name"),
node.interfaces.length > 0
? [" implements ", ...printInterfaces(path, options, print)]
: "",
printDirectives(path, print, node),
node.fields.length > 0
? [
" {",
indent([
hardline,
join(hardline, printSequence(path, options, print, "fields")),
]),
hardline,
"}",
]
: "",
];
}

case "FragmentSpread": {
return ["...", print("name"), printDirectives(path, print, node)];
}
Expand All @@ -464,8 +429,7 @@ function genericPrint(path, options, print) {
case "UnionTypeExtension":
case "UnionTypeDefinition": {
return group([
print("description"),
node.description ? hardline : "",
...(node.description ? [print("description"), hardline] : []),
group([
node.kind === "UnionTypeExtension" ? "extend " : "",
"union ",
Expand All @@ -488,8 +452,7 @@ function genericPrint(path, options, print) {
case "ScalarTypeExtension":
case "ScalarTypeDefinition": {
return [
print("description"),
node.description ? hardline : "",
...(node.description ? [print("description"), hardline] : []),
node.kind === "ScalarTypeExtension" ? "extend " : "",
"scalar ",
print("name"),
Expand Down
6 changes: 0 additions & 6 deletions src/language-handlebars/printer-glimmer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
import { replaceEndOfLine } from "../document/utils.js";
import { getPreferredQuote, isNonEmptyArray } from "../common/util.js";
import createGetVisitorKeys from "../utils/create-get-visitor-keys.js";
import createPrintPreCheckFunction from "../utils/create-print-pre-check-function.js";
import UnexpectedNodeError from "../utils/unexpected-node-error.js";
import { locStart, locEnd } from "./loc.js";
import clean from "./clean.js";
Expand All @@ -31,7 +30,6 @@ import {
import visitorKeys from "./visitor-keys.evaluate.js";

const getVisitorKeys = createGetVisitorKeys(visitorKeys);
const ensurePrintingNode = createPrintPreCheckFunction(getVisitorKeys);

const NEWLINES_TO_PRESERVE_MAX = 2;

Expand All @@ -41,10 +39,6 @@ const NEWLINES_TO_PRESERVE_MAX = 2;
function print(path, options, print) {
const node = path.getValue();

if (process.env.NODE_ENV !== "production") {
ensurePrintingNode(path);
}

const favoriteQuote = options.singleQuote ? "'" : '"';

switch (node.type) {
Expand Down
6 changes: 0 additions & 6 deletions src/language-html/printer-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import { fill, group, hardline } from "../document/builders.js";
import { cleanDoc, replaceEndOfLine } from "../document/utils.js";
import createGetVisitorKeys from "../utils/create-get-visitor-keys.js";
import createPrintPreCheckFunction from "../utils/create-print-pre-check-function.js";
import UnexpectedNodeError from "../utils/unexpected-node-error.js";
import clean from "./clean.js";
import {
Expand All @@ -28,15 +27,10 @@ import { printChildren } from "./print/children.js";
import visitorKeys from "./visitor-keys.js";

const getVisitorKeys = createGetVisitorKeys(visitorKeys);
const ensurePrintingNode = createPrintPreCheckFunction(getVisitorKeys);

function genericPrint(path, options, print) {
const node = path.getValue();

if (process.env.NODE_ENV !== "production") {
ensurePrintingNode(path);
}

switch (node.type) {
case "front-matter":
return replaceEndOfLine(node.raw);
Expand Down
4 changes: 3 additions & 1 deletion src/language-js/print/assignment.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ function printAssignment(
) {
const layout = chooseLayout(path, options, print, leftDoc, rightPropertyName);

const rightDoc = print(rightPropertyName, { assignmentLayout: layout });
const rightDoc = rightPropertyName
? print(rightPropertyName, { assignmentLayout: layout })
: "";

switch (layout) {
// First break after operator, then the sides are broken independently on their own lines
Expand Down
12 changes: 11 additions & 1 deletion src/language-js/print/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,17 @@ function printClassProperty(path, options, print) {
printTypeAnnotation(path, options, print)
);

return [printAssignment(path, options, print, parts, " =", "value"), semi];
return [
printAssignment(
path,
options,
print,
parts,
" =",
node.type === "TSAbstractPropertyDefinition" ? undefined : "value"
),
semi,
];
}

export {
Expand Down
6 changes: 3 additions & 3 deletions src/language-js/print/type-annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,9 @@ function printFunctionType(path, options, print) {
node.returnType || node.predicate || node.typeAnnotation
? [
isArrowFunctionTypeAnnotation ? " => " : ": ",
print("returnType"),
print("predicate"),
print("typeAnnotation"),
node.returnType ? print("returnType") : "",
node.predicate ? print("predicate") : "",
node.typeAnnotation ? print("typeAnnotation") : "",
]
: "";

Expand Down
7 changes: 0 additions & 7 deletions src/language-js/printer-estree.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
indent,
} from "../document/builders.js";
import { replaceEndOfLine } from "../document/utils.js";
import createPrintPreCheckFunction from "../utils/create-print-pre-check-function.js";
import UnexpectedNodeError from "../utils/unexpected-node-error.js";
import embed from "./embed.js";
import clean from "./clean.js";
Expand Down Expand Up @@ -90,15 +89,9 @@ import { printComment } from "./print/comment.js";
import { printLiteral } from "./print/literal.js";
import { printDecorators } from "./print/decorators.js";

const ensurePrintingNode = createPrintPreCheckFunction(getVisitorKeys);

function genericPrint(path, options, print, args) {
const node = path.getValue();

if (process.env.NODE_ENV !== "production") {
ensurePrintingNode(path);
}

const printed = printPathNoParens(path, options, print, args);
if (!printed) {
return "";
Expand Down
6 changes: 0 additions & 6 deletions src/language-markdown/printer-markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
import { normalizeDoc, replaceEndOfLine } from "../document/utils.js";
import { printDocToString } from "../document/printer.js";
import createGetVisitorKeys from "../utils/create-get-visitor-keys.js";
import createPrintPreCheckFunction from "../utils/create-print-pre-check-function.js";
import UnexpectedNodeError from "../utils/unexpected-node-error.js";
import embed from "./embed.js";
import { insertPragma } from "./pragma.js";
Expand All @@ -42,7 +41,6 @@ import {
import visitorKeys from "./visitor-keys.js";

const getVisitorKeys = createGetVisitorKeys(visitorKeys);
const ensurePrintingNode = createPrintPreCheckFunction(getVisitorKeys);

/**
* @typedef {import("../document/builders.js").Doc} Doc
Expand All @@ -59,10 +57,6 @@ const SIBLING_NODE_TYPES = new Set([
function genericPrint(path, options, print) {
const node = path.getValue();

if (process.env.NODE_ENV !== "production") {
ensurePrintingNode(path);
}

if (shouldRemainTheSameContent(path)) {
return splitText(
options.originalText.slice(
Expand Down
6 changes: 0 additions & 6 deletions src/language-yaml/printer-yaml.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
import { replaceEndOfLine } from "../document/utils.js";
import { isPreviousLineEmpty } from "../common/util.js";
import createGetVisitorKeys from "../utils/create-get-visitor-keys.js";
import createPrintPreCheckFunction from "../utils/create-print-pre-check-function.js";
import UnexpectedNodeError from "../utils/unexpected-node-error.js";
import { insertPragma, isPragma } from "./pragma.js";
import { locStart } from "./loc.js";
Expand Down Expand Up @@ -44,15 +43,10 @@ import printMappingItem from "./print/mapping-item.js";
import printBlock from "./print/block.js";

const getVisitorKeys = createGetVisitorKeys(visitorKeys);
const ensurePrintingNode = createPrintPreCheckFunction(getVisitorKeys);

function genericPrint(path, options, print) {
const node = path.getValue();

if (process.env.NODE_ENV !== "production") {
ensurePrintingNode(path);
}

/** @type {Doc[]} */
const parts = [];

Expand Down

0 comments on commit f47a852

Please sign in to comment.