[tests] Allow .ts, .tsx fixture files

This commit is contained in:
Mofei Zhang
2023-08-22 12:54:01 -04:00
parent 6be03b681e
commit 1c56843518
8 changed files with 42 additions and 21 deletions
@@ -4,7 +4,7 @@
```javascript
import { StaticText1, StaticText2 } from "shared-runtime";
function Component(props) {
function Component(props: { value: string }) {
let Tag = StaticText1;
// Currently, Forget preserves jsx whitespace in the source text.
@@ -1,6 +1,6 @@
import { StaticText1, StaticText2 } from "shared-runtime";
function Component(props) {
function Component(props: { value: string }) {
let Tag = StaticText1;
// Currently, Forget preserves jsx whitespace in the source text.
@@ -2,7 +2,7 @@
## Input
```javascript
function foo(props) {
function foo(props: { x: number }) {
let x = props.x;
let y = x++;
let z = x--;
@@ -1,4 +1,4 @@
function foo(props) {
function foo(props: { x: number }) {
let x = props.x;
let y = x++;
let z = x--;
@@ -11,10 +11,15 @@
"useUnknownInCatchVariables": false,
"noUnusedLocals": false,
"baseUrl": ".",
"jsx": "preserve",
"paths": {
// Editor integration for sprout shared runtime files
"shared-runtime": ["../../../../sprout/src/shared-runtime.ts"]
}
},
"include": ["./compiler/**/*.js"]
"include": [
"./compiler/**/*.js",
"./compiler/**/*.ts",
"./compiler/**/*.tsx"
]
}
@@ -67,14 +67,17 @@ export function getFixtures(
filter: TestFilter | null
): Map<string, TestFixture> {
// search for fixtures within nested directories
const files = glob.sync(`**/*.{js,md}`, {
const files = glob.sync(`**/*.{js,ts,tsx,md}`, {
cwd: FIXTURES_PATH,
});
const fixtures: Map<string, TestFixture> = new Map();
for (const filePath of files) {
const basename = path.basename(
path.basename(filePath, ".js"),
path.basename(
path.basename(path.basename(filePath, ".js"), ".ts"),
".tsx"
),
".expect.md"
);
// "partial" paths do not include suffixes
@@ -110,7 +113,13 @@ export function getFixtures(
fixtures.set(partialRelativePath, fixtureInfo);
}
if (filePath.endsWith(".js")) {
if (
filePath.endsWith(".js") ||
filePath.endsWith(".ts") ||
filePath.endsWith(".tsx")
) {
// inputPath may have a different file extension than the .js default
fixtureInfo.inputPath = path.join(FIXTURES_PATH, filePath);
fixtureInfo.inputExists = true;
} else {
fixtureInfo.outputExists = true;
@@ -17,6 +17,7 @@ import fs from "fs/promises";
import * as parser from "@babel/parser";
import * as t from "@babel/types";
import { doEval, EvaluatorResult } from "./runner-evaluator";
import path from "path";
const { runReactForgetBabelPlugin } = require(COMPILER_PATH) as {
runReactForgetBabelPlugin: typeof RunReactForgetBabelPlugin;
@@ -105,16 +106,17 @@ function transformAST(
}
return code;
}
function transformFixtureForget(
input: string,
basename: string
filename: string
): TransformResult {
try {
const language = parseLanguage(input.split("\n", 1)[0]);
const forgetResult = transformFixtureInput(
input,
basename,
filename,
runReactForgetBabelPlugin,
true
);
@@ -129,7 +131,7 @@ function transformFixtureForget(
const code = transformAST(
forgetResult.ast,
forgetResult.code,
basename,
filename,
language,
false
);
@@ -147,17 +149,17 @@ function transformFixtureForget(
function transformFixtureNoForget(
input: string,
basename: string
filename: string
): TransformResult {
try {
const language = parseLanguage(input.split("\n", 1)[0]);
const ast = parser.parse(input, {
sourceFilename: basename,
sourceFilename: filename,
plugins: ["jsx", language],
sourceType: "module",
});
const code = transformAST(ast, input, basename, language, true);
const code = transformAST(ast, input, filename, language, true);
return {
type: "Ok",
value: code,
@@ -175,8 +177,7 @@ export async function run(fixture: TestFixture): Promise<TestResult> {
console.error = (...messages: Array<string>) => {
seenConsoleErrors.push(...messages);
};
const { inputPath, inputExists, basename } = fixture;
const { inputPath, inputExists } = fixture;
if (!inputExists) {
return {
nonForgetResult: null,
@@ -185,8 +186,11 @@ export async function run(fixture: TestFixture): Promise<TestResult> {
};
}
const inputRaw = await fs.readFile(inputPath, "utf8");
const forgetCode = transformFixtureForget(inputRaw, basename);
const noForgetCode = transformFixtureNoForget(inputRaw, basename);
// We need to include the file extension as it determines typescript
// babel plugin's mode (e.g. stripping types, parsing rules for brackets)
const filename = path.basename(inputPath);
const forgetCode = transformFixtureForget(inputRaw, filename);
const noForgetCode = transformFixtureNoForget(inputRaw, filename);
if (forgetCode.type === "UnexpectedError") {
return {
nonForgetResult: null,
@@ -66,15 +66,18 @@ export function sum(...args: Array<number>): number {
/**
* React Components
*/
export function Text(props: { value: string; children: any }) {
export function Text(props: {
value: string;
children?: Array<React.ReactNode>;
}) {
return React.createElement("div", null, props.value, props.children);
}
export function StaticText1(props: { children: any }) {
export function StaticText1(props: { children?: Array<React.ReactNode> }) {
return React.createElement("div", null, "StaticText1", props.children);
}
export function StaticText2(props: { children: any }) {
export function StaticText2(props: { children?: Array<React.ReactNode> }) {
return React.createElement("div", null, "StaticText2", props.children);
}