Integrate babylon into babel workflow

This commit is contained in:
Daniel Tschinder
2017-10-14 20:04:20 +02:00
parent 52537410ef
commit 3b540e3f5a
49 changed files with 685 additions and 5379 deletions

View File

@@ -0,0 +1,60 @@
# This file lists tests that are known to produce incorrect results when parsed
# with Babylon:
#
# - Tests that are expected to parse successfully but for which Babylon reports
# a syntax error
# - Tests that contain invalid syntax but for which Babylon reports no syntax
# error
#
# Entries should be removed incrementally as Babylon is improved.
ES6/binding-pattern/object-pattern/await-prop-in-async-function.js
JSX_invalid/migrated_0000.js
arrow_function_invalid/migrated_0002.js
async_await/async_generic_method.js
async_await/migrated_0020.js
async_await/migrated_0024.js
async_await/migrated_0027.js
async_generators/migrated_0007.js
class_properties/migrated_0000.js
class_properties/migrated_0005.js
class_properties/migrated_0011.js
class_properties/migrated_0016.js
class_properties/migrated_0021.js
class_properties/migrated_0026.js
decorators/migrated_0003.js
decorators/migrated_0007.js
dynamic_import/migrated_0000.js
dynamic_import/migrated_0001.js
dynamic_import/migrated_0002.js
dynamic_import/migrated_0003.js
dynamic_import/migrated_0004.js
invalid_syntax/migrated_0000.js
invalid_syntax/migrated_0001.js
invalid_syntax/migrated_0002.js
invalid_syntax/migrated_0003.js
invalid_syntax/migrated_0010.js
private_class_properties/valid.js
types/annotations/migrated_0001.js
types/annotations_in_comments_invalid/migrated_0000.js
types/annotations_in_comments_invalid/migrated_0001.js
types/annotations_in_comments_invalid/migrated_0002.js
types/annotations_in_comments_invalid/migrated_0003.js
types/annotations_in_comments_invalid/migrated_0004.js
types/annotations/static_is_reserved_param.js
types/annotations/static_is_reserved_type.js
types/annotations/void_is_reserved_param.js
types/declare_export_invalid/migrated_0013.js
types/declare_statements_invalid/migrated_0001.js
types/member/reserved_words.js
types/number_literal_invalid/migrated_0000.js
types/parameter_defaults/migrated_0023.js
types/parameter_defaults/migrated_0026.js
types/parameter_defaults/migrated_0028.js
types/parameter_defaults/migrated_0029.js
types/parameter_defaults/migrated_0030.js
types/parameter_defaults/migrated_0031.js
types/parameter_defaults/migrated_0032.js
types/string_literal_invalid/migrated_0000.js
types/typecasts_invalid/migrated_0001.js
types/import_types/typeof_named_reserved_type.js

View File

