-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
148 lines (135 loc) · 4.13 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const syntax = require('@babel/plugin-syntax-jsx').default;
const {
createClassPropAST,
createClassPropLogicalExpressionAST,
getKeyValue,
} = require('./utils');
const REACT_NATIVE_STYLE_PROP = 'style';
const EVALUATE_CLASS = 'evaluateClass';
module.exports = function(babel, { propName = 'class', classes }) {
const { types: t } = babel;
if (!classes) {
throw new Error(
'"babel-plugin-react-native-class-prop": required argument "classes" is missing.'
);
}
return {
inherits: syntax,
visitor: {
JSXAttribute(path) {
if (
path.node &&
path.node.name &&
path.node.name.name &&
path.node.name.name !== propName
) {
// Don't do anything if we didn't find an attribute named 'class'
return;
}
const elem = path.parentPath;
elem.node.attributes = elem.node.attributes.filter(
attr => attr !== path.node
);
const stylePropAST = elem.node.attributes.find(attr => {
return (
attr.name &&
attr.name.name &&
attr.name.name === REACT_NATIVE_STYLE_PROP
);
});
let classPropAST;
if (t.isLiteral(path.node.value)) {
classPropAST = createClassPropAST(
path.node.value.value,
babel,
classes
);
}
if (
t.isJSXExpressionContainer(path.node.value) &&
t.isArrayExpression(path.node.value.expression)
) {
let values = [];
path.node.value.expression.elements.forEach(item => {
if (t.isStringLiteral(item)) {
values.unshift(createClassPropAST(item.value, babel, classes));
}
if (t.isObjectExpression(item)) {
item.properties.forEach(i => {
values.push(
createClassPropLogicalExpressionAST(
getKeyValue(i.key, t),
i.value,
babel,
t,
classes
)
);
});
}
});
classPropAST = t.arrayExpression(values);
}
if (!classPropAST) {
return;
}
// style prop is not defined
if (!stylePropAST) {
elem.node.attributes.push(
t.jSXAttribute(
t.jSXIdentifier(REACT_NATIVE_STYLE_PROP),
t.jSXExpressionContainer(classPropAST)
)
);
return;
}
let result = t.ArrayExpression();
if (t.isArrayExpression(stylePropAST.value.expression)) {
result.elements = result.elements.concat(
stylePropAST.value.expression.elements
);
} else {
result.elements.push(stylePropAST.value.expression);
}
if (t.isArrayExpression(classPropAST)) {
result.elements = classPropAST.elements.concat(result.elements);
} else {
result.elements.unshift(classPropAST);
}
stylePropAST.value.expression = result;
},
ImportDeclaration(path, state) {
if (
!t.isLiteral(path.node.source, {
value: 'babel-plugin-react-native-class-prop',
}) ||
!(path.node.specifiers[0].local.name === EVALUATE_CLASS)
) {
return;
}
const program = path.findParent(t.isProgram);
const evaluateVisitor = {
JSXExpressionContainer(path) {
if (
!t.isJSXExpressionContainer(path.node) ||
!t.isCallExpression(path.node.expression) ||
!t.isIdentifier(path.node.expression.callee, {
name: EVALUATE_CLASS,
})
) {
return;
}
const classNames = path.node.expression.arguments[0].value;
path.node.expression = createClassPropAST(
classNames,
babel,
classes
);
},
};
program.traverse(evaluateVisitor, { state });
},
},
};
};
module.exports[EVALUATE_CLASS] = () => {};