[transform-react-jsx] Add useSpread option to transform JSX (#10572)
* [transform-react-jsx] Add useSpread option to transform JSX * Add validation for default option * Add error when using useSpread and useBuiltIns at the same time * Move useSpread to convertAttribute helper function * Add useSpread option to presect-react * Remove casting useSpread to boolean in preset-react option. Co-Authored-By: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
This commit is contained in:
committed by
Nicolò Ribaudo
parent
8ffca0475a
commit
3d2f365074
@@ -87,6 +87,10 @@ You can turn on the 'throwIfNamespace' flag to bypass this warning.`,
|
||||
function convertAttribute(node) {
|
||||
const value = convertAttributeValue(node.value || t.booleanLiteral(true));
|
||||
|
||||
if (t.isJSXSpreadAttribute(node)) {
|
||||
return t.spreadElement(node.argument);
|
||||
}
|
||||
|
||||
if (t.isStringLiteral(value) && !t.isJSXExpressionContainer(node.value)) {
|
||||
value.value = value.value.replace(/\n\s+/g, " ");
|
||||
|
||||
@@ -170,6 +174,14 @@ You can turn on the 'throwIfNamespace' flag to bypass this warning.`,
|
||||
let _props = [];
|
||||
const objs = [];
|
||||
|
||||
const { useSpread = false } = file.opts;
|
||||
if (typeof useSpread !== "boolean") {
|
||||
throw new Error(
|
||||
"transform-react-jsx currently only accepts a boolean option for " +
|
||||
"useSpread (defaults to false)",
|
||||
);
|
||||
}
|
||||
|
||||
const useBuiltIns = file.opts.useBuiltIns || false;
|
||||
if (typeof useBuiltIns !== "boolean") {
|
||||
throw new Error(
|
||||
@@ -178,6 +190,18 @@ You can turn on the 'throwIfNamespace' flag to bypass this warning.`,
|
||||
);
|
||||
}
|
||||
|
||||
if (useSpread && useBuiltIns) {
|
||||
throw new Error(
|
||||
"transform-react-jsx currently only accepts useBuiltIns or useSpread " +
|
||||
"but not both",
|
||||
);
|
||||
}
|
||||
|
||||
if (useSpread) {
|
||||
const props = attribs.map(convertAttribute);
|
||||
return t.objectExpression(props);
|
||||
}
|
||||
|
||||
while (attribs.length) {
|
||||
const prop = attribs.shift();
|
||||
if (t.isJSXSpreadAttribute(prop)) {
|
||||
|
||||
Reference in New Issue
Block a user