refactor(range): minor cleanup (#8421)
This commit is contained in:
1 parent
507c3403e3
commit
7f2ba08fb0
2 files changed
+26
-38
No files matched your search
+1
-2
@@ -175,8 +175,7 @@ function formatRange(text, opts) {
|
||||
const { ast } = parsed;
|
||||
text = parsed.text;
|
||||
|
||||
const range = rangeUtil.calculateRange(text, opts, ast);
|
||||
const { rangeStart, rangeEnd } = range;
|
||||
const { rangeStart, rangeEnd } = rangeUtil.calculateRange(text, opts, ast);
|
||||
const rangeString = text.slice(rangeStart, rangeEnd);
|
||||
|
||||
// Try to extend the range backwards to the beginning of the line.
|
||||
|
||||
+25
-36
@@ -43,32 +43,30 @@ function findSiblingAncestors(startNodeAndParents, endNodeAndParents, opts) {
|
||||
};
|
||||
}
|
||||
|
||||
function findNodeAtOffset(node, offset, options, predicate, parentNodes) {
|
||||
predicate = predicate || (() => true);
|
||||
parentNodes = parentNodes || [];
|
||||
const start = options.locStart(node, options.locStart);
|
||||
const end = options.locEnd(node, options.locEnd);
|
||||
if (start <= offset && offset <= end) {
|
||||
for (const childNode of comments.getSortedChildNodes(node, options)) {
|
||||
const childResult = findNodeAtOffset(
|
||||
childNode,
|
||||
offset,
|
||||
options,
|
||||
predicate,
|
||||
[node].concat(parentNodes)
|
||||
);
|
||||
if (childResult) {
|
||||
return childResult;
|
||||
}
|
||||
}
|
||||
function findNodeAtOffset(node, offset, options, predicate, parentNodes = []) {
|
||||
if (offset < options.locStart(node) || offset > options.locEnd(node)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (predicate(node)) {
|
||||
return {
|
||||
node,
|
||||
parentNodes,
|
||||
};
|
||||
for (const childNode of comments.getSortedChildNodes(node, options)) {
|
||||
const childResult = findNodeAtOffset(
|
||||
childNode,
|
||||
offset,
|
||||
options,
|
||||
predicate,
|
||||
[node, ...parentNodes]
|
||||
);
|
||||
if (childResult) {
|
||||
return childResult;
|
||||
}
|
||||
}
|
||||
|
||||
if (!predicate || predicate(node)) {
|
||||
return {
|
||||
node,
|
||||
parentNodes,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// See https://www.ecma-international.org/ecma-262/5.1/#sec-A.5
|
||||
@@ -166,7 +164,7 @@ function calculateRange(text, opts, ast) {
|
||||
endNonWhitespace > opts.rangeStart;
|
||||
--endNonWhitespace
|
||||
) {
|
||||
if (text[endNonWhitespace - 1].match(/\S/)) {
|
||||
if (/\S/.test(text[endNonWhitespace - 1])) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -191,24 +189,15 @@ function calculateRange(text, opts, ast) {
|
||||
};
|
||||
}
|
||||
|
||||
const siblingAncestors = findSiblingAncestors(
|
||||
const { startNode, endNode } = findSiblingAncestors(
|
||||
startNodeAndParents,
|
||||
endNodeAndParents,
|
||||
opts
|
||||
);
|
||||
const { startNode, endNode } = siblingAncestors;
|
||||
const rangeStart = Math.min(
|
||||
opts.locStart(startNode, opts.locStart),
|
||||
opts.locStart(endNode, opts.locStart)
|
||||
);
|
||||
const rangeEnd = Math.max(
|
||||
opts.locEnd(startNode, opts.locEnd),
|
||||
opts.locEnd(endNode, opts.locEnd)
|
||||
);
|
||||
|
||||
return {
|
||||
rangeStart,
|
||||
rangeEnd,
|
||||
rangeStart: Math.min(opts.locStart(startNode), opts.locStart(endNode)),
|
||||
rangeEnd: Math.max(opts.locEnd(startNode), opts.locEnd(endNode)),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user