mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
[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.
```
This commit is contained in:
@@ -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({
|
||||
|
||||
+43
@@ -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 <Stringify truthyAttribute />;
|
||||
}
|
||||
|
||||
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 = <Stringify truthyAttribute={true} />;
|
||||
$[0] = t0;
|
||||
} else {
|
||||
t0 = $[0];
|
||||
}
|
||||
return t0;
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: Component,
|
||||
params: [{}],
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
+11
@@ -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 <Stringify truthyAttribute />;
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: Component,
|
||||
params: [{}],
|
||||
};
|
||||
@@ -4,6 +4,7 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"allowJs": true,
|
||||
"isolatedModules": true,
|
||||
"noEmit": true,
|
||||
"noImplicitAny": false,
|
||||
"noUncheckedIndexedAccess": false,
|
||||
|
||||
@@ -135,6 +135,7 @@ export function Text(props: {
|
||||
return React.createElement("div", null, props.value, props.children);
|
||||
}
|
||||
|
||||
|
||||
export function StaticText1(props: { children?: Array<React.ReactNode> }) {
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user