-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
259 lines (224 loc) · 6.45 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/**
* This plugin DRYs up lit-element's `property` decorator by automatically
* determining the value for `type` based on the decorated field's TS type
* annotation.
*
* `String` types are omitted since this is the default. `type` is also
* omitted when `attribute: false` is set.
*
* This plugin will not override explicitly set `type` values.
*
* Example input:
*
* ```js
* class MyElement extends LitElement {
* @property()
* prop1: string;
*
* @property()
* prop2: boolean;
*
* @property()
* prop3: number;
*
* @property()
* prop4: MyObject[];
*
* @property()
* prop5 = false;
*
* @property({ attribute: false })
* prop6: string;
* }
* ```
*
* Example output:
*
* ```js
* class MyElement extends LitElement {
* @property()
* prop1: string;
*
* @property({ type: Boolean })
* prop2: boolean;
*
* @property({ type: Number })
* prop3: number;
*
* @property({ type: Array })
* prop4: MyObject[];
*
* @property({ type: Boolean })
* prop5 = false;
*
* @property({ attribute: false })
* prop6: string;
* }
* ```
*/
module.exports = function (babel) {
const t = babel.types;
return {
visitor: {
// we must start from `Program`, otherwise the TS plugin will strip out
// types before we have a chance to look at them
Program(path) {
path.traverse({
'ClassProperty|ClassMethod'(path) {
const decoratorExpression = findDecoratorCallExpression(
path.node,
'property',
);
if (!decoratorExpression) {
// node doesn't have a `property` decorator
// that is a CallExpression
return;
}
if (decoratorExpression.arguments.length > 1) {
throw path.buildCodeFrameError(
`Expected @property decorator to have at most 1 argument, ` +
`but found ${decoratorExpression.arguments.length}`,
);
}
let decoratorObj = decoratorExpression.arguments[0];
if (getObjectProperty(decoratorObj, 'type')) {
// this property already has a `type` value, skip it
return;
}
const attribute = getObjectProperty(decoratorObj, 'attribute');
if (attribute && isBooleanValue(attribute.value, false)) {
// this property is not reflected to an attribute, no need to add a type
return;
}
const type = determineType(path.node);
if (!type) {
throw path.buildCodeFrameError(
`Could not determine the type for this @property ` +
`decorated field, please explicity add a type`,
);
}
if (type === 'String') {
// `String` is the default type for lit-element properties, we can
// omit it
return;
}
// if the decorator didn't already have an options argument, we have
// to create it first
if (!decoratorObj) {
decoratorObj = t.objectExpression([]);
decoratorExpression.arguments.push(decoratorObj);
}
decoratorObj.properties.push(
createObjectProperty('type', t.identifier(type)),
);
},
});
},
},
};
function findDecoratorCallExpression(node, name) {
if (!node.decorators) {
return undefined;
}
const decorator = node.decorators.find(
(d) =>
d.expression.type === 'CallExpression' &&
d.expression.callee.name === name,
);
// TODO: should we throw if we encounter a @property decorator that isn't a
// call expression? That would most likely be a bug...
return decorator ? decorator.expression : undefined;
}
function getObjectProperty(obj, attr) {
if (!obj) return undefined;
return obj.properties.find((p) => p.key.name === attr);
}
function createObjectProperty(key, value) {
return t.objectProperty(t.identifier(key), value);
}
function determineType(node) {
if (t.isClassMethod(node)) {
// @property decorator can be placed on a `getter` class method
if (node.kind === 'get' && node.returnType) {
return determineTypeFromTypeAnnotation(node.returnType.typeAnnotation);
}
return null;
}
if (node.typeAnnotation) {
return determineTypeFromTypeAnnotation(
node.typeAnnotation.typeAnnotation,
);
}
if (node.value) {
return determineTypeFromNodeValue(node.value);
}
return null;
}
function determineTypeFromTypeAnnotation(node) {
if (t.isTSStringKeyword(node)) {
// field: string
return 'String';
}
if (t.isTSNumberKeyword(node)) {
// field: number
return 'Number';
}
if (t.isTSBooleanKeyword(node)) {
// field: boolean
return 'Boolean';
}
if (
t.isTSArrayType(node) ||
t.isTSTupleType(node) ||
(t.isTSTypeReference(node) && node.typeName.name === 'Array')
) {
// field: string[]
// field: [string, number]
// field: Array<string>
return 'Array';
}
if (t.isTSLiteralType(node)) {
// field: 'value';
// field: true;
const value = node.literal.value;
const type = typeof value;
return upperFirst(type);
}
if (t.isTSTypeReference(node) || t.isTSTypeLiteral(node)) {
// field: MyInterface;
// field: { prop: string };
return 'Object';
}
if (t.isTSUnionType(node)) {
// field: 'blue' | 'green' | 'red';
return allSameOrNull(node.types, determineTypeFromTypeAnnotation);
}
return null;
}
function determineTypeFromNodeValue(node) {
const type = [
[t.isStringLiteral, 'String'],
[t.isNumericLiteral, 'Number'],
[t.isBooleanLiteral, 'Boolean'],
[t.isObjectExpression, 'Object'],
[t.isArrayExpression, 'Array'],
].find((cfg) => cfg[0](node));
return type ? type[1] : undefined;
}
function allSameOrNull(values, convert) {
const [first, ...rest] = values;
const converted = convert(first);
for (const val of rest) {
if (converted !== convert(val)) {
return null;
}
}
return converted;
}
function isBooleanValue(node, expected) {
return t.isBooleanLiteral(node) && node.value === expected;
}
};
function upperFirst(str) {
return str[0].toUpperCase() + str.substr(1);
}