From 1c568435185789c1916e2474b2e96fb2fb1d7e6f Mon Sep 17 00:00:00 2001 From: Mofei Zhang Date: Tue, 22 Aug 2023 12:54:01 -0400 Subject: [PATCH] [tests] Allow .ts, .tsx fixture files --- .../jsx-tag-evaluation-order.expect.md | 2 +- ...-order.js => jsx-tag-evaluation-order.tsx} | 2 +- .../compiler/update-expression.expect.md | 2 +- ...ate-expression.js => update-expression.ts} | 2 +- .../src/__tests__/fixtures/tsconfig.json | 7 +++++- .../fixture-test-utils/src/fixture-utils.ts | 15 +++++++++--- .../packages/sprout/src/runner-worker.ts | 24 +++++++++++-------- .../packages/sprout/src/shared-runtime.ts | 9 ++++--- 8 files changed, 42 insertions(+), 21 deletions(-) rename compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/{jsx-tag-evaluation-order.js => jsx-tag-evaluation-order.tsx} (88%) rename compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/{update-expression.js => update-expression.ts} (81%) diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-tag-evaluation-order.expect.md b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-tag-evaluation-order.expect.md index 736ffd2eb6..7118915f1b 100644 --- a/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-tag-evaluation-order.expect.md +++ b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-tag-evaluation-order.expect.md @@ -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. diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-tag-evaluation-order.js b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-tag-evaluation-order.tsx similarity index 88% rename from compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-tag-evaluation-order.js rename to compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-tag-evaluation-order.tsx index 228fe6b5f1..ce720699cc 100644 --- a/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-tag-evaluation-order.js +++ b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-tag-evaluation-order.tsx @@ -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. diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/update-expression.expect.md b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/update-expression.expect.md index d0954e238b..3afed6867b 100644 --- a/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/update-expression.expect.md +++ b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/update-expression.expect.md @@ -2,7 +2,7 @@ ## Input ```javascript -function foo(props) { +function foo(props: { x: number }) { let x = props.x; let y = x++; let z = x--; diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/update-expression.js b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/update-expression.ts similarity index 81% rename from compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/update-expression.js rename to compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/update-expression.ts index 306e87a42b..d085eb78b3 100644 --- a/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/update-expression.js +++ b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/update-expression.ts @@ -1,4 +1,4 @@ -function foo(props) { +function foo(props: { x: number }) { let x = props.x; let y = x++; let z = x--; diff --git a/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/tsconfig.json b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/tsconfig.json index 1849eb3cf8..5e3f769361 100644 --- a/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/tsconfig.json +++ b/compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/tsconfig.json @@ -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" + ] } diff --git a/compiler/forget/packages/fixture-test-utils/src/fixture-utils.ts b/compiler/forget/packages/fixture-test-utils/src/fixture-utils.ts index 1cc3bfa557..3df8d20761 100644 --- a/compiler/forget/packages/fixture-test-utils/src/fixture-utils.ts +++ b/compiler/forget/packages/fixture-test-utils/src/fixture-utils.ts @@ -67,14 +67,17 @@ export function getFixtures( filter: TestFilter | null ): Map { // 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 = 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; diff --git a/compiler/forget/packages/sprout/src/runner-worker.ts b/compiler/forget/packages/sprout/src/runner-worker.ts index a41539469f..4585d047e5 100644 --- a/compiler/forget/packages/sprout/src/runner-worker.ts +++ b/compiler/forget/packages/sprout/src/runner-worker.ts @@ -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 { console.error = (...messages: Array) => { 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 { }; } 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, diff --git a/compiler/forget/packages/sprout/src/shared-runtime.ts b/compiler/forget/packages/sprout/src/shared-runtime.ts index 6ded7f3564..076cc3c71c 100644 --- a/compiler/forget/packages/sprout/src/shared-runtime.ts +++ b/compiler/forget/packages/sprout/src/shared-runtime.ts @@ -66,15 +66,18 @@ export function sum(...args: Array): number { /** * React Components */ -export function Text(props: { value: string; children: any }) { +export function Text(props: { + value: string; + children?: Array; +}) { return React.createElement("div", null, props.value, props.children); } -export function StaticText1(props: { children: any }) { +export function StaticText1(props: { children?: Array }) { return React.createElement("div", null, "StaticText1", props.children); } -export function StaticText2(props: { children: any }) { +export function StaticText2(props: { children?: Array }) { return React.createElement("div", null, "StaticText2", props.children); }