Skip to content

Commit

Permalink
Use loc{Start,End} directly (#2227)
Browse files Browse the repository at this point in the history
* Use `loc{Start,End}` function directly

* Linting
  • Loading branch information
fisker authored Aug 28, 2023
1 parent a1ce8e7 commit 6d21c9b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 43 deletions.
33 changes: 14 additions & 19 deletions src/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
isPreviousLineEmpty,
isLookupNode,
} from "./util.js";
import { locStart, locEnd } from "./loc.js";

const {
addLeadingComment,
Expand Down Expand Up @@ -380,16 +381,12 @@ function handleRetifComments(
precedingNode,
followingNode,
comment,
text,
options
text
/* options */
) {
const isSameLineAsPrecedingNode =
precedingNode &&
!hasNewlineInRange(
text,
options.locEnd(precedingNode),
options.locStart(comment)
);
!hasNewlineInRange(text, locEnd(precedingNode), locStart(comment));

if (
(!precedingNode || !isSameLineAsPrecedingNode) &&
Expand Down Expand Up @@ -518,14 +515,14 @@ function handleFunction(text, enclosingNode, followingNode, comment, options) {
let argumentsLocEnd = 0;
for (let i = 0; i < enclosingNode.arguments.length; i++) {
argumentsLocEnd =
options.locEnd(enclosingNode.arguments[i]) > argumentsLocEnd
? options.locEnd(enclosingNode.arguments[i])
locEnd(enclosingNode.arguments[i]) > argumentsLocEnd
? locEnd(enclosingNode.arguments[i])
: argumentsLocEnd;
}
const commentIsBetweenArgumentsAndBody =
enclosingNode.body &&
options.locStart(comment) > argumentsLocEnd &&
options.locEnd(comment) < options.locStart(enclosingNode.body);
locStart(comment) > argumentsLocEnd &&
locEnd(comment) < locStart(enclosingNode.body);
const nextCharIndex = getNextNonSpaceNonCommentCharacterIndex(
text,
comment,
Expand All @@ -538,7 +535,7 @@ function handleFunction(text, enclosingNode, followingNode, comment, options) {
commentIsBetweenArgumentsAndBody &&
text.charAt(nextCharIndex) !== ")"
) {
if (options.locEnd(comment) < options.locStart(enclosingNode.type)) {
if (locEnd(comment) < locStart(enclosingNode.type)) {
// we need to store this as a dangling comment in case the type is nullable
// ie function(): ?string {} - the "nullable" attribute is part of the
// function node, not the type.
Expand Down Expand Up @@ -868,11 +865,11 @@ function hasTrailingComment(node) {
return node.comments && node.comments.some((comment) => comment.trailing);
}

function hasLeadingOwnLineComment(text, node, options) {
function hasLeadingOwnLineComment(text, node) {
return (
node.comments &&
node.comments.some(
(comment) => comment.leading && hasNewline(text, options.locEnd(comment))
(comment) => comment.leading && hasNewline(text, locEnd(comment))
)
);
}
Expand Down Expand Up @@ -941,9 +938,7 @@ function printLeadingComment(path, print, options) {
if (isBlock) {
return [
contents,
hasNewline(options.originalText, options.locEnd(comment))
? hardline
: " ",
hasNewline(options.originalText, locEnd(comment)) ? hardline : " ",
];
}

Expand All @@ -961,7 +956,7 @@ function printTrailingComment(path, print, options) {
options.printer.isBlockComment && options.printer.isBlockComment(comment);

if (
hasNewline(options.originalText, options.locStart(comment), {
hasNewline(options.originalText, locStart(comment), {
backwards: true,
})
) {
Expand Down Expand Up @@ -1015,7 +1010,7 @@ function printAllComments(path, print, options, needsSemi) {
leadingParts.push(contents);

const text = options.originalText;
if (hasNewline(text, skipNewline(text, options.locEnd(comment)))) {
if (hasNewline(text, skipNewline(text, locEnd(comment)))) {
leadingParts.push(hardline);
}
} else if (trailing) {
Expand Down
9 changes: 3 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
isBlockComment,
} from "./comments.js";
import { hasPragma, insertPragma } from "./pragma.js";
import { locStart, locEnd } from "./loc.js";

const { join, hardline } = doc.builders;

Expand Down Expand Up @@ -55,16 +56,12 @@ const languages = [
}),
];

const loc = (prop) => (node) => {
return node.loc && node.loc[prop] && node.loc[prop].offset;
};

const parsers = {
php: {
parse,
astFormat: "php",
locStart: loc("start"),
locEnd: loc("end"),
locStart,
locEnd,
hasPragma,
},
};
Expand Down
6 changes: 6 additions & 0 deletions src/loc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const loc = (prop) => (node) => {
return node.loc && node.loc[prop] && node.loc[prop].offset;
};

export const locStart = loc("start");
export const locEnd = loc("end");
31 changes: 14 additions & 17 deletions src/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
hasLeadingOwnLineComment,
} from "./comments.js";
import pathNeedsParens from "./needs-parens.js";
import { locStart, locEnd } from "./loc.js";

import {
getLast,
Expand Down Expand Up @@ -1069,8 +1070,8 @@ function printLines(path, options, print, childrenAttribute = "children") {
? [
hasNewlineInRange(
options.originalText.trimEnd(),
options.locEnd(lastNode),
options.locEnd(node)
locEnd(lastNode),
locEnd(node)
)
? !(
lastNode.kind === "inline" &&
Expand Down Expand Up @@ -1502,7 +1503,7 @@ function printAssignmentRight(
) {
const ref = hasRef ? "&" : "";

if (hasLeadingOwnLineComment(options.originalText, rightNode, options)) {
if (hasLeadingOwnLineComment(options.originalText, rightNode)) {
return indent([hardline, ref, printedRight]);
}

Expand Down Expand Up @@ -1545,13 +1546,13 @@ function stringHasNewLines(node) {
return node.raw.includes("\n");
}

function isStringOnItsOwnLine(node, text, options) {
function isStringOnItsOwnLine(node, text) {
return (
(node.kind === "string" ||
(node.kind === "encapsed" &&
(node.type === "string" || node.type === "shell"))) &&
stringHasNewLines(node) &&
!hasNewline(text, options.locStart(node), { backwards: true })
!hasNewline(text, locStart(node), { backwards: true })
);
}

Expand Down Expand Up @@ -1632,11 +1633,7 @@ function printNode(path, options, print) {
? ""
: [
hardline,
isNextLineEmptyAfterNamespace(
options.originalText,
node,
options.locStart
)
isNextLineEmptyAfterNamespace(options.originalText, node)
? hardline
: "",
],
Expand Down Expand Up @@ -2039,7 +2036,7 @@ function printNode(path, options, print) {
// Multiline strings as single arguments
if (
node.arguments.length === 1 &&
isStringOnItsOwnLine(node.arguments[0], options.originalText, options)
isStringOnItsOwnLine(node.arguments[0], options.originalText)
) {
return [
print("what"),
Expand All @@ -2064,7 +2061,7 @@ function printNode(path, options, print) {
if (
!isAnonymousClassNode &&
node.arguments.length === 1 &&
isStringOnItsOwnLine(node.arguments[0], options.originalText, options)
isStringOnItsOwnLine(node.arguments[0], options.originalText)
) {
return [
"new ",
Expand Down Expand Up @@ -2156,7 +2153,7 @@ function printNode(path, options, print) {
node.useDie ? "die" : "exit",
"(",
node.expression
? isStringOnItsOwnLine(node.expression, options.originalText, options)
? isStringOnItsOwnLine(node.expression, options.originalText)
? print("expression")
: [indent([softline, print("expression")]), softline]
: printDanglingComments(path, options),
Expand Down Expand Up @@ -2199,7 +2196,7 @@ function printNode(path, options, print) {
case "eval":
return group([
"eval(",
isStringOnItsOwnLine(node.source, options.originalText, options)
isStringOnItsOwnLine(node.source, options.originalText)
? print("source")
: [indent([softline, print("source")]), softline],
")",
Expand Down Expand Up @@ -2388,15 +2385,15 @@ function printNode(path, options, print) {

const [firstProperty] = node.items
.filter((node) => node.kind !== "noop")
.sort((a, b) => options.locStart(a) - options.locStart(b));
.sort((a, b) => locStart(a) - locStart(b));
const isAssociative = !!(firstProperty && firstProperty.key);
const shouldBreak =
isAssociative &&
firstProperty &&
hasNewlineInRange(
options.originalText,
options.locStart(node),
options.locStart(firstProperty)
locStart(node),
locStart(firstProperty)
);

return group(
Expand Down
3 changes: 2 additions & 1 deletion src/util.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { util as prettierUtil, version as prettierVersion } from "prettier";
import { locStart } from "./loc.js";

const {
hasNewline,
Expand Down Expand Up @@ -605,7 +606,7 @@ function hasEmptyBody(path, name = "body") {
);
}

function isNextLineEmptyAfterNamespace(text, node, locStart) {
function isNextLineEmptyAfterNamespace(text, node) {
let idx = locStart(node);
idx = skipEverythingButNewLine(text, idx);
idx = skipNewline(text, idx);
Expand Down

0 comments on commit 6d21c9b

Please sign in to comment.