Skip to content

Commit 7b56a54

Browse files
committed
[compiler][playground] create playground API in pipeline, and allow spaces in pass names
Summary: 1. Minor refactor to provide a stable API for calling the compiler from the playground 2. Allows spaces in pass names without breaking the appearance of the playground by replacing spaces with   in pass tabs ghstack-source-id: 12a43ad Pull Request resolved: #30988
1 parent a99d8e8 commit 7b56a54

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

compiler/apps/playground/components/Editor/EditorImpl.tsx

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import {parse as babelParse, ParserPlugin} from '@babel/parser';
8+
import {parse as babelParse} from '@babel/parser';
99
import * as HermesParser from 'hermes-parser';
1010
import traverse, {NodePath} from '@babel/traverse';
1111
import * as t from '@babel/types';
@@ -15,10 +15,8 @@ import {
1515
Effect,
1616
ErrorSeverity,
1717
parseConfigPragma,
18-
printHIR,
19-
printReactiveFunction,
20-
run,
2118
ValueKind,
19+
runPlayground,
2220
type Hook,
2321
} from 'babel-plugin-react-compiler/src';
2422
import {type ReactFunctionType} from 'babel-plugin-react-compiler/src/HIR/Environment';
@@ -214,17 +212,13 @@ function compile(source: string): [CompilerOutput, 'flow' | 'typescript'] {
214212

215213
for (const fn of parseFunctions(source, language)) {
216214
const id = withIdentifier(getFunctionIdentifier(fn));
217-
for (const result of run(
215+
for (const result of runPlayground(
218216
fn,
219217
{
220218
...config,
221219
customHooks: new Map([...COMMON_HOOKS]),
222220
},
223221
getReactFunctionType(id),
224-
'_c',
225-
null,
226-
null,
227-
null,
228222
)) {
229223
const fnName = id.name;
230224
switch (result.kind) {

compiler/apps/playground/components/TabbedWindow.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ function TabbedWindowItem({
6969
setTabsOpen(nextState);
7070
}, [tabsOpen, name, setTabsOpen]);
7171

72+
// Replace spaces with non-breaking spaces
73+
const displayName = name.replace(/ /g, '\u00A0');
74+
7275
return (
7376
<div key={name} className="flex flex-row">
7477
{isShow ? (
@@ -80,7 +83,7 @@ function TabbedWindowItem({
8083
className={`p-4 duration-150 ease-in border-b cursor-pointer border-grey-200 ${
8184
hasChanged ? 'font-bold' : 'font-light'
8285
} text-secondary hover:text-link`}>
83-
- {name}
86+
- {displayName}
8487
</h2>
8588
{tabs.get(name) ?? <div>No output for {name}</div>}
8689
</Resizable>
@@ -94,7 +97,7 @@ function TabbedWindowItem({
9497
className={`flex-grow-0 w-5 transition-colors duration-150 ease-in ${
9598
hasChanged ? 'font-bold' : 'font-light'
9699
} text-secondary hover:text-link`}>
97-
{name}
100+
{displayName}
98101
</button>
99102
</div>
100103
)}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,3 +554,14 @@ export function log(value: CompilerPipelineValue): CompilerPipelineValue {
554554
}
555555
return value;
556556
}
557+
558+
export function* runPlayground(
559+
func: NodePath<
560+
t.FunctionDeclaration | t.ArrowFunctionExpression | t.FunctionExpression
561+
>,
562+
config: EnvironmentConfig,
563+
fnType: ReactFunctionType,
564+
): Generator<CompilerPipelineValue, CodegenFunction> {
565+
const ast = yield* run(func, config, fnType, '_c', null, null, null);
566+
return ast;
567+
}

compiler/packages/babel-plugin-react-compiler/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export {
1818
compileProgram,
1919
parsePluginOptions,
2020
run,
21+
runPlayground,
2122
OPT_OUT_DIRECTIVES,
2223
type CompilerPipelineValue,
2324
type PluginOptions,

0 commit comments

Comments
 (0)