Skip to content

Commit ccde31c

Browse files
committed
Fix range formatting expanding to a non-source-element node
findSiblingAncestors walked the start node's ancestor chain comparing only offsets, so it could settle on a node that isSourceElement rejects. Printing an expression as a standalone element appended a semicolon after the closing brace, and object/class methods threw outright.
1 parent 07a814e commit ccde31c

5 files changed

Lines changed: 98 additions & 6 deletions

File tree

‎src/main/range.js‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,8 @@ function dropRootParents(parents) {
2222
return parents.slice(0, index + 1);
2323
}
2424

25-
function findSiblingAncestors(
26-
startNodeAndAncestors,
27-
endNodeAndAncestors,
28-
{ locStart, locEnd },
29-
) {
25+
function findSiblingAncestors(startNodeAndAncestors, endNodeAndAncestors, opts) {
26+
const { locStart, locEnd } = opts;
3027
let [resultStartNode, ...startNodeAncestors] = startNodeAndAncestors;
3128
let [resultEndNode, ...endNodeAncestors] = endNodeAndAncestors;
3229

@@ -46,7 +43,9 @@ function findSiblingAncestors(
4643
const endNodeEnd = locEnd(resultEndNode);
4744
for (const startAncestor of dropRootParents(startNodeAncestors)) {
4845
if (locEnd(startAncestor) <= endNodeEnd) {
49-
resultStartNode = startAncestor;
46+
if (isSourceElement(opts, startAncestor)) {
47+
resultStartNode = startAncestor;
48+
}
5049
} else {
5150
break;
5251
}

‎tests/format/js/range/__snapshots__/format.test.js.snap‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,83 @@ exports[`issue-12540.js format 1`] = `
426426
: ================================================================================
427427
`;
428428
429+
exports[`issue-19795.js format 1`] = `
430+
: ====================================options=====================================
431+
: parsers: ["babel", "flow", "typescript"]
432+
: rangeEnd: 32
433+
: rangeStart: 19
434+
: printWidth: 80 (default) |
435+
: =====================================input======================================
436+
1 | beforeEach(() => {
437+
> 2 | // c
438+
| ^^^^^^
439+
> 3 | a();
440+
| ^^^^^^
441+
4 | });
442+
5 |
443+
: =====================================output=====================================
444+
: beforeEach(() => {
445+
: // c
446+
: a();
447+
: });
448+
:
449+
: ================================================================================
450+
`;
451+
452+
exports[`issue-19795-2.js format 1`] = `
453+
: ====================================options=====================================
454+
: parsers: ["babel", "flow", "typescript"]
455+
: rangeEnd: 37
456+
: rangeStart: 20
457+
: printWidth: 80 (default) |
458+
: =====================================input======================================
459+
1 | const o = {
460+
2 | m() {
461+
> 3 | // c
462+
| ^^^^^^^^
463+
> 4 | a();
464+
| ^^^^^^^^
465+
5 | },
466+
6 | };
467+
7 |
468+
: =====================================output=====================================
469+
: const o = {
470+
: m() {
471+
: // c
472+
: a();
473+
: },
474+
: };
475+
:
476+
: ================================================================================
477+
`;
478+
479+
exports[`issue-19795-3.js format 1`] = `
480+
: ====================================options=====================================
481+
: parsers: ["babel", "flow", "typescript"]
482+
: rangeEnd: 35
483+
: rangeStart: 18
484+
: printWidth: 80 (default) |
485+
: =====================================input======================================
486+
1 | class A {
487+
2 | m() {
488+
> 3 | // c
489+
| ^^^^^^^^
490+
> 4 | a();
491+
| ^^^^^^^^
492+
5 | }
493+
6 | }
494+
7 |
495+
: =====================================output=====================================
496+
: class A {
497+
: m() {
498+
: // c
499+
: a();
500+
: }
501+
: }
502+
:
503+
: ================================================================================
504+
`;
505+
429506
exports[`large-dict.js format 1`] = `
430507
: ====================================options=====================================
431508
: parsers: ["babel", "flow", "typescript"]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const o = {
2+
m() {
3+
<<<PRETTIER_RANGE_START>>> // c
4+
a();<<<PRETTIER_RANGE_END>>>
5+
},
6+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class A {
2+
m() {
3+
<<<PRETTIER_RANGE_START>>> // c
4+
a();<<<PRETTIER_RANGE_END>>>
5+
}
6+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
beforeEach(() => {
2+
<<<PRETTIER_RANGE_START>>> // c
3+
a();<<<PRETTIER_RANGE_END>>>
4+
});

0 commit comments

Comments
 (0)