Skip to content

Commit db6d96d

Browse files
committed
moving replace placeholders into it's own file
1 parent 2f5f3d3 commit db6d96d

2 files changed

Lines changed: 50 additions & 42 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import {
2+
CURSOR_PLACEHOLDER,
3+
RANGE_END_PLACEHOLDER,
4+
RANGE_START_PLACEHOLDER,
5+
} from "./constants.js";
6+
7+
const indexProperties = [
8+
{
9+
property: "cursorOffset",
10+
placeholder: CURSOR_PLACEHOLDER,
11+
},
12+
{
13+
property: "rangeStart",
14+
placeholder: RANGE_START_PLACEHOLDER,
15+
},
16+
{
17+
property: "rangeEnd",
18+
placeholder: RANGE_END_PLACEHOLDER,
19+
},
20+
];
21+
22+
function replacePlaceholders(originalText, originalOptions) {
23+
const indexes = indexProperties
24+
.map(({ property, placeholder }) => {
25+
const value = originalText.indexOf(placeholder);
26+
return value === -1 ? undefined : { property, value, placeholder };
27+
})
28+
.filter(Boolean)
29+
.sort((a, b) => a.value - b.value);
30+
31+
const options = { ...originalOptions };
32+
let text = originalText;
33+
let offset = 0;
34+
for (const { property, value, placeholder } of indexes) {
35+
text = text.replace(placeholder, "");
36+
options[property] = value + offset;
37+
offset -= placeholder.length;
38+
}
39+
return { text, options };
40+
}
41+
42+
export { replacePlaceholders };

tests/config/run-prettier.js

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
import getPrettier from "./get-prettier.js";
22
import getPlugins from "./get-plugins.js";
3-
import {
4-
CURSOR_PLACEHOLDER,
5-
RANGE_START_PLACEHOLDER,
6-
RANGE_END_PLACEHOLDER,
7-
} from "./constants.js";
3+
import { CURSOR_PLACEHOLDER } from "./constants.js";
84
import visualizeEndOfLine from "./utils/visualize-end-of-line.js";
5+
import { replacePlaceholders } from "./replace-placeholders.js";
96

10-
async function parse(source, options) {
7+
async function parse(input, options) {
118
const prettier = await getPrettier();
129

1310
const { ast } = await prettier.__debug.parse(
14-
source,
15-
{ ...options, plugins: await getPlugins() },
11+
input,
12+
await loadPlugins(options),
1613
{ massage: true },
1714
);
1815
return ast;
@@ -28,7 +25,7 @@ async function format(originalText, originalOptions) {
2825

2926
const { formatted: output, cursorOffset } = await prettier.formatWithCursor(
3027
input,
31-
{ ...options, plugins: await getPlugins() },
28+
await loadPlugins(options),
3229
);
3330
const outputWithCursor = insertCursor(output, cursorOffset);
3431
const eolVisualizedOutput = visualizeEndOfLine(outputWithCursor);
@@ -53,39 +50,8 @@ const insertCursor = (text, cursorOffset) =>
5350
text.slice(cursorOffset)
5451
: text;
5552

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 };
53+
async function loadPlugins(options) {
54+
return { ...options, plugins: await getPlugins() };
8955
}
9056

9157
export { format, parse };

0 commit comments

Comments
 (0)