Skip to content
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

Turn on key spread warning in jsx-runtime for everyone #25697

Merged
merged 3 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Nits
  • Loading branch information
sebmarkbage committed Nov 16, 2022
commit 92daf551dcb0bc0a641644e278fad0d97ef053e9
16 changes: 8 additions & 8 deletions packages/react/src/ReactElementValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,21 +382,21 @@ export function jsxWithValidation(
if (warnAboutSpreadingKeyToJSX) {
if (hasOwnProperty.call(props, 'key')) {
const componentName = getComponentNameFromType(type);
const keys = Object.keys(props);
const beforeExample = '{' + keys.join(': ..., ') + ': ...}';
const keys = Object.keys(props).filter(k => k !== 'key');
const beforeExample =
keys.length > 0
? '{key: someKey, ' + keys.join(': ..., ') + ': ...}'
: '{key: someKey}';
if (!didWarnAboutKeySpread[componentName + beforeExample]) {
const keysWithoutKey = keys.filter(k => k !== 'key');
const afterExample =
keysWithoutKey.length > 0
? '{' + keysWithoutKey.join(': ..., ') + ': ...}'
: '{}';
keys.length > 0 ? '{' + keys.join(': ..., ') + ': ...}' : '{}';
console.error(
'An props object containing a "key" prop is being spread into JSX:\n' +
'A props object containing a "key" prop is being spread into JSX:\n' +
' let props = %s;\n' +
' <%s {...props} />\n' +
'React keys must be passed directly to JSX without using spread:\n' +
' let props = %s;\n' +
' <%s key={...} {...props} />',
' <%s key={someKey} {...props} />',
beforeExample,
componentName,
afterExample,
Expand Down
10 changes: 5 additions & 5 deletions packages/react/src/__tests__/ReactElementJSX-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,19 +275,19 @@ describe('ReactElement.jsx', () => {
class Parent extends React.Component {
render() {
return JSXRuntime.jsx('div', {
children: [JSXRuntime.jsx(Child, {key: '0'})],
children: [JSXRuntime.jsx(Child, {key: '0', prop: 'hi'})],
});
}
}
expect(() =>
ReactDOM.render(JSXRuntime.jsx(Parent, {}), container),
).toErrorDev(
'Warning: An props object containing a "key" prop is being spread into JSX:\n' +
' let props = {key: ...};\n' +
'Warning: A props object containing a "key" prop is being spread into JSX:\n' +
' let props = {key: someKey, prop: ...};\n' +
' <Child {...props} />\n' +
'React keys must be passed directly to JSX without using spread:\n' +
' let props = {};\n' +
' <Child key={...} {...props} />',
' let props = {prop: ...};\n' +
' <Child key={someKey} {...props} />',
);
});
}
Expand Down
16 changes: 8 additions & 8 deletions packages/react/src/jsx/ReactJSXElementValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,21 +393,21 @@ export function jsxWithValidation(
if (warnAboutSpreadingKeyToJSX) {
if (hasOwnProperty.call(props, 'key')) {
const componentName = getComponentNameFromType(type);
const keys = Object.keys(props);
const beforeExample = '{' + keys.join(': ..., ') + ': ...}';
const keys = Object.keys(props).filter(k => k !== 'key');
const beforeExample =
keys.length > 0
? '{key: someKey, ' + keys.join(': ..., ') + ': ...}'
: '{key: someKey}';
if (!didWarnAboutKeySpread[componentName + beforeExample]) {
const keysWithoutKey = keys.filter(k => k !== 'key');
const afterExample =
keysWithoutKey.length > 0
? '{' + keysWithoutKey.join(': ..., ') + ': ...}'
: '{}';
keys.length > 0 ? '{' + keys.join(': ..., ') + ': ...}' : '{}';
console.error(
'An props object containing a "key" prop is being spread into JSX:\n' +
'A props object containing a "key" prop is being spread into JSX:\n' +
' let props = %s;\n' +
' <%s {...props} />\n' +
'React keys must be passed directly to JSX without using spread:\n' +
' let props = %s;\n' +
' <%s key={...} {...props} />',
' <%s key={someKey} {...props} />',
beforeExample,
componentName,
afterExample,
Expand Down