Skip to content

Commit b13f5b3

Browse files
committed
[compiler][entrypoint] Fix edgecases for noEmit and opt-outs
Title
1 parent 2036a97 commit b13f5b3

File tree

4 files changed

+36
-31
lines changed

4 files changed

+36
-31
lines changed

compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Imports.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ type ProgramContextOptions = {
5959
opts: PluginOptions;
6060
filename: string | null;
6161
code: string | null;
62+
hasModuleScopeOptOut: boolean;
6263
};
6364
export class ProgramContext {
6465
/**
@@ -70,6 +71,7 @@ export class ProgramContext {
7071
code: string | null;
7172
reactRuntimeModule: string;
7273
suppressions: Array<SuppressionRange>;
74+
hasModuleScopeOptOut: boolean;
7375

7476
/*
7577
* This is a hack to work around what seems to be a Babel bug. Babel doesn't
@@ -94,13 +96,15 @@ export class ProgramContext {
9496
opts,
9597
filename,
9698
code,
99+
hasModuleScopeOptOut,
97100
}: ProgramContextOptions) {
98101
this.scope = program.scope;
99102
this.opts = opts;
100103
this.filename = filename;
101104
this.code = code;
102105
this.reactRuntimeModule = getReactCompilerRuntimeModule(opts.target);
103106
this.suppressions = suppressions;
107+
this.hasModuleScopeOptOut = hasModuleScopeOptOut;
104108
}
105109

106110
isHookName(name: string): boolean {

compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,8 @@ export function compileProgram(
325325
filename: pass.filename,
326326
code: pass.code,
327327
suppressions,
328+
hasModuleScopeOptOut:
329+
findDirectiveDisablingMemoization(program.node.directives) != null,
328330
});
329331

330332
const queue: Array<CompileSource> = findFunctionsToCompile(
@@ -368,7 +370,19 @@ export function compileProgram(
368370
}
369371

370372
// Avoid modifying the program if we find a program level opt-out
371-
if (findDirectiveDisablingMemoization(program.node.directives) != null) {
373+
if (programContext.hasModuleScopeOptOut) {
374+
if (compiledFns.length > 0) {
375+
const error = new CompilerError();
376+
error.pushErrorDetail(
377+
new CompilerErrorDetail({
378+
reason:
379+
'Unexpected compiled functions when module scope opt-out is present',
380+
severity: ErrorSeverity.Invariant,
381+
loc: null,
382+
}),
383+
);
384+
handleError(error, programContext, null);
385+
}
372386
return null;
373387
}
374388

@@ -520,32 +534,33 @@ function processFn(
520534
prunedMemoValues: compiledFn.prunedMemoValues,
521535
});
522536

537+
/**
538+
* inferEffectDependencies + noEmit is currently only used for linting. In
539+
* this mode, add source locations for where the compiler *can* infer effect
540+
* dependencies.
541+
*/
542+
for (const loc of compileResult.compiledFn.inferredEffectLocations) {
543+
if (loc !== GeneratedSource) {
544+
programContext.inferredEffectLocations.add(loc);
545+
}
546+
}
547+
523548
/**
524549
* Always compile functions with opt in directives.
525550
*/
526-
if (directives.optIn != null) {
551+
if (programContext.hasModuleScopeOptOut || programContext.opts.noEmit) {
552+
return null;
553+
} else if (directives.optIn != null) {
527554
return compiledFn;
528555
} else if (programContext.opts.compilationMode === 'annotation') {
529556
/**
530557
* If no opt-in directive is found and the compiler is configured in
531558
* annotation mode, don't insert the compiled function.
532559
*/
533560
return null;
534-
} else if (!programContext.opts.noEmit) {
561+
} else {
535562
return compiledFn;
536563
}
537-
538-
/**
539-
* inferEffectDependencies + noEmit is currently only used for linting. In
540-
* this mode, add source locations for where the compiler *can* infer effect
541-
* dependencies.
542-
*/
543-
for (const loc of compileResult.compiledFn.inferredEffectLocations) {
544-
if (loc !== GeneratedSource) {
545-
programContext.inferredEffectLocations.add(loc);
546-
}
547-
}
548-
return null;
549564
}
550565

551566
function tryCompileFunction(

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/repro-bailout-nopanic-shouldnt-outline.expect.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ function Foo() {
2020
function Foo() {
2121
return <button onClick={() => alert("hello!")}>Click me!</button>;
2222
}
23-
function _temp() {
24-
return alert("hello!");
25-
}
2623

2724
```
2825

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/use-memo-noemit.expect.md

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,11 @@ export const FIXTURE_ENTRYPOINT = {
1919
## Code
2020

2121
```javascript
22-
import { c as _c } from "react/compiler-runtime"; // @noEmit
22+
// @noEmit
2323

2424
function Foo() {
2525
"use memo";
26-
const $ = _c(1);
27-
let t0;
28-
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
29-
t0 = <button onClick={_temp}>Click me!</button>;
30-
$[0] = t0;
31-
} else {
32-
t0 = $[0];
33-
}
34-
return t0;
35-
}
36-
function _temp() {
37-
return alert("hello!");
26+
return <button onClick={() => alert("hello!")}>Click me!</button>;
3827
}
3928

4029
export const FIXTURE_ENTRYPOINT = {

0 commit comments

Comments
 (0)