Files
react/compiler/forget/packages/babel-plugin-react-forget/scripts/test262-preprocessor.js
T
Lauren Tan 40d1459115 Scaffold workspaces
![image](https://github.com/facebook/react-forget/assets/1390709/bb27e33f-6a88-4b71-8ce2-aeb8db0372f7) 

This is an attempt to scaffold yarn workspaces into our repo with as minimal as 
possible changes to our current setup and directory structure. Essentially this 
PR moves current `src` into `packages/babel-plugin-react-forget` as a first step 
to keep everything working. Later on when we're ready we can split out a 
`react-compiler` package that is decoupled from Babel, but it's too early for 
that now
2023-06-05 13:35:39 -04:00

51 lines
1.7 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const {
runReactForgetBabelPlugin,
} = require("../dist/Babel/RunReactForgetBabelPlugin");
// 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 {
test.contents = runReactForgetBabelPlugin(
test.contents,
test.file,
"typescript"
).code;
} catch (error) {
// We use the `stderr` output to group errors so we can count them, so we need to dedupe errors
// that are the same but differ slightly.
let { name, message } = error;
message = message.replace(/^\/.*?:\s/, ""); // babel seems to output filenames
message = message.split(/\(\d+:\d+\)/)[0]; // some errors report line numbers and codeframes
message = message.trim();
message = formatReactForgetErrorMessage(message);
// For unknown reasons I don't have the energy to dig into some Babel error instances can't be
// written to, so we construct a psedudo object here so the correctly formatted error messages
// are emitted
test.result = {
stderr: `${name}: ${message}\n`,
stdout: "",
error: { name, message },
};
}
return test;
};
function formatReactForgetErrorMessage(message) {
// Strip codeframes so we can group errors
const matches = message.match(/^\[ReactForget\].*\n?/);
if (Array.isArray(matches) && matches.length > 0) {
return matches[0].replace("\n", "");
}
return message;
}