-
Notifications
You must be signed in to change notification settings - Fork 47.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[compiler][be] Playground now compiles entire program #31774
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
function TestComponent({ x }) { | ||
"use no memo"; | ||
export default function TestComponent({ x }) { | ||
return <Button>{x}</Button>; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { c as _c } from "react/compiler-runtime"; | ||
function useFoo(propVal) { | ||
const $ = _c(2); | ||
const t0 = (propVal.baz: number); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Flow now codegens and prints successfully. This means that Flow users can use playground without needing to edit / manually strip their annotations |
||
let t1; | ||
if ($[0] !== t0) { | ||
t1 = <div>{t0}</div>; | ||
$[0] = t0; | ||
$[1] = t1; | ||
} else { | ||
t1 = $[1]; | ||
} | ||
return t1; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { c as _c } from "react/compiler-runtime"; | ||
function Foo() { | ||
const $ = _c(2); | ||
let t0; | ||
if ($[0] === Symbol.for("react.memo_cache_sentinel")) { | ||
t0 = foo(); | ||
$[0] = t0; | ||
} else { | ||
t0 = $[0]; | ||
} | ||
const x = t0 as number; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typescript also codegens successfully 🎉 No type annotation changes needed |
||
let t1; | ||
if ($[1] === Symbol.for("react.memo_cache_sentinel")) { | ||
t1 = <div>{x}</div>; | ||
$[1] = t1; | ||
} else { | ||
t1 = $[1]; | ||
} | ||
return t1; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
"use no memo"; | ||
function TestComponent({ x }) { | ||
"use memo"; | ||
return <Button>{x}</Button>; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
function anonymous_1() { | ||
const TestComponent = function () { | ||
"use no memo"; | ||
return <Button>{x}</Button>; | ||
} | ||
function anonymous_3({ x }) { | ||
}; | ||
const TestComponent2 = ({ x }) => { | ||
"use no memo"; | ||
return <Button>{x}</Button>; | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,11 +9,11 @@ import {expect, test} from '@playwright/test'; | |
import {encodeStore, type Store} from '../../lib/stores'; | ||
import {format} from 'prettier'; | ||
|
||
function print(data: Array<string>): Promise<string> { | ||
function formatPrint(data: Array<string>): Promise<string> { | ||
return format(data.join(''), {parser: 'babel'}); | ||
} | ||
|
||
const DIRECTIVE_TEST_CASES = [ | ||
const TEST_CASE_INPUTS = [ | ||
{ | ||
name: 'module-scope-use-memo', | ||
input: ` | ||
|
@@ -55,14 +55,34 @@ const TestComponent2 = ({ x }) => { | |
};`, | ||
}, | ||
{ | ||
name: 'function-scope-beats-module-scope', | ||
name: 'todo-function-scope-does-not-beat-module-scope', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See attached snap fixture -- playground and the babel plugin diverged before this PR |
||
input: ` | ||
'use no memo'; | ||
function TestComponent({ x }) { | ||
'use memo'; | ||
return <Button>{x}</Button>; | ||
}`, | ||
}, | ||
{ | ||
name: 'parse-typescript', | ||
input: ` | ||
function Foo() { | ||
const x = foo() as number; | ||
return <div>{x}</div>; | ||
} | ||
`, | ||
noFormat: true, | ||
}, | ||
{ | ||
name: 'parse-flow', | ||
input: ` | ||
// @flow | ||
function useFoo(propVal: {+baz: number}) { | ||
return <div>{(propVal.baz as number)}</div>; | ||
} | ||
`, | ||
noFormat: true, | ||
}, | ||
]; | ||
|
||
test('editor should open successfully', async ({page}) => { | ||
|
@@ -90,7 +110,7 @@ test('editor should compile from hash successfully', async ({page}) => { | |
}); | ||
const text = | ||
(await page.locator('.monaco-editor').nth(1).allInnerTexts()) ?? []; | ||
const output = await print(text); | ||
const output = await formatPrint(text); | ||
|
||
expect(output).not.toEqual(''); | ||
expect(output).toMatchSnapshot('01-user-output.txt'); | ||
|
@@ -115,14 +135,14 @@ test('reset button works', async ({page}) => { | |
}); | ||
const text = | ||
(await page.locator('.monaco-editor').nth(1).allInnerTexts()) ?? []; | ||
const output = await print(text); | ||
const output = await formatPrint(text); | ||
|
||
expect(output).not.toEqual(''); | ||
expect(output).toMatchSnapshot('02-default-output.txt'); | ||
}); | ||
|
||
DIRECTIVE_TEST_CASES.forEach((t, idx) => | ||
test(`directives work: ${t.name}`, async ({page}) => { | ||
TEST_CASE_INPUTS.forEach((t, idx) => | ||
test(`playground compiles: ${t.name}`, async ({page}) => { | ||
const store: Store = { | ||
source: t.input, | ||
}; | ||
|
@@ -135,7 +155,12 @@ DIRECTIVE_TEST_CASES.forEach((t, idx) => | |
|
||
const text = | ||
(await page.locator('.monaco-editor').nth(1).allInnerTexts()) ?? []; | ||
const output = await print(text); | ||
let output: string; | ||
if (t.noFormat) { | ||
output = text.join(''); | ||
} else { | ||
output = await formatPrint(text); | ||
} | ||
|
||
expect(output).not.toEqual(''); | ||
expect(output).toMatchSnapshot(`${t.name}-output.txt`); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that we now print inserted imports. Other program level changes (e.g.
Options.gating
, outlining) should work as expected without hardcoding