diff --git a/.github/workflows/compiler-typescript.yml b/.github/workflows/compiler-typescript.yml index 1448f66961..4b4a668c72 100644 --- a/.github/workflows/compiler-typescript.yml +++ b/.github/workflows/compiler-typescript.yml @@ -24,6 +24,25 @@ jobs: run: echo "matrix=$(find packages -mindepth 1 -maxdepth 1 -type d | sed 's!packages/!!g' | tr '\n' ',' | sed s/.$// | jq -Rsc '. / "," - [""]')" >> $GITHUB_OUTPUT # Hardcoded to improve parallelism for babel-plugin-react-compiler + prettier: + name: Run prettier + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 18.x + cache: "yarn" + cache-dependency-path: compiler/yarn.lock + - name: Restore cached node_modules + uses: actions/cache@v4 + with: + path: "**/node_modules" + key: ${{ runner.arch }}-${{ runner.os }}-modules-${{ hashFiles('compiler/**/yarn.lock') }} + - run: yarn install --frozen-lockfile + - run: yarn prettier:ci + + # Hardcoded to improve parallelism lint: name: Lint babel-plugin-react-compiler runs-on: ubuntu-latest @@ -40,10 +59,9 @@ jobs: path: "**/node_modules" key: ${{ runner.arch }}-${{ runner.os }}-modules-${{ hashFiles('compiler/**/yarn.lock') }} - run: yarn install --frozen-lockfile - - run: yarn workspace babel-plugin-react-compiler prettier:ci - run: yarn workspace babel-plugin-react-compiler lint - # Hardcoded to improve parallelism for babel-plugin-react-compiler + # Hardcoded to improve parallelism jest: name: Jest babel-plugin-react-compiler runs-on: ubuntu-latest diff --git a/compiler/packages/babel-plugin-react-compiler/.eslintrc.js b/compiler/.eslintrc.js similarity index 96% rename from compiler/packages/babel-plugin-react-compiler/.eslintrc.js rename to compiler/.eslintrc.js index f669ee5d41..996046a130 100644 --- a/compiler/packages/babel-plugin-react-compiler/.eslintrc.js +++ b/compiler/.eslintrc.js @@ -82,11 +82,12 @@ module.exports = { ], "@typescript-eslint/array-type": ["error", { default: "generic" }], "@typescript-eslint/triple-slash-reference": "off", + "@typescript-eslint/no-var-requires": "off" }, parser: "@typescript-eslint/parser", plugins: ["@typescript-eslint"], root: true, - ignorePatterns: ["src/__tests__/**/*", "src/**/*.d.ts", "dist/**/*"], + ignorePatterns: ["**/__tests__/**/*", "**/*.d.ts", "**/dist/**/*"], env: { node: true, }, diff --git a/compiler/.prettierignore b/compiler/.prettierignore index 68457e6f8c..410e88836c 100644 --- a/compiler/.prettierignore +++ b/compiler/.prettierignore @@ -1,12 +1,21 @@ -.fixtures/ -bench/ **/dist **/__tests__/fixtures/**/*.expect.md **/__tests__/fixtures/**/*.flow.js **/.next -test262/ -*.md + +crates +apps/playground/public + +**/LICENSE +.* +*.md* *.json *.css *.webmanifest -packages/js-fuzzer \ No newline at end of file +*.map +*.sh +*.txt +*.ico +*.svg +*.lock +*.toml diff --git a/compiler/.prettierrc.js b/compiler/.prettierrc.js new file mode 100644 index 0000000000..37917d7082 --- /dev/null +++ b/compiler/.prettierrc.js @@ -0,0 +1,9 @@ +const config = { + requirePragma: false, + parser: "babel-ts", + semi: true, + singleQuote: false, + trailingComma: "es5" +} + +module.exports = config; diff --git a/compiler/.prettierrc.json b/compiler/.prettierrc.json deleted file mode 100644 index d2203e6dc6..0000000000 --- a/compiler/.prettierrc.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "requirePragma": false, - "parser": "babel-ts" -} diff --git a/compiler/package.json b/compiler/package.json index 76386be1d8..868d9b069b 100644 --- a/compiler/package.json +++ b/compiler/package.json @@ -24,6 +24,8 @@ "build": "yarn workspaces run build", "dev": "concurrently --kill-others -n compiler,runtime,playground \"yarn workspace babel-plugin-react-compiler run build --watch\" \"yarn workspace react-compiler-runtime run build --watch\" \"wait-on packages/babel-plugin-react-compiler/dist/index.js && yarn workspace playground run dev\"", "test": "yarn workspaces run test", + "prettier:write": "prettier --write . --log-level=warn", + "prettier:ci": "prettier --check . --log-level=warn", "snap": "yarn workspace babel-plugin-react-compiler run snap", "snap:build": "yarn workspace snap run build", "postinstall": "perl -p -i -e 's/react\\.element/react.transitional.element/' packages/snap/node_modules/fbt/lib/FbtReactUtil.js && perl -p -i -e 's/didWarnAboutUsingAct = false;/didWarnAboutUsingAct = true;/' packages/babel-plugin-react-compiler/node_modules/react-dom/cjs/react-dom-test-utils.development.js", @@ -40,6 +42,7 @@ "concurrently": "^7.4.0", "folder-hash": "^4.0.4", "ora": "5.4.1", + "prettier": "^3.2.5", "prompt-promise": "^1.0.3", "rollup": "^4.13.2", "rollup-plugin-banner2": "^1.2.3", diff --git a/compiler/packages/babel-plugin-react-compiler/.prettierignore b/compiler/packages/babel-plugin-react-compiler/.prettierignore deleted file mode 100644 index a1e3528290..0000000000 --- a/compiler/packages/babel-plugin-react-compiler/.prettierignore +++ /dev/null @@ -1,5 +0,0 @@ -**/dist -**/__tests__/fixtures/**/*.expect.md -**/__tests__/fixtures/**/*.flow.js -*.md -*.json \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/.prettierrc.json b/compiler/packages/babel-plugin-react-compiler/.prettierrc.json deleted file mode 100644 index 9d725d0136..0000000000 --- a/compiler/packages/babel-plugin-react-compiler/.prettierrc.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "requirePragma": false, - "parser": "babel-ts" -} \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/package.json b/compiler/packages/babel-plugin-react-compiler/package.json index 746120e0ff..4d1d9f35a7 100644 --- a/compiler/packages/babel-plugin-react-compiler/package.json +++ b/compiler/packages/babel-plugin-react-compiler/package.json @@ -15,9 +15,6 @@ "snap:build": "yarn workspace snap run build", "snap:ci": "yarn snap:build && yarn snap", "ts:analyze-trace": "scripts/ts-analyze-trace.sh", - "prettier": "node ./scripts/prettier.js write-changed", - "prettier:all": "node ./scripts/prettier.js write", - "prettier:ci": "prettier --check .", "lint": "yarn eslint src" }, "dependencies": { @@ -53,7 +50,6 @@ "glob": "^7.1.6", "jest": "^29.0.3", "jest-environment-jsdom": "^29.0.3", - "prettier": "2.8.8", "react": "19.0.0-beta-b498834eab-20240506", "react-dom": "19.0.0-beta-b498834eab-20240506", "rimraf": "^3.0.2", diff --git a/compiler/packages/babel-plugin-react-compiler/scripts/prettier.js b/compiler/packages/babel-plugin-react-compiler/scripts/prettier.js deleted file mode 100644 index d5b8a1cda7..0000000000 --- a/compiler/packages/babel-plugin-react-compiler/scripts/prettier.js +++ /dev/null @@ -1,79 +0,0 @@ -/** - * 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. - */ - -"use strict"; - -/* - * Based on similar script in React - * https://github.com/facebook/react/blob/main/scripts/prettier/index.js - */ - -const chalk = require("chalk"); -const glob = require("glob"); -const prettier = require("prettier"); -const fs = require("fs"); -const listChangedFiles = require("./shared/list-changed-files"); -const prettierConfigPath = require.resolve("../.prettierrc"); - -const mode = process.argv[2] || "check"; -const shouldWrite = mode === "write" || mode === "write-changed"; -const onlyChanged = mode === "check-changed" || mode === "write-changed"; - -const changedFiles = onlyChanged ? listChangedFiles() : null; -let didWarn = false; -let didError = false; - -const files = glob - .sync("**/*.{js,ts,tsx,jsx}", { - ignore: ["**/node_modules/**", "**/__tests__/fixtures/**/*.flow.js"], - }) - .filter((f) => !onlyChanged || changedFiles.has(f)); -if (!files.length) { - return; -} - -files.forEach((file) => { - const options = prettier.resolveConfig.sync(file, { - config: prettierConfigPath, - }); - try { - const input = fs.readFileSync(file, "utf8"); - if (shouldWrite) { - const output = prettier.format(input, options); - if (output !== input) { - fs.writeFileSync(file, output, "utf8"); - } - } else { - if (!prettier.check(input, options)) { - if (!didWarn) { - console.log( - "\n" + - chalk.red( - ` This project uses prettier to format all JavaScript code.\n` - ) + - chalk.dim(` Please run `) + - chalk.reset("yarn prettier:all") + - chalk.dim( - ` and add changes to files listed below to your commit:` - ) + - `\n\n` - ); - didWarn = true; - } - console.log(file); - } - } - } catch (error) { - didError = true; - console.log("\n\n" + error.message); - console.log(file); - } -}); - -if (didWarn || didError) { - process.exitCode = 1; -} diff --git a/compiler/packages/babel-plugin-react-compiler/scripts/shared/list-changed-files.js b/compiler/packages/babel-plugin-react-compiler/scripts/shared/list-changed-files.js deleted file mode 100644 index f6a5e61bee..0000000000 --- a/compiler/packages/babel-plugin-react-compiler/scripts/shared/list-changed-files.js +++ /dev/null @@ -1,39 +0,0 @@ -/** - * 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. - */ - -"use strict"; - -const execFileSync = require("child_process").execFileSync; - -const exec = (command, args) => { - console.log("> " + [command].concat(args).join(" ")); - const options = { - cwd: process.cwd(), - env: process.env, - stdio: "pipe", - encoding: "utf-8", - }; - return execFileSync(command, args, options); -}; - -const execGitCmd = (args) => exec("git", args).trim().toString().split("\n"); - -const listChangedFiles = () => { - const mergeBase = execGitCmd(["merge-base", "HEAD", "main"]); - return new Set([ - ...execGitCmd([ - "diff", - "--name-only", - "--relative", - "--diff-filter=ACMRTUB", - mergeBase, - ]), - ...execGitCmd(["ls-files", "--others", "--exclude-standard"]), - ]); -}; - -module.exports = listChangedFiles; diff --git a/compiler/packages/make-read-only-util/package.json b/compiler/packages/make-read-only-util/package.json index e07616700c..fd79714bb4 100644 --- a/compiler/packages/make-read-only-util/package.json +++ b/compiler/packages/make-read-only-util/package.json @@ -17,7 +17,6 @@ "@types/jest": "^28.1.6", "@types/node": "^20.2.5", "jest": "^28.1.3", - "prettier": "2.8.8", "ts-jest": "^28.0.7", "ts-node": "^10.9.2" } diff --git a/compiler/packages/snap/package.json b/compiler/packages/snap/package.json index df3af415a8..60ccff029c 100644 --- a/compiler/packages/snap/package.json +++ b/compiler/packages/snap/package.json @@ -31,7 +31,6 @@ "glob": "^10.3.10", "hermes-parser": "^0.19.1", "jsdom": "^22.1.0", - "prettier": "2.8.8", "react": "19.0.0-beta-b498834eab-20240506", "react-dom": "19.0.0-beta-b498834eab-20240506", "readline": "^1.3.0", @@ -50,7 +49,6 @@ "@types/node": "^18.7.18", "@typescript-eslint/eslint-plugin": "^7.4.0", "@typescript-eslint/parser": "^7.4.0", - "prettier": "2.8.8", "rimraf": "^3.0.2" }, "resolutions": { diff --git a/compiler/packages/snap/src/compiler.ts b/compiler/packages/snap/src/compiler.ts index 69b909645a..6c947273b1 100644 --- a/compiler/packages/snap/src/compiler.ts +++ b/compiler/packages/snap/src/compiler.ts @@ -271,8 +271,8 @@ function getEvaluatorPresets( ); return presets; } -function format(inputCode: string, language: "typescript" | "flow"): string { - return prettier.format(inputCode, { +async function format(inputCode: string, language: "typescript" | "flow"): Promise { + return await prettier.format(inputCode, { semi: true, parser: language === "typescript" ? "babel-ts" : "flow", }); @@ -288,13 +288,13 @@ export type TransformResult = { } | null; }; -export function transformFixtureInput( +export async function transformFixtureInput( input: string, fixturePath: string, parseConfigPragmaFn: typeof ParseConfigPragma, plugin: BabelCore.PluginObj, includeEvaluator: boolean -): { kind: "ok"; value: TransformResult } | { kind: "err"; msg: string } { +): Promise<{ kind: "ok"; value: TransformResult } | { kind: "err"; msg: string }> { // Extract the first line to quickly check for custom test directives const firstLine = input.substring(0, input.indexOf("\n")); @@ -398,7 +398,7 @@ export function transformFixtureInput( return { kind: "ok", value: { - forgetOutput: format(forgetOutput, language), + forgetOutput: await format(forgetOutput, language), evaluatorCode, }, }; diff --git a/compiler/packages/snap/src/runner-worker.ts b/compiler/packages/snap/src/runner-worker.ts index 47a0be00d6..54977f9b9a 100644 --- a/compiler/packages/snap/src/runner-worker.ts +++ b/compiler/packages/snap/src/runner-worker.ts @@ -33,16 +33,16 @@ export function clearRequireCache() { }); } -function compile( +async function compile( input: string, fixturePath: string, compilerVersion: number, shouldLog: boolean, includeEvaluator: boolean -): { +): Promise<{ error: string | null; compileResult: TransformResult | null; -} { +}> { const seenConsoleErrors: Array = []; console.error = (...messages: Array) => { seenConsoleErrors.push(...messages); @@ -68,7 +68,7 @@ function compile( // only try logging if we filtered out all but one fixture, // since console log order is non-deterministic toggleLogging(shouldLog); - const result = transformFixtureInput( + const result = await transformFixtureInput( input, fixturePath, parseConfigPragma, @@ -147,7 +147,7 @@ export async function transformFixture( unexpectedError: null, }; } - const { compileResult, error } = compile( + const { compileResult, error } = await compile( input, fixture.fixturePath, compilerVersion, diff --git a/compiler/yarn.lock b/compiler/yarn.lock index ff6485ad35..4e4c82e193 100644 --- a/compiler/yarn.lock +++ b/compiler/yarn.lock @@ -8423,16 +8423,11 @@ prelude-ls@~1.1.2: resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54" integrity sha512-ESF23V4SKG6lVSGZgYNpbsiaAkdab6ZgOxe52p7+Kid3W3u3bxR4Vfd/o21dmN7jSt0IwgZ4v5MUd26FEtXE9w== -prettier@*: +prettier@*, prettier@^3.2.5: version "3.2.5" resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.2.5.tgz#e52bc3090586e824964a8813b09aba6233b28368" integrity sha512-3/GWa9aOC0YeD7LUfvOG2NiDyhOWRvt1k+rcKhOuYnMY24iiCphgneUfJDyFXd6rZCAnuLBv6UeAULtrhT/F4A== -prettier@2.8.8: - version "2.8.8" - resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.8.8.tgz#e8c5d7e98a4305ffe3de2e1fc4aca1a71c28b1da" - integrity sha512-tdN8qQGvNjw4CHbY+XXk0JgCXn9QiF21a55rBe5LJAU+kDyC4WQn4+awm2Xfk2lQMk5fKup9XgzTZtGkjBdP9Q== - prettier@3.0.3: version "3.0.3" resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.0.3.tgz#432a51f7ba422d1469096c0fdc28e235db8f9643"