[tests] Allow .ts, .tsx fixture files
Mofei Zhang committed
Aug 22, 2023 at 12:54 UTC
1c568435185789c1916e2474b2e96fb2fb1d7e6f
8 files changed
+42
-21
compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-tag-evaluation-order.expect.md
+1
-1
@@ -4,7 +4,7 @@
4
```javascript
5
import { StaticText1, StaticText2 } from "shared-runtime";
6
7
-function Component(props) {
7
+function Component(props: { value: string }) {
8
let Tag = StaticText1;
9
10
// Currently, Forget preserves jsx whitespace in the source text.
compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-tag-evaluation-order.tsx
renamed
+1
-1
@@ -1,6 +1,6 @@
1
import { StaticText1, StaticText2 } from "shared-runtime";
2
3
-function Component(props) {
3
+function Component(props: { value: string }) {
4
let Tag = StaticText1;
5
6
// Currently, Forget preserves jsx whitespace in the source text.
compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/update-expression.expect.md
+1
-1
@@ -2,7 +2,7 @@
2
## Input
3
4
```javascript
5
-function foo(props) {
5
+function foo(props: { x: number }) {
6
let x = props.x;
7
let y = x++;
8
let z = x--;
compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/update-expression.ts
renamed
+1
-1
@@ -1,4 +1,4 @@
1
-function foo(props) {
1
+function foo(props: { x: number }) {
2
let x = props.x;
3
let y = x++;
4
let z = x--;
compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/tsconfig.json
+6
-1
@@ -11,10 +11,15 @@
11
"useUnknownInCatchVariables": false,
12
"noUnusedLocals": false,
13
"baseUrl": ".",
14
+ "jsx": "preserve",
15
"paths": {
16
// Editor integration for sprout shared runtime files
17
"shared-runtime": ["../../../../sprout/src/shared-runtime.ts"]
18
}
19
},
19
- "include": ["./compiler/**/*.js"]
20
+ "include": [
21
+ "./compiler/**/*.js",
22
+ "./compiler/**/*.ts",
23
+ "./compiler/**/*.tsx"
24
+ ]
25
}
compiler/forget/packages/fixture-test-utils/src/fixture-utils.ts
+12
-3
@@ -67,14 +67,17 @@ export function getFixtures(
67
filter: TestFilter | null
68
): Map<string, TestFixture> {
69
// search for fixtures within nested directories
70
- const files = glob.sync(`**/*.{js,md}`, {
70
+ const files = glob.sync(`**/*.{js,ts,tsx,md}`, {
71
cwd: FIXTURES_PATH,
72
});
73
const fixtures: Map<string, TestFixture> = new Map();
74
75
for (const filePath of files) {
76
const basename = path.basename(
77
- path.basename(filePath, ".js"),
77
+ path.basename(
78
+ path.basename(path.basename(filePath, ".js"), ".ts"),
79
+ ".tsx"
80
+ ),
81
".expect.md"
82
);
83
// "partial" paths do not include suffixes
@@ -110,7 +113,13 @@ export function getFixtures(
113
fixtures.set(partialRelativePath, fixtureInfo);
114
}
115
113
- if (filePath.endsWith(".js")) {
116
+ if (
117
+ filePath.endsWith(".js") ||
118
+ filePath.endsWith(".ts") ||
119
+ filePath.endsWith(".tsx")
120
+ ) {
121
+ // inputPath may have a different file extension than the .js default
122
+ fixtureInfo.inputPath = path.join(FIXTURES_PATH, filePath);
123
fixtureInfo.inputExists = true;
124
} else {
125
fixtureInfo.outputExists = true;
compiler/forget/packages/sprout/src/runner-worker.ts
+14
-10
@@ -17,6 +17,7 @@ import fs from "fs/promises";
17
import * as parser from "@babel/parser";
18
import * as t from "@babel/types";
19
import { doEval, EvaluatorResult } from "./runner-evaluator";
20
+import path from "path";
21
22
const { runReactForgetBabelPlugin } = require(COMPILER_PATH) as {
23
runReactForgetBabelPlugin: typeof RunReactForgetBabelPlugin;
@@ -105,16 +106,17 @@ function transformAST(
106
}
107
return code;
108
}
109
+
110
function transformFixtureForget(
111
input: string,
110
- basename: string
112
+ filename: string
113
): TransformResult {
114
try {
115
const language = parseLanguage(input.split("\n", 1)[0]);
116
117
const forgetResult = transformFixtureInput(
118
input,
117
- basename,
119
+ filename,
120
runReactForgetBabelPlugin,
121
true
122
);
@@ -129,7 +131,7 @@ function transformFixtureForget(
131
const code = transformAST(
132
forgetResult.ast,
133
forgetResult.code,
132
- basename,
134
+ filename,
135
language,
136
false
137
);
@@ -147,17 +149,17 @@ function transformFixtureForget(
149
150
function transformFixtureNoForget(
151
input: string,
150
- basename: string
152
+ filename: string
153
): TransformResult {
154
try {
155
const language = parseLanguage(input.split("\n", 1)[0]);
156
const ast = parser.parse(input, {
155
- sourceFilename: basename,
157
+ sourceFilename: filename,
158
plugins: ["jsx", language],
159
sourceType: "module",
160
});
161
160
- const code = transformAST(ast, input, basename, language, true);
162
+ const code = transformAST(ast, input, filename, language, true);
163
return {
164
type: "Ok",
165
value: code,
@@ -175,8 +177,7 @@ export async function run(fixture: TestFixture): Promise<TestResult> {
177
console.error = (...messages: Array<string>) => {
178
seenConsoleErrors.push(...messages);
179
};
178
- const { inputPath, inputExists, basename } = fixture;
179
-
180
+ const { inputPath, inputExists } = fixture;
181
if (!inputExists) {
182
return {
183
nonForgetResult: null,
@@ -185,8 +186,11 @@ export async function run(fixture: TestFixture): Promise<TestResult> {
186
};
187
}
188
const inputRaw = await fs.readFile(inputPath, "utf8");
188
- const forgetCode = transformFixtureForget(inputRaw, basename);
189
- const noForgetCode = transformFixtureNoForget(inputRaw, basename);
189
+ // We need to include the file extension as it determines typescript
190
+ // babel plugin's mode (e.g. stripping types, parsing rules for brackets)
191
+ const filename = path.basename(inputPath);
192
+ const forgetCode = transformFixtureForget(inputRaw, filename);
193
+ const noForgetCode = transformFixtureNoForget(inputRaw, filename);
194
if (forgetCode.type === "UnexpectedError") {
195
return {
196
nonForgetResult: null,
compiler/forget/packages/sprout/src/shared-runtime.ts
+6
-3
@@ -66,15 +66,18 @@ export function sum(...args: Array<number>): number {
66
/**
67
* React Components
68
*/
69
-export function Text(props: { value: string; children: any }) {
69
+export function Text(props: {
70
+ value: string;
71
+ children?: Array<React.ReactNode>;
72
+}) {
73
return React.createElement("div", null, props.value, props.children);
74
}
75
73
-export function StaticText1(props: { children: any }) {
76
+export function StaticText1(props: { children?: Array<React.ReactNode> }) {
77
return React.createElement("div", null, "StaticText1", props.children);
78
}
79
77
-export function StaticText2(props: { children: any }) {
80
+export function StaticText2(props: { children?: Array<React.ReactNode> }) {
81
return React.createElement("div", null, "StaticText2", props.children);
82
}
83