Skip to content

Commit 1b44175

Browse files
authored
Simplify (#2231)
* Simplify? * Kill `getNextNode` * Remove unnecessary export * Use optional chaining * Update printer.js
1 parent e0530b1 commit 1b44175

File tree

3 files changed

+6
-50
lines changed

3 files changed

+6
-50
lines changed

src/loc.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
const loc = (prop) => (node) => {
2-
return node.loc && node.loc[prop] && node.loc[prop].offset;
3-
};
1+
const loc = (prop) => (node) => node.loc?.[prop]?.offset;
42

53
export const locStart = loc("start");
64
export const locEnd = loc("end");

src/printer.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import { locStart, locEnd } from "./loc.js";
1414
import {
1515
getLast,
1616
getPenultimate,
17-
isLastStatement,
1817
lineShouldEndWithSemicolon,
1918
printNumber,
2019
shouldFlatten,
@@ -36,7 +35,6 @@ import {
3635
isDocNode,
3736
getAncestorNode,
3837
isReferenceLikeNode,
39-
getNextNode,
4038
normalizeMagicMethodName,
4139
} from "./util.js";
4240

@@ -934,7 +932,7 @@ function printLines(path, options, print, childrenAttribute = "children") {
934932
const isInlineNode = childNode.kind === "inline";
935933
const printedPath = print();
936934
const canPrintBlankLine =
937-
!isLastStatement(path) &&
935+
!isLastNode &&
938936
!isInlineNode &&
939937
(nextNode && nextNode.kind === "case"
940938
? !isFirstChildrenInlineNode(path)
@@ -1102,15 +1100,15 @@ function printLines(path, options, print, childrenAttribute = "children") {
11021100
}
11031101

11041102
function printStatements(path, options, print, childrenAttribute) {
1105-
return path.map(() => {
1103+
return path.map(({ node, isLast }) => {
11061104
const parts = [];
11071105

11081106
parts.push(print());
11091107

1110-
if (!isLastStatement(path)) {
1108+
if (!isLast) {
11111109
parts.push(hardline);
11121110

1113-
if (isNextLineEmpty(options.originalText, locEnd(path.node))) {
1111+
if (isNextLineEmpty(options.originalText, locEnd(node))) {
11141112
parts.push(hardline);
11151113
}
11161114
}
@@ -1607,13 +1605,11 @@ function printNode(path, options, print) {
16071605
];
16081606
}
16091607

1610-
const nextNode = getNextNode(path, node);
1611-
16121608
return [
16131609
"declare(",
16141610
printDeclareArguments(path),
16151611
")",
1616-
nextNode && nextNode.kind === "inline" ? "" : ";",
1612+
path.next?.kind === "inline" ? "" : ";",
16171613
];
16181614
}
16191615
case "declaredirective":

src/util.js

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,6 @@ function getNodeListProperty(node) {
154154
return Array.isArray(body) ? body : null;
155155
}
156156

157-
function getParentNodeListProperty(path) {
158-
const { parent } = path;
159-
if (!parent) {
160-
return null;
161-
}
162-
return getNodeListProperty(parent);
163-
}
164-
165157
function getLast(arr) {
166158
if (arr.length > 0) {
167159
return arr[arr.length - 1];
@@ -176,15 +168,6 @@ function getPenultimate(arr) {
176168
return null;
177169
}
178170

179-
function isLastStatement(path) {
180-
const body = getParentNodeListProperty(path);
181-
if (!body) {
182-
return true;
183-
}
184-
const { node } = path;
185-
return body[body.length - 1] === node;
186-
}
187-
188171
function isFirstChildrenInlineNode(path) {
189172
const { node } = path;
190173

@@ -499,23 +482,6 @@ function getAlignment(text) {
499482
return lastLine.length - lastLine.trimLeft().length + 1;
500483
}
501484

502-
function getNextNode(path, node) {
503-
const { parent } = path;
504-
const children = getNodeListProperty(parent);
505-
506-
if (!children) {
507-
return null;
508-
}
509-
510-
const index = children.indexOf(node);
511-
512-
if (index === -1) {
513-
return null;
514-
}
515-
516-
return parent.children[index + 1];
517-
}
518-
519485
function isProgramLikeNode(node) {
520486
return ["program", "declare", "namespace"].includes(node.kind);
521487
}
@@ -652,11 +618,8 @@ export {
652618
isBitwiseOperator,
653619
shouldFlatten,
654620
nodeHasStatement,
655-
getNodeListProperty,
656-
getParentNodeListProperty,
657621
getLast,
658622
getPenultimate,
659-
isLastStatement,
660623
getBodyFirstChild,
661624
lineShouldEndWithSemicolon,
662625
fileShouldEndWithHardline,
@@ -677,6 +640,5 @@ export {
677640
shouldPrintHardlineBeforeTrailingComma,
678641
isDocNode,
679642
getAncestorNode,
680-
getNextNode,
681643
normalizeMagicMethodName,
682644
};

0 commit comments

Comments
 (0)