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

Simplify #2231

Merged
merged 6 commits into from
Aug 30, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/loc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const loc = (prop) => (node) => {
return node.loc && node.loc[prop] && node.loc[prop].offset;
};
const loc = (prop) => (node) => node.loc?.[prop]?.offset;

export const locStart = loc("start");
export const locEnd = loc("end");
14 changes: 5 additions & 9 deletions src/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { locStart, locEnd } from "./loc.js";
import {
getLast,
getPenultimate,
isLastStatement,
lineShouldEndWithSemicolon,
printNumber,
shouldFlatten,
Expand All @@ -36,7 +35,6 @@ import {
isDocNode,
getAncestorNode,
isReferenceLikeNode,
getNextNode,
normalizeMagicMethodName,
} from "./util.js";

Expand Down Expand Up @@ -934,7 +932,7 @@ function printLines(path, options, print, childrenAttribute = "children") {
const isInlineNode = childNode.kind === "inline";
const printedPath = print();
const canPrintBlankLine =
!isLastStatement(path) &&
!isLastNode &&
!isInlineNode &&
(nextNode && nextNode.kind === "case"
? !isFirstChildrenInlineNode(path)
Expand Down Expand Up @@ -1102,15 +1100,15 @@ function printLines(path, options, print, childrenAttribute = "children") {
}

function printStatements(path, options, print, childrenAttribute) {
return path.map(() => {
return path.map(({ node, isLast }) => {
const parts = [];

parts.push(print());

if (!isLastStatement(path)) {
if (!isLast) {
parts.push(hardline);

if (isNextLineEmpty(options.originalText, locEnd(path.node))) {
if (isNextLineEmpty(options.originalText, locEnd(node))) {
parts.push(hardline);
}
}
Expand Down Expand Up @@ -1607,13 +1605,11 @@ function printNode(path, options, print) {
];
}

const nextNode = getNextNode(path, node);

return [
"declare(",
printDeclareArguments(path),
")",
nextNode && nextNode.kind === "inline" ? "" : ";",
path.next?.kind === "inline" ? "" : ";",
];
}
case "declaredirective":
Expand Down
38 changes: 0 additions & 38 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,6 @@ function getNodeListProperty(node) {
return Array.isArray(body) ? body : null;
}

function getParentNodeListProperty(path) {
const { parent } = path;
if (!parent) {
return null;
}
return getNodeListProperty(parent);
}

function getLast(arr) {
if (arr.length > 0) {
return arr[arr.length - 1];
Expand All @@ -176,15 +168,6 @@ function getPenultimate(arr) {
return null;
}

function isLastStatement(path) {
const body = getParentNodeListProperty(path);
if (!body) {
return true;
}
const { node } = path;
return body[body.length - 1] === node;
}

function isFirstChildrenInlineNode(path) {
const { node } = path;

Expand Down Expand Up @@ -499,23 +482,6 @@ function getAlignment(text) {
return lastLine.length - lastLine.trimLeft().length + 1;
}

function getNextNode(path, node) {
const { parent } = path;
const children = getNodeListProperty(parent);

if (!children) {
return null;
}

const index = children.indexOf(node);

if (index === -1) {
return null;
}

return parent.children[index + 1];
}

function isProgramLikeNode(node) {
return ["program", "declare", "namespace"].includes(node.kind);
}
Expand Down Expand Up @@ -652,11 +618,8 @@ export {
isBitwiseOperator,
shouldFlatten,
nodeHasStatement,
getNodeListProperty,
getParentNodeListProperty,
getLast,
getPenultimate,
isLastStatement,
getBodyFirstChild,
lineShouldEndWithSemicolon,
fileShouldEndWithHardline,
Expand All @@ -677,6 +640,5 @@ export {
shouldPrintHardlineBeforeTrailingComma,
isDocNode,
getAncestorNode,
getNextNode,
normalizeMagicMethodName,
};