Skip to content

Commit

Permalink
Update on "compiler: ValidateNoRefInRender detects writes of refs"
Browse files Browse the repository at this point in the history
Improves ValidateNoRefAccessInRender, detecting modifications of refs during render.

Fixes #29161

[ghstack-poisoned]
  • Loading branch information
josephsavona committed May 29, 2024
2 parents a925f2b + f5d45a6 commit 6d4a083
Show file tree
Hide file tree
Showing 352 changed files with 8,598 additions and 4,604 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ module.exports = {
$ReadOnlyArray: 'readonly',
$ArrayBufferView: 'readonly',
$Shape: 'readonly',
ConsoleTask: 'readonly', // TOOD: Figure out what the official name of this will be.
ReturnType: 'readonly',
AnimationFrameID: 'readonly',
// For Flow type annotation. Only `BigInt` is valid at runtime.
Expand Down
1 change: 1 addition & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c998bb1ed4b3285398c9c7797135d3f060243c6a
22 changes: 20 additions & 2 deletions .github/workflows/compiler-typescript.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
2 changes: 0 additions & 2 deletions compiler/.git-blame-ignore-revs

This file was deleted.

19 changes: 14 additions & 5 deletions compiler/.prettierignore
Original file line number Diff line number Diff line change
@@ -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
*.map
*.sh
*.txt
*.ico
*.svg
*.lock
*.toml
9 changes: 9 additions & 0 deletions compiler/.prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const config = {
requirePragma: false,
parser: "babel-ts",
semi: true,
singleQuote: false,
trailingComma: "es5"
}

module.exports = config;
4 changes: 0 additions & 4 deletions compiler/.prettierrc.json

This file was deleted.

4 changes: 2 additions & 2 deletions compiler/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion compiler/apps/playground/__tests__/e2e/page.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ test("editor should compile successfully", async ({ page }) => {
});

// User input from hash compiles
await page.getByRole("button", { name: /JS/ }).click();
await page.screenshot({
fullPage: true,
path: "test-results/01-show-js-before.png",
Expand Down
21 changes: 7 additions & 14 deletions compiler/apps/playground/babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,13 @@ module.exports = function (api) {
api.cache(true);
return {
presets: ["next/babel"],
overrides: [
{
test: /^((?!node_modules).)*$/g,
plugins: [
[
"babel-plugin-react-compiler",
{
compilationMode: "infer",
panicThreshold: "NONE",
runtimeModule: "react-compiler-runtime",
},
],
],
},
plugins: [
[
"babel-plugin-react-compiler",
{
runtimeModule: "react-compiler-runtime",
},
],
],
};
};
1 change: 0 additions & 1 deletion compiler/apps/playground/colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* LICENSE file in the root directory of this source tree.
*/


/**
* Sync from <https://github.com/reactjs/reactjs.org/blob/main/beta/colors.js>.
*/
Expand Down
18 changes: 8 additions & 10 deletions compiler/apps/playground/components/Editor/EditorImpl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import {
} from "./Output";

function parseFunctions(
source: string,
source: string
): Array<
NodePath<
t.FunctionDeclaration | t.ArrowFunctionExpression | t.FunctionExpression
Expand Down Expand Up @@ -148,7 +148,7 @@ function isHookName(s: string): boolean {
}

function getReactFunctionType(
id: NodePath<t.Identifier | null | undefined>,
id: NodePath<t.Identifier | null | undefined>
): ReactFunctionType {
if (id && id.node && id.isIdentifier()) {
if (isHookName(id.node.name)) {
Expand Down Expand Up @@ -189,7 +189,7 @@ function compile(source: string): CompilerOutput {
severity: ErrorSeverity.Todo,
loc: fn.node.loc ?? null,
suggestions: null,
}),
})
);
continue;
}
Expand All @@ -205,7 +205,7 @@ function compile(source: string): CompilerOutput {
"_c",
null,
null,
null,
null
)) {
const fnName = fn.node.id?.name ?? null;
switch (result.kind) {
Expand Down Expand Up @@ -274,7 +274,7 @@ function compile(source: string): CompilerOutput {
reason: `Unexpected failure when transforming input! ${err}`,
loc: null,
suggestions: null,
}),
})
);
}
}
Expand All @@ -291,7 +291,7 @@ export default function Editor() {
const { enqueueSnackbar } = useSnackbar();
const compilerOutput = useMemo(
() => compile(deferredStore.source),
[deferredStore.source],
[deferredStore.source]
);

useMountEffect(() => {
Expand All @@ -305,7 +305,7 @@ export default function Editor() {
...createMessage(
"Bad URL - fell back to the default Playground.",
MessageLevel.Info,
MessageSource.Playground,
MessageSource.Playground
),
});
mountStore = defaultStore;
Expand All @@ -319,9 +319,7 @@ export default function Editor() {
return (
<>
<div className="relative flex basis top-14">
<div
className={clsx("relative sm:basis-1/4")}
>
<div className={clsx("relative sm:basis-1/4")}>
<Input
errors={
compilerOutput.kind === "err" ? compilerOutput.error.details : []
Expand Down
2 changes: 1 addition & 1 deletion compiler/apps/playground/components/Editor/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export default function Input({ errors }: Props) {
allowSyntheticDefaultImports: true,
};
monaco.languages.typescript.javascriptDefaults.setCompilerOptions(
tscOptions,
tscOptions
);
monaco.languages.typescript.typescriptDefaults.setCompilerOptions({
...tscOptions,
Expand Down
34 changes: 25 additions & 9 deletions compiler/apps/playground/components/Editor/Output.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ async function tabify(source: string, compilerOutput: CompilerOutput) {
output={text}
diff={lastPassOutput ?? null}
showInfoPanel={true}
></TextTabContent>,
></TextTabContent>
);
lastPassOutput = text;
}
Expand All @@ -122,7 +122,7 @@ async function tabify(source: string, compilerOutput: CompilerOutput) {
output={code}
diff={null}
showInfoPanel={false}
></TextTabContent>,
></TextTabContent>
);
if (sourceMapUrl) {
reorderedTabs.set(
Expand All @@ -133,7 +133,7 @@ async function tabify(source: string, compilerOutput: CompilerOutput) {
className="w-full h-monaco_small sm:h-monaco"
title="Generated Code"
/>
</>,
</>
);
}
}
Expand All @@ -145,16 +145,16 @@ async function tabify(source: string, compilerOutput: CompilerOutput) {

async function codegen(
ast: t.Program,
source: string,
source: string
): Promise<{ code: any; sourceMapUrl: string | null }> {
const generated = generate(
ast,
{ sourceMaps: true, sourceFileName: "input.js" },
source,
source
);
const sourceMapUrl = getSourceMapUrl(
generated.code,
JSON.stringify(generated.map),
JSON.stringify(generated.map)
);
const codegenOutput = await prettier.format(generated.code, {
semi: true,
Expand All @@ -172,28 +172,44 @@ function getSourceMapUrl(code: string, map: string): string | null {
code = utf16ToUTF8(code);
map = utf16ToUTF8(map);
return `https://evanw.github.io/source-map-visualization/#${btoa(
`${code.length}\0${code}${map.length}\0${map}`,
`${code.length}\0${code}${map.length}\0${map}`
)}`;
}

function Output({ store, compilerOutput }: Props) {
const [tabsOpen, setTabsOpen] = useState<Set<string>>(() => new Set());
const [tabsOpen, setTabsOpen] = useState<Set<string>>(() => new Set(["JS"]));
const [tabs, setTabs] = useState<Map<string, React.ReactNode>>(
() => new Map(),
() => new Map()
);
useEffect(() => {
tabify(store.source, compilerOutput).then((tabs) => {
setTabs(tabs);
});
}, [store.source, compilerOutput]);

const changedPasses: Set<string> = new Set();
let lastResult: string = "";
for (const [passName, results] of compilerOutput.results) {
for (const result of results) {
let currResult = "";
if (result.kind === "hir" || result.kind === "reactive") {
currResult += `function ${result.fnName}\n\n${result.value}`;
}
if (currResult !== lastResult) {
changedPasses.add(passName);
}
lastResult = currResult;
}
}

return (
<>
<TabbedWindow
defaultTab="HIR"
setTabsOpen={setTabsOpen}
tabsOpen={tabsOpen}
tabs={tabs}
changedPasses={changedPasses}
/>
{compilerOutput.kind === "err" ? (
<div
Expand Down
2 changes: 1 addition & 1 deletion compiler/apps/playground/components/Editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ const Editor = dynamic(() => import("./EditorImpl"), {
ssr: false,
});

export default Editor;
export default Editor;
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* LICENSE file in the root directory of this source tree.
*/


import type { EditorProps } from "@monaco-editor/react";

export const monacoOptions: Partial<EditorProps["options"]> = {
Expand Down
2 changes: 1 addition & 1 deletion compiler/apps/playground/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default function Header() {
<Logo
className={clsx(
"w-8 h-8 text-link",
process.env.NODE_ENV === "development" && "text-yellow-600",
process.env.NODE_ENV === "development" && "text-yellow-600"
)}
/>
<p className="hidden select-none sm:block">React Compiler Playground</p>
Expand Down
2 changes: 1 addition & 1 deletion compiler/apps/playground/components/Icons/IconGitHub.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ export const IconGitHub = memo<JSX.IntrinsicElements["svg"]>(
<path d="M10 0a10 10 0 0 0-3.16 19.49c.5.1.68-.22.68-.48l-.01-1.7c-2.78.6-3.37-1.34-3.37-1.34-.46-1.16-1.11-1.47-1.11-1.47-.9-.62.07-.6.07-.6 1 .07 1.53 1.03 1.53 1.03.9 1.52 2.34 1.08 2.91.83.1-.65.35-1.09.63-1.34-2.22-.25-4.55-1.11-4.55-4.94 0-1.1.39-1.99 1.03-2.69a3.6 3.6 0 0 1 .1-2.64s.84-.27 2.75 1.02a9.58 9.58 0 0 1 5 0c1.91-1.3 2.75-1.02 2.75-1.02.55 1.37.2 2.4.1 2.64.64.7 1.03 1.6 1.03 2.69 0 3.84-2.34 4.68-4.57 4.93.36.31.68.92.68 1.85l-.01 2.75c0 .26.18.58.69.48A10 10 0 0 0 10 0"></path>
</svg>
);
},
}
);
1 change: 0 additions & 1 deletion compiler/apps/playground/components/Logo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* LICENSE file in the root directory of this source tree.
*/


// https://github.com/reactjs/reactjs.org/blob/main/beta/src/components/Logo.tsx

export default function Logo(props: JSX.IntrinsicElements["svg"]) {
Expand Down
1 change: 0 additions & 1 deletion compiler/apps/playground/components/StoreContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* LICENSE file in the root directory of this source tree.
*/


import type { Dispatch, ReactNode } from "react";
import { useReducer } from "react";
import createContext from "../lib/createContext";
Expand Down
Loading

0 comments on commit 6d4a083

Please sign in to comment.