Skip to content

Commit 2f5f3d3

Browse files
committed
moving parse and format into it's own file
1 parent bc9a584 commit 2f5f3d3

3 files changed

Lines changed: 94 additions & 81 deletions

File tree

tests/config/constants.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ export const FORMAT_TEST_DIRECTORY = normalizeDirectory(
99
);
1010

1111
export const CURSOR_PLACEHOLDER = "<|>";
12+
export const RANGE_START_PLACEHOLDER = "<<<PRETTIER_RANGE_START>>>";
13+
export const RANGE_END_PLACEHOLDER = "<<<PRETTIER_RANGE_END>>>";

tests/config/run-format-test.js

Lines changed: 1 addition & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@ import {
1616
isAstUnstable,
1717
isUnstable,
1818
} from "./failed-format-tests.js";
19+
import { format, parse } from "./run-prettier.js";
1920

2021
const { __dirname } = createEsmUtils(import.meta);
2122

2223
const { FULL_TEST } = process.env;
2324
const BOM = "\uFEFF";
2425

2526
const CURSOR_PLACEHOLDER = "<|>";
26-
const RANGE_START_PLACEHOLDER = "<<<PRETTIER_RANGE_START>>>";
27-
const RANGE_END_PLACEHOLDER = "<<<PRETTIER_RANGE_END>>>";
2827

2928
const testsWithAstChanges = new Map(
3029
[
@@ -370,83 +369,4 @@ function shouldSkipEolTest(code, options) {
370369
return false;
371370
}
372371

373-
async function parse(source, options) {
374-
const prettier = await getPrettier();
375-
376-
const { ast } = await prettier.__debug.parse(
377-
source,
378-
{ ...options, plugins: await getPlugins() },
379-
{ massage: true },
380-
);
381-
return ast;
382-
}
383-
384-
const indexProperties = [
385-
{
386-
property: "cursorOffset",
387-
placeholder: CURSOR_PLACEHOLDER,
388-
},
389-
{
390-
property: "rangeStart",
391-
placeholder: RANGE_START_PLACEHOLDER,
392-
},
393-
{
394-
property: "rangeEnd",
395-
placeholder: RANGE_END_PLACEHOLDER,
396-
},
397-
];
398-
function replacePlaceholders(originalText, originalOptions) {
399-
const indexes = indexProperties
400-
.map(({ property, placeholder }) => {
401-
const value = originalText.indexOf(placeholder);
402-
return value === -1 ? undefined : { property, value, placeholder };
403-
})
404-
.filter(Boolean)
405-
.sort((a, b) => a.value - b.value);
406-
407-
const options = { ...originalOptions };
408-
let text = originalText;
409-
let offset = 0;
410-
for (const { property, value, placeholder } of indexes) {
411-
text = text.replace(placeholder, "");
412-
options[property] = value + offset;
413-
offset -= placeholder.length;
414-
}
415-
return { text, options };
416-
}
417-
418-
const insertCursor = (text, cursorOffset) =>
419-
cursorOffset >= 0
420-
? text.slice(0, cursorOffset) +
421-
CURSOR_PLACEHOLDER +
422-
text.slice(cursorOffset)
423-
: text;
424-
async function format(originalText, originalOptions) {
425-
const { text: input, options } = replacePlaceholders(
426-
originalText,
427-
originalOptions,
428-
);
429-
const inputWithCursor = insertCursor(input, options.cursorOffset);
430-
const prettier = await getPrettier();
431-
432-
const { formatted: output, cursorOffset } = await prettier.formatWithCursor(
433-
input,
434-
{ ...options, plugins: await getPlugins() },
435-
);
436-
const outputWithCursor = insertCursor(output, cursorOffset);
437-
const eolVisualizedOutput = visualizeEndOfLine(outputWithCursor);
438-
439-
const changed = outputWithCursor !== inputWithCursor;
440-
441-
return {
442-
changed,
443-
options,
444-
input,
445-
inputWithCursor,
446-
output,
447-
outputWithCursor,
448-
eolVisualizedOutput,
449-
};
450-
}
451-
452372
export default runFormatTest;

tests/config/run-prettier.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import getPrettier from "./get-prettier.js";
2+
import getPlugins from "./get-plugins.js";
3+
import {
4+
CURSOR_PLACEHOLDER,
5+
RANGE_START_PLACEHOLDER,
6+
RANGE_END_PLACEHOLDER,
7+
} from "./constants.js";
8+
import visualizeEndOfLine from "./utils/visualize-end-of-line.js";
9+
10+
async function parse(source, options) {
11+
const prettier = await getPrettier();
12+
13+
const { ast } = await prettier.__debug.parse(
14+
source,
15+
{ ...options, plugins: await getPlugins() },
16+
{ massage: true },
17+
);
18+
return ast;
19+
}
20+
21+
async function format(originalText, originalOptions) {
22+
const { text: input, options } = replacePlaceholders(
23+
originalText,
24+
originalOptions,
25+
);
26+
const inputWithCursor = insertCursor(input, options.cursorOffset);
27+
const prettier = await getPrettier();
28+
29+
const { formatted: output, cursorOffset } = await prettier.formatWithCursor(
30+
input,
31+
{ ...options, plugins: await getPlugins() },
32+
);
33+
const outputWithCursor = insertCursor(output, cursorOffset);
34+
const eolVisualizedOutput = visualizeEndOfLine(outputWithCursor);
35+
36+
const changed = outputWithCursor !== inputWithCursor;
37+
38+
return {
39+
changed,
40+
options,
41+
input,
42+
inputWithCursor,
43+
output,
44+
outputWithCursor,
45+
eolVisualizedOutput,
46+
};
47+
}
48+
49+
const insertCursor = (text, cursorOffset) =>
50+
cursorOffset >= 0
51+
? text.slice(0, cursorOffset) +
52+
CURSOR_PLACEHOLDER +
53+
text.slice(cursorOffset)
54+
: text;
55+
56+
const indexProperties = [
57+
{
58+
property: "cursorOffset",
59+
placeholder: CURSOR_PLACEHOLDER,
60+
},
61+
{
62+
property: "rangeStart",
63+
placeholder: RANGE_START_PLACEHOLDER,
64+
},
65+
{
66+
property: "rangeEnd",
67+
placeholder: RANGE_END_PLACEHOLDER,
68+
},
69+
];
70+
71+
function replacePlaceholders(originalText, originalOptions) {
72+
const indexes = indexProperties
73+
.map(({ property, placeholder }) => {
74+
const value = originalText.indexOf(placeholder);
75+
return value === -1 ? undefined : { property, value, placeholder };
76+
})
77+
.filter(Boolean)
78+
.sort((a, b) => a.value - b.value);
79+
80+
const options = { ...originalOptions };
81+
let text = originalText;
82+
let offset = 0;
83+
for (const { property, value, placeholder } of indexes) {
84+
text = text.replace(placeholder, "");
85+
options[property] = value + offset;
86+
offset -= placeholder.length;
87+
}
88+
return { text, options };
89+
}
90+
91+
export { format, parse };

0 commit comments

Comments
 (0)