Skip to content

Commit 6d21c9b

Browse files
authored
Use loc{Start,End} directly (#2227)
* Use `loc{Start,End}` function directly * Linting
1 parent a1ce8e7 commit 6d21c9b

5 files changed

Lines changed: 39 additions & 43 deletions

File tree

src/comments.js

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
isPreviousLineEmpty,
66
isLookupNode,
77
} from "./util.js";
8+
import { locStart, locEnd } from "./loc.js";
89

910
const {
1011
addLeadingComment,
@@ -380,16 +381,12 @@ function handleRetifComments(
380381
precedingNode,
381382
followingNode,
382383
comment,
383-
text,
384-
options
384+
text
385+
/* options */
385386
) {
386387
const isSameLineAsPrecedingNode =
387388
precedingNode &&
388-
!hasNewlineInRange(
389-
text,
390-
options.locEnd(precedingNode),
391-
options.locStart(comment)
392-
);
389+
!hasNewlineInRange(text, locEnd(precedingNode), locStart(comment));
393390

394391
if (
395392
(!precedingNode || !isSameLineAsPrecedingNode) &&
@@ -518,14 +515,14 @@ function handleFunction(text, enclosingNode, followingNode, comment, options) {
518515
let argumentsLocEnd = 0;
519516
for (let i = 0; i < enclosingNode.arguments.length; i++) {
520517
argumentsLocEnd =
521-
options.locEnd(enclosingNode.arguments[i]) > argumentsLocEnd
522-
? options.locEnd(enclosingNode.arguments[i])
518+
locEnd(enclosingNode.arguments[i]) > argumentsLocEnd
519+
? locEnd(enclosingNode.arguments[i])
523520
: argumentsLocEnd;
524521
}
525522
const commentIsBetweenArgumentsAndBody =
526523
enclosingNode.body &&
527-
options.locStart(comment) > argumentsLocEnd &&
528-
options.locEnd(comment) < options.locStart(enclosingNode.body);
524+
locStart(comment) > argumentsLocEnd &&
525+
locEnd(comment) < locStart(enclosingNode.body);
529526
const nextCharIndex = getNextNonSpaceNonCommentCharacterIndex(
530527
text,
531528
comment,
@@ -538,7 +535,7 @@ function handleFunction(text, enclosingNode, followingNode, comment, options) {
538535
commentIsBetweenArgumentsAndBody &&
539536
text.charAt(nextCharIndex) !== ")"
540537
) {
541-
if (options.locEnd(comment) < options.locStart(enclosingNode.type)) {
538+
if (locEnd(comment) < locStart(enclosingNode.type)) {
542539
// we need to store this as a dangling comment in case the type is nullable
543540
// ie function(): ?string {} - the "nullable" attribute is part of the
544541
// function node, not the type.
@@ -868,11 +865,11 @@ function hasTrailingComment(node) {
868865
return node.comments && node.comments.some((comment) => comment.trailing);
869866
}
870867

871-
function hasLeadingOwnLineComment(text, node, options) {
868+
function hasLeadingOwnLineComment(text, node) {
872869
return (
873870
node.comments &&
874871
node.comments.some(
875-
(comment) => comment.leading && hasNewline(text, options.locEnd(comment))
872+
(comment) => comment.leading && hasNewline(text, locEnd(comment))
876873
)
877874
);
878875
}
@@ -941,9 +938,7 @@ function printLeadingComment(path, print, options) {
941938
if (isBlock) {
942939
return [
943940
contents,
944-
hasNewline(options.originalText, options.locEnd(comment))
945-
? hardline
946-
: " ",
941+
hasNewline(options.originalText, locEnd(comment)) ? hardline : " ",
947942
];
948943
}
949944

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

963958
if (
964-
hasNewline(options.originalText, options.locStart(comment), {
959+
hasNewline(options.originalText, locStart(comment), {
965960
backwards: true,
966961
})
967962
) {
@@ -1015,7 +1010,7 @@ function printAllComments(path, print, options, needsSemi) {
10151010
leadingParts.push(contents);
10161011

10171012
const text = options.originalText;
1018-
if (hasNewline(text, skipNewline(text, options.locEnd(comment)))) {
1013+
if (hasNewline(text, skipNewline(text, locEnd(comment)))) {
10191014
leadingParts.push(hardline);
10201015
}
10211016
} else if (trailing) {

src/index.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
isBlockComment,
1717
} from "./comments.js";
1818
import { hasPragma, insertPragma } from "./pragma.js";
19+
import { locStart, locEnd } from "./loc.js";
1920

2021
const { join, hardline } = doc.builders;
2122

@@ -55,16 +56,12 @@ const languages = [
5556
}),
5657
];
5758

58-
const loc = (prop) => (node) => {
59-
return node.loc && node.loc[prop] && node.loc[prop].offset;
60-
};
61-
6259
const parsers = {
6360
php: {
6461
parse,
6562
astFormat: "php",
66-
locStart: loc("start"),
67-
locEnd: loc("end"),
63+
locStart,
64+
locEnd,
6865
hasPragma,
6966
},
7067
};

src/loc.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const loc = (prop) => (node) => {
2+
return node.loc && node.loc[prop] && node.loc[prop].offset;
3+
};
4+
5+
export const locStart = loc("start");
6+
export const locEnd = loc("end");

src/printer.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
hasLeadingOwnLineComment,
2929
} from "./comments.js";
3030
import pathNeedsParens from "./needs-parens.js";
31+
import { locStart, locEnd } from "./loc.js";
3132

3233
import {
3334
getLast,
@@ -1069,8 +1070,8 @@ function printLines(path, options, print, childrenAttribute = "children") {
10691070
? [
10701071
hasNewlineInRange(
10711072
options.originalText.trimEnd(),
1072-
options.locEnd(lastNode),
1073-
options.locEnd(node)
1073+
locEnd(lastNode),
1074+
locEnd(node)
10741075
)
10751076
? !(
10761077
lastNode.kind === "inline" &&
@@ -1502,7 +1503,7 @@ function printAssignmentRight(
15021503
) {
15031504
const ref = hasRef ? "&" : "";
15041505

1505-
if (hasLeadingOwnLineComment(options.originalText, rightNode, options)) {
1506+
if (hasLeadingOwnLineComment(options.originalText, rightNode)) {
15061507
return indent([hardline, ref, printedRight]);
15071508
}
15081509

@@ -1545,13 +1546,13 @@ function stringHasNewLines(node) {
15451546
return node.raw.includes("\n");
15461547
}
15471548

1548-
function isStringOnItsOwnLine(node, text, options) {
1549+
function isStringOnItsOwnLine(node, text) {
15491550
return (
15501551
(node.kind === "string" ||
15511552
(node.kind === "encapsed" &&
15521553
(node.type === "string" || node.type === "shell"))) &&
15531554
stringHasNewLines(node) &&
1554-
!hasNewline(text, options.locStart(node), { backwards: true })
1555+
!hasNewline(text, locStart(node), { backwards: true })
15551556
);
15561557
}
15571558

@@ -1632,11 +1633,7 @@ function printNode(path, options, print) {
16321633
? ""
16331634
: [
16341635
hardline,
1635-
isNextLineEmptyAfterNamespace(
1636-
options.originalText,
1637-
node,
1638-
options.locStart
1639-
)
1636+
isNextLineEmptyAfterNamespace(options.originalText, node)
16401637
? hardline
16411638
: "",
16421639
],
@@ -2039,7 +2036,7 @@ function printNode(path, options, print) {
20392036
// Multiline strings as single arguments
20402037
if (
20412038
node.arguments.length === 1 &&
2042-
isStringOnItsOwnLine(node.arguments[0], options.originalText, options)
2039+
isStringOnItsOwnLine(node.arguments[0], options.originalText)
20432040
) {
20442041
return [
20452042
print("what"),
@@ -2064,7 +2061,7 @@ function printNode(path, options, print) {
20642061
if (
20652062
!isAnonymousClassNode &&
20662063
node.arguments.length === 1 &&
2067-
isStringOnItsOwnLine(node.arguments[0], options.originalText, options)
2064+
isStringOnItsOwnLine(node.arguments[0], options.originalText)
20682065
) {
20692066
return [
20702067
"new ",
@@ -2156,7 +2153,7 @@ function printNode(path, options, print) {
21562153
node.useDie ? "die" : "exit",
21572154
"(",
21582155
node.expression
2159-
? isStringOnItsOwnLine(node.expression, options.originalText, options)
2156+
? isStringOnItsOwnLine(node.expression, options.originalText)
21602157
? print("expression")
21612158
: [indent([softline, print("expression")]), softline]
21622159
: printDanglingComments(path, options),
@@ -2199,7 +2196,7 @@ function printNode(path, options, print) {
21992196
case "eval":
22002197
return group([
22012198
"eval(",
2202-
isStringOnItsOwnLine(node.source, options.originalText, options)
2199+
isStringOnItsOwnLine(node.source, options.originalText)
22032200
? print("source")
22042201
: [indent([softline, print("source")]), softline],
22052202
")",
@@ -2388,15 +2385,15 @@ function printNode(path, options, print) {
23882385

23892386
const [firstProperty] = node.items
23902387
.filter((node) => node.kind !== "noop")
2391-
.sort((a, b) => options.locStart(a) - options.locStart(b));
2388+
.sort((a, b) => locStart(a) - locStart(b));
23922389
const isAssociative = !!(firstProperty && firstProperty.key);
23932390
const shouldBreak =
23942391
isAssociative &&
23952392
firstProperty &&
23962393
hasNewlineInRange(
23972394
options.originalText,
2398-
options.locStart(node),
2399-
options.locStart(firstProperty)
2395+
locStart(node),
2396+
locStart(firstProperty)
24002397
);
24012398

24022399
return group(

src/util.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { util as prettierUtil, version as prettierVersion } from "prettier";
2+
import { locStart } from "./loc.js";
23

34
const {
45
hasNewline,
@@ -605,7 +606,7 @@ function hasEmptyBody(path, name = "body") {
605606
);
606607
}
607608

608-
function isNextLineEmptyAfterNamespace(text, node, locStart) {
609+
function isNextLineEmptyAfterNamespace(text, node) {
609610
let idx = locStart(node);
610611
idx = skipEverythingButNewLine(text, idx);
611612
idx = skipNewline(text, idx);

0 commit comments

Comments
 (0)