/
run_spec.js
304 lines (272 loc) · 8.62 KB
/
run_spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
"use strict";
const fs = require("fs");
const path = require("path");
const raw = require("jest-snapshot-serializer-raw").wrap;
const { isCI } = require("ci-info");
const { TEST_STANDALONE } = process.env;
const AST_COMPARE = isCI || process.env.AST_COMPARE;
const DEEP_COMPARE = isCI || process.env.DEEP_COMPARE;
const TEST_CRLF =
(isCI && process.platform === "win32") || process.env.TEST_CRLF;
const CURSOR_PLACEHOLDER = "<|>";
const RANGE_START_PLACEHOLDER = "<<<PRETTIER_RANGE_START>>>";
const RANGE_END_PLACEHOLDER = "<<<PRETTIER_RANGE_END>>>";
const prettier = !TEST_STANDALONE
? require("prettier/local")
: require("prettier/standalone");
// TODO: these test files need fix
const unstableTests = new Map(
[
"class_comment/comments.js",
["comments/dangling_array.js", (options) => options.semi === false],
["comments/jsx.js", (options) => options.semi === false],
"comments/return-statement.js",
"comments/tagged-template-literal.js",
"comments_closure_typecast/iife.js",
"css_atrule/include.css",
"graphql_interface/separator-detection.graphql",
[
"html_angular/attributes.component.html",
(options) => options.printWidth === 1,
],
"js_empty/semicolon.js",
"markdown_footnoteDefinition/multiline.md",
"markdown_spec/example-234.md",
"markdown_spec/example-235.md",
"multiparser_html_js/script-tag-escaping.html",
[
"multiparser_js_markdown/codeblock.js",
(options) => options.proseWrap === "always",
],
["no-semi/comments.js", (options) => options.semi === false],
"yaml_prettier_ignore/document.yml",
].map((fixture) => {
const [file, isUnstable = () => true] = Array.isArray(fixture)
? fixture
: [fixture];
return [path.join(__dirname, "../tests/", file), isUnstable];
})
);
global.run_spec = (dirname, parsers, options) => {
// `IS_PARSER_INFERENCE_TESTS` mean to test `inferParser` on `standalone`
const IS_PARSER_INFERENCE_TESTS = dirname.endsWith("parser-inference");
if (IS_PARSER_INFERENCE_TESTS) {
parsers = [];
} else if (!parsers || !parsers.length) {
throw new Error(`No parsers were specified for ${dirname}`);
}
const files = fs.readdirSync(dirname, { withFileTypes: true });
for (const file of files) {
const basename = file.name;
const filename = path.join(dirname, basename);
if (
path.extname(basename) === ".snap" ||
!file.isFile() ||
basename[0] === "." ||
basename === "jsfmt.spec.js"
) {
continue;
}
let rangeStart;
let rangeEnd;
let cursorOffset;
const text = fs.readFileSync(filename, "utf8");
const source = (TEST_CRLF ? text.replace(/\n/g, "\r\n") : text)
.replace(RANGE_START_PLACEHOLDER, (match, offset) => {
rangeStart = offset;
return "";
})
.replace(RANGE_END_PLACEHOLDER, (match, offset) => {
rangeEnd = offset;
return "";
});
const input = source.replace(CURSOR_PLACEHOLDER, (match, offset) => {
cursorOffset = offset;
return "";
});
const baseOptions = {
printWidth: 80,
...options,
rangeStart,
rangeEnd,
cursorOffset,
};
const mainOptions = {
...baseOptions,
...(IS_PARSER_INFERENCE_TESTS
? { filepath: filename }
: { parser: parsers[0] }),
};
const hasEndOfLine = "endOfLine" in mainOptions;
const output = format(input, filename, mainOptions);
const visualizedOutput = visualizeEndOfLine(output);
test(basename, () => {
expect(visualizedOutput).toEqual(
visualizeEndOfLine(consistentEndOfLine(output))
);
expect(
raw(
createSnapshot(
hasEndOfLine
? visualizeEndOfLine(
text
.replace(RANGE_START_PLACEHOLDER, "")
.replace(RANGE_END_PLACEHOLDER, "")
)
: source,
hasEndOfLine ? visualizedOutput : output,
{ ...baseOptions, parsers }
)
)
).toMatchSnapshot();
});
const parsersToVerify = parsers.slice(1);
if (parsers.includes("typescript") && !parsers.includes("babel-ts")) {
parsersToVerify.push("babel-ts");
}
if (
DEEP_COMPARE &&
typeof rangeStart === "undefined" &&
typeof rangeEnd === "undefined" &&
typeof cursorOffset === "undefined" &&
!TEST_CRLF
) {
test(`${basename} second format`, () => {
const secondOutput = format(output, filename, mainOptions);
const isUnstable = unstableTests.get(filename);
if (isUnstable && isUnstable(options || {})) {
// To keep eye on failed tests, this assert never supposed to pass,
// if it fails, just remove the file from `unstableTests`
expect(secondOutput).not.toEqual(output);
} else {
expect(secondOutput).toEqual(output);
}
});
}
for (const parser of parsersToVerify) {
const verifyOptions = { ...baseOptions, parser };
test(`${basename} - ${parser}-verify`, () => {
if (
parser === "babel-ts" &&
options &&
(options.disableBabelTS === true ||
(Array.isArray(options.disableBabelTS) &&
options.disableBabelTS.includes(basename)))
) {
expect(() => {
format(input, filename, verifyOptions);
}).toThrow(TEST_STANDALONE ? undefined : SyntaxError);
} else {
const verifyOutput = format(input, filename, verifyOptions);
expect(visualizeEndOfLine(verifyOutput)).toEqual(visualizedOutput);
}
});
}
if (AST_COMPARE) {
test(`${basename} parse`, () => {
const parseOptions = { ...mainOptions };
delete parseOptions.cursorOffset;
const originalAst = parse(input, parseOptions);
let formattedAst;
expect(() => {
formattedAst = parse(
output.replace(CURSOR_PLACEHOLDER, ""),
parseOptions
);
}).not.toThrow();
expect(originalAst).toEqual(formattedAst);
});
}
}
};
function parse(source, options) {
return prettier.__debug.parse(source, options, /* massage */ true).ast;
}
function format(source, filename, options) {
const result = prettier.formatWithCursor(source, {
filepath: filename,
...options,
});
return options.cursorOffset >= 0
? result.formatted.slice(0, result.cursorOffset) +
CURSOR_PLACEHOLDER +
result.formatted.slice(result.cursorOffset)
: result.formatted;
}
function consistentEndOfLine(text) {
let firstEndOfLine;
return text.replace(/\r\n?|\n/g, (endOfLine) => {
if (!firstEndOfLine) {
firstEndOfLine = endOfLine;
}
return firstEndOfLine;
});
}
function visualizeEndOfLine(text) {
return text.replace(/\r\n?|\n/g, (endOfLine) => {
switch (endOfLine) {
case "\n":
return "<LF>\n";
case "\r\n":
return "<CRLF>\n";
case "\r":
return "<CR>\n";
default:
throw new Error(`Unexpected end of line ${JSON.stringify(endOfLine)}`);
}
});
}
function createSnapshot(input, output, options) {
const separatorWidth = 80;
const printWidthIndicator =
options.printWidth > 0 && Number.isFinite(options.printWidth)
? " ".repeat(options.printWidth) + "| printWidth"
: [];
return []
.concat(
printSeparator(separatorWidth, "options"),
printOptions(
omit(
options,
(k) =>
k === "rangeStart" ||
k === "rangeEnd" ||
k === "cursorOffset" ||
k === "disableBabelTS"
)
),
printWidthIndicator,
printSeparator(separatorWidth, "input"),
input,
printSeparator(separatorWidth, "output"),
output,
printSeparator(separatorWidth)
)
.join("\n");
}
function printSeparator(width, description) {
description = description || "";
const leftLength = Math.floor((width - description.length) / 2);
const rightLength = width - leftLength - description.length;
return "=".repeat(leftLength) + description + "=".repeat(rightLength);
}
function printOptions(options) {
const keys = Object.keys(options).sort();
return keys.map((key) => `${key}: ${stringify(options[key])}`).join("\n");
function stringify(value) {
return value === Infinity
? "Infinity"
: Array.isArray(value)
? `[${value.map((v) => JSON.stringify(v)).join(", ")}]`
: JSON.stringify(value);
}
}
function omit(obj, fn) {
return Object.keys(obj).reduce((reduced, key) => {
const value = obj[key];
if (!fn(key, value)) {
reduced[key] = value;
}
return reduced;
}, {});
}