Skip to content

Commit e0530b1

Browse files
authored
Remove legacy code (#2229)
* Remove `getNextNonSpaceNonCommentCharacterIndex` * `isNextLineEmpty` * `isPreviousLineEmpty` * Fix
1 parent c80e8da commit e0530b1

File tree

3 files changed

+65
-94
lines changed

3 files changed

+65
-94
lines changed

src/comments.js

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import { util as prettierUtil, doc } from "prettier";
2-
import {
3-
getNextNonSpaceNonCommentCharacterIndex,
4-
isNextLineEmpty,
5-
isPreviousLineEmpty,
6-
isLookupNode,
7-
} from "./util.js";
2+
import { isLookupNode } from "./util.js";
83
import { locStart, locEnd } from "./loc.js";
94

105
const {
@@ -14,6 +9,9 @@ const {
149
skipNewline,
1510
hasNewline,
1611
hasNewlineInRange,
12+
getNextNonSpaceNonCommentCharacterIndex,
13+
isNextLineEmpty,
14+
isPreviousLineEmpty,
1715
} = prettierUtil;
1816
const { join, indent, hardline, cursor, lineSuffix, breakParent } =
1917
doc.builders;
@@ -275,13 +273,12 @@ function handleLastFunctionArgComments(
275273
precedingNode,
276274
enclosingNode,
277275
followingNode,
278-
comment,
279-
options
276+
comment
277+
/* options */
280278
) {
281279
const nextCharIndex = getNextNonSpaceNonCommentCharacterIndex(
282280
text,
283-
comment,
284-
options
281+
locEnd(comment)
285282
);
286283
const nextCharacter = text.charAt(nextCharIndex);
287284

@@ -330,17 +327,16 @@ function handleIfStatementComments(
330327
precedingNode,
331328
enclosingNode,
332329
followingNode,
333-
comment,
334-
options
330+
comment
331+
/* options */
335332
) {
336333
if (!enclosingNode || enclosingNode.kind !== "if" || !followingNode) {
337334
return false;
338335
}
339336

340337
const nextCharIndex = getNextNonSpaceNonCommentCharacterIndex(
341338
text,
342-
comment,
343-
options
339+
locEnd(comment)
344340
);
345341
const nextCharacter = text.charAt(nextCharIndex);
346342

@@ -504,7 +500,13 @@ function handleClassComments(enclosingNode, followingNode, comment) {
504500
return false;
505501
}
506502

507-
function handleFunction(text, enclosingNode, followingNode, comment, options) {
503+
function handleFunction(
504+
text,
505+
enclosingNode,
506+
followingNode,
507+
comment
508+
/* options */
509+
) {
508510
if (
509511
enclosingNode &&
510512
(enclosingNode.kind === "function" || enclosingNode.kind === "method")
@@ -525,8 +527,7 @@ function handleFunction(text, enclosingNode, followingNode, comment, options) {
525527
locEnd(comment) < locStart(enclosingNode.body);
526528
const nextCharIndex = getNextNonSpaceNonCommentCharacterIndex(
527529
text,
528-
comment,
529-
options
530+
locEnd(comment)
530531
);
531532
// we additionally need to check if this isn't a trailing argument comment,
532533
// by checking the next character isn't ")"
@@ -606,11 +607,15 @@ function handleHalt(precedingNode, enclosingNode, followingNode, comment) {
606607
return false;
607608
}
608609

609-
function handleCommentInEmptyParens(text, enclosingNode, comment, options) {
610+
function handleCommentInEmptyParens(
611+
text,
612+
enclosingNode,
613+
comment
614+
/* options */
615+
) {
610616
const nextCharIndex = getNextNonSpaceNonCommentCharacterIndex(
611617
text,
612-
comment,
613-
options
618+
locEnd(comment)
614619
);
615620

616621
if (text.charAt(nextCharIndex) !== ")") {
@@ -787,8 +792,8 @@ function handleWhileComments(
787792
precedingNode,
788793
enclosingNode,
789794
followingNode,
790-
comment,
791-
options
795+
comment
796+
/* options */
792797
) {
793798
if (!enclosingNode || enclosingNode.kind !== "while" || !followingNode) {
794799
return false;
@@ -801,8 +806,7 @@ function handleWhileComments(
801806

802807
const nextCharIndex = getNextNonSpaceNonCommentCharacterIndex(
803808
text,
804-
comment,
805-
options
809+
locEnd(comment)
806810
);
807811
const nextCharacter = text.charAt(nextCharIndex);
808812

@@ -884,7 +888,7 @@ function printComments(comments, options) {
884888
parts.push(hardline);
885889
}
886890
if (
887-
isNextLineEmpty(options.originalText, comment, options) &&
891+
isNextLineEmpty(options.originalText, locEnd(comment)) &&
888892
!isLastComment
889893
) {
890894
parts.push(hardline);
@@ -974,8 +978,7 @@ function printTrailingComment(path, print, options) {
974978

975979
const isLineBeforeEmpty = isPreviousLineEmpty(
976980
options.originalText,
977-
comment,
978-
options
981+
locStart(comment)
979982
);
980983

981984
return lineSuffix([hardline, isLineBeforeEmpty ? hardline : "", contents]);

src/printer.js

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,4 @@
11
import { util as prettierUtil, doc } from "prettier";
2-
const {
3-
breakParent,
4-
join,
5-
line,
6-
lineSuffix,
7-
group,
8-
conditionalGroup,
9-
indent,
10-
dedent,
11-
ifBreak,
12-
hardline,
13-
softline,
14-
literalline,
15-
align,
16-
dedentToRoot,
17-
} = doc.builders;
18-
const { willBreak } = doc.utils;
19-
const { isNextLineEmptyAfterIndex, hasNewline, hasNewlineInRange } =
20-
prettierUtil;
212
import {
223
printAllComments,
234
hasTrailingComment,
@@ -57,10 +38,33 @@ import {
5738
isReferenceLikeNode,
5839
getNextNode,
5940
normalizeMagicMethodName,
41+
} from "./util.js";
42+
43+
const {
44+
breakParent,
45+
join,
46+
line,
47+
lineSuffix,
48+
group,
49+
conditionalGroup,
50+
indent,
51+
dedent,
52+
ifBreak,
53+
hardline,
54+
softline,
55+
literalline,
56+
align,
57+
dedentToRoot,
58+
} = doc.builders;
59+
const { willBreak } = doc.utils;
60+
const {
61+
isNextLineEmptyAfterIndex,
62+
hasNewline,
63+
hasNewlineInRange,
6064
getNextNonSpaceNonCommentCharacterIndex,
6165
isNextLineEmpty,
6266
isPreviousLineEmpty,
63-
} from "./util.js";
67+
} = prettierUtil;
6468

6569
function isMinVersion(actualVersion, requiredVersion) {
6670
return parseFloat(actualVersion) >= parseFloat(requiredVersion);
@@ -197,8 +201,7 @@ function printMemberChain(path, options, print) {
197201
const { originalText } = options;
198202
const nextCharIndex = getNextNonSpaceNonCommentCharacterIndex(
199203
originalText,
200-
node,
201-
options
204+
locEnd(node)
202205
);
203206
const nextChar = originalText.charAt(nextCharIndex);
204207

@@ -212,7 +215,7 @@ function printMemberChain(path, options, print) {
212215
);
213216
}
214217

215-
return isNextLineEmpty(originalText, node, options);
218+
return isNextLineEmpty(originalText, locEnd(node));
216219
}
217220

218221
function traverse(path) {
@@ -592,7 +595,7 @@ function printArgumentsList(path, options, print, argumentsKey = "arguments") {
592595

593596
if (isLast) {
594597
// do nothing
595-
} else if (isNextLineEmpty(options.originalText, arg, options)) {
598+
} else if (isNextLineEmpty(options.originalText, locEnd(arg))) {
596599
if (isFirst) {
597600
hasEmptyLineFollowingFirstArg = true;
598601
}
@@ -859,7 +862,7 @@ function printArrayItems(path, options, print) {
859862

860863
separatorParts = [",", line];
861864

862-
if (node && isNextLineEmpty(options.originalText, node, options)) {
865+
if (node && isNextLineEmpty(options.originalText, locEnd(node))) {
863866
separatorParts.push(softline);
864867
}
865868
}, "items");
@@ -941,7 +944,7 @@ function printLines(path, options, print, childrenAttribute = "children") {
941944
printedPath,
942945
canPrintBlankLine ? hardline : "",
943946
canPrintBlankLine &&
944-
isNextLineEmpty(options.originalText, childNode, options)
947+
isNextLineEmpty(options.originalText, locEnd(childNode))
945948
? hardline
946949
: "",
947950
];
@@ -1081,7 +1084,7 @@ function printLines(path, options, print, childrenAttribute = "children") {
10811084
? hardline
10821085
: ""
10831086
: " ",
1084-
isNextLineEmpty(options.originalText, lastNode, options)
1087+
isNextLineEmpty(options.originalText, locEnd(lastNode))
10851088
? hardline
10861089
: "",
10871090
]
@@ -1107,7 +1110,7 @@ function printStatements(path, options, print, childrenAttribute) {
11071110
if (!isLastStatement(path)) {
11081111
parts.push(hardline);
11091112

1110-
if (isNextLineEmpty(options.originalText, path.node, options)) {
1113+
if (isNextLineEmpty(options.originalText, locEnd(path.node))) {
11111114
parts.push(hardline);
11121115
}
11131116
}
@@ -1854,7 +1857,7 @@ function printNode(path, options, print) {
18541857

18551858
if (hasDanglingComments(node)) {
18561859
parts.push(
1857-
isNextLineEmpty(options.originalText, node.body, options)
1860+
isNextLineEmpty(options.originalText, locEnd(node.body))
18581861
? hardline
18591862
: "",
18601863
printDanglingComments(path, options, true),
@@ -2828,7 +2831,7 @@ function printNode(path, options, print) {
28282831
const body = print("body");
28292832
const maybeEmptyLineBetweenArms =
28302833
!path.isFirst &&
2831-
isPreviousLineEmpty(options.originalText, armNode, options)
2834+
isPreviousLineEmpty(options.originalText, locStart(armNode))
28322835
? hardline
28332836
: "";
28342837

src/util.js

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,7 @@
1-
import { util as prettierUtil, version as prettierVersion } from "prettier";
1+
import { util as prettierUtil } from "prettier";
22
import { locStart } from "./loc.js";
33

4-
const {
5-
hasNewline,
6-
skipEverythingButNewLine,
7-
skipNewline,
8-
isNextLineEmpty: _isNextLineEmpty,
9-
isPreviousLineEmpty: _isPreviousLineEmpty,
10-
getNextNonSpaceNonCommentCharacterIndex:
11-
_getNextNonSpaceNonCommentCharacterIndex,
12-
} = prettierUtil;
13-
14-
function lookupIfPrettier2(options, prop) {
15-
return parseInt(prettierVersion[0]) > 1 ? options[prop] : options;
16-
}
17-
18-
function isPreviousLineEmpty(text, node, options) {
19-
return _isPreviousLineEmpty(
20-
text,
21-
node,
22-
lookupIfPrettier2(options, "locStart")
23-
);
24-
}
25-
26-
function isNextLineEmpty(text, node, options) {
27-
return _isNextLineEmpty(text, node, lookupIfPrettier2(options, "locEnd"));
28-
}
29-
30-
function getNextNonSpaceNonCommentCharacterIndex(text, node, options) {
31-
return _getNextNonSpaceNonCommentCharacterIndex(
32-
text,
33-
node,
34-
lookupIfPrettier2(options, "locEnd")
35-
);
36-
}
4+
const { hasNewline, skipEverythingButNewLine, skipNewline } = prettierUtil;
375

386
function printNumber(rawNumber) {
397
return (
@@ -711,7 +679,4 @@ export {
711679
getAncestorNode,
712680
getNextNode,
713681
normalizeMagicMethodName,
714-
isPreviousLineEmpty,
715-
isNextLineEmpty,
716-
getNextNonSpaceNonCommentCharacterIndex,
717682
};

0 commit comments

Comments
 (0)