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 all commits
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
62 changes: 45 additions & 17 deletions packages/babel-plugin-transform-react-jsx/src/create-plugin.js
Expand Up @@ -341,9 +341,9 @@ You can set \`throwIfNamespace: false\` to bypass this warning.`,
}
}

function accumulateAttribute(array, node) {
if (t.isJSXSpreadAttribute(node)) {
const arg = node.argument;
function accumulateAttribute(array, attribute) {
if (t.isJSXSpreadAttribute(attribute.node)) {
const arg = attribute.node.argument;
// Collect properties into props array if spreading object expression
if (t.isObjectExpression(arg)) {
array.push(...arg.properties);
Expand All @@ -353,26 +353,46 @@ You can set \`throwIfNamespace: false\` to bypass this warning.`,
return array;
}

const value = convertAttributeValue(node.value || t.booleanLiteral(true));
const value = convertAttributeValue(
attribute.node.name.name !== "key"
? attribute.node.value || t.booleanLiteral(true)
: attribute.node.value,
);

if (t.isStringLiteral(value) && !t.isJSXExpressionContainer(node.value)) {
if (attribute.node.name.name === "key" && value === null) {
throw attribute.buildCodeFrameError(
'Please provide an explicit key value. Using "key" as a shorthand for "key={true}" is not allowed.',
);
}

if (
t.isStringLiteral(value) &&
!t.isJSXExpressionContainer(attribute.node.value)
) {
value.value = value.value.replace(/\n\s+/g, " ");

// "raw" JSXText should not be used from a StringLiteral because it needs to be escaped.
delete value.extra?.raw;
}

if (t.isJSXNamespacedName(node.name)) {
node.name = t.stringLiteral(
node.name.namespace.name + ":" + node.name.name.name,
if (t.isJSXNamespacedName(attribute.node.name)) {
attribute.node.name = t.stringLiteral(
attribute.node.name.namespace.name +
":" +
attribute.node.name.name.name,
);
} else if (t.isValidIdentifier(node.name.name, false)) {
node.name.type = "Identifier";
} else if (t.isValidIdentifier(attribute.node.name.name, false)) {
attribute.node.name.type = "Identifier";
} else {
node.name = t.stringLiteral(node.name.name);
attribute.node.name = t.stringLiteral(attribute.node.name.name);
}

array.push(t.inherits(t.objectProperty(node.name, value), node));
array.push(
t.inherits(
t.objectProperty(attribute.node.name, value),
attribute.node,
),
);
return array;
}

Expand Down Expand Up @@ -411,14 +431,22 @@ 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 attr.buildCodeFrameError(
'Please provide an explicit key value. Using "key" as a shorthand for "key={true}" is not allowed.',
);
}

extracted[name] = keyValue;
break;
}
default:
attribs.push(attr.node);
attribs.push(attr);
}
} else {
attribs.push(attr.node);
attribs.push(attr);
}
}

Expand Down Expand Up @@ -511,7 +539,7 @@ You can set \`throwIfNamespace: false\` to bypass this warning.`,
buildCreateElementOpeningElementAttributes(
file,
path,
openingPath.node.attributes,
openingPath.get("attributes"),
),
...t.react.buildChildren(path.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": "Please provide an explicit key value. Using \"key\" as a shorthand for \"key={true}\" is not allowed."
}
@@ -0,0 +1,2 @@

var x = [<div key></div>];
@@ -0,0 +1,3 @@
{
"throws": "Please provide an explicit key value. Using \"key\" as a shorthand for \"key={true}\" is not allowed."
}