Skip to content

Commit

Permalink
Minor refactor to property print (#15924)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Feb 3, 2024
1 parent 08f1940 commit 9fb32a1
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 46 deletions.
9 changes: 6 additions & 3 deletions src/language-js/print/estree.js
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
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
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
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
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
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 @@ -1193,7 +1192,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 @@ -1241,10 +1240,9 @@ export {
isCallExpression,
isCallLikeExpression,
isExportDeclaration,
isFlowObjectTypePropertyAFunction,
isFunctionCompositionArgs,
isFunctionNotation,
isFunctionOrArrowExpression,
isGetterOrSetter,
isIntersectionType,
isJsxElement,
isLineComment,
Expand All @@ -1253,12 +1251,12 @@ export {
isLongCurriedCallExpression,
isMemberExpression,
isMemberish,
isMethod,
isNextLineEmpty,
isNumericLiteral,
isObjectOrRecordExpression,
isObjectProperty,
isObjectType,
isObjectTypePropertyAFunction,
isPrettierIgnoreComment,
isRegExpLiteral,
isSignedNumericLiteral,
Expand Down

0 comments on commit 9fb32a1

Please sign in to comment.