Skip to content

Commit 9fb32a1

Browse files
authored
Minor refactor to property print (#15924)
1 parent 08f1940 commit 9fb32a1

File tree

6 files changed

+40
-46
lines changed

6 files changed

+40
-46
lines changed

src/language-js/print/estree.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
isCallExpression,
1919
isLiteral,
2020
isMemberExpression,
21+
isMethod,
2122
isNextLineEmpty,
2223
isObjectOrRecordExpression,
2324
needsHardlineAfterDanglingComment,
@@ -252,13 +253,15 @@ function printEstree(path, options, print, args) {
252253
case "ObjectPattern":
253254
case "RecordExpression":
254255
return printObject(path, options, print);
255-
// Babel 6
256-
case "ObjectProperty": // Non-standard AST node type.
257256
case "Property":
258-
if (node.method || node.kind === "get" || node.kind === "set") {
257+
if (isMethod(node)) {
259258
return printMethod(path, options, print);
260259
}
261260
return printProperty(path, options, print);
261+
// Babel
262+
case "ObjectProperty":
263+
return printProperty(path, options, print);
264+
// Babel
262265
case "ObjectMethod":
263266
return printMethod(path, options, print);
264267
case "Decorator":

src/language-js/print/flow.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ import assert from "node:assert";
55
import { replaceEndOfLine } from "../../document/utils.js";
66
import printNumber from "../../utils/print-number.js";
77
import printString from "../../utils/print-string.js";
8-
import {
9-
isFunctionNotation,
10-
isGetterOrSetter,
11-
rawText,
12-
} from "../utils/index.js";
8+
import { isMethod, rawText } from "../utils/index.js";
139
import isFlowKeywordType from "../utils/is-flow-keyword-type.js";
1410
import { printArray } from "./array.js";
1511
import { printBinaryCastExpression } from "./cast-expression.js";
@@ -210,11 +206,11 @@ function printFlow(path, options, print) {
210206

211207
return [
212208
modifier,
213-
isGetterOrSetter(node) ? node.kind + " " : "",
209+
node.kind !== "init" ? node.kind + " " : "",
214210
node.variance ? print("variance") : "",
215211
printPropertyKey(path, options, print),
216212
printOptionalToken(path),
217-
isFunctionNotation(node) ? "" : ": ",
213+
isMethod(node) ? "" : ": ",
218214
print("value"),
219215
];
220216
}

src/language-js/print/function-parameters.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ import {
1717
hasComment,
1818
hasRestParameter,
1919
isArrayOrTupleExpression,
20+
isFlowObjectTypePropertyAFunction,
2021
isNextLineEmpty,
2122
isObjectOrRecordExpression,
2223
isObjectType,
23-
isObjectTypePropertyAFunction,
2424
isSimpleType,
2525
isTestCall,
2626
isTypeAnnotationAFunction,
@@ -120,7 +120,7 @@ function printFunctionParameters(
120120
}
121121

122122
const isFlowShorthandWithOneArg =
123-
(isObjectTypePropertyAFunction(parent) ||
123+
(isFlowObjectTypePropertyAFunction(parent) ||
124124
isTypeAnnotationAFunction(parent) ||
125125
parent.type === "TypeAlias" ||
126126
parent.type === "UnionTypeAnnotation" ||

src/language-js/print/function.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
isBinaryish,
2222
isCallExpression,
2323
isJsxElement,
24+
isMethod,
2425
} from "../utils/index.js";
2526
import {
2627
printFunctionParameters,
@@ -36,20 +37,16 @@ import { printTypeAnnotationProperty } from "./type-annotation.js";
3637
* @typedef {import("../../document/builders.js").Doc} Doc
3738
*/
3839

39-
const isMethod = (node) =>
40-
node.type === "ObjectMethod" ||
41-
node.type === "ClassMethod" ||
42-
node.type === "ClassPrivateMethod" ||
43-
node.type === "MethodDefinition" ||
44-
node.type === "TSAbstractMethodDefinition" ||
45-
node.type === "TSDeclareMethod" ||
46-
((node.type === "Property" || node.type === "ObjectProperty") &&
47-
(node.method || node.kind === "get" || node.kind === "set"));
48-
49-
const isMethodValue = (path) =>
50-
path.node.type === "FunctionExpression" &&
51-
path.key === "value" &&
52-
isMethod(path.parent);
40+
const isMethodValue = ({ node, key, parent }) =>
41+
key === "value" &&
42+
node.type === "FunctionExpression" &&
43+
(parent.type === "ObjectMethod" ||
44+
parent.type === "ClassMethod" ||
45+
parent.type === "ClassPrivateMethod" ||
46+
parent.type === "MethodDefinition" ||
47+
parent.type === "TSAbstractMethodDefinition" ||
48+
parent.type === "TSDeclareMethod" ||
49+
(parent.type === "Property" && isMethod(parent)));
5350

5451
/*
5552
- "FunctionDeclaration"

src/language-js/print/type-annotation.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import {
1515
createTypeCheckFunction,
1616
hasComment,
1717
hasLeadingOwnLineComment,
18+
isFlowObjectTypePropertyAFunction,
1819
isObjectType,
19-
isObjectTypePropertyAFunction,
2020
isSimpleType,
2121
isUnionType,
2222
} from "../utils/index.js";
@@ -256,7 +256,7 @@ function isFlowArrowFunctionTypeAnnotation(path) {
256256
const { node, parent } = path;
257257
return (
258258
node.type === "FunctionTypeAnnotation" &&
259-
(isObjectTypePropertyAFunction(parent) ||
259+
(isFlowObjectTypePropertyAFunction(parent) ||
260260
!(
261261
((parent.type === "ObjectTypeProperty" ||
262262
parent.type === "ObjectTypeInternalSlot") &&

src/language-js/utils/index.js

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -243,30 +243,29 @@ function isAngularTestWrapper(node) {
243243

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

246-
function isGetterOrSetter(node) {
247-
return node.kind === "get" || node.kind === "set";
248-
}
249-
250-
// TODO: This is a bad hack and we need a better way to distinguish between
251-
// arrow functions and otherwise
252-
function isFunctionNotation(node) {
253-
return isGetterOrSetter(node) || hasSameLocStart(node, node.value);
246+
function isMethod(node) {
247+
return (
248+
(node.method && node.kind === "init") ||
249+
node.kind === "get" ||
250+
node.kind === "set"
251+
);
254252
}
255253

256-
// Hack to differentiate between the following two which have the same ast
257-
// type T = { method: () => void };
258-
// type T = { method(): void };
259254
/**
260255
* @param {Node} node
261256
* @returns {boolean}
262257
*/
263-
function isObjectTypePropertyAFunction(node) {
258+
function isFlowObjectTypePropertyAFunction(node) {
264259
return (
265260
(node.type === "ObjectTypeProperty" ||
266261
node.type === "ObjectTypeInternalSlot") &&
267-
node.value.type === "FunctionTypeAnnotation" &&
268262
!node.static &&
269-
!isFunctionNotation(node)
263+
!node.method &&
264+
// @ts-expect-error -- exists on `ObjectTypeProperty` but not `ObjectTypeInternalSlot`
265+
node.kind !== "get" &&
266+
// @ts-expect-error -- exists on `ObjectTypeProperty` but not `ObjectTypeInternalSlot`
267+
node.kind !== "set" &&
268+
node.value.type === "FunctionTypeAnnotation"
270269
);
271270
}
272271

@@ -1193,7 +1192,7 @@ function isObjectProperty(node) {
11931192
return (
11941193
node &&
11951194
(node.type === "ObjectProperty" ||
1196-
(node.type === "Property" && !node.method && node.kind === "init"))
1195+
(node.type === "Property" && !isMethod(node)))
11971196
);
11981197
}
11991198

@@ -1241,10 +1240,9 @@ export {
12411240
isCallExpression,
12421241
isCallLikeExpression,
12431242
isExportDeclaration,
1243+
isFlowObjectTypePropertyAFunction,
12441244
isFunctionCompositionArgs,
1245-
isFunctionNotation,
12461245
isFunctionOrArrowExpression,
1247-
isGetterOrSetter,
12481246
isIntersectionType,
12491247
isJsxElement,
12501248
isLineComment,
@@ -1253,12 +1251,12 @@ export {
12531251
isLongCurriedCallExpression,
12541252
isMemberExpression,
12551253
isMemberish,
1254+
isMethod,
12561255
isNextLineEmpty,
12571256
isNumericLiteral,
12581257
isObjectOrRecordExpression,
12591258
isObjectProperty,
12601259
isObjectType,
1261-
isObjectTypePropertyAFunction,
12621260
isPrettierIgnoreComment,
12631261
isRegExpLiteral,
12641262
isSignedNumericLiteral,

0 commit comments

Comments
 (0)