Skip to content

Commit

Permalink
Remove legacy code (#2229)
Browse files Browse the repository at this point in the history
* Remove `getNextNonSpaceNonCommentCharacterIndex`

* `isNextLineEmpty`

* `isPreviousLineEmpty`

* Fix
  • Loading branch information
fisker authored Aug 30, 2023
1 parent c80e8da commit e0530b1
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 94 deletions.
57 changes: 30 additions & 27 deletions src/comments.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { util as prettierUtil, doc } from "prettier";
import {
getNextNonSpaceNonCommentCharacterIndex,
isNextLineEmpty,
isPreviousLineEmpty,
isLookupNode,
} from "./util.js";
import { isLookupNode } from "./util.js";
import { locStart, locEnd } from "./loc.js";

const {
Expand All @@ -14,6 +9,9 @@ const {
skipNewline,
hasNewline,
hasNewlineInRange,
getNextNonSpaceNonCommentCharacterIndex,
isNextLineEmpty,
isPreviousLineEmpty,
} = prettierUtil;
const { join, indent, hardline, cursor, lineSuffix, breakParent } =
doc.builders;
Expand Down Expand Up @@ -275,13 +273,12 @@ function handleLastFunctionArgComments(
precedingNode,
enclosingNode,
followingNode,
comment,
options
comment
/* options */
) {
const nextCharIndex = getNextNonSpaceNonCommentCharacterIndex(
text,
comment,
options
locEnd(comment)
);
const nextCharacter = text.charAt(nextCharIndex);

Expand Down Expand Up @@ -330,17 +327,16 @@ function handleIfStatementComments(
precedingNode,
enclosingNode,
followingNode,
comment,
options
comment
/* options */
) {
if (!enclosingNode || enclosingNode.kind !== "if" || !followingNode) {
return false;
}

const nextCharIndex = getNextNonSpaceNonCommentCharacterIndex(
text,
comment,
options
locEnd(comment)
);
const nextCharacter = text.charAt(nextCharIndex);

Expand Down Expand Up @@ -504,7 +500,13 @@ function handleClassComments(enclosingNode, followingNode, comment) {
return false;
}

function handleFunction(text, enclosingNode, followingNode, comment, options) {
function handleFunction(
text,
enclosingNode,
followingNode,
comment
/* options */
) {
if (
enclosingNode &&
(enclosingNode.kind === "function" || enclosingNode.kind === "method")
Expand All @@ -525,8 +527,7 @@ function handleFunction(text, enclosingNode, followingNode, comment, options) {
locEnd(comment) < locStart(enclosingNode.body);
const nextCharIndex = getNextNonSpaceNonCommentCharacterIndex(
text,
comment,
options
locEnd(comment)
);
// we additionally need to check if this isn't a trailing argument comment,
// by checking the next character isn't ")"
Expand Down Expand Up @@ -606,11 +607,15 @@ function handleHalt(precedingNode, enclosingNode, followingNode, comment) {
return false;
}

function handleCommentInEmptyParens(text, enclosingNode, comment, options) {
function handleCommentInEmptyParens(
text,
enclosingNode,
comment
/* options */
) {
const nextCharIndex = getNextNonSpaceNonCommentCharacterIndex(
text,
comment,
options
locEnd(comment)
);

if (text.charAt(nextCharIndex) !== ")") {
Expand Down Expand Up @@ -787,8 +792,8 @@ function handleWhileComments(
precedingNode,
enclosingNode,
followingNode,
comment,
options
comment
/* options */
) {
if (!enclosingNode || enclosingNode.kind !== "while" || !followingNode) {
return false;
Expand All @@ -801,8 +806,7 @@ function handleWhileComments(

const nextCharIndex = getNextNonSpaceNonCommentCharacterIndex(
text,
comment,
options
locEnd(comment)
);
const nextCharacter = text.charAt(nextCharIndex);

Expand Down Expand Up @@ -884,7 +888,7 @@ function printComments(comments, options) {
parts.push(hardline);
}
if (
isNextLineEmpty(options.originalText, comment, options) &&
isNextLineEmpty(options.originalText, locEnd(comment)) &&
!isLastComment
) {
parts.push(hardline);
Expand Down Expand Up @@ -974,8 +978,7 @@ function printTrailingComment(path, print, options) {

const isLineBeforeEmpty = isPreviousLineEmpty(
options.originalText,
comment,
options
locStart(comment)
);

return lineSuffix([hardline, isLineBeforeEmpty ? hardline : "", contents]);
Expand Down
63 changes: 33 additions & 30 deletions src/printer.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,4 @@
import { util as prettierUtil, doc } from "prettier";
const {
breakParent,
join,
line,
lineSuffix,
group,
conditionalGroup,
indent,
dedent,
ifBreak,
hardline,
softline,
literalline,
align,
dedentToRoot,
} = doc.builders;
const { willBreak } = doc.utils;
const { isNextLineEmptyAfterIndex, hasNewline, hasNewlineInRange } =
prettierUtil;
import {
printAllComments,
hasTrailingComment,
Expand Down Expand Up @@ -57,10 +38,33 @@ import {
isReferenceLikeNode,
getNextNode,
normalizeMagicMethodName,
} from "./util.js";

const {
breakParent,
join,
line,
lineSuffix,
group,
conditionalGroup,
indent,
dedent,
ifBreak,
hardline,
softline,
literalline,
align,
dedentToRoot,
} = doc.builders;
const { willBreak } = doc.utils;
const {
isNextLineEmptyAfterIndex,
hasNewline,
hasNewlineInRange,
getNextNonSpaceNonCommentCharacterIndex,
isNextLineEmpty,
isPreviousLineEmpty,
} from "./util.js";
} = prettierUtil;

function isMinVersion(actualVersion, requiredVersion) {
return parseFloat(actualVersion) >= parseFloat(requiredVersion);
Expand Down Expand Up @@ -197,8 +201,7 @@ function printMemberChain(path, options, print) {
const { originalText } = options;
const nextCharIndex = getNextNonSpaceNonCommentCharacterIndex(
originalText,
node,
options
locEnd(node)
);
const nextChar = originalText.charAt(nextCharIndex);

Expand All @@ -212,7 +215,7 @@ function printMemberChain(path, options, print) {
);
}

return isNextLineEmpty(originalText, node, options);
return isNextLineEmpty(originalText, locEnd(node));
}

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

if (isLast) {
// do nothing
} else if (isNextLineEmpty(options.originalText, arg, options)) {
} else if (isNextLineEmpty(options.originalText, locEnd(arg))) {
if (isFirst) {
hasEmptyLineFollowingFirstArg = true;
}
Expand Down Expand Up @@ -859,7 +862,7 @@ function printArrayItems(path, options, print) {

separatorParts = [",", line];

if (node && isNextLineEmpty(options.originalText, node, options)) {
if (node && isNextLineEmpty(options.originalText, locEnd(node))) {
separatorParts.push(softline);
}
}, "items");
Expand Down Expand Up @@ -941,7 +944,7 @@ function printLines(path, options, print, childrenAttribute = "children") {
printedPath,
canPrintBlankLine ? hardline : "",
canPrintBlankLine &&
isNextLineEmpty(options.originalText, childNode, options)
isNextLineEmpty(options.originalText, locEnd(childNode))
? hardline
: "",
];
Expand Down Expand Up @@ -1081,7 +1084,7 @@ function printLines(path, options, print, childrenAttribute = "children") {
? hardline
: ""
: " ",
isNextLineEmpty(options.originalText, lastNode, options)
isNextLineEmpty(options.originalText, locEnd(lastNode))
? hardline
: "",
]
Expand All @@ -1107,7 +1110,7 @@ function printStatements(path, options, print, childrenAttribute) {
if (!isLastStatement(path)) {
parts.push(hardline);

if (isNextLineEmpty(options.originalText, path.node, options)) {
if (isNextLineEmpty(options.originalText, locEnd(path.node))) {
parts.push(hardline);
}
}
Expand Down Expand Up @@ -1854,7 +1857,7 @@ function printNode(path, options, print) {

if (hasDanglingComments(node)) {
parts.push(
isNextLineEmpty(options.originalText, node.body, options)
isNextLineEmpty(options.originalText, locEnd(node.body))
? hardline
: "",
printDanglingComments(path, options, true),
Expand Down Expand Up @@ -2828,7 +2831,7 @@ function printNode(path, options, print) {
const body = print("body");
const maybeEmptyLineBetweenArms =
!path.isFirst &&
isPreviousLineEmpty(options.originalText, armNode, options)
isPreviousLineEmpty(options.originalText, locStart(armNode))
? hardline
: "";

Expand Down
39 changes: 2 additions & 37 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,7 @@
import { util as prettierUtil, version as prettierVersion } from "prettier";
import { util as prettierUtil } from "prettier";
import { locStart } from "./loc.js";

const {
hasNewline,
skipEverythingButNewLine,
skipNewline,
isNextLineEmpty: _isNextLineEmpty,
isPreviousLineEmpty: _isPreviousLineEmpty,
getNextNonSpaceNonCommentCharacterIndex:
_getNextNonSpaceNonCommentCharacterIndex,
} = prettierUtil;

function lookupIfPrettier2(options, prop) {
return parseInt(prettierVersion[0]) > 1 ? options[prop] : options;
}

function isPreviousLineEmpty(text, node, options) {
return _isPreviousLineEmpty(
text,
node,
lookupIfPrettier2(options, "locStart")
);
}

function isNextLineEmpty(text, node, options) {
return _isNextLineEmpty(text, node, lookupIfPrettier2(options, "locEnd"));
}

function getNextNonSpaceNonCommentCharacterIndex(text, node, options) {
return _getNextNonSpaceNonCommentCharacterIndex(
text,
node,
lookupIfPrettier2(options, "locEnd")
);
}
const { hasNewline, skipEverythingButNewLine, skipNewline } = prettierUtil;

function printNumber(rawNumber) {
return (
Expand Down Expand Up @@ -711,7 +679,4 @@ export {
getAncestorNode,
getNextNode,
normalizeMagicMethodName,
isPreviousLineEmpty,
isNextLineEmpty,
getNextNonSpaceNonCommentCharacterIndex,
};

0 comments on commit e0530b1

Please sign in to comment.