-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.lsc
More file actions
243 lines (195 loc) · 6.24 KB
/
types.lsc
File metadata and controls
243 lines (195 loc) · 6.24 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
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
import { PLUGIN_ALIAS_KEYS, addPluginAliasKey, seal } from './util/pluginAliasKeys'
import { registerLightscriptNodeTypes } from './lscNodeTypes'
import * as bt from '@babel/types'
t = Object.assign({_mutable: true}, bt)
// Corrected implementation of babel `isType` that works with node types
// added by `definePluginType`.
export isType(nodeType, targetType) ->
if nodeType === targetType: return true
let aliases = t.FLIPPED_ALIAS_KEYS[targetType]
if aliases:
for elem e in aliases: if nodeType === e: return true
now aliases = PLUGIN_ALIAS_KEYS[targetType]
if aliases:
for elem e in aliases: if nodeType === e: return true
false
export is(type: string, node: Object): boolean ->
node and isType(node.type, type)
// Flipped for use with lightscript ~ operator
export isa(node: Object, type: string): boolean ->
node and isType(node.type, type)
// Get all aliases (including plugin aliases) for the given type
aliases = {}
export getAliasesFor(type: string): Array<string> ->
if aliases[type]: return aliases[type]
aliases[type] = if PLUGIN_ALIAS_KEYS[type]:
t.FLIPPED_ALIAS_KEYS[type].concat(PLUGIN_ALIAS_KEYS[type])
else:
t.FLIPPED_ALIAS_KEYS[type]
aliases[type]
//////////////////////////////// Babel validator helpers
_getValueType(val) -> {
if (Array.isArray(val)) {
return "array";
} else if (val === null) {
return "null";
} else if (val === undefined) {
return "undefined";
} else {
return typeof val;
}
}
export assertValueType(type: string): Function -> {
function validate(node, key, val) {
const valid = _getValueType(val) === type;
if (!valid) {
throw new TypeError(`Property ${key} expected type of ${type} but got ${t.getType(val)}`);
}
}
validate.type = type;
return validate;
}
export chain(...fns): Function -> {
function validate(...args) {
for (const fn of fns) {
fn(...args);
}
}
validate.chainOf = fns;
return validate;
}
export assertEach(callback: Function): Function -> {
function validator(node, key, val) {
if (!Array.isArray(val)) return;
for (let i = 0; i < val.length; i++) {
callback(node, `${key}[${i}]`, val[i]);
}
}
validator.each = callback;
return validator;
}
export assertOneOf(...vals): Function -> {
function validate(node, key, val) {
if (vals.indexOf(val) < 0) {
throw new TypeError(
`Property ${key} expected value to be one of ${JSON.stringify(vals)} but got ${JSON.stringify(val)}`
);
}
}
validate.oneOf = vals;
return validate;
}
export assertNodeType(...types): Function -> {
function validate(node, key, val) {
let valid = false;
for (const type of types) {
if (is(type, val)) {
now valid = true;
break;
}
}
if (!valid) {
throw new TypeError(
`Property ${key} of ${node.type} expected node to be of a type ${JSON.stringify(types)} ` +
`but instead got ${JSON.stringify(val && val.type)}`
);
}
}
validate.oneOfNodeTypes = types;
return validate;
}
//////////////////////////////// Babel builder helpers
makeGenericBuilder(type) -> {
const keys = t.BUILDER_KEYS[type];
(...args) -> {
const countArgs = args.length;
if (countArgs > keys.length) {
throw new Error(
`${type}: Too many arguments passed. Received ${countArgs} but can receive no more than ${
keys.length
}`,
);
}
const node = { type };
let i = 0;
keys.forEach(key => {
let arg;
if (i < countArgs) now arg = args[i];
// if (arg === undefined) arg = loClone(field.default);
node[key] = arg;
i++;
});
return node;
}
}
export hasType(t, type: string): boolean -> {
return (t.TYPES.indexOf(type) > -1)
}
export definePluginType(t, type: string, opts): void -> {
// No duplicate reg
if (t~hasType(type)) {
throw new Error(`Duplicate registration of plugin type ${type}`)
}
// Babel does store[opts.inherits] here, but that var is not exported.
// So we reconstruct the inherited opts from the records.
const inherits = {};
if (opts.inherits) {
inherits.visitor = t.VISITOR_KEYS[opts.inherits];
inherits.builder = t.BUILDER_KEYS[opts.inherits];
inherits.fields = t.NODE_FIELDS[opts.inherits];
inherits.aliases = t.ALIAS_KEYS[opts.inherits];
}
opts.fields = opts.fields || inherits.fields || {};
opts.visitor = opts.visitor || inherits.visitor || [];
opts.aliases = opts.aliases || inherits.aliases || [];
opts.builder = opts.builder || inherits.builder || opts.visitor || [];
opts.pluginAliases = opts.pluginAliases || [];
if (opts.deprecatedAlias) {
t.DEPRECATED_KEYS[opts.deprecatedAlias] = type;
}
// ensure all field keys are represented in `fields`
for (const key of (opts.visitor.concat(opts.builder): Array<string>)) {
opts.fields[key] = opts.fields[key] || {};
}
for (const key in opts.fields) {
const field = opts.fields[key];
if (opts.builder.indexOf(key) === -1) {
field.optional = true;
}
if (field.default === undefined) {
field.default = null;
} else if (!field.validate) {
field.validate = assertValueType(_getValueType(field.default));
}
}
t.VISITOR_KEYS[type] = opts.visitor;
t.BUILDER_KEYS[type] = opts.builder;
t.NODE_FIELDS[type] = opts.fields;
t.ALIAS_KEYS[type] = opts.aliases;
// Babel assembles its TYPES list internally after it finishes internal registration.
// Here we add our newly registered type by hand.
// see https://github.com/babel/babel/pull/4886
t.TYPES.push(type);
opts.aliases.forEach((alias) => {
t.FLIPPED_ALIAS_KEYS[alias] = t.FLIPPED_ALIAS_KEYS[alias] || [alias];
t.FLIPPED_ALIAS_KEYS[alias].push(type);
// See above
if (!t.TYPES[alias]) t.TYPES.push(alias);
});
// Add plugin-only aliases
opts.pluginAliases.forEach((alias) => {
addPluginAliasKey(type, alias)
})
seal(type)
// Create a builder for the new node type.
// Babel does this through code generation but we must do it manually
if t._mutable:
t[type] = makeGenericBuilder(type)
}
export registrationAPI = {
hasType, definePluginType, assertNodeType, assertOneOf, assertValueType
assertEach, chain
}
registerLightscriptNodeTypes(t, registrationAPI)
export { t as types }
export default t