Test: Fix indexes calculation (#9152)

* Test: Fix indexes calculation

* Simplify logic

* Update snapshots
This commit is contained in:
fisker Cheung authored and GitHub committed 2020-09-17 07:31:29 +08:00
1 parent 549090a5ba
commit 94ef5f5683
15 files changed
+231 -37

No files matched your search

@@ -2,6 +2,7 @@
exports[`test.css format 1`] = `
====================================options=====================================
cursorOffset: 19
parsers: ["css"]
printWidth: 80
| printWidth
@@ -4,6 +4,7 @@ exports[`issue2267.css format 1`] = `
====================================options=====================================
parsers: ["css"]
printWidth: 80
rangeEnd: 4
| | printWidth
=====================================input======================================
> 1 | .x{}
@@ -4,6 +4,7 @@ exports[`issue2296.graphql format 1`] = `
====================================options=====================================
parsers: ["graphql"]
printWidth: 80
rangeEnd: 3
| | printWidth
=====================================input======================================
> 1 | {NPC{life}}
@@ -2,6 +2,7 @@
exports[`snippet: cursor-1.js format 1`] = `
====================================options=====================================
cursorOffset: 27
parsers: ["babel"]
printWidth: 80
| printWidth
@@ -18,8 +19,11 @@ printWidth: 80
exports[`snippet: cursor-and-range.js format 1`] = `
====================================options=====================================
cursorOffset: 22
parsers: ["babel"]
printWidth: 80
rangeEnd: 27
rangeStart: 11
| | printWidth
=====================================input======================================
1 | 
@@ -46,6 +50,8 @@ exports[`snippet: range-1.js format 1`] = `
====================================options=====================================
parsers: ["babel"]
printWidth: 80
rangeEnd: 27
rangeStart: 11
| | printWidth
=====================================input======================================
1 | 
@@ -72,8 +78,11 @@ let x
exports[`snippet: range-and-cursor-1.js format 1`] = `
====================================options=====================================
cursorOffset: 23
parsers: ["babel"]
printWidth: 80
rangeEnd: 28
rangeStart: 12
| | printWidth
=====================================input======================================
1 | 
@@ -2,6 +2,7 @@
exports[`comments-1.js format 1`] = `
====================================options=====================================
cursorOffset: 7
parsers: ["babel", "typescript", "flow"]
printWidth: 80
| printWidth
@@ -22,6 +23,7 @@ function ehllooo() {
exports[`comments-2.js format 1`] = `
====================================options=====================================
cursorOffset: 0
parsers: ["babel", "typescript", "flow"]
printWidth: 80
| printWidth
@@ -41,6 +43,7 @@ const y = 5;
exports[`comments-3.js format 1`] = `
====================================options=====================================
cursorOffset: 1
parsers: ["babel", "typescript", "flow"]
printWidth: 80
| printWidth
@@ -59,6 +62,7 @@ const y = 5;
exports[`comments-4.js format 1`] = `
====================================options=====================================
cursorOffset: 44
parsers: ["babel", "typescript", "flow"]
printWidth: 80
| printWidth
@@ -79,6 +83,7 @@ const y = 5;
exports[`cursor-0.js format 1`] = `
====================================options=====================================
cursorOffset: 27
parsers: ["babel", "typescript", "flow"]
printWidth: 80
| printWidth
@@ -95,6 +100,7 @@ printWidth: 80
exports[`cursor-1.js format 1`] = `
====================================options=====================================
cursorOffset: 26
parsers: ["babel", "typescript", "flow"]
printWidth: 80
| printWidth
@@ -111,6 +117,7 @@ printWidth: 80
exports[`cursor-2.js format 1`] = `
====================================options=====================================
cursorOffset: 6
parsers: ["babel", "typescript", "flow"]
printWidth: 80
| printWidth
@@ -126,6 +133,7 @@ foo<|>(bar);
exports[`cursor-3.js format 1`] = `
====================================options=====================================
cursorOffset: 4
parsers: ["babel", "typescript", "flow"]
printWidth: 80
| printWidth
@@ -144,6 +152,7 @@ printWidth: 80
exports[`cursor-4.js format 1`] = `
====================================options=====================================
cursorOffset: 19
parsers: ["babel", "typescript", "flow"]
printWidth: 80
| printWidth
@@ -166,6 +175,7 @@ const y = 5;
exports[`cursor-5.js format 1`] = `
====================================options=====================================
cursorOffset: 13
parsers: ["babel", "typescript", "flow"]
printWidth: 80
| printWidth
@@ -180,6 +190,7 @@ const /* h<|>i */ y = 5;
exports[`cursor-6.js format 1`] = `
====================================options=====================================
cursorOffset: 20
parsers: ["babel", "typescript", "flow"]
printWidth: 80
| printWidth
@@ -194,6 +205,7 @@ const y /* h<|>i */ = 5;
exports[`cursor-7.js format 1`] = `
====================================options=====================================
cursorOffset: 12
parsers: ["babel", "typescript", "flow"]
printWidth: 80
| printWidth
@@ -213,6 +225,7 @@ const z = 9;
exports[`cursor-8.js format 1`] = `
====================================options=====================================
cursorOffset: 6
parsers: ["babel", "typescript", "flow"]
printWidth: 80
| printWidth
@@ -226,6 +239,7 @@ func<|>tion banana() {}
exports[`cursor-9.js format 1`] = `
====================================options=====================================
cursorOffset: 26
parsers: ["babel", "typescript", "flow"]
printWidth: 80
| printWidth
@@ -239,6 +253,7 @@ thisWillBeFormatted<|>(2, 3);
exports[`cursor-10.js format 1`] = `
====================================options=====================================
cursorOffset: 16
parsers: ["babel", "typescript", "flow"]
printWidth: 80
| printWidth
@@ -258,6 +273,7 @@ const y = 5;
exports[`file-start-with-comment-1.js format 1`] = `
====================================options=====================================
cursorOffset: 5
parsers: ["babel", "typescript", "flow"]
printWidth: 80
| printWidth
@@ -274,6 +290,7 @@ haha();
exports[`file-start-with-comment-2.js format 1`] = `
====================================options=====================================
cursorOffset: 16
parsers: ["babel", "typescript", "flow"]
printWidth: 80
| printWidth
@@ -290,8 +307,11 @@ haha()<|>;
exports[`range-0.js format 1`] = `
====================================options=====================================
cursorOffset: 56
parsers: ["babel", "typescript", "flow"]
printWidth: 80
rangeEnd: 72
rangeStart: 31
| | printWidth
=====================================input======================================
1 | thisWontBeFormatted ( 1 ,3)
@@ -315,8 +335,11 @@ thisWontBeFormatted ( 1 ,3)
exports[`range-1.js format 1`] = `
====================================options=====================================
cursorOffset: 64
parsers: ["babel", "typescript", "flow"]
printWidth: 80
rangeEnd: 72
rangeStart: 31
| | printWidth
=====================================input======================================
1 | thisWontBeFormatted ( 1 ,3)
@@ -340,8 +363,11 @@ thisWontBeFormatted ( 1 ,3)
exports[`range-2.js format 1`] = `
====================================options=====================================
cursorOffset: 67
parsers: ["babel", "typescript", "flow"]
printWidth: 80
rangeEnd: 72
rangeStart: 31
| | printWidth
=====================================input======================================
1 | thisWontBeFormatted ( 1 ,3)
@@ -365,18 +391,20 @@ thisWontBeFormatted ( 1 ,3)
exports[`range-3.js format 1`] = `
====================================options=====================================
cursorOffset: 20
parsers: ["babel", "typescript", "flow"]
printWidth: 80
rangeEnd: 72
rangeStart: 30
| | printWidth
=====================================input======================================
> 1 | thisWontBeFormatted <|> ( 1 ,3)
| ^
1 | thisWontBeFormatted <|> ( 1 ,3)
> 2 |
| ^
> 3 | thisWillBeFormatted (2 ,3, )
| ^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 4 |
| ^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 5 | thisWontBeFormatted (2, 90 ,)
| ^
6 |
@@ -392,8 +420,11 @@ thisWontBeFormatted <|> ( 1 ,3)
exports[`range-4.js format 1`] = `
====================================options=====================================
cursorOffset: 101
parsers: ["babel", "typescript", "flow"]
printWidth: 80
rangeEnd: 75
rangeStart: 31
| | printWidth
=====================================input======================================
1 | thisWontBeFormatted ( 1 ,3)
+2 -2
View File
@@ -1,5 +1,5 @@
thisWontBeFormatted <|> ( 1 ,3<<<PRETTIER_RANGE_START>>>)
thisWontBeFormatted <|> ( 1 ,3)
<<<PRETTIER_RANGE_START>>>
thisWillBeFormatted (2 ,3, )
<<<PRETTIER_RANGE_END>>> thisWontBeFormatted (2, 90 ,)
@@ -2,6 +2,7 @@
exports[`cursor-without-pragma.js - {"requirePragma":true} format 1`] = `
====================================options=====================================
cursorOffset: 27
parsers: ["babel", "typescript", "flow"]
printWidth: 80
requirePragma: true
@@ -2,6 +2,7 @@
exports[`cursor-1.js - {"endOfLine":"auto"} format 1`] = `
====================================options=====================================
cursorOffset: 26
endOfLine: "auto"
parsers: ["babel"]
printWidth: 80
@@ -19,6 +20,7 @@ printWidth: 80
exports[`cursor-1.js - {"endOfLine":"cr"} format 1`] = `
====================================options=====================================
cursorOffset: 26
endOfLine: "cr"
parsers: ["babel"]
printWidth: 80
@@ -36,6 +38,7 @@ printWidth: 80
exports[`cursor-1.js - {"endOfLine":"crlf"} format 1`] = `
====================================options=====================================
cursorOffset: 26
endOfLine: "crlf"
parsers: ["babel"]
printWidth: 80
@@ -53,6 +56,7 @@ printWidth: 80
exports[`cursor-1.js - {"endOfLine":"lf"} format 1`] = `
====================================options=====================================
cursorOffset: 26
endOfLine: "lf"
parsers: ["babel"]
printWidth: 80
@@ -70,9 +74,12 @@ printWidth: 80
exports[`cursor-and-range.js - {"endOfLine":"auto"} format 1`] = `
====================================options=====================================
cursorOffset: 21
endOfLine: "auto"
parsers: ["babel"]
printWidth: 80
rangeEnd: 26
rangeStart: 10
| | printWidth
=====================================input======================================
1 | <LF>
@@ -97,9 +104,12 @@ let x
exports[`cursor-and-range.js - {"endOfLine":"cr"} format 1`] = `
====================================options=====================================
cursorOffset: 21
endOfLine: "cr"
parsers: ["babel"]
printWidth: 80
rangeEnd: 26
rangeStart: 10
| | printWidth
=====================================input======================================
1 | <LF>
@@ -124,9 +134,12 @@ let x
exports[`cursor-and-range.js - {"endOfLine":"crlf"} format 1`] = `
====================================options=====================================
cursorOffset: 21
endOfLine: "crlf"
parsers: ["babel"]
printWidth: 80
rangeEnd: 26
rangeStart: 10
| | printWidth
=====================================input======================================
1 | <LF>
@@ -151,9 +164,12 @@ let x
exports[`cursor-and-range.js - {"endOfLine":"lf"} format 1`] = `
====================================options=====================================
cursorOffset: 21
endOfLine: "lf"
parsers: ["babel"]
printWidth: 80
rangeEnd: 26
rangeStart: 10
| | printWidth
=====================================input======================================
1 | <LF>
@@ -181,6 +197,8 @@ exports[`range-1.js - {"endOfLine":"auto"} format 1`] = `
endOfLine: "auto"
parsers: ["babel"]
printWidth: 80
rangeEnd: 26
rangeStart: 10
| | printWidth
=====================================input======================================
1 | <LF>
@@ -210,6 +228,8 @@ exports[`range-1.js - {"endOfLine":"cr"} format 1`] = `
endOfLine: "cr"
parsers: ["babel"]
printWidth: 80
rangeEnd: 26
rangeStart: 10
| | printWidth
=====================================input======================================
1 | <LF>
@@ -239,6 +259,8 @@ exports[`range-1.js - {"endOfLine":"crlf"} format 1`] = `
endOfLine: "crlf"
parsers: ["babel"]
printWidth: 80
rangeEnd: 26
rangeStart: 10
| | printWidth
=====================================input======================================
1 | <LF>
@@ -268,6 +290,8 @@ exports[`range-1.js - {"endOfLine":"lf"} format 1`] = `
endOfLine: "lf"
parsers: ["babel"]
printWidth: 80
rangeEnd: 26
rangeStart: 10
| | printWidth
=====================================input======================================
1 | <LF>
@@ -294,9 +318,12 @@ let x<LF>
exports[`range-and-cursor-1.js - {"endOfLine":"auto"} format 1`] = `
====================================options=====================================
cursorOffset: 22
endOfLine: "auto"
parsers: ["babel"]
printWidth: 80
rangeEnd: 27
rangeStart: 11
| | printWidth
=====================================input======================================
1 | <LF>
@@ -325,9 +352,12 @@ let x <LF>
exports[`range-and-cursor-1.js - {"endOfLine":"cr"} format 1`] = `
====================================options=====================================
cursorOffset: 22
endOfLine: "cr"
parsers: ["babel"]
printWidth: 80
rangeEnd: 27
rangeStart: 11
| | printWidth
=====================================input======================================
1 | <LF>
@@ -356,9 +386,12 @@ let x <CR>
exports[`range-and-cursor-1.js - {"endOfLine":"crlf"} format 1`] = `
====================================options=====================================
cursorOffset: 22
endOfLine: "crlf"
parsers: ["babel"]
printWidth: 80
rangeEnd: 27
rangeStart: 11
| | printWidth
=====================================input======================================
1 | <LF>
@@ -387,9 +420,12 @@ let x <CRLF>
exports[`range-and-cursor-1.js - {"endOfLine":"lf"} format 1`] = `
====================================options=====================================
cursorOffset: 22
endOfLine: "lf"
parsers: ["babel"]
printWidth: 80
rangeEnd: 27
rangeStart: 11
| | printWidth
=====================================input======================================
1 | <LF>
@@ -4,6 +4,8 @@ exports[`array.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
rangeEnd: 16
rangeStart: 8
| | printWidth
=====================================input======================================
1 | a = [
@@ -28,6 +30,8 @@ exports[`class-declaration.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
rangeEnd: 26
rangeStart: 10
| | printWidth
=====================================input======================================
1 |
@@ -54,6 +58,8 @@ exports[`different-levels.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
rangeEnd: 48
rangeStart: 19
| | printWidth
=====================================input======================================
1 | call(1,2,3)
@@ -79,6 +85,8 @@ exports[`directive.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
rangeEnd: 1
rangeStart: 0
| | printWidth
=====================================input======================================
> 1 | 'aaa';
@@ -96,6 +104,8 @@ exports[`function-declaration.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
rangeEnd: 43
rangeStart: 16
| | printWidth
=====================================input======================================
> 1 | function ugly ( {a=1, b = 2 } ) {}
@@ -111,6 +121,8 @@ exports[`ignore-indentation.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
rangeEnd: 225
rangeStart: 222
| | printWidth
=====================================input======================================
1 | function ugly ( {a=1, b = 2 } ) {
@@ -140,6 +152,8 @@ exports[`issue-3789-1.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
rangeEnd: 31
rangeStart: 17
| | printWidth
=====================================input======================================
1 | export class F {
@@ -171,6 +185,8 @@ exports[`issue-3789-2.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
rangeEnd: 18
rangeStart: 17
| | printWidth
=====================================input======================================
1 | export class F {
@@ -202,6 +218,8 @@ exports[`issue-4206-1.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
rangeEnd: 30
rangeStart: 16
| | printWidth
=====================================input======================================
> 1 | export default class Foo{
@@ -222,6 +240,8 @@ exports[`issue-4206-2.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
rangeEnd: 30
rangeStart: 16
| | printWidth
=====================================input======================================
> 1 | export default function Foo(){
@@ -241,6 +261,8 @@ exports[`issue-4206-3.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
rangeEnd: 28
rangeStart: 16
| | printWidth
=====================================input======================================
> 1 | /* */ function Foo(){
@@ -262,6 +284,8 @@ exports[`issue-4206-4.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
rangeEnd: 23
rangeStart: 17
| | printWidth
=====================================input======================================
1 | /* */ class Foo{
@@ -282,6 +306,8 @@ exports[`issue-7082.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
rangeEnd: 42
rangeStart: 29
| | printWidth
=====================================input======================================
> 1 | export const Button = styled.button\`
@@ -302,6 +328,8 @@ exports[`large-dict.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
rangeEnd: 155
rangeStart: 80
| | printWidth
=====================================input======================================
1 | function ugly() {
@@ -333,6 +361,8 @@ exports[`module-export1.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
rangeEnd: 56
rangeStart: 45
| | printWidth
=====================================input======================================
1 | import def , {named} from 'x'
@@ -364,6 +394,8 @@ exports[`module-export2.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
rangeEnd: 79
rangeStart: 57
| | printWidth
=====================================input======================================
1 | import def , {named} from 'x'
@@ -396,6 +428,8 @@ exports[`module-export3.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
rangeEnd: 108
rangeStart: 104
| | printWidth
=====================================input======================================
1 | import def , {named} from 'x'
@@ -427,6 +461,8 @@ exports[`module-import.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
rangeEnd: 22
rangeStart: 7
| | printWidth
=====================================input======================================
> 1 | import def , {named} from 'x'
@@ -458,6 +494,8 @@ exports[`multiple-statements.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
rangeEnd: 49
rangeStart: 30
| | printWidth
=====================================input======================================
1 | call(
@@ -501,6 +539,8 @@ exports[`multiple-statements2.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
rangeEnd: 56
rangeStart: 18
| | printWidth
=====================================input======================================
1 | call(
@@ -548,6 +588,8 @@ exports[`nested.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
rangeEnd: 37
rangeStart: 0
| | printWidth
=====================================input======================================
> 1 | try {
@@ -575,6 +617,8 @@ exports[`nested-print-width.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
rangeEnd: 110
rangeStart: 28
| | printWidth
=====================================input======================================
1 | function f() {
@@ -600,6 +644,8 @@ exports[`nested2.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
rangeEnd: 37
rangeStart: 9
| | printWidth
=====================================input======================================
1 | try {
@@ -627,6 +673,8 @@ exports[`nested3.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
rangeEnd: 37
rangeStart: 9
| | printWidth
=====================================input======================================
1 | try {
@@ -654,6 +702,8 @@ exports[`object-expression.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
rangeEnd: 14
rangeStart: 11
| | printWidth
=====================================input======================================
> 1 | const y = {a:1, b:2}
@@ -669,6 +719,8 @@ exports[`object-expression2.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
rangeEnd: 104
rangeStart: 53
| | printWidth
=====================================input======================================
1 |
@@ -706,6 +758,8 @@ exports[`range.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
rangeEnd: 227
rangeStart: 224
| | printWidth
=====================================input======================================
1 | function ugly ( {a=1, b = 2 } ) {
@@ -735,6 +789,8 @@ exports[`range-end.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
rangeEnd: 50
rangeStart: 32
| | printWidth
=====================================input======================================
1 | // Unchanged
@@ -765,6 +821,8 @@ exports[`range-start.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
rangeEnd: 18
rangeStart: 0
| | printWidth
=====================================input======================================
> 1 | call(
@@ -797,6 +855,8 @@ exports[`try-catch.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
rangeEnd: 8
rangeStart: 7
| | printWidth
=====================================input======================================
1 | try {}
@@ -814,6 +874,8 @@ exports[`whitespace.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
rangeEnd: 1
rangeStart: 0
| | printWidth
=====================================input======================================
> 1 |
@@ -4,6 +4,8 @@ exports[`identifier.json format 1`] = `
====================================options=====================================
parsers: ["json"]
printWidth: 80
rangeEnd: 4
rangeStart: 1
| | printWidth
=====================================input======================================
> 1 | {"a":1, "b":2}
@@ -19,6 +21,8 @@ exports[`issue2297.json format 1`] = `
====================================options=====================================
parsers: ["json"]
printWidth: 80
rangeEnd: 6
rangeStart: 5
| | printWidth
=====================================input======================================
> 1 | {"a":1, "b":2}
@@ -78,6 +78,8 @@ exports[`range-with-pragma.js - {"requirePragma":true} format 1`] = `
====================================options=====================================
parsers: ["flow", "babel", "typescript"]
printWidth: 80
rangeEnd: 36
rangeStart: 21
requirePragma: true
| | printWidth
=====================================input======================================
@@ -100,6 +102,8 @@ exports[`range-without-pragma.js - {"requirePragma":true} format 1`] = `
====================================options=====================================
parsers: ["flow", "babel", "typescript"]
printWidth: 80
rangeEnd: 15
rangeStart: 0
requirePragma: true
| | printWidth
=====================================input======================================
@@ -4,6 +4,8 @@ exports[`export-assignment.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
rangeEnd: 18
rangeStart: 15
| | printWidth
=====================================input======================================
1 | f ( );
@@ -23,6 +25,8 @@ exports[`issue-4926.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
rangeEnd: 304
rangeStart: 102
| | printWidth
=====================================input======================================
1 | class Foo {
@@ -61,6 +65,8 @@ exports[`issue-7148.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
rangeEnd: 59
rangeStart: 29
| | printWidth
=====================================input======================================
1 | export default class Test {
@@ -4,6 +4,8 @@ exports[`example.vue format 1`] = `
====================================options=====================================
parsers: ["vue"]
printWidth: 80
rangeEnd: 145
rangeStart: 73
| | printWidth
=====================================input======================================
1 | <template>
+67 -28
View File
@@ -237,12 +237,28 @@ function runTest({
let codeForSnapshot = formatResult.inputWithCursor;
let codeOffset = 0;
let resultForSnapshot = formatResult.outputWithCursor;
const { rangeStart, rangeEnd } = formatResult.options;
const { rangeStart, rangeEnd, cursorOffset } = formatResult.options;
if (typeof rangeStart === "number" || typeof rangeEnd === "number") {
let rangeStartWithCursor = rangeStart;
let rangeEndWithCursor = rangeEnd;
if (typeof cursorOffset === "number") {
if (
typeof rangeStartWithCursor === "number" &&
rangeStartWithCursor > cursorOffset
) {
rangeStartWithCursor += CURSOR_PLACEHOLDER.length;
}
if (
typeof rangeEndWithCursor === "number" &&
rangeEndWithCursor > cursorOffset
) {
rangeEndWithCursor += CURSOR_PLACEHOLDER.length;
}
}
codeForSnapshot = visualizeRange(codeForSnapshot, {
rangeStart,
rangeEnd,
rangeStart: rangeStartWithCursor,
rangeEnd: rangeEndWithCursor,
});
codeOffset = codeForSnapshot.match(/^>?\s+1 \| /)[0].length;
}
@@ -333,35 +349,58 @@ function parse(source, options) {
return prettier.__debug.parse(source, options, /* massage */ true).ast;
}
function format(text, options) {
options = {
...options,
};
const inputWithCursor = text
.replace(RANGE_START_PLACEHOLDER, (match, offset) => {
options.rangeStart = offset;
return "";
const indexProperties = [
{
property: "cursorOffset",
placeholder: CURSOR_PLACEHOLDER,
},
{
property: "rangeStart",
placeholder: RANGE_START_PLACEHOLDER,
},
{
property: "rangeEnd",
placeholder: RANGE_END_PLACEHOLDER,
},
];
function replacePlaceholders(originalText, originalOptions) {
const indexes = indexProperties
.map(({ property, placeholder }) => {
const value = originalText.indexOf(placeholder);
return value === -1 ? undefined : { property, value, placeholder };
})
.replace(RANGE_END_PLACEHOLDER, (match, offset) => {
options.rangeEnd = offset;
return "";
});
.filter(Boolean)
.sort((a, b) => a.value - b.value);
const input = inputWithCursor.replace(CURSOR_PLACEHOLDER, (match, offset) => {
options.cursorOffset = offset;
return "";
});
const options = { ...originalOptions };
let text = originalText;
let offset = 0;
for (const { property, value, placeholder } of indexes) {
text = text.replace(placeholder, "");
options[property] = value + offset;
offset -= placeholder.length;
}
return { text, options };
}
const result = prettier.formatWithCursor(input, options);
const output = result.formatted;
const insertCursor = (text, cursorOffset) =>
cursorOffset >= 0
? text.slice(0, cursorOffset) +
CURSOR_PLACEHOLDER +
text.slice(cursorOffset)
: text;
function format(originalText, originalOptions) {
const { text: input, options } = replacePlaceholders(
originalText,
originalOptions
);
const inputWithCursor = insertCursor(input, options.cursorOffset);
const outputWithCursor =
options.cursorOffset >= 0
? output.slice(0, result.cursorOffset) +
CURSOR_PLACEHOLDER +
output.slice(result.cursorOffset)
: output;
const { formatted: output, cursorOffset } = prettier.formatWithCursor(
input,
options
);
const outputWithCursor = insertCursor(output, cursorOffset);
const eolVisualizedOutput = visualizeEndOfLine(outputWithCursor);
const changed = outputWithCursor !== inputWithCursor;
@@ -2,9 +2,6 @@
function composeOptionsForSnapshot(baseOptions, parsers) {
const {
rangeStart,
rangeEnd,
cursorOffset,
disableBabelTS,
plugins,
filepath,