Skip to content

Commit

Permalink
feat: ts support
Browse files Browse the repository at this point in the history
  • Loading branch information
rayan1810 committed Feb 20, 2024
1 parent ab58a05 commit 8d20fb8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 84 deletions.
126 changes: 44 additions & 82 deletions babel-plugin/out.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,59 @@
'use strict';
"use strict";

Check failure on line 1 in babel-plugin/out.js

View workflow job for this annotation

GitHub Actions / lint

Replace `"use·strict"` with `'use·strict'`

// index.js
var CONFIG = {
p: 'padding',
m: 'margin',
t: 'top',
b: 'bottom',
l: 'left',
r: 'right',
h: 'height',
w: 'width',
bg: 'backgroundColor',
c: 'color',
p: "padding",

Check failure on line 5 in babel-plugin/out.js

View workflow job for this annotation

GitHub Actions / lint

Replace `"padding"` with `'padding'`
m: "margin",

Check failure on line 6 in babel-plugin/out.js

View workflow job for this annotation

GitHub Actions / lint

Replace `"margin"` with `'margin'`
t: "top",

Check failure on line 7 in babel-plugin/out.js

View workflow job for this annotation

GitHub Actions / lint

Replace `"top"` with `'top'`
b: "bottom",

Check failure on line 8 in babel-plugin/out.js

View workflow job for this annotation

GitHub Actions / lint

Replace `"bottom"` with `'bottom'`
l: "left",

Check failure on line 9 in babel-plugin/out.js

View workflow job for this annotation

GitHub Actions / lint

Replace `"left"` with `'left'`
r: "right",

Check failure on line 10 in babel-plugin/out.js

View workflow job for this annotation

GitHub Actions / lint

Replace `"right"` with `'right'`
h: "height",

Check failure on line 11 in babel-plugin/out.js

View workflow job for this annotation

GitHub Actions / lint

Replace `"height"` with `'height'`
w: "width",

Check failure on line 12 in babel-plugin/out.js

View workflow job for this annotation

GitHub Actions / lint

Replace `"width"` with `'width'`
bg: "backgroundColor",

Check failure on line 13 in babel-plugin/out.js

View workflow job for this annotation

GitHub Actions / lint

Replace `"backgroundColor"` with `'backgroundColor'`
c: "color"
};
module.exports = function (babel) {
module.exports = function(babel) {
const { types: t } = babel;
let importName = 'react-native-ustyle';
let importName = "react-native-ustyle";
let importedComponents = [];
let styleId = 0;
let Styles = [];
let styleExpression = [];
function resolver(name) {
console.log(name, 'resolver');
if (name in CONFIG) {
console.log(name, CONFIG[name]);
return CONFIG[name];
}
return name;
}
function checkIfStylesheetImportedAndImport(programPath) {
let importDeclaration = programPath.node.body.find(
(node) =>
node.type === 'ImportDeclaration' &&
node.source.value === 'react-native'
(node) => node.type === "ImportDeclaration" && node.source.value === "react-native"
);
if (!importDeclaration) {
programPath.node.body.unshift(
t.importDeclaration(
[
t.importSpecifier(
t.identifier('StyleSheet'),
t.identifier('StyleSheet')
),
],
t.stringLiteral('react-native')
)
);
if (importDeclaration) {
}
}
function attributesToObject(attributes) {
if (!Array.isArray(attributes)) {
throw new TypeError('Expected attributes to be an array');
throw new TypeError("Expected attributes to be an array");
}
const obj = {};
attributes.forEach((attribute) => {
const key = resolver(attribute.name.name);
let value;
if (attribute.value.type === 'JSXExpressionContainer') {
if (attribute.value.expression.type === 'ObjectExpression') {
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;
const propName = prop.key.type === "StringLiteral" ? prop.key.value : prop.key.name;
if (prop.value.value !== void 0) {

Check warning on line 49 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') {
} else if (attribute.value.type === "JSXElement") {
value = attributesToObject(attribute.value.openingElement.attributes);
} else {
value = attribute.value.value;
Expand All @@ -83,100 +65,80 @@ module.exports = function (babel) {
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') {
if (styleAttr.value.expression.type !== "ArrayExpression") {
styleAttr.value.expression = t.arrayExpression([
styleAttr.value.expression,
styleAttr.value.expression
]);
}
let styleArray = styleAttr.value.expression.elements;
styleArray.push(t.identifier('rnuStyles.styles' + styleId2));
styleArray.push(t.identifier("rnuStyles.styles" + styleId2));
} else {
jsxAttrArray.push(
t.jSXAttribute(
t.jSXIdentifier('style'),
t.jSXIdentifier("style"),
t.jSXExpressionContainer(
t.arrayExpression([t.identifier('rnuStyles.styles' + styleId2)])
t.arrayExpression([t.identifier("rnuStyles.styles" + styleId2)])
)
)
);
}
}
return {
name: 'ast-transform',
name: "ast-transform",
// not required
visitor: {
ImportDeclaration(path, opts) {
if (
opts.filename.includes('node_modules') ||
opts.filename.includes('.expo') ||
opts.filename.includes('.next') ||
opts.filename.includes('ios')
)
if (opts.filename.includes("node_modules") || opts.filename.includes(".expo") || opts.filename.includes(".next"))
return;
if (path.node.source.value === importName) {
path.traverse({
ImportSpecifier(path2) {
importedComponents.push(path2.node.local.name);
},
}
});
path.node.source.value = 'react-native';
path.node.source.value = "react-native";
}
},
JSXOpeningElement(path, f, o) {
if (
f.filename.includes('node_modules') ||
f.filename.includes('.expo') ||
f.filename.includes('.next') ||
f.filename.includes('ios')
)
if (f.filename.includes("node_modules") || f.filename.includes(".expo") || f.filename.includes(".next"))
return;
if (importedComponents.includes(path.node.name.name)) {
addRnuStyleIdInStyleArrayOfCOmponent(path.node.attributes, styleId);
styleExpression.push(
t.objectProperty(
t.identifier('styles' + styleId++),
t.identifier("styles" + styleId++),
t.valueToNode(attributesToObject(path.node.attributes))
)
);
let declaration = f.file.ast.program.body.find(
(node) =>
node.type === 'VariableDeclaration' &&
node.declarations[0].id.name === 'rnuStyles'
(node) => node.type === "VariableDeclaration" && node.declarations[0].id.name === "rnuStyles"
);
console.log(declaration);
if (declaration) {
f.file.ast.program.body = f.file.ast.program.body.filter(
(node) =>
node.type !== 'VariableDeclaration' ||
node.declarations[0].id.name !== 'rnuStyles'
(node) => node.type !== "VariableDeclaration" || node.declarations[0].id.name !== "rnuStyles"
);
} else {
declaration = t.variableDeclaration('const', [
declaration = t.variableDeclaration("const", [
t.variableDeclarator(
t.identifier('rnuStyles'),
t.callExpression(t.identifier('StyleSheet.create'), [
t.objectExpression(styleExpression),
])
),
t.identifier("rnuStyles"),
// Not using StyleSheet.create since it is not working in the latest metro version
// t.callExpression(t.identifier("StyleSheet.create"), [
t.objectExpression(styleExpression)
// ])
)
]);
}
Styles.push(declaration);
console.log(attributesToObject(path.node.attributes));
f.file.ast.program.body.push(declaration);
}
},
Program(path, opts) {
if (
opts.filename.includes('node_modules') ||
opts.filename.includes('.expo') ||
opts.filename.includes('.next') ||
opts.filename.includes('ios')
)
if (opts.filename.includes("node_modules") || opts.filename.includes(".expo") || opts.filename.includes(".next"))
return;
checkIfStylesheetImportedAndImport(path);
},
},
}
}
};
};
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.5.0",
"version": "0.6.0",
"description": "Official support babel plugin for react-native-ustyle",
"main": "out.js",
"module": "out.js",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-ustyle",
"version": "0.2.0",
"version": "0.6.0",
"description": "Easy and configurable utiltity styling props support for React Native",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down

0 comments on commit 8d20fb8

Please sign in to comment.