@@ -0,0 +1,275 @@
"use strict";
const path = require("path");
const fs = require("fs");
const chalk = require("chalk");
const parse = require("../../../packages/babylon").parse;
const TESTS_FOLDER = path.join(
__dirname,
"../../../build/flow/src/parser/test/flow"
);
const WHITELIST_PATH = path.join(__dirname, "./flow_tests_whitelist.txt");
const shouldUpdateWhitelist = process.argv.indexOf("--update-whitelist") > 0;
function map_get_default(map, key, defaultConstructor) {
if (map.has(key)) {
return map.get(key);
}
const value = new defaultConstructor();
map.set(key, value);
return value;
}
function get_whitelist(filename) {
return fs
.readFileSync(filename, "utf8")
.split("\n")
.map(line => line.replace(/#.*$/, "").trim())
.filter(Boolean);
}
function list_files(root, dir) {
const files = fs.readdirSync(dir ? path.join(root, dir) : root);
let result = [];
for (let i = 0; i < files.length; i++) {
const file = dir ? path.join(dir, files[i]) : files[i];
const stats = fs.statSync(path.join(root, file));
if (stats.isDirectory()) {
result = result.concat(list_files(root, file));
} else {
result.push(file);
}
}
return result.sort();
}
function get_tests(root_dir) {
const files = list_files(root_dir);
const tests = new Map();
for (let i = 0; i < files.length; i++) {
const file = files[i];
const test_name = path.dirname(file);
const case_parts = path.basename(file).split(".");
const case_name = case_parts[0];
// Hack to ignore hidden files.
if (case_name === "") {
continue;
}
const cases = map_get_default(tests, test_name, Map);
const case_ = map_get_default(cases, case_name, Object);
const content = fs.readFileSync(path.join(root_dir, file), "utf8");
const ext = case_parts[case_parts.length - 1];
const kind =
case_parts.length > 2 ? case_parts[case_parts.length - 2] : null;
if (ext === "js") {
case_.file = file;
case_.content = content;
} else if (ext === "json" && kind === "tree") {
case_.expected_ast = JSON.parse(content);
} else if (ext === "json" && kind === "options") {
case_.options = JSON.parse(content);
}
}
return tests;
}
function update_whitelist(summary) {
const contains = (tests, file) =>
tests.some(({ test }) => test.file === file);
const result = fs
.readFileSync(WHITELIST_PATH, "utf8")
.split("\n")
.filter(line => {
const file = line.replace(/#.*$/, "").trim();
return (
!contains(summary.disallowed.success, file) &&
!contains(summary.disallowed.failure, file) &&
summary.unrecognized.indexOf(file) === -1
);
})
.join("\n");
fs.writeFileSync(WHITELIST_PATH, result);
}
const options = {
plugins: ["jsx", "flow", "asyncGenerators", "objectRestSpread"],
sourceType: "module",
ranges: true,
};
const flowOptionsMapping = {
esproposal_class_instance_fields: "classProperties",
esproposal_class_static_fields: "classProperties",
esproposal_export_star_as: "exportExtensions",
esproposal_decorators: "decorators",
};
const summary = {
passed: true,
allowed: {
success: [],
failure: [],
},
disallowed: {
success: [],
failure: [],
},
unrecognized: [],
};
const tests = get_tests(TESTS_FOLDER);
const whitelist = get_whitelist(WHITELIST_PATH);
const unrecognized = new Set(whitelist);
tests.forEach(section => {
section.forEach(test => {
const shouldSuccess =
test.expected_ast &&
(!Array.isArray(test.expected_ast.errors) ||
test.expected_ast.errors.length === 0);
const inWhitelist = whitelist.indexOf(test.file) > -1;
const babylonOptions = Object.assign({}, options);
babylonOptions.plugins = babylonOptions.plugins.slice();
if (test.options) {
Object.keys(test.options).forEach(option => {
if (!test.options[option]) return;
if (!flowOptionsMapping[option]) {
throw new Error("Parser options not mapped " + option);
}
babylonOptions.plugins.push(flowOptionsMapping[option]);
});
}
let failed = false;
let exception = null;
try {
parse(test.content, babylonOptions);
} catch (e) {
exception = e;
failed = true;
// lets retry in script mode
try {
parse(
test.content,
Object.assign({}, babylonOptions, { sourceType: "script" })
);
exception = null;
failed = false;
} catch (e) {}
}
const isSuccess = shouldSuccess !== failed;
const isAllowed = isSuccess !== inWhitelist;
summary[isAllowed ? "allowed" : "disallowed"][
isSuccess ? "success" : "failure"
].push({ test, exception, shouldSuccess, babylonOptions });
summary.passed &= isAllowed;
unrecognized.delete(test.file);
process.stdout.write(chalk.gray("."));
});
});
summary.unrecognized = Array.from(unrecognized);
summary.passed &= summary.unrecognized.length === 0;
// This is needed because, after the dots written using
// `process.stdout.write(".")` there is no final newline
console.log();
if (summary.disallowed.failure.length || summary.disallowed.success.length) {
console.log("\n-- FAILED TESTS --");
summary.disallowed.failure.forEach(
({ test, shouldSuccess, exception, babylonOptions }) => {
console.log(chalk.red(`${test.file}`));
if (shouldSuccess) {
console.log(chalk.yellow(" Should parse successfully, but did not"));
console.log(chalk.yellow(` Failed with: \`${exception.message}\``));
} else {
console.log(chalk.yellow(" Should fail parsing, but did not"));
}
console.log(
chalk.yellow(
` Active plugins: ${JSON.stringify(babylonOptions.plugins)}`
)
);
}
);
summary.disallowed.success.forEach(
({ test, shouldSuccess, babylonOptions }) => {
console.log(chalk.red(`${test.file}`));
if (shouldSuccess) {
console.log(
chalk.yellow(
" Correctly parsed successfully, but" +
" was disallowed by the whitelist"
)
);
} else {
console.log(
chalk.yellow(
" Correctly failed parsing, but" +
" was disallowed by the whitelist"
)
);
}
console.log(
chalk.yellow(
` Active plugins: ${JSON.stringify(babylonOptions.plugins)}`
)
);
}
);
}
console.log("-- SUMMARY --");
console.log(
chalk.green("✔ " + summary.allowed.success.length + " tests passed")
);
console.log(
chalk.green(
"✔ " +
summary.allowed.failure.length +
" tests failed but were allowed in the whitelist"
)
);
console.log(
chalk.red("✘ " + summary.disallowed.failure.length + " tests failed")
);
console.log(
chalk.red(
"✘ " +
summary.disallowed.success.length +
" tests passed but were disallowed in the whitelist"
)
);
console.log(
chalk.red(
"✘ " +
summary.unrecognized.length +
" tests specified in the whitelist were not found"
)
);
// Some padding to separate the output from the message `make`
// adds at the end of failing scripts
console.log();
if (shouldUpdateWhitelist) {
update_whitelist(summary);
console.log("\nWhitelist updated");
} else {
process.exit(summary.passed ? 0 : 1);
}

View File

@@ -0,0 +1,113 @@
"use strict";
const path = require("path");
const chalk = require("chalk");
const utils = require("./run_babylon_test262_utils");
const testDir = path.join(__dirname, "../../../build/test262/test");
const whitelistFile = path.join(__dirname, "test262_whitelist.txt");
const plugins = ["asyncGenerators", "objectRestSpread", "optionalCatchBinding"];
const shouldUpdate = process.argv.indexOf("--update-whitelist") > -1;
Promise.all([utils.getTests(testDir), utils.getWhitelist(whitelistFile)])
.then(function([tests, whitelist]) {
const total = tests.length;
const reportInc = Math.floor(total / 20);
console.log(`Now running ${total} tests...`);
const results = tests.map(function(test, idx) {
if (idx % reportInc === 0) {
console.log(`> ${Math.round(100 * idx / total)}% complete`);
}
return utils.runTest(test, plugins);
});
return utils.interpret(results, whitelist);
})
.then(function(summary) {
const goodnews = [
summary.allowed.success.length + " valid programs parsed without error",
summary.allowed.failure.length +
" invalid programs produced a parsing error",
summary.allowed.falsePositive.length +
" invalid programs did not produce a parsing error" +
" (and allowed by the whitelist file)",
summary.allowed.falseNegative.length +
" valid programs produced a parsing error" +
" (and allowed by the whitelist file)",
];
const badnews = [];
const badnewsDetails = [];
void [
{
tests: summary.disallowed.success,
label:
"valid programs parsed without error" +
" (in violation of the whitelist file)",
},
{
tests: summary.disallowed.failure,
label:
"invalid programs produced a parsing error" +
" (in violation of the whitelist file)",
},
{
tests: summary.disallowed.falsePositive,
label:
"invalid programs did not produce a parsing error" +
" (without a corresponding entry in the whitelist file)",
},
{
tests: summary.disallowed.falseNegative,
label:
"valid programs produced a parsing error" +
" (without a corresponding entry in the whitelist file)",
},
{
tests: summary.unrecognized,
label: "non-existent programs specified in the whitelist file",
},
].forEach(function({ tests, label }) {
if (!tests.length) {
return;
}
const desc = tests.length + " " + label;
badnews.push(desc);
badnewsDetails.push(desc + ":");
badnewsDetails.push(
...tests.map(function(test) {
return test.id || test;
})
);
});
console.log("Testing complete.");
console.log("Summary:");
console.log(chalk.green(goodnews.join("\n").replace(/^/gm, " ✔ ")));
if (!summary.passed) {
console.log("");
console.log(chalk.red(badnews.join("\n").replace(/^/gm, " ✘ ")));
console.log("");
console.log("Details:");
console.log(badnewsDetails.join("\n").replace(/^/gm, " "));
}
if (shouldUpdate) {
return utils.updateWhitelist(whitelistFile, summary).then(function() {
console.log("");
console.log("Whitelist file updated.");
});
} else {
process.exitCode = summary.passed ? 0 : 1;
}
})
.catch(function(err) {
console.error(err);
process.exitCode = 1;
});

View File

@@ -0,0 +1,233 @@
"use strict";
const fs = require("graceful-fs");
const path = require("path");
const promisify = require("util.promisify");
const pfs = {
readFile: promisify(fs.readFile),
writeFile: promisify(fs.writeFile),
readdir: promisify(fs.readdir),
stat: promisify(fs.stat),
};
const parse = require("../../../packages/babylon").parse;
const modulePattern = /^\s*-\s*module\s*$|^\s*flags\s*:.*\bmodule\b/m;
const noStrictPattern = /^\s*-\s*noStrict\s*$|^\s*flags\s*:.*\bnoStrict\b/m;
const onlyStrictPattern = /^\s*-\s*onlyStrict\s*$|^\s*flags\s*:.*\bonlyStrict\b/m;
const rawPattern = /^\s*-\s*raw\s*$|^\s*flags\s*:.*\braw\b/m;
const testNamePattern = /^(?!.*_FIXTURE).*\.[jJ][sS]$/;
function flatten(array) {
const flattened = [];
array.forEach(function(element) {
if (Array.isArray(element)) {
flattened.push.apply(flattened, element);
} else {
flattened.push(element);
}
});
return flattened;
}
function hasEarlyError(src) {
return !!(
src.match(/^\s*negative:\s*$/m) && src.match(/^\s+phase:\s*early\s*$/m)
);
}
function readDirDeep(dirName) {
return pfs.readdir(dirName).then(function(contents) {
return Promise.all(
contents.map(function(name) {
return findTests(path.join(dirName, name));
})
).then(flatten);
});
}
function findTests(name) {
return pfs.stat(name).then(function(stat) {
if (stat.isDirectory()) {
return readDirDeep(name);
}
return name;
});
}
function readTest(fileName, testDir) {
if (!testNamePattern.test(fileName)) {
return Promise.resolve([]);
}
return pfs.readFile(fileName, "utf-8").then(function(contents) {
return makeScenarios(path.relative(testDir, fileName), contents);
});
}
function makeScenarios(fileName, testContent) {
const scenarios = [];
const base = {
fileName: fileName,
isModule: modulePattern.test(testContent),
expectedError: hasEarlyError(testContent),
};
const isNoStrict = noStrictPattern.test(testContent);
const isOnlyStrict = onlyStrictPattern.test(testContent);
const isRaw = rawPattern.test(testContent);
if (!isOnlyStrict) {
scenarios.push(
Object.assign(
{
id: fileName + "(default)",
content: testContent,
},
base
)
);
}
if (!isNoStrict && !isRaw) {
scenarios.push(
Object.assign(
{
id: fileName + "(strict mode)",
content: "'use strict';\n" + testContent,
},
base
)
);
}
return scenarios;
}
exports.getTests = function(testDir) {
return findTests(testDir)
.then(function(testPaths) {
return Promise.all(
testPaths.map(function(path) {
return readTest(path, testDir);
})
);
})
.then(flatten);
};
exports.runTest = function(test, plugins) {
const sourceType = test.isModule ? "module" : "script";
try {
parse(test.content, { sourceType: sourceType, plugins: plugins });
test.actualError = false;
} catch (err) {
test.actualError = true;
}
test.result = test.expectedError !== test.actualError ? "fail" : "pass";
return test;
};
exports.getWhitelist = function(filename) {
return pfs.readFile(filename, "utf-8").then(function(contents) {
return contents
.split("\n")
.map(function(line) {
return line.replace(/#.*$/, "").trim();
})
.filter(function(line) {
return line.length > 0;
})
.reduce(function(table, filename) {
table[filename] = true;
return table;
}, Object.create(null));
});
};
exports.updateWhitelist = function(filename, summary) {
return pfs.readFile(filename, "utf-8").then(function(contents) {
const toRemove = summary.disallowed.success
.concat(summary.disallowed.failure)
.map(function(test) {
return test.id;
});
const toAdd = summary.disallowed.falsePositive
.concat(summary.disallowed.falseNegative)
.map(function(test) {
return test.id;
});
const newContents = contents
.split("\n")
.map(function(line) {
const testId = line.replace(/#.*$/, "").trim();
if (toRemove.indexOf(testId) > -1) {
return null;
}
return line;
})
.filter(function(line) {
return line !== null;
})
.concat(toAdd)
.join("\n");
return pfs.writeFile(filename, newContents, "utf-8");
});
};
exports.interpret = function(results, whitelist) {
const summary = {
passed: true,
allowed: {
success: [],
failure: [],
falsePositive: [],
falseNegative: [],
},
disallowed: {
success: [],
failure: [],
falsePositive: [],
falseNegative: [],
},
unrecognized: null,
};
results.forEach(function(result) {
let classification, isAllowed;
const inWhitelist = result.id in whitelist;
delete whitelist[result.id];
if (!result.expectedError) {
if (!result.actualError) {
classification = "success";
isAllowed = !inWhitelist;
} else {
classification = "falseNegative";
isAllowed = inWhitelist;
}
} else {
if (!result.actualError) {
classification = "falsePositive";
isAllowed = inWhitelist;
} else {
classification = "failure";
isAllowed = !inWhitelist;
}
}
summary.passed &= isAllowed;
summary[isAllowed ? "allowed" : "disallowed"][classification].push(result);
});
summary.unrecognized = Object.keys(whitelist);
summary.passed = !!summary.passed && summary.unrecognized.length === 0;
return summary;
};

File diff suppressed because it is too large Load Diff