Skip to content

Commit

Permalink
fix: babel plugin ignoring jsx spread attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
rayan1810 committed Feb 22, 2024
1 parent e905462 commit 4c40e25
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 44 deletions.
50 changes: 27 additions & 23 deletions babel-plugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,38 +81,42 @@ module.exports = function (babel) {

const obj = {};
attributes.forEach((attribute) => {
const key = resolver(attribute.name.name);
let value;
if (attribute.value.type === 'JSXExpressionContainer') {
if (attribute.value.expression.type === 'ObjectExpression') {
value = {};
attribute.value.expression.properties.forEach((prop) => {
const propName =
prop.key.type === 'StringLiteral'
? prop.key.value
: prop.key.name;
if (prop.value.value !== undefined) {
value[propName] = prop.value.value;
}
});
if (t.isJSXSpreadAttribute(attribute)) {
return;
} else {
const key = resolver(attribute.name.name);
let value;
if (attribute.value.type === 'JSXExpressionContainer') {
if (attribute.value.expression.type === 'ObjectExpression') {
value = {};
attribute.value.expression.properties.forEach((prop) => {
const propName =
prop.key.type === 'StringLiteral'
? prop.key.value
: prop.key.name;
if (prop.value.value !== undefined) {
value[propName] = prop.value.value;
}
});
} else {
value = attribute.value.expression.value;
}
} else if (attribute.value.type === 'JSXElement') {
value = attributesToObject(attribute.value.openingElement.attributes);
} else {
value = attribute.value.expression.value;
value = attribute.value.value;
}
} else if (attribute.value.type === 'JSXElement') {
value = attributesToObject(attribute.value.openingElement.attributes);
} else {
value = attribute.value.value;
}

if (value !== undefined) {
obj[key] = value;
if (value !== undefined) {
obj[key] = value;
}
}
});
return obj;
}
function addRnuStyleIdInStyleArrayOfCOmponent(jsxAttrArray, styleId) {

Check warning on line 117 in babel-plugin/index.js

View workflow job for this annotation

GitHub Actions / lint

'styleId' is already declared in the upper scope on line 44 column 7
// find the style attribute
let styleAttr = jsxAttrArray.find((attr) => attr.name.name === 'style');
let styleAttr = jsxAttrArray.find((attr) => attr.name?.name === 'style');
// insert the styleId in the style array
// style can be an array or a single object
if (styleAttr) {
Expand Down
44 changes: 24 additions & 20 deletions babel-plugin/out.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,33 +59,37 @@ module.exports = function(babel) {
}
const obj = {};
attributes.forEach((attribute) => {
const key = resolver(attribute.name.name);
let value;
if (attribute.value.type === "JSXExpressionContainer") {
if (attribute.value.expression.type === "ObjectExpression") {
value = {};
attribute.value.expression.properties.forEach((prop) => {
const propName = prop.key.type === "StringLiteral" ? prop.key.value : prop.key.name;
if (prop.value.value !== void 0) {
value[propName] = prop.value.value;
}
});
if (t.isJSXSpreadAttribute(attribute)) {
return;
} else {
const key = resolver(attribute.name.name);
let value;
if (attribute.value.type === "JSXExpressionContainer") {
if (attribute.value.expression.type === "ObjectExpression") {
value = {};
attribute.value.expression.properties.forEach((prop) => {
const propName = prop.key.type === "StringLiteral" ? prop.key.value : prop.key.name;
if (prop.value.value !== void 0) {

Check warning on line 72 in babel-plugin/out.js

View workflow job for this annotation

GitHub Actions / lint

Expected 'undefined' and instead saw 'void'
value[propName] = prop.value.value;
}
});
} else {
value = attribute.value.expression.value;
}
} else if (attribute.value.type === "JSXElement") {
value = attributesToObject(attribute.value.openingElement.attributes);
} else {
value = attribute.value.expression.value;
value = attribute.value.value;
}
if (value !== void 0) {

Check warning on line 84 in babel-plugin/out.js

View workflow job for this annotation

GitHub Actions / lint

Expected 'undefined' and instead saw 'void'
obj[key] = value;
}
} else if (attribute.value.type === "JSXElement") {
value = attributesToObject(attribute.value.openingElement.attributes);
} else {
value = attribute.value.value;
}
if (value !== void 0) {
obj[key] = value;
}
});
return obj;
}
function addRnuStyleIdInStyleArrayOfCOmponent(jsxAttrArray, styleId2) {
let styleAttr = jsxAttrArray.find((attr) => attr.name.name === "style");
let styleAttr = jsxAttrArray.find((attr) => attr.name?.name === "style");
if (styleAttr) {
if (styleAttr.value.expression.type !== "ArrayExpression") {
styleAttr.value.expression = t.arrayExpression([
Expand Down
2 changes: 1 addition & 1 deletion babel-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "babel-plugin-react-native-ustyle",
"version": "0.7.2",
"version": "1.0.0",
"description": "Official support babel plugin for react-native-ustyle",
"main": "out.js",
"module": "out.js",
Expand Down

0 comments on commit 4c40e25

Please sign in to comment.