Files
react/compiler/forget/scripts/test262-preprocessor.js
T
Lauren Tan 9027117c4f Scaffolding for test262
- Adds a shallow git submodule for test262 as the tests aren't available   as an 
npm module - To run all tests: `yarn test262:all`. Note that this chunks up the  
 tests by test262 folder as there are over 50k+ tests and the test harness only 
accepts arrays of filepaths which exceeds arg limits - To run a specific test: 
`yarn test262 test262/test/folder/file.js`.   You can also pass globs which 
expand into an array of filepaths: `yarn test262 test262/test/folder/**/*.js` - 
More instructions for the test-harness can be found here: 
https://github.com/bterlson/test262-harness
2022-11-15 13:24:14 -05:00

62 lines
2.0 KiB
JavaScript

const { argv } = require("node:process");
const generate = require("@babel/generator").default;
const parser = require("@babel/parser");
const traverse = require("@babel/traverse").default;
const { Environment } = require("../dist/HIR/HIRBuilder");
const { lower } = require("../dist/HIR/BuildHIR");
const codegen = require("../dist/HIR/Codegen").default;
const enterSSA = require("../dist/HIR/EnterSSA").default;
const { eliminateRedundantPhi } = require("../dist/HIR/EliminateRedundantPhi");
const inferReferenceEffects =
require("../dist/HIR/InferReferenceEffects").default;
const { inferMutableRanges } = require("../dist/HIR/InferMutableLifetimes");
const leaveSSA = require("../dist/HIR/LeaveSSA").default;
const prettier = require("prettier");
// Preprocessor that runs Forget on the test262 test prior to execution. Compilation errors short
// circuit test execution and report an error immediately.
module.exports = (test) => {
try {
let codegenText = null;
// todo: replace with a better entrypoint
const sourceAst = parser.parse(test.contents, {
sourceFilename: test.file,
plugins: ["jsx"],
});
traverse(sourceAst, {
FunctionDeclaration: {
enter(nodePath) {
const env = new Environment();
const ir = lower(nodePath, env);
enterSSA(ir, env);
eliminateRedundantPhi(ir);
inferReferenceEffects(ir);
inferMutableRanges(ir);
leaveSSA(ir);
const ast = codegen(ir);
codegenText = prettier.format(
generate(ast).code.replace("\n\n", "\n"),
{
semi: true,
parser: "babel-ts",
}
);
},
},
});
if (codegenText != null) {
test.contents = codegenText;
} else {
throw new Error("Codegen returned an empty string");
}
} catch (error) {
test.result = {
stderr: `${error.name}: ${error.message}\n`,
stdout: "",
error,
};
}
return test;
};