From b55cf78a4d23de06b96a3a5025ad86e4961baa96 Mon Sep 17 00:00:00 2001 From: Mofei Zhang Date: Fri, 6 Oct 2023 17:38:02 -0400 Subject: [PATCH] [syntax] Lower empty jsx attributes to true MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Found when enabling Forget for webamp ``` % yarn sprout --filter --verbose yarn run v1.22.19 $ node ../sprout/dist/main.js --filter --verbose PASS jsx-attribute-default-to-true ok
{"truthyAttribute":true}
1 Tests, 1 Passed, 0 Failed ✨ Done in 2.45s. ``` --- .../src/HIR/BuildHIR.ts | 6 +++ .../jsx-attribute-default-to-true.expect.md | 43 +++++++++++++++++++ .../jsx-attribute-default-to-true.tsx | 11 +++++ .../src/__tests__/fixtures/tsconfig.json | 1 + .../packages/sprout/src/shared-runtime.ts | 9 +++- 5 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-attribute-default-to-true.expect.md create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-attribute-default-to-true.tsx diff --git a/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts b/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts index 29aa632222..92169a9599 100644 --- a/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts +++ b/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts @@ -1927,6 +1927,12 @@ function lowerExpression( let value; if (valueExpr.isJSXElement() || valueExpr.isStringLiteral()) { value = lowerExpressionToTemporary(builder, valueExpr); + } else if (valueExpr.type == null) { + value = lowerValueToTemporary(builder, { + kind: "Primitive", + value: true, + loc: attribute.node.loc ?? GeneratedSource, + }); } else { if (!valueExpr.isJSXExpressionContainer()) { builder.errors.push({ diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-attribute-default-to-true.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-attribute-default-to-true.expect.md new file mode 100644 index 0000000000..e1b59d19c6 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-attribute-default-to-true.expect.md @@ -0,0 +1,43 @@ + +## Input + +```javascript +import { Stringify } from "shared-runtime"; + +function Component() { + // https://legacy.reactjs.org/docs/jsx-in-depth.html#props-default-to-true + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + +## Code + +```javascript +import { unstable_useMemoCache as useMemoCache } from "react"; +import { Stringify } from "shared-runtime"; + +function Component() { + const $ = useMemoCache(1); + let t0; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + t0 = ; + $[0] = t0; + } else { + t0 = $[0]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-attribute-default-to-true.tsx b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-attribute-default-to-true.tsx new file mode 100644 index 0000000000..63dc0a68cd --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-attribute-default-to-true.tsx @@ -0,0 +1,11 @@ +import { Stringify } from "shared-runtime"; + +function Component() { + // https://legacy.reactjs.org/docs/jsx-in-depth.html#props-default-to-true + return ; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/tsconfig.json b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/tsconfig.json index 5e3f769361..c4e800fe03 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/tsconfig.json +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/tsconfig.json @@ -4,6 +4,7 @@ { "compilerOptions": { "allowJs": true, + "isolatedModules": true, "noEmit": true, "noImplicitAny": false, "noUncheckedIndexedAccess": false, diff --git a/compiler/packages/sprout/src/shared-runtime.ts b/compiler/packages/sprout/src/shared-runtime.ts index 991518af19..f266a5e519 100644 --- a/compiler/packages/sprout/src/shared-runtime.ts +++ b/compiler/packages/sprout/src/shared-runtime.ts @@ -135,6 +135,7 @@ export function Text(props: { return React.createElement("div", null, props.value, props.children); } + export function StaticText1(props: { children?: Array }) { return React.createElement("div", null, "StaticText1", props.children); } @@ -154,8 +155,12 @@ export function RenderPropAsChild(props: { ); } -export function Stringify(props: any) { - return toJSON(props); +export function Stringify(props: any): React.ReactElement { + return React.createElement( + "div", + null, + toJSON(props), + ); } // helper functions