Files
test-repo/tests/config/format-test.js
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

510 lines
14 KiB
JavaScript
Raw Normal View History

2022-01-04 17:00:25 +08:00
import fs from "node:fs";
import path from "node:path";
import url from "node:url";
import createEsmUtils from "esm-utils";
2022-05-10 15:43:02 +08:00
import getPrettier from "./get-prettier.js";
2022-01-04 17:00:25 +08:00
import checkParsers from "./utils/check-parsers.js";
import createSnapshot from "./utils/create-snapshot.js";
import visualizeEndOfLine from "./utils/visualize-end-of-line.js";
import consistentEndOfLine from "./utils/consistent-end-of-line.js";
import stringifyOptionsForTitle from "./utils/stringify-options-for-title.js";
2022-02-12 15:39:16 +08:00
const { __dirname } = createEsmUtils(import.meta);
2017-04-12 13:41:51 -07:00
const { FULL_TEST, TEST_STANDALONE } = process.env;
2020-09-04 23:43:41 +08:00
const BOM = "\uFEFF";
2018-12-08 18:28:29 +08:00
const CURSOR_PLACEHOLDER = "<|>";
const RANGE_START_PLACEHOLDER = "<<<PRETTIER_RANGE_START>>>";
const RANGE_END_PLACEHOLDER = "<<<PRETTIER_RANGE_END>>>";
2020-03-21 19:43:01 +08:00
// TODO: these test files need fix
const unstableTests = new Map(
[
2020-05-24 21:53:29 +03:00
"js/class-comment/misc.js",
2020-03-21 19:43:01 +08:00
["js/comments/dangling_array.js", (options) => options.semi === false],
["js/comments/jsx.js", (options) => options.semi === false],
"js/comments/return-statement.js",
"js/comments/tagged-template-literal.js",
"markdown/spec/example-234.md",
"markdown/spec/example-235.md",
"html/multiparser/js/script-tag-escaping.html",
2020-03-21 19:43:01 +08:00
[
"js/multiparser-markdown/codeblock.js",
(options) => options.proseWrap === "always",
],
["js/no-semi/comments.js", (options) => options.semi === false],
["flow/no-semi/comments.js", (options) => options.semi === false],
"typescript/prettier-ignore/mapped-types.ts",
2020-11-05 14:08:37 +08:00
"js/comments/html-like/comment.js",
"js/for/continue-and-break-comment-without-blocks.js",
2020-03-21 19:43:01 +08:00
].map((fixture) => {
const [file, isUnstable = () => true] = Array.isArray(fixture)
? fixture
: [fixture];
return [path.join(__dirname, "../format/", file), isUnstable];
})
);
2021-03-05 19:17:42 +09:00
const unstableAstTests = new Map();
2020-10-13 09:14:02 +08:00
const espreeDisabledTests = new Set(
[
// These tests only work for `babel`
"comments-closure-typecast",
].map((directory) => path.join(__dirname, "../format/js", directory))
);
2022-01-28 15:25:30 +08:00
const acornDisabledTests = espreeDisabledTests;
const meriyahDisabledTests = new Set([
...espreeDisabledTests,
// Meriyah does not support decorator auto accessors syntax.
// But meriyah can parse it as an ordinary class property.
// So meriyah does not throw parsing error for it.
...[
"basic.js",
"computed.js",
"private.js",
"static-computed.js",
"static-private.js",
"static.js",
"with-semicolon-1.js",
"with-semicolon-2.js",
].map((filename) =>
path.join(__dirname, "../format/js/decorator-auto-accessors", filename)
),
path.join(
__dirname,
"../format/js/babel-plugins/decorator-auto-accessors.js"
),
]);
2020-10-13 09:14:02 +08:00
2020-08-10 22:17:51 +08:00
const isUnstable = (filename, options) => {
const testFunction = unstableTests.get(filename);
if (!testFunction) {
return false;
}
return testFunction(options);
};
const isAstUnstable = (filename, options) => {
const testFunction = unstableAstTests.get(filename);
if (!testFunction) {
return false;
}
return testFunction(options);
};
2020-09-04 23:43:41 +08:00
const shouldThrowOnFormat = (filename, options) => {
const { errors = {} } = options;
if (errors === true) {
return true;
}
2020-10-13 09:14:02 +08:00
const files = errors[options.parser];
2020-08-10 22:17:51 +08:00
2020-10-13 09:14:02 +08:00
if (files === true || (Array.isArray(files) && files.includes(filename))) {
2020-08-10 22:17:51 +08:00
return true;
}
return false;
};
2020-04-14 18:19:13 +08:00
const isTestDirectory = (dirname, name) =>
2020-10-13 09:14:02 +08:00
(dirname + path.sep).startsWith(
path.join(__dirname, "../format", name) + path.sep
);
2020-04-14 18:19:13 +08:00
const ensurePromise = (value) => {
const isPromise = TEST_STANDALONE
? // In standalone test, promise is from another context
Object.prototype.toString.call(value) === "[object Promise]"
: value instanceof Promise;
if (!isPromise) {
throw new TypeError("Expected value to be a 'Promise'.");
}
return value;
};
2020-08-28 10:02:08 +08:00
function runSpec(fixtures, parsers, options) {
2022-01-04 17:00:25 +08:00
let { importMeta, snippets = [] } = fixtures.importMeta
? fixtures
: { importMeta: fixtures };
const dirname = path.dirname(url.fileURLToPath(importMeta.url));
2020-02-03 07:33:19 +08:00
// `IS_PARSER_INFERENCE_TESTS` mean to test `inferParser` on `standalone`
2020-04-14 18:19:13 +08:00
const IS_PARSER_INFERENCE_TESTS = isTestDirectory(
dirname,
"misc/parser-inference"
);
// `IS_ERROR_TESTS` mean to watch errors like:
// - syntax parser hasn't supported yet
// - syntax errors that should throws
const IS_ERROR_TESTS = isTestDirectory(dirname, "misc/errors");
if (IS_ERROR_TESTS) {
options = { errors: true, ...options };
}
2020-04-14 18:19:13 +08:00
2021-02-25 19:28:06 +09:00
const IS_TYPESCRIPT_ONLY_TEST = isTestDirectory(
dirname,
"misc/typescript-only"
);
2020-02-03 07:33:19 +08:00
if (IS_PARSER_INFERENCE_TESTS) {
2020-09-04 23:43:41 +08:00
parsers = [undefined];
}
2020-09-04 23:43:41 +08:00
snippets = snippets.map((test, index) => {
test = typeof test === "string" ? { code: test } : test;
return {
2020-04-25 09:23:34 +08:00
...test,
name: `snippet: ${test.name || `#${index}`}`,
};
});
2018-12-08 18:28:29 +08:00
const files = fs
.readdirSync(dirname, { withFileTypes: true })
.map((file) => {
const basename = file.name;
const filename = path.join(dirname, basename);
if (
path.extname(basename) === ".snap" ||
!file.isFile() ||
basename[0] === "." ||
basename === "jsfmt.spec.js" ||
// VSCode creates this file sometime https://github.com/microsoft/vscode/issues/105191
basename === "debug.log"
) {
return;
}
2020-04-18 21:09:07 +08:00
const text = fs.readFileSync(filename, "utf8");
2020-04-18 21:09:07 +08:00
return {
name: basename,
filename,
code: text,
};
})
.filter(Boolean);
2020-05-21 21:10:00 +08:00
// Make sure tests are in correct location
2020-09-04 23:43:41 +08:00
if (process.env.CHECK_TEST_PARSERS) {
if (!Array.isArray(parsers) || parsers.length === 0) {
2020-09-04 23:43:41 +08:00
throw new Error(`No parsers were specified for ${dirname}`);
}
2020-05-21 21:10:00 +08:00
checkParsers({ dirname, files }, parsers);
}
2020-09-04 23:43:41 +08:00
const [parser] = parsers;
const allParsers = [...parsers];
2020-11-05 14:08:37 +08:00
if (!IS_ERROR_TESTS) {
2021-02-25 19:28:06 +09:00
if (
parsers.includes("typescript") &&
!parsers.includes("babel-ts") &&
!IS_TYPESCRIPT_ONLY_TEST
) {
allParsers.push("babel-ts");
2020-11-05 14:08:37 +08:00
}
if (parsers.includes("babel") && isTestDirectory(dirname, "js")) {
if (!parsers.includes("espree") && !espreeDisabledTests.has(dirname)) {
allParsers.push("espree");
}
if (!parsers.includes("meriyah") && !meriyahDisabledTests.has(dirname)) {
allParsers.push("meriyah");
}
2022-01-28 15:25:30 +08:00
if (!parsers.includes("acorn") && !acornDisabledTests.has(dirname)) {
allParsers.push("acorn");
}
2020-11-05 14:08:37 +08:00
}
2021-05-19 09:22:52 +08:00
if (parsers.includes("babel") && !parsers.includes("__babel_estree")) {
allParsers.push("__babel_estree");
}
2020-10-13 09:14:02 +08:00
}
2020-09-04 23:43:41 +08:00
const stringifiedOptions = stringifyOptionsForTitle(options);
2020-04-25 09:23:34 +08:00
for (const { name, filename, code, output } of [...files, ...snippets]) {
2020-09-04 23:43:41 +08:00
const title = `${name}${
2020-04-18 21:09:07 +08:00
stringifiedOptions ? ` - ${stringifiedOptions}` : ""
2020-09-04 23:43:41 +08:00
}`;
describe(title, () => {
2020-08-10 22:17:51 +08:00
const formatOptions = {
2020-04-10 19:12:01 +08:00
printWidth: 80,
// Should not search plugins by default
pluginSearchDirs: false,
2020-04-10 19:12:01 +08:00
...options,
2020-08-10 22:17:51 +08:00
filepath: filename,
parser,
2020-04-10 19:12:01 +08:00
};
2022-04-01 10:21:44 +08:00
const shouldThrowOnMainParserFormat = shouldThrowOnFormat(
name,
formatOptions
);
let mainParserFormatResult;
if (shouldThrowOnMainParserFormat) {
mainParserFormatResult = { options: formatOptions, error: true };
} else {
beforeAll(async () => {
mainParserFormatResult = await format(code, formatOptions);
});
}
2018-12-08 18:28:29 +08:00
2020-09-04 23:43:41 +08:00
for (const currentParser of allParsers) {
if (
(currentParser === "espree" && espreeDisabledTests.has(filename)) ||
(currentParser === "meriyah" && meriyahDisabledTests.has(filename)) ||
(currentParser === "acorn" && acornDisabledTests.has(filename))
) {
continue;
}
2022-04-01 10:21:44 +08:00
const testTitle =
shouldThrowOnMainParserFormat ||
formatOptions.parser !== currentParser
? `[${currentParser}] format`
: "format";
test(testTitle, async () => {
await runTest({
parsers,
name,
filename,
code,
output,
parser: currentParser,
mainParserFormatResult,
mainParserFormatOptions: formatOptions,
});
2020-09-04 23:43:41 +08:00
});
}
});
}
}
2020-08-10 22:17:51 +08:00
2022-04-01 10:21:44 +08:00
async function runTest({
2020-09-04 23:43:41 +08:00
parsers,
name,
filename,
code,
output,
parser,
mainParserFormatResult,
mainParserFormatOptions,
}) {
let formatOptions = mainParserFormatOptions;
let formatResult = mainParserFormatResult;
2020-08-10 22:17:51 +08:00
// Verify parsers or error tests
if (
mainParserFormatResult.error ||
mainParserFormatOptions.parser !== parser
) {
2020-09-04 23:43:41 +08:00
formatOptions = { ...mainParserFormatResult.options, parser };
const runFormat = () => format(code, formatOptions);
2020-08-10 22:17:51 +08:00
2020-09-04 23:43:41 +08:00
if (shouldThrowOnFormat(name, formatOptions)) {
2022-04-01 10:21:44 +08:00
await expect(runFormat()).rejects.toThrowErrorMatchingSnapshot();
2020-09-04 23:43:41 +08:00
return;
}
2020-03-21 19:43:01 +08:00
2020-09-04 23:43:41 +08:00
// Verify parsers format result should be the same as main parser
output = mainParserFormatResult.outputWithCursor;
2022-04-01 10:21:44 +08:00
formatResult = await runFormat();
2020-09-04 23:43:41 +08:00
}
2020-04-10 19:12:01 +08:00
2022-04-01 10:21:44 +08:00
// Make sure output has consistent EOL
expect(formatResult.eolVisualizedOutput).toEqual(
visualizeEndOfLine(consistentEndOfLine(formatResult.outputWithCursor))
);
// The result is assert to equals to `output`
if (typeof output === "string") {
2020-09-04 23:43:41 +08:00
expect(formatResult.eolVisualizedOutput).toEqual(
2022-04-01 10:21:44 +08:00
visualizeEndOfLine(output)
2020-09-04 23:43:41 +08:00
);
2022-04-01 10:21:44 +08:00
return;
}
2020-08-10 22:17:51 +08:00
2022-04-01 10:21:44 +08:00
// All parsers have the same result, only snapshot the result from main parser
expect(
createSnapshot(formatResult, {
parsers,
formatOptions,
CURSOR_PLACEHOLDER,
})
).toMatchSnapshot();
2020-09-04 23:43:41 +08:00
if (!FULL_TEST) {
return;
}
const isUnstableTest = isUnstable(filename, formatOptions);
if (
(formatResult.changed || isUnstableTest) &&
// No range and cursor
formatResult.input === code
) {
2022-04-01 10:21:44 +08:00
const { eolVisualizedOutput: firstOutput, output } = formatResult;
const { eolVisualizedOutput: secondOutput } = await format(
output,
formatOptions
);
if (isUnstableTest) {
// 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(firstOutput);
} else {
expect(secondOutput).toEqual(firstOutput);
}
2020-09-04 23:43:41 +08:00
}
2020-04-10 19:12:01 +08:00
const isAstUnstableTest = isAstUnstable(filename, formatOptions);
2020-09-04 23:43:41 +08:00
// Some parsers skip parsing empty files
if (formatResult.changed && code.trim()) {
2022-04-01 10:21:44 +08:00
const { input, output } = formatResult;
2022-05-04 11:15:48 +08:00
const originalAst = await parse(input, formatOptions);
const formattedAst = await parse(output, formatOptions);
2022-04-01 10:21:44 +08:00
if (isAstUnstableTest) {
expect(formattedAst).not.toEqual(originalAst);
} else {
expect(formattedAst).toEqual(originalAst);
}
2020-09-04 23:43:41 +08:00
}
2020-04-10 19:12:01 +08:00
2020-11-30 12:29:48 +08:00
if (!shouldSkipEolTest(code, formatResult.options)) {
2020-09-17 07:35:33 +08:00
for (const eol of ["\r\n", "\r"]) {
2022-04-01 10:21:44 +08:00
const { eolVisualizedOutput: output } = await format(
code.replace(/\n/g, eol),
formatOptions
);
// Only if `endOfLine: "auto"` the result will be different
const expected =
formatOptions.endOfLine === "auto"
? visualizeEndOfLine(
// All `code` use `LF`, so the `eol` of result is always `LF`
formatResult.outputWithCursor.replace(/\n/g, eol)
)
: formatResult.eolVisualizedOutput;
expect(output).toEqual(expected);
2020-09-04 23:43:41 +08:00
}
}
if (code.charAt(0) !== BOM) {
2022-04-01 10:21:44 +08:00
const { eolVisualizedOutput: output } = await format(
BOM + code,
formatOptions
);
const expected = BOM + formatResult.eolVisualizedOutput;
expect(output).toEqual(expected);
}
2020-08-28 10:02:08 +08:00
}
2018-12-08 18:28:29 +08:00
2020-11-30 12:29:48 +08:00
function shouldSkipEolTest(code, options) {
if (code.includes("\r")) {
return true;
}
const { requirePragma, rangeStart, rangeEnd } = options;
if (requirePragma) {
return true;
}
if (
typeof rangeStart === "number" &&
typeof rangeEnd === "number" &&
rangeStart >= rangeEnd
) {
return true;
}
return false;
}
2022-05-04 11:15:48 +08:00
async function parse(source, options) {
2022-05-10 15:43:02 +08:00
const prettier = await getPrettier();
2022-05-04 11:15:48 +08:00
const { ast } = await prettier.__debug.parse(
source,
options,
/* massage */ true
);
return ast;
2018-12-08 18:28:29 +08:00
}
2020-09-17 07:31:29 +08:00
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 };
2020-08-10 22:17:51 +08:00
})
2020-09-17 07:31:29 +08:00
.filter(Boolean)
.sort((a, b) => a.value - b.value);
2020-08-10 22:17:51 +08:00
2020-09-17 07:31:29 +08:00
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 };
}
2018-12-08 18:28:29 +08:00
2020-09-17 07:31:29 +08:00
const insertCursor = (text, cursorOffset) =>
cursorOffset >= 0
? text.slice(0, cursorOffset) +
CURSOR_PLACEHOLDER +
text.slice(cursorOffset)
: text;
2022-04-01 10:21:44 +08:00
async function format(originalText, originalOptions) {
2020-09-17 07:31:29 +08:00
const { text: input, options } = replacePlaceholders(
originalText,
originalOptions
);
const inputWithCursor = insertCursor(input, options.cursorOffset);
2022-05-10 15:43:02 +08:00
const prettier = await getPrettier();
2020-08-10 22:17:51 +08:00
const { formatted: output, cursorOffset } = await ensurePromise(
prettier.formatWithCursor(input, options)
2020-09-17 07:31:29 +08:00
);
const outputWithCursor = insertCursor(output, cursorOffset);
2020-08-10 22:17:51 +08:00
const eolVisualizedOutput = visualizeEndOfLine(outputWithCursor);
const changed = outputWithCursor !== inputWithCursor;
return {
changed,
options,
input,
inputWithCursor,
output,
outputWithCursor,
eolVisualizedOutput,
};
2018-12-08 18:28:29 +08:00
}
2020-08-28 10:02:08 +08:00
2022-01-04 17:00:25 +08:00
export default runSpec;