Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor refactor to property print #15924

Merged
merged 4 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/language-js/print/estree.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
isCallExpression,
isLiteral,
isMemberExpression,
isMethod,
isNextLineEmpty,
isObjectOrRecordExpression,
needsHardlineAfterDanglingComment,
Expand Down Expand Up @@ -252,13 +253,15 @@ function printEstree(path, options, print, args) {
case "ObjectPattern":
case "RecordExpression":
return printObject(path, options, print);
// Babel 6
case "ObjectProperty": // Non-standard AST node type.
case "Property":
if (node.method || node.kind === "get" || node.kind === "set") {
if (isMethod(node)) {
return printMethod(path, options, print);
}
return printProperty(path, options, print);
// Babel
case "ObjectProperty":
return printProperty(path, options, print);
// Babel
case "ObjectMethod":
return printMethod(path, options, print);
case "Decorator":
Expand Down
10 changes: 3 additions & 7 deletions src/language-js/print/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import assert from "node:assert";
import { replaceEndOfLine } from "../../document/utils.js";
import printNumber from "../../utils/print-number.js";
import printString from "../../utils/print-string.js";
import {
isFunctionNotation,
isGetterOrSetter,
rawText,
} from "../utils/index.js";
import { isMethod, rawText } from "../utils/index.js";
import isFlowKeywordType from "../utils/is-flow-keyword-type.js";
import { printArray } from "./array.js";
import { printBinaryCastExpression } from "./cast-expression.js";
Expand Down Expand Up @@ -210,11 +206,11 @@ function printFlow(path, options, print) {

return [
modifier,
isGetterOrSetter(node) ? node.kind + " " : "",
node.kind !== "init" ? node.kind + " " : "",
node.variance ? print("variance") : "",
printPropertyKey(path, options, print),
printOptionalToken(path),
isFunctionNotation(node) ? "" : ": ",
isMethod(node) ? "" : ": ",
print("value"),
];
}
Expand Down
4 changes: 2 additions & 2 deletions src/language-js/print/function-parameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import {
hasComment,
hasRestParameter,
isArrayOrTupleExpression,
isFlowObjectTypePropertyAFunction,
isNextLineEmpty,
isObjectOrRecordExpression,
isObjectType,
isObjectTypePropertyAFunction,
isSimpleType,
isTestCall,
isTypeAnnotationAFunction,
Expand Down Expand Up @@ -120,7 +120,7 @@ function printFunctionParameters(
}

const isFlowShorthandWithOneArg =
(isObjectTypePropertyAFunction(parent) ||
(isFlowObjectTypePropertyAFunction(parent) ||
isTypeAnnotationAFunction(parent) ||
parent.type === "TypeAlias" ||
parent.type === "UnionTypeAnnotation" ||
Expand Down
25 changes: 11 additions & 14 deletions src/language-js/print/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
isBinaryish,
isCallExpression,
isJsxElement,
isMethod,
} from "../utils/index.js";
import {
printFunctionParameters,
Expand All @@ -36,20 +37,16 @@ import { printTypeAnnotationProperty } from "./type-annotation.js";
* @typedef {import("../../document/builders.js").Doc} Doc
*/

const isMethod = (node) =>
node.type === "ObjectMethod" ||
node.type === "ClassMethod" ||
node.type === "ClassPrivateMethod" ||
node.type === "MethodDefinition" ||
node.type === "TSAbstractMethodDefinition" ||
node.type === "TSDeclareMethod" ||
((node.type === "Property" || node.type === "ObjectProperty") &&
(node.method || node.kind === "get" || node.kind === "set"));

const isMethodValue = (path) =>
path.node.type === "FunctionExpression" &&
path.key === "value" &&
isMethod(path.parent);
const isMethodValue = ({ node, key, parent }) =>
key === "value" &&
node.type === "FunctionExpression" &&
(parent.type === "ObjectMethod" ||
parent.type === "ClassMethod" ||
parent.type === "ClassPrivateMethod" ||
parent.type === "MethodDefinition" ||
parent.type === "TSAbstractMethodDefinition" ||
parent.type === "TSDeclareMethod" ||
(parent.type === "Property" && isMethod(parent)));

/*
- "FunctionDeclaration"
Expand Down
4 changes: 2 additions & 2 deletions src/language-js/print/type-annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {
createTypeCheckFunction,
hasComment,
hasLeadingOwnLineComment,
isFlowObjectTypePropertyAFunction,
isObjectType,
isObjectTypePropertyAFunction,
isSimpleType,
isUnionType,
} from "../utils/index.js";
Expand Down Expand Up @@ -256,7 +256,7 @@ function isFlowArrowFunctionTypeAnnotation(path) {
const { node, parent } = path;
return (
node.type === "FunctionTypeAnnotation" &&
(isObjectTypePropertyAFunction(parent) ||
(isFlowObjectTypePropertyAFunction(parent) ||
!(
((parent.type === "ObjectTypeProperty" ||
parent.type === "ObjectTypeInternalSlot") &&
Expand Down
34 changes: 16 additions & 18 deletions src/language-js/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,30 +243,29 @@ function isAngularTestWrapper(node) {

const isJsxElement = createTypeCheckFunction(["JSXElement", "JSXFragment"]);

function isGetterOrSetter(node) {
return node.kind === "get" || node.kind === "set";
}

// TODO: This is a bad hack and we need a better way to distinguish between
// arrow functions and otherwise
function isFunctionNotation(node) {
return isGetterOrSetter(node) || hasSameLocStart(node, node.value);
function isMethod(node) {
return (
(node.method && node.kind === "init") ||
node.kind === "get" ||
node.kind === "set"
);
}

// Hack to differentiate between the following two which have the same ast
// type T = { method: () => void };
// type T = { method(): void };
/**
* @param {Node} node
* @returns {boolean}
*/
function isObjectTypePropertyAFunction(node) {
function isFlowObjectTypePropertyAFunction(node) {
return (
(node.type === "ObjectTypeProperty" ||
node.type === "ObjectTypeInternalSlot") &&
node.value.type === "FunctionTypeAnnotation" &&
!node.static &&
!isFunctionNotation(node)
!node.method &&
// @ts-expect-error -- exists on `ObjectTypeProperty` but not `ObjectTypeInternalSlot`
node.kind !== "get" &&
// @ts-expect-error -- exists on `ObjectTypeProperty` but not `ObjectTypeInternalSlot`
node.kind !== "set" &&
node.value.type === "FunctionTypeAnnotation"
);
}

Expand Down Expand Up @@ -1209,7 +1208,7 @@ function isObjectProperty(node) {
return (
node &&
(node.type === "ObjectProperty" ||
(node.type === "Property" && !node.method && node.kind === "init"))
(node.type === "Property" && !isMethod(node)))
);
}

Expand Down Expand Up @@ -1258,10 +1257,9 @@ export {
isCallExpression,
isCallLikeExpression,
isExportDeclaration,
isFlowObjectTypePropertyAFunction,
isFunctionCompositionArgs,
isFunctionNotation,
isFunctionOrArrowExpression,
isGetterOrSetter,
isIntersectionType,
isJsxElement,
isLineComment,
Expand All @@ -1270,12 +1268,12 @@ export {
isLongCurriedCallExpression,
isMemberExpression,
isMemberish,
isMethod,
isNextLineEmpty,
isNumericLiteral,
isObjectOrRecordExpression,
isObjectProperty,
isObjectType,
isObjectTypePropertyAFunction,
isPrettierIgnoreComment,
isRegExpLiteral,
isSignedNumericLiteral,
Expand Down