@samitouri / QOS-React-2 / commits / b55cf78a4d

[syntax] Lower empty jsx attributes to true

--- 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 <div>{"truthyAttribute":true}</div> 1 Tests, 1 Passed, 0 Failed ✨ Done in 2.45s. ```

Mofei Zhang committed Oct 6, 2023 at 17:38 UTC b55cf78a4d23de06b96a3a5025ad86e4961baa96
5 files changed +68 -2
compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts
+6
@@ -1927,6 +1927,12 @@ function lowerExpression(
1927 let value;
1928 if (valueExpr.isJSXElement() || valueExpr.isStringLiteral()) {
1929 value = lowerExpressionToTemporary(builder, valueExpr);
1930 + } else if (valueExpr.type == null) {
1931 + value = lowerValueToTemporary(builder, {
1932 + kind: "Primitive",
1933 + value: true,
1934 + loc: attribute.node.loc ?? GeneratedSource,
1935 + });
1936 } else {
1937 if (!valueExpr.isJSXExpressionContainer()) {
1938 builder.errors.push({
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-attribute-default-to-true.expect.md new
+43
@@ -0,0 +1,43 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +import { Stringify } from "shared-runtime";
6 +
7 +function Component() {
8 + // https://legacy.reactjs.org/docs/jsx-in-depth.html#props-default-to-true
9 + return <Stringify truthyAttribute />;
10 +}
11 +
12 +export const FIXTURE_ENTRYPOINT = {
13 + fn: Component,
14 + params: [{}],
15 +};
16 +
17 +```
18 +
19 +## Code
20 +
21 +```javascript
22 +import { unstable_useMemoCache as useMemoCache } from "react";
23 +import { Stringify } from "shared-runtime";
24 +
25 +function Component() {
26 + const $ = useMemoCache(1);
27 + let t0;
28 + if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
29 + t0 = <Stringify truthyAttribute={true} />;
30 + $[0] = t0;
31 + } else {
32 + t0 = $[0];
33 + }
34 + return t0;
35 +}
36 +
37 +export const FIXTURE_ENTRYPOINT = {
38 + fn: Component,
39 + params: [{}],
40 +};
41 +
42 +```
43 +
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/jsx-attribute-default-to-true.tsx new
+11
@@ -0,0 +1,11 @@
1 +import { Stringify } from "shared-runtime";
2 +
3 +function Component() {
4 + // https://legacy.reactjs.org/docs/jsx-in-depth.html#props-default-to-true
5 + return <Stringify truthyAttribute />;
6 +}
7 +
8 +export const FIXTURE_ENTRYPOINT = {
9 + fn: Component,
10 + params: [{}],
11 +};
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/tsconfig.json
+1
@@ -4,6 +4,7 @@
4 {
5 "compilerOptions": {
6 "allowJs": true,
7 + "isolatedModules": true,
8 "noEmit": true,
9 "noImplicitAny": false,
10 "noUncheckedIndexedAccess": false,
compiler/packages/sprout/src/shared-runtime.ts
+7 -2
@@ -135,6 +135,7 @@ export function Text(props: {
135 return React.createElement("div", null, props.value, props.children);
136 }
137
138 +
139 export function StaticText1(props: { children?: Array<React.ReactNode> }) {
140 return React.createElement("div", null, "StaticText1", props.children);
141 }
@@ -154,8 +155,12 @@ export function RenderPropAsChild(props: {
155 );
156 }
157
157 -export function Stringify(props: any) {
158 - return toJSON(props);
158 +export function Stringify(props: any): React.ReactElement {
159 + return React.createElement(
160 + "div",
161 + null,
162 + toJSON(props),
163 + );
164 }
165
166 // helper functions