Run parser tests from the official TypeScript parser (#10444)
This commit is contained in:
132
scripts/parser-tests/flow/index.js
Normal file
132
scripts/parser-tests/flow/index.js
Normal file
@@ -0,0 +1,132 @@
|
||||
const fs = require("fs").promises;
|
||||
const path = require("path");
|
||||
const merge = require("mergeiterator");
|
||||
const TestRunner = require("../utils/parser-test-runner");
|
||||
|
||||
const flowOptionsMapping = {
|
||||
esproposal_class_instance_fields: "classProperties",
|
||||
esproposal_class_static_fields: "classProperties",
|
||||
esproposal_export_star_as: "exportNamespaceFrom",
|
||||
esproposal_decorators: "decorators-legacy",
|
||||
esproposal_nullish_coalescing: "nullishCoalescingOperator",
|
||||
esproposal_optional_chaining: "optionalChaining",
|
||||
types: "flowComments",
|
||||
intern_comments: false,
|
||||
};
|
||||
|
||||
function getPlugins(test) {
|
||||
const plugins = [
|
||||
"dynamicImport",
|
||||
["flow", { all: true }],
|
||||
"flowComments",
|
||||
"jsx",
|
||||
"classProperties",
|
||||
"classPrivateProperties",
|
||||
"classPrivateMethods",
|
||||
"bigInt",
|
||||
"numericSeparator",
|
||||
];
|
||||
|
||||
if (!test.options) return plugins;
|
||||
|
||||
for (const [option, enabled] of Object.entries(test.options)) {
|
||||
if (!enabled) {
|
||||
const idx = plugins.indexOf(flowOptionsMapping[option]);
|
||||
if (idx !== -1) plugins.splice(idx, 1);
|
||||
} else if (!(option in flowOptionsMapping)) {
|
||||
throw new Error("Parser options not mapped " + option);
|
||||
} else if (flowOptionsMapping[option]) {
|
||||
plugins.push(flowOptionsMapping[option]);
|
||||
}
|
||||
}
|
||||
|
||||
return plugins;
|
||||
}
|
||||
|
||||
async function* readdirRecursive(root, dir = ".") {
|
||||
const names = await fs.readdir(path.join(root, dir));
|
||||
|
||||
const dirs = [];
|
||||
|
||||
for (const name of names) {
|
||||
const file = path.join(dir, name);
|
||||
const stats = await fs.stat(path.join(root, file));
|
||||
if (!stats.isDirectory()) {
|
||||
if (!file) continue;
|
||||
yield file;
|
||||
} else {
|
||||
dirs.push(readdirRecursive(root, file));
|
||||
}
|
||||
}
|
||||
|
||||
yield* merge(dirs);
|
||||
}
|
||||
|
||||
async function* loadTests(root) {
|
||||
for await (const file of readdirRecursive(root)) {
|
||||
if (file.slice(-3) === ".js") {
|
||||
const noExt = path.join(root, file).slice(0, -3);
|
||||
|
||||
const [contents, tree, options] = await Promise.all([
|
||||
fs.readFile(noExt + ".js", "utf8"),
|
||||
fs.readFile(noExt + ".tree.json", "utf8").catch(() => null),
|
||||
fs.readFile(noExt + ".options.json", "utf8").catch(() => null),
|
||||
]);
|
||||
|
||||
yield {
|
||||
file,
|
||||
contents,
|
||||
tree: JSON.parse(tree),
|
||||
options: JSON.parse(options),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const runner = new TestRunner({
|
||||
testDir: path.join(__dirname, "../../../build/flow/src/parser/test/flow"),
|
||||
whitelist: path.join(__dirname, "whitelist.txt"),
|
||||
shouldUpdate: process.argv.includes("--update-whitelist"),
|
||||
|
||||
async *getTests() {
|
||||
for await (const test of loadTests(this.testDir)) {
|
||||
const shouldSuccess =
|
||||
test.tree && (!test.tree.errors || !test.tree.errors.length);
|
||||
|
||||
yield {
|
||||
contents: test.contents,
|
||||
fileName: test.file,
|
||||
id: test.file,
|
||||
expectedError: !shouldSuccess,
|
||||
plugins: getPlugins(test),
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
parse(test, parser) {
|
||||
try {
|
||||
parser(test.contents, {
|
||||
sourceType: "module",
|
||||
plugins: test.plugins,
|
||||
});
|
||||
} catch (e) {
|
||||
// lets retry in script mode
|
||||
if (!test.expectedError) {
|
||||
try {
|
||||
parser(test.contents, {
|
||||
sourceType: "script",
|
||||
plugins: test.plugins,
|
||||
});
|
||||
return;
|
||||
} catch {}
|
||||
}
|
||||
|
||||
throw e;
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
runner.run().catch(err => {
|
||||
console.error(err);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
23
scripts/parser-tests/flow/whitelist.txt
Normal file
23
scripts/parser-tests/flow/whitelist.txt
Normal file
@@ -0,0 +1,23 @@
|
||||
ES6/modules/migrated_0020.js
|
||||
JSX_invalid/migrated_0000.js
|
||||
arrow_function_invalid/migrated_0002.js
|
||||
async_await/migrated_0007.js
|
||||
async_await/migrated_0020.js
|
||||
async_await/migrated_0024.js
|
||||
async_await/migrated_0027.js
|
||||
async_generators/migrated_0007.js
|
||||
class_method_kinds/polymorphic_getter.js
|
||||
class_properties/migrated_0021.js
|
||||
class_properties/migrated_0026.js
|
||||
decorators/migrated_0003.js
|
||||
export_import_reserved_words/migrated_0003.js
|
||||
export_statements/export_trailing_comma.js
|
||||
nullish_coalescing/precedence_and.js
|
||||
nullish_coalescing/precedence_or.js
|
||||
private_class_properties/getter_and_field.js
|
||||
private_class_properties/getter_duplicate.js
|
||||
private_class_properties/multiple.js
|
||||
private_class_properties/multiple.js
|
||||
private_class_properties/setter_and_field.js
|
||||
private_class_properties/setter_duplicate.js
|
||||
types/member/reserved_words.js
|
||||
Reference in New Issue
Block a user