-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
182 lines (164 loc) · 5.08 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
"use strict"
module.exports = function(base) {
const t = base.types;
const ast = (obj) => {
switch (typeof obj) {
case 'string':
return t.StringLiteral(obj);
case 'object':
if (Array.isArray(obj)) {
return t.arrayExpression(obj.map(ast));
}
return t.ObjectExpression(
Object.keys(obj).filter((k) => obj[k] !== undefined).map((k) => t.ObjectProperty(t.Identifier(k), ast(obj[k])))
);
}
};
const requireBem = (bemId, bemedId, opts) => {
opts = ast(opts || {});
return [
t.ImportDeclaration(
[
t.ImportDefaultSpecifier(
bemedId
)
],
t.StringLiteral('bemed')
),
t.VariableDeclaration(
'const',
[
t.VariableDeclarator(
bemId,
t.MemberExpression(
t.CallExpression(
bemedId,
[ opts ]
),
t.Identifier('generate')
)
)
]
)
];
};
const callBem = (bemId, attrs) => {
for (let i = 0; i < attrs.length; i++) if (attrs[i] === undefined) {
attrs[i] = t.NullLiteral();
}
return t.JSXAttribute(
t.JSXIdentifier('className'),
t.JSXExpressionContainer(
t.CallExpression(bemId, attrs)
)
)
};
const getScopeName = (scope) => {
if (scope.block && scope.block.type === 'ArrowFunctionExpression' && scope.parentBlock && scope.parentBlock.type === 'VariableDeclarator') {
return scope.parentBlock.id.name;
}
if (scope.parent && scope.parent.block && scope.parent.block.type === 'ClassDeclaration') {
return scope.parent.block.id.name;
}
};
const readAttributes = (path, properties) => {
const paths = [],
attrs = [],
attributes = path.node.openingElement.attributes;
for (let i = 0; i < attributes.length; i++) {
let name = attributes[i].name.name,
value = attributes[i].value,
p = path.get('openingElement.attributes.' + i);
switch (name) {
case properties.block:
case properties.element:
if (!t.isStringLiteral(value)) {
throw p.buildCodeFrameError("Attribute value must be a string");
}
paths.push(p);
attrs[+(name === properties.element)] = value;
break;
case properties.modifiers:
case properties.mixin:
if (t.isJSXExpressionContainer(value)) {
value = value.expression;
} else {
value = value;
}
paths.push(p);
attrs[2 + (name === properties.mixin)] = value;
break;
case 'className':
return null;
}
}
return { paths, attrs };
};
const mutateAttributes = (path, attributes, bemId) => {
path.get('openingElement').pushContainer('attributes', callBem(bemId, attributes.attrs));
attributes.paths.forEach((path) => path.remove());
};
const JSXChildElementVisitor = {
JSXElement(path) {
const properties = this.properties;
const attributes = readAttributes(path, properties);
if (attributes === null || !attributes.attrs.length) return;
const attrs = attributes.attrs,
bemId = this.bemId;
if (attrs[0]) {
throw path.buildCodeFrameError("Block definition must be in the root JSXElement");
}
if (!attrs[1]) {
throw path.buildCodeFrameError("Element must be specified");
}
attrs[0] = this.block;
mutateAttributes(path, attributes, bemId);
}
};
const JSXRootElementVisitor = {
JSXElement(path) {
if (t.isJSXElement(path.parent)) return;
const properties = this.properties;
const attributes = readAttributes(path, properties);
if (attributes === null) return;
const attrs = attributes.attrs;
if (!attrs[0]) {
const sname = getScopeName(path.scope);
if (sname) {
attrs[0] = t.StringLiteral(sname);
} else {
if (attrs.length) {
throw path.buildCodeFrameError("Block must be specified");
}
return;
}
}
const bemId = this.bemId,
use = this.use;
use.flag = true;
mutateAttributes(path, attributes, bemId);
path.traverse(JSXChildElementVisitor, { bemId, properties, block: attrs[0] });
}
}
return {
visitor: {
Program(path, state) {
const bemId = path.scope.generateUidIdentifier('bem'),
bemedId = path.scope.generateUidIdentifier('bemed');
const separators = state.opts.separators,
properties = Object.assign({
block: 'block',
element: 'elem',
modifiers: 'mods',
mixin: 'mix'
}, state.opts.properties || {});
const use = { flag: false };
path.traverse(JSXRootElementVisitor, { bemId, properties, use });
if (use.flag) {
path.unshiftContainer('body', requireBem(bemId, bemedId, { separators }));
}
}
},
inherits: require('babel-plugin-syntax-jsx')
}
}