-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
81 lines (73 loc) · 1.78 KB
/
utils.js
File metadata and controls
81 lines (73 loc) · 1.78 KB
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
function objectToAST(object, babel) {
const stringified = stringify(object);
const variableDeclarationNode = babel.template(`var x = ${stringified}`, {
preserveComments: true,
placeholderPattern: false,
sourceType: 'module',
})();
return variableDeclarationNode.declarations[0].init;
}
function stringify(object) {
let str = JSON.stringify(object, stringifyReplacer);
if (str === undefined) {
str = 'undefined';
}
return str.replace(
/"__FUNCTION_START__(.*?)__FUNCTION_END__"/g,
functionReplacer
);
function stringifyReplacer(key, value) {
if (typeof value === 'function') {
return `__FUNCTION_START__${value.toString()}__FUNCTION_END__`;
}
return value;
}
function functionReplacer(match, p1) {
return p1.replace(/\\"/g, '"').replace(/\\n/g, '\n');
}
}
function createClassPropAST(classes, babel, styles) {
if (!classes || !classes.length) {
throw new Error('Passed arguments are invalid');
}
let classArray = classes.trim().split(' ');
let mappedClassArray = classArray
.filter(item => styles[item])
.map(function(item) {
return styles[item];
});
if (!mappedClassArray.length) {
return null;
}
if (mappedClassArray.length === 1) {
return objectToAST(mappedClassArray[0], babel);
}
return objectToAST(mappedClassArray, babel);
}
function createClassPropLogicalExpressionAST(
classes,
expression,
babel,
t,
styles
) {
return t.logicalExpression(
'&&',
expression,
createClassPropAST(classes, babel, styles)
);
}
function getKeyValue(key, t) {
if (t.isLiteral(key)) {
return key.value;
}
if (t.isIdentifier(key)) {
return key.name;
}
}
module.exports = {
objectToAST,
createClassPropAST,
createClassPropLogicalExpressionAST,
getKeyValue,
};