Simplify use of getVisitorKeys by putting it directly on options (#18059)

This commit is contained in:
fisker Cheung authored and GitHub committed 2025-10-12 21:41:14 +08:00
1 parent 1b9c98269e
commit 172c0051e3
6 files changed
+23 -36

No files matched your search

+3 -9
View File
@@ -2,7 +2,6 @@ import * as assert from "#universal/assert";
import { getChildren } from "../../utils/ast-utils.js";
import hasNewline from "../../utils/has-newline.js";
import isNonEmptyArray from "../../utils/is-non-empty-array.js";
import createGetVisitorKeysFunction from "../create-get-visitor-keys-function.js";
import {
addDanglingComment,
addLeadingComment,
@@ -20,13 +19,10 @@ function getSortedChildNodes(node, options, ancestors) {
}
const {
printer: {
getCommentChildNodes,
canAttachComment,
getVisitorKeys: printerGetVisitorKeys,
},
printer: { getCommentChildNodes, canAttachComment },
locStart,
locEnd,
getVisitorKeys,
} = options;
if (!canAttachComment) {
@@ -36,9 +32,7 @@ function getSortedChildNodes(node, options, ancestors) {
let childAncestors;
const childNodes = (
getCommentChildNodes?.(node, options) ?? [
...getChildren(node, {
getVisitorKeys: createGetVisitorKeysFunction(printerGetVisitorKeys),
}),
...getChildren(node, { getVisitorKeys }),
]
).flatMap((child) => {
childAncestors ??= [node, ...ancestors];
+1 -4
View File
@@ -1,12 +1,9 @@
import noop from "../utils/noop.js";
import createGetVisitorKeysFunction from "./create-get-visitor-keys-function.js";
function createPrintPreCheckFunction(options) {
// All core plugins have full list of keys for possible child nodes
// Ensure we only pass node to `print`
const getVisitorKeys = createGetVisitorKeysFunction(
options.printer.getVisitorKeys,
);
const { getVisitorKeys } = options;
return function (path) {
if (path.isRoot) {
+1 -5
View File
@@ -1,5 +1,4 @@
import { getChildren, getDescendants, isLeaf } from "../utils/ast-utils.js";
import createGetVisitorKeysFunction from "./create-get-visitor-keys-function.js";
/**
* Find the location of the cursor in the AST, represented in one of the
@@ -18,10 +17,7 @@ import createGetVisitorKeysFunction from "./create-get-visitor-keys-function.js"
* the other possibilities otherwise.
*/
function getCursorLocation(ast, options) {
const { cursorOffset, locStart, locEnd } = options;
const getVisitorKeys = createGetVisitorKeysFunction(
options.printer.getVisitorKeys,
);
const { cursorOffset, locStart, locEnd, getVisitorKeys } = options;
const nodeContainsCursor = (node) =>
locStart(node) <= cursorOffset && locEnd(node) >= cursorOffset;
+8 -10
View File
@@ -1,24 +1,22 @@
import isObject from "../utils/is-object.js";
import createGetVisitorKeysFunction from "./create-get-visitor-keys-function.js";
function massageAst(ast, options) {
const {
printer: {
massageAstNode: cleanFunction,
getVisitorKeys: printerGetVisitorKeys,
},
} = options;
const cleanFunction = options.printer.massageAstNode;
if (!cleanFunction) {
return ast;
}
const getVisitorKeys = createGetVisitorKeysFunction(printerGetVisitorKeys);
const ignoredProperties = cleanFunction.ignoredProperties ?? new Set();
const getVisitorKeys =
options.getVisitorKeys ??
createGetVisitorKeysFunction(options.printer.getVisitorKeys);
const { ignoredProperties } = cleanFunction;
return recurse(ast);
function recurse(original, parent) {
if (!(original !== null && typeof original === "object")) {
if (!isObject(original)) {
return original;
}
@@ -29,7 +27,7 @@ function massageAst(ast, options) {
const cloned = {};
const childrenKeys = new Set(getVisitorKeys(original));
for (const key in original) {
if (!Object.hasOwn(original, key) || ignoredProperties.has(key)) {
if (!Object.hasOwn(original, key) || ignoredProperties?.has(key)) {
continue;
}
+5 -8
View File
@@ -14,11 +14,7 @@ async function printEmbeddedLanguages(
) {
const {
embeddedLanguageFormatting,
printer: {
embed,
hasPrettierIgnore = () => false,
getVisitorKeys: printerGetVisitorKeys,
},
printer: { embed, hasPrettierIgnore = () => false },
} = options;
if (!embed || embeddedLanguageFormatting !== "auto") {
@@ -31,9 +27,10 @@ async function printEmbeddedLanguages(
);
}
const getVisitorKeys = createGetVisitorKeysFunction(
embed.getVisitorKeys ?? printerGetVisitorKeys,
);
const getVisitorKeys = embed.getVisitorKeys
? createGetVisitorKeysFunction(embed.getVisitorKeys)
: options.getVisitorKeys;
const embedCallResults = [];
recurse();
+5
View File
@@ -1,6 +1,7 @@
import { UndefinedParserError } from "../common/errors.js";
import { getSupportInfo } from "../main/support.js";
import inferParser from "../utils/infer-parser.js";
import createGetVisitorKeysFunction from "./create-get-visitor-keys-function.js";
import normalizeOptions from "./normalize-options.js";
import {
getParserPluginByParserName,
@@ -15,6 +16,7 @@ const formatOptionsHiddenDefaults = {
originalText: undefined,
locStart: null,
locEnd: null,
getVisitorKeys: null,
};
// Copy options and fill in default values.
@@ -68,6 +70,9 @@ async function normalizeFormatOptions(options, opts = {}) {
const printer = await initPrinter(printerPlugin, parser.astFormat);
rawOptions.printer = printer;
rawOptions.getVisitorKeys = createGetVisitorKeysFunction(
printer.getVisitorKeys,
);
const pluginDefaults = printerPlugin.defaultOptions
? Object.fromEntries(