Range Formatter: Remove Program/File from valid Ancestor (#2248)

* Remove Program/File from valid Ancestor

Fix edge range formatting entire file.

* Fix lint with `yarn lint -- --fix`

See https://travis-ci.org/prettier/prettier/jobs/246569943#L242-L260
master
Cyril Junod 2017-06-24 18:30:27 +02:00 committed by Joseph Frazier
parent 811885d635
commit b0413fc1fe
4 changed files with 68 additions and 2 deletions

View File

@ -104,7 +104,11 @@ function findSiblingAncestors(startNodeAndParents, endNodeAndParents) {
let resultEndNode = endNodeAndParents.node;
for (const endParent of endNodeAndParents.parentNodes) {
if (util.locStart(endParent) >= util.locStart(startNodeAndParents.node)) {
if (
endParent.type !== "Program" &&
endParent.type !== "File" &&
util.locStart(endParent) >= util.locStart(startNodeAndParents.node)
) {
resultEndNode = endParent;
} else {
break;
@ -112,7 +116,11 @@ function findSiblingAncestors(startNodeAndParents, endNodeAndParents) {
}
for (const startParent of startNodeAndParents.parentNodes) {
if (util.locEnd(startParent) <= util.locEnd(endNodeAndParents.node)) {
if (
startParent.type !== "Program" &&
startParent.type !== "File" &&
util.locEnd(startParent) <= util.locEnd(endNodeAndParents.node)
) {
resultStartNode = startParent;
} else {
break;

View File

@ -176,6 +176,46 @@ function ugly ( {a=1, b = 2 } ) {
`;
exports[`range-end.js 1`] = `
// Unchanged
call(
1, 2,3
);
call(
1, 2,3
);~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Unchanged
call(
1, 2,3
);
call(1, 2, 3);
`;
exports[`range-start.js 1`] = `
call(
1, 2,3
);
// Unchanged
call(
1, 2,3
);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
call(1, 2, 3);
// Unchanged
call(
1, 2,3
);
`;
exports[`try-catch.js 1`] = `
try {}
catch (err) {}

9
tests/range/range-end.js Normal file
View File

@ -0,0 +1,9 @@
// Unchanged
call(
1, 2,3
);
<<<PRETTIER_RANGE_START>>>
call(
1, 2,3
);<<<PRETTIER_RANGE_END>>>

View File

@ -0,0 +1,9 @@
<<<PRETTIER_RANGE_START>>>call(
1, 2,3
);
<<<PRETTIER_RANGE_END>>>
// Unchanged
call(
1, 2,3
);