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

Improve error message when not providing a value for JSX key #12983

Merged
merged 4 commits into from Mar 16, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions packages/babel-plugin-transform-react-jsx/src/create-plugin.js
Expand Up @@ -411,9 +411,17 @@ You can set \`throwIfNamespace: false\` to bypass this warning.`,
case "__self":
if (extracted[name]) throw sourceSelfError(path, name);
/* falls through */
case "key":
extracted[name] = convertAttributeValue(attr.node.value);
case "key": {
const keyValue = convertAttributeValue(attr.node.value);
if (keyValue === null) {
throw path.buildCodeFrameError(
hbendev marked this conversation as resolved.
Show resolved Hide resolved
'Provide an explicit value to be used as a key. You are not supposed to use the "key" shorthand for "key={true}".',
hbendev marked this conversation as resolved.
Show resolved Hide resolved
);
}

extracted[name] = keyValue;
break;
}
default:
attribs.push(attr.node);
}
Expand Down
@@ -0,0 +1,2 @@

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you duplicate this test to fixtures/react/, which shares test suites with react-automatic but with runtime set to classic, the default behaviour of Babel 7.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. I've done that. As I mentioned in the issue, the compilation was successful in development mode. Fixing it was a bit trickier. Let me know if I have to improve my solution in any way.

var x = [<div key></div>];
@@ -0,0 +1,3 @@
{
"throws": "Provide an explicit value to be used as a key. You are not supposed to use the \"key\" shorthand for \"key={true}\"."
}