@buxlabs/es6-to-amd 1.2.2 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/settings.local.json +7 -0
- package/CHANGELOG.md +16 -0
- package/package.json +6 -15
- package/src/class/Module.js +231 -217
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
+
## 2.0.0
|
|
2
|
+
|
|
3
|
+
- breaking: require node >= 24.12.0
|
|
4
|
+
- breaking: renamed imports (`import { a as b }`) resolve to the imported member instead of the whole module
|
|
5
|
+
- update: abstract-syntax-tree to 3.0.0
|
|
6
|
+
- remove: pure-utilities, ava, nyc, standard
|
|
7
|
+
- edit: tests run on node:test with 100% coverage
|
|
8
|
+
- add: support `export * from`, `export * as ns from` and `export { a } from`
|
|
9
|
+
- add: support default export combined with named exports
|
|
10
|
+
- add: support destructured exports
|
|
11
|
+
- fix: generated params no longer collide with identifiers used in the file
|
|
12
|
+
- fix: property names, labels and shadowing variables are no longer rewritten as imports
|
|
13
|
+
- fix: code after `export default` is executed
|
|
14
|
+
- fix: `export { a as b }` uses the exported name
|
|
15
|
+
- fix: a dependency imported multiple times is listed once
|
|
16
|
+
|
|
1
17
|
## 1.2.1
|
|
2
18
|
|
|
3
19
|
- update: all dependencies
|
package/package.json
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@buxlabs/es6-to-amd",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "es6 to amd converter",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "
|
|
8
|
-
"
|
|
9
|
-
"watch:test": "npm t -- --watch",
|
|
10
|
-
"coverage": "nyc npm t"
|
|
7
|
+
"test": "node --test test/spec/*.js",
|
|
8
|
+
"coverage": "node --test --experimental-test-coverage --test-coverage-lines=100 --test-coverage-branches=100 --test-coverage-functions=100 test/spec/*.js"
|
|
11
9
|
},
|
|
12
10
|
"repository": {
|
|
13
11
|
"type": "git",
|
|
@@ -24,17 +22,10 @@
|
|
|
24
22
|
"url": "https://github.com/buxlabs/es6-to-amd/issues"
|
|
25
23
|
},
|
|
26
24
|
"homepage": "https://github.com/buxlabs/es6-to-amd#readme",
|
|
27
|
-
"devDependencies": {
|
|
28
|
-
"ava": "^6.4.1",
|
|
29
|
-
"nyc": "^17.1.0"
|
|
30
|
-
},
|
|
31
25
|
"dependencies": {
|
|
32
|
-
"abstract-syntax-tree": "^
|
|
33
|
-
"pure-utilities": "^1.4.6"
|
|
26
|
+
"abstract-syntax-tree": "^3.0.0"
|
|
34
27
|
},
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"test/fixture/*"
|
|
38
|
-
]
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=24.12.0"
|
|
39
30
|
}
|
|
40
31
|
}
|
package/src/class/Module.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const AbstractSyntaxTree = require('abstract-syntax-tree')
|
|
4
|
-
const { identifier, unique } = require('pure-utilities/array')
|
|
5
|
-
const { flatten } = require('pure-utilities/collection')
|
|
6
4
|
|
|
7
5
|
const {
|
|
8
6
|
BlockStatement,
|
|
@@ -15,32 +13,68 @@ const {
|
|
|
15
13
|
ReturnStatement
|
|
16
14
|
} = AbstractSyntaxTree
|
|
17
15
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
16
|
+
const MODULE_DECLARATIONS = [
|
|
17
|
+
'ImportDeclaration',
|
|
18
|
+
'ExportAllDeclaration',
|
|
19
|
+
'ExportDefaultDeclaration',
|
|
20
|
+
'ExportNamedDeclaration'
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
function getFreeIdentifier (identifiers) {
|
|
24
|
+
let alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('')
|
|
25
|
+
let index = 0
|
|
26
|
+
while (identifiers.includes(alphabet[index])) {
|
|
27
|
+
index += 1
|
|
28
|
+
if (index === alphabet.length) {
|
|
29
|
+
index = 0
|
|
30
|
+
alphabet = alphabet.map(character => '_' + character)
|
|
26
31
|
}
|
|
27
32
|
}
|
|
33
|
+
return alphabet[index]
|
|
34
|
+
}
|
|
28
35
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
36
|
+
function uniqueBy (array, iteratee) {
|
|
37
|
+
const seen = new Set()
|
|
38
|
+
return array.filter(item => {
|
|
39
|
+
const key = iteratee(item)
|
|
40
|
+
if (seen.has(key)) { return false }
|
|
41
|
+
seen.add(key)
|
|
42
|
+
return true
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function isModuleDeclaration (node) {
|
|
47
|
+
return MODULE_DECLARATIONS.includes(node.type)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// import and export names are either identifiers or string literals
|
|
51
|
+
function getName (node) {
|
|
52
|
+
return node.type === 'Identifier' ? node.name : node.value
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function getIdentifier (name) {
|
|
56
|
+
return { type: 'Identifier', name }
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
class Module extends AbstractSyntaxTree {
|
|
60
|
+
convert () {
|
|
61
|
+
const dynamic = this.has('ImportExpression')
|
|
62
|
+
if (!dynamic && !this.body.some(isModuleDeclaration)) {
|
|
63
|
+
return
|
|
34
64
|
}
|
|
35
|
-
this.
|
|
36
|
-
this.
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
65
|
+
this.names = this.find('Identifier').map(node => node.name)
|
|
66
|
+
this.params = new Map()
|
|
67
|
+
this.scope = AbstractSyntaxTree.scope(this._tree)
|
|
68
|
+
this.exports = { properties: [], spreads: [], default: null }
|
|
69
|
+
const dependencies = this.getDependencies()
|
|
70
|
+
if (dynamic) {
|
|
71
|
+
dependencies.unshift({ element: 'require', param: 'require' })
|
|
72
|
+
this.convertImportExpressions()
|
|
41
73
|
}
|
|
74
|
+
this.convertModuleDeclarations()
|
|
75
|
+
this.appendReturnStatement()
|
|
42
76
|
this.prependUseStrictLiteral()
|
|
43
|
-
this.
|
|
77
|
+
this.wrapWithDefine(dependencies)
|
|
44
78
|
}
|
|
45
79
|
|
|
46
80
|
prependUseStrictLiteral () {
|
|
@@ -49,55 +83,72 @@ class Module extends AbstractSyntaxTree {
|
|
|
49
83
|
}))
|
|
50
84
|
}
|
|
51
85
|
|
|
52
|
-
|
|
53
|
-
|
|
86
|
+
getFreeName (name) {
|
|
87
|
+
while (this.names.includes(name)) {
|
|
88
|
+
name = '_' + name
|
|
89
|
+
}
|
|
90
|
+
this.names.push(name)
|
|
91
|
+
return name
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// every dependency gets one param: the local name of its default or
|
|
95
|
+
// namespace import when there is one, a generated identifier otherwise
|
|
96
|
+
getParam (element) {
|
|
97
|
+
if (!this.params.has(element)) {
|
|
98
|
+
const param = getFreeIdentifier(this.names)
|
|
99
|
+
this.names.push(param)
|
|
100
|
+
this.params.set(element, param)
|
|
101
|
+
}
|
|
102
|
+
return this.params.get(element)
|
|
54
103
|
}
|
|
55
104
|
|
|
56
|
-
|
|
57
|
-
const
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
return {
|
|
63
|
-
element: node.source.value
|
|
105
|
+
getDependencies () {
|
|
106
|
+
const imports = this.body.filter(node => node.type === 'ImportDeclaration')
|
|
107
|
+
for (const node of imports) {
|
|
108
|
+
for (const specifier of node.specifiers) {
|
|
109
|
+
if (specifier.type !== 'ImportSpecifier' && !this.params.has(node.source.value)) {
|
|
110
|
+
this.params.set(node.source.value, specifier.local.name)
|
|
64
111
|
}
|
|
65
112
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
if (specifier.imported.name !== specifier.local.name) {
|
|
74
|
-
return this.getLocalSpecifier(node, specifier)
|
|
75
|
-
} else if (Object.prototype.hasOwnProperty.call(dependencyToIdentifierMap, value)) {
|
|
76
|
-
param = dependencyToIdentifierMap[value]
|
|
77
|
-
} else {
|
|
78
|
-
const identifiers = unique(flatten(ids)).concat(Object.values(dependencyToIdentifierMap))
|
|
79
|
-
param = identifier(identifiers)
|
|
80
|
-
dependencyToIdentifierMap[value] = param
|
|
81
|
-
}
|
|
82
|
-
return {
|
|
83
|
-
param,
|
|
84
|
-
element: node.source.value,
|
|
85
|
-
name: specifier.local.name
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}.bind(this))
|
|
89
|
-
}))
|
|
90
|
-
return result.sort((a, b) => {
|
|
91
|
-
if (a.param && !b.param) { return -1 }
|
|
92
|
-
if (!a.param && b.param) { return 1 }
|
|
93
|
-
return 0
|
|
113
|
+
}
|
|
114
|
+
const dependencies = this.body.flatMap(node => {
|
|
115
|
+
if (node.type === 'ImportDeclaration') {
|
|
116
|
+
return this.getImportDependencies(node)
|
|
117
|
+
}
|
|
118
|
+
// export ... from
|
|
119
|
+
return node.source ? { element: node.source.value, param: this.getParam(node.source.value) } : []
|
|
94
120
|
})
|
|
121
|
+
const elements = new Set(dependencies.filter(pair => pair.param).map(pair => pair.element))
|
|
122
|
+
const pairs = dependencies.filter(pair => pair.param || !elements.has(pair.element))
|
|
123
|
+
return uniqueBy(pairs, pair => pair.element + '\n' + pair.param)
|
|
124
|
+
.sort((a, b) => Boolean(b.param) - Boolean(a.param))
|
|
95
125
|
}
|
|
96
126
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
127
|
+
getImportDependencies (node) {
|
|
128
|
+
const element = node.source.value
|
|
129
|
+
if (node.specifiers.length === 0) {
|
|
130
|
+
return { element }
|
|
131
|
+
}
|
|
132
|
+
return node.specifiers.map(specifier => {
|
|
133
|
+
if (specifier.type !== 'ImportSpecifier') {
|
|
134
|
+
return { element, param: specifier.local.name }
|
|
135
|
+
}
|
|
136
|
+
const param = this.getParam(element)
|
|
137
|
+
this.convertReferences(specifier.local.name, param, specifier.imported)
|
|
138
|
+
return { element, param }
|
|
139
|
+
})
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// scope analysis gives the identifiers that really refer to the import,
|
|
143
|
+
// so property keys, labels and shadowing variables are left untouched
|
|
144
|
+
convertReferences (name, param, imported) {
|
|
145
|
+
const binding = this.scope.getBinding(name)
|
|
146
|
+
for (const { identifier, parent } of binding.references) {
|
|
147
|
+
if (parent.type === 'Property' && parent.shorthand) {
|
|
148
|
+
parent.shorthand = false
|
|
149
|
+
}
|
|
150
|
+
delete identifier.name
|
|
151
|
+
Object.assign(identifier, this.getMemberExpression(param, imported))
|
|
101
152
|
}
|
|
102
153
|
}
|
|
103
154
|
|
|
@@ -125,7 +176,7 @@ class Module extends AbstractSyntaxTree {
|
|
|
125
176
|
this.getBinaryExpression(new Literal({ value: 'default' }), 'in', module)
|
|
126
177
|
),
|
|
127
178
|
this.getObjectExpression([this.getProperty(defaultIdentifier, module)]),
|
|
128
|
-
this.getCallExpression(this.getMemberExpression('Object', 'defineProperty'), [
|
|
179
|
+
this.getCallExpression(this.getMemberExpression('Object', getIdentifier('defineProperty')), [
|
|
129
180
|
module,
|
|
130
181
|
new Literal({ value: 'default' }),
|
|
131
182
|
this.getObjectExpression([
|
|
@@ -143,47 +194,113 @@ class Module extends AbstractSyntaxTree {
|
|
|
143
194
|
})
|
|
144
195
|
}
|
|
145
196
|
|
|
146
|
-
|
|
147
|
-
const
|
|
148
|
-
this.
|
|
149
|
-
this.
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
197
|
+
convertModuleDeclarations () {
|
|
198
|
+
const last = this.body.at(-1)
|
|
199
|
+
const single = this.body.filter(node => node.type.startsWith('Export')).length === 1
|
|
200
|
+
this.body = this.body.flatMap(node => {
|
|
201
|
+
if (node.type === 'ImportDeclaration') {
|
|
202
|
+
return []
|
|
203
|
+
}
|
|
204
|
+
if (node.type === 'ExportAllDeclaration') {
|
|
205
|
+
return this.convertExportAllDeclaration(node)
|
|
206
|
+
}
|
|
207
|
+
if (node.type === 'ExportNamedDeclaration') {
|
|
208
|
+
return this.convertExportNamedDeclaration(node)
|
|
209
|
+
}
|
|
210
|
+
if (node.type === 'ExportDefaultDeclaration') {
|
|
211
|
+
return this.convertExportDefaultDeclaration(node, node === last && single)
|
|
212
|
+
}
|
|
213
|
+
return node
|
|
214
|
+
})
|
|
153
215
|
}
|
|
154
216
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
217
|
+
addExport (exported, value, shorthand = false) {
|
|
218
|
+
if (getName(exported) === 'default') {
|
|
219
|
+
this.exports.default = value
|
|
220
|
+
} else {
|
|
221
|
+
this.exports.properties.push(this.getProperty(exported, value, shorthand))
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
convertExportAllDeclaration (node) {
|
|
226
|
+
const param = getIdentifier(this.getParam(node.source.value))
|
|
227
|
+
if (node.exported) {
|
|
228
|
+
this.addExport(node.exported, param)
|
|
229
|
+
} else {
|
|
230
|
+
this.exports.spreads.push(param)
|
|
231
|
+
}
|
|
232
|
+
return []
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
convertExportNamedDeclaration (node) {
|
|
236
|
+
if (node.declaration) {
|
|
237
|
+
const identifiers = new Set(AbstractSyntaxTree.find(node.declaration, 'Identifier'))
|
|
238
|
+
for (const binding of this.scope.bindings) {
|
|
239
|
+
if (binding.declarations.some(identifier => identifiers.has(identifier))) {
|
|
240
|
+
this.addExport(getIdentifier(binding.name), getIdentifier(binding.name))
|
|
160
241
|
}
|
|
161
242
|
}
|
|
162
|
-
|
|
243
|
+
return node.declaration
|
|
244
|
+
}
|
|
245
|
+
for (const { local, exported } of node.specifiers) {
|
|
246
|
+
if (!node.source) {
|
|
247
|
+
this.addExport(exported, local, local.type === 'Identifier' && local.name === exported.name)
|
|
248
|
+
} else if (getName(local) === 'default') {
|
|
249
|
+
this.addExport(exported, getIdentifier(this.getParam(node.source.value)))
|
|
250
|
+
} else {
|
|
251
|
+
this.addExport(exported, this.getMemberExpression(this.getParam(node.source.value), local))
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return []
|
|
163
255
|
}
|
|
164
256
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
257
|
+
convertExportDefaultDeclaration (node, returnable) {
|
|
258
|
+
const { declaration } = node
|
|
259
|
+
if (returnable) {
|
|
260
|
+
return new ReturnStatement({ argument: this.getExpression(declaration) })
|
|
261
|
+
}
|
|
262
|
+
if (declaration.type.endsWith('Declaration') && declaration.id) {
|
|
263
|
+
this.exports.default = getIdentifier(declaration.id.name)
|
|
264
|
+
return declaration
|
|
265
|
+
}
|
|
266
|
+
const name = this.getFreeName('_default')
|
|
267
|
+
this.exports.default = getIdentifier(name)
|
|
268
|
+
return {
|
|
269
|
+
type: 'VariableDeclaration',
|
|
270
|
+
kind: 'const',
|
|
271
|
+
declarations: [{
|
|
272
|
+
type: 'VariableDeclarator',
|
|
273
|
+
id: getIdentifier(name),
|
|
274
|
+
init: this.getExpression(declaration)
|
|
275
|
+
}]
|
|
276
|
+
}
|
|
171
277
|
}
|
|
172
278
|
|
|
173
|
-
|
|
174
|
-
|
|
279
|
+
// default exported functions and classes are declarations
|
|
280
|
+
getExpression (node) {
|
|
281
|
+
node.type = node.type.replace('Declaration', 'Expression')
|
|
282
|
+
return node
|
|
175
283
|
}
|
|
176
284
|
|
|
177
|
-
|
|
178
|
-
this.
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
return node
|
|
184
|
-
}
|
|
285
|
+
appendReturnStatement () {
|
|
286
|
+
const { properties, spreads } = this.exports
|
|
287
|
+
let argument = this.exports.default
|
|
288
|
+
if (properties.length > 0 || spreads.length > 0) {
|
|
289
|
+
if (argument) {
|
|
290
|
+
properties.push(this.getProperty(getIdentifier('default'), argument))
|
|
185
291
|
}
|
|
186
|
-
|
|
292
|
+
argument = this.getObjectExpression(properties)
|
|
293
|
+
if (spreads.length > 0) {
|
|
294
|
+
const sources = properties.length > 0 ? [...spreads, argument] : spreads
|
|
295
|
+
argument = this.getCallExpression(
|
|
296
|
+
this.getMemberExpression('Object', getIdentifier('assign')),
|
|
297
|
+
[this.getObjectExpression([]), ...sources]
|
|
298
|
+
)
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
if (argument) {
|
|
302
|
+
this.append(new ReturnStatement({ argument }))
|
|
303
|
+
}
|
|
187
304
|
}
|
|
188
305
|
|
|
189
306
|
getDefine (nodes) {
|
|
@@ -195,17 +312,9 @@ class Module extends AbstractSyntaxTree {
|
|
|
195
312
|
})
|
|
196
313
|
}
|
|
197
314
|
|
|
198
|
-
convertExportNamedDeclarationToDefine () {
|
|
199
|
-
this.prependUseStrictLiteral()
|
|
200
|
-
this.convertExportNamedDeclarations()
|
|
201
|
-
this.wrap(body => {
|
|
202
|
-
return [this.getDefineWithFunctionExpression(body)]
|
|
203
|
-
})
|
|
204
|
-
}
|
|
205
|
-
|
|
206
315
|
getFunctionExpression (params, body) {
|
|
207
316
|
return new FunctionExpression({
|
|
208
|
-
params
|
|
317
|
+
params,
|
|
209
318
|
body: new BlockStatement({ body })
|
|
210
319
|
})
|
|
211
320
|
}
|
|
@@ -224,145 +333,50 @@ class Module extends AbstractSyntaxTree {
|
|
|
224
333
|
})
|
|
225
334
|
}
|
|
226
335
|
|
|
227
|
-
getMemberExpression (object,
|
|
336
|
+
getMemberExpression (object, property) {
|
|
337
|
+
const computed = property.type !== 'Identifier'
|
|
228
338
|
return {
|
|
229
339
|
type: 'MemberExpression',
|
|
230
|
-
object:
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
},
|
|
234
|
-
property: {
|
|
235
|
-
type: 'Identifier',
|
|
236
|
-
name: member
|
|
237
|
-
}
|
|
340
|
+
object: getIdentifier(object),
|
|
341
|
+
property: computed ? { type: 'Literal', value: property.value } : getIdentifier(property.name),
|
|
342
|
+
computed
|
|
238
343
|
}
|
|
239
344
|
}
|
|
240
345
|
|
|
241
346
|
getConditionalExpression (test, consequent, alternate) {
|
|
242
|
-
return {
|
|
243
|
-
type: 'ConditionalExpression',
|
|
244
|
-
test: test,
|
|
245
|
-
consequent: consequent,
|
|
246
|
-
alternate: alternate
|
|
247
|
-
}
|
|
347
|
+
return { type: 'ConditionalExpression', test, consequent, alternate }
|
|
248
348
|
}
|
|
249
349
|
|
|
250
350
|
getLogicalExpression (left, operator, right) {
|
|
251
|
-
return {
|
|
252
|
-
type: 'LogicalExpression',
|
|
253
|
-
left: left,
|
|
254
|
-
operator: operator,
|
|
255
|
-
right: right
|
|
256
|
-
}
|
|
351
|
+
return { type: 'LogicalExpression', left, operator, right }
|
|
257
352
|
}
|
|
258
353
|
|
|
259
354
|
getBinaryExpression (left, operator, right) {
|
|
260
|
-
return {
|
|
261
|
-
type: 'BinaryExpression',
|
|
262
|
-
left: left,
|
|
263
|
-
operator: operator,
|
|
264
|
-
right: right
|
|
265
|
-
}
|
|
355
|
+
return { type: 'BinaryExpression', left, operator, right }
|
|
266
356
|
}
|
|
267
357
|
|
|
268
358
|
getTypeof (argument) {
|
|
269
|
-
return {
|
|
270
|
-
type: 'UnaryExpression',
|
|
271
|
-
prefix: true,
|
|
272
|
-
operator: 'typeof',
|
|
273
|
-
argument: argument
|
|
274
|
-
}
|
|
359
|
+
return { type: 'UnaryExpression', prefix: true, operator: 'typeof', argument }
|
|
275
360
|
}
|
|
276
361
|
|
|
277
|
-
getProperty (
|
|
278
|
-
return {
|
|
279
|
-
type: 'Property',
|
|
280
|
-
key: nodeOrKey,
|
|
281
|
-
value: typeof shorthandOrValue === 'boolean' || typeof shorthandOrValue === 'undefined' ? nodeOrKey : shorthandOrValue,
|
|
282
|
-
shorthand: shorthandOrValue === true,
|
|
283
|
-
kind: 'init'
|
|
284
|
-
}
|
|
362
|
+
getProperty (key, value, shorthand = false) {
|
|
363
|
+
return { type: 'Property', key, value, shorthand, kind: 'init' }
|
|
285
364
|
}
|
|
286
365
|
|
|
287
366
|
getObjectExpression (properties) {
|
|
288
|
-
return {
|
|
289
|
-
type: 'ObjectExpression',
|
|
290
|
-
properties: properties
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
getObjectExpressionForDeclarations (declarations) {
|
|
295
|
-
return this.getObjectExpression(this.mapDeclarationsToProperties(declarations))
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
mapDeclarationsToProperties (declarations) {
|
|
299
|
-
return flatten(declarations.map(this.mapDeclarationToProperty.bind(this)))
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
mapDeclarationToProperty (declaration) {
|
|
303
|
-
if (!declaration.declaration && declaration.specifiers) {
|
|
304
|
-
return declaration.specifiers.map(node => {
|
|
305
|
-
return this.getProperty(node.local, true)
|
|
306
|
-
})
|
|
307
|
-
}
|
|
308
|
-
if (declaration.declaration.type === 'VariableDeclaration') {
|
|
309
|
-
return declaration.declaration.declarations.map(node => {
|
|
310
|
-
return this.getProperty(node.id)
|
|
311
|
-
})
|
|
312
|
-
}
|
|
313
|
-
return this.getProperty(declaration.declaration.id)
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
normalizePairs (pairs) {
|
|
317
|
-
const nodes = pairs.filter(pair => !!pair.name)
|
|
318
|
-
const names = nodes.map(node => node.name)
|
|
319
|
-
this.replace({
|
|
320
|
-
leave: (current, parent) => {
|
|
321
|
-
if (current.type === 'Identifier') {
|
|
322
|
-
const index = names.indexOf(current.name)
|
|
323
|
-
if (index !== -1) {
|
|
324
|
-
const pair = nodes[index]
|
|
325
|
-
return this.convertIdentifierToMemberExpression(pair)
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
return current
|
|
329
|
-
}
|
|
330
|
-
})
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
convertIdentifierToMemberExpression (pair, current) {
|
|
334
|
-
return {
|
|
335
|
-
type: 'MemberExpression',
|
|
336
|
-
object: {
|
|
337
|
-
type: 'Identifier',
|
|
338
|
-
name: pair.param
|
|
339
|
-
},
|
|
340
|
-
property: {
|
|
341
|
-
type: 'Identifier',
|
|
342
|
-
name: pair.name
|
|
343
|
-
}
|
|
344
|
-
}
|
|
367
|
+
return { type: 'ObjectExpression', properties }
|
|
345
368
|
}
|
|
346
369
|
|
|
347
370
|
getArrayExpression (elements) {
|
|
348
|
-
return { type: 'ArrayExpression', elements
|
|
371
|
+
return { type: 'ArrayExpression', elements }
|
|
349
372
|
}
|
|
350
373
|
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
const
|
|
354
|
-
.map(function (element) {
|
|
355
|
-
return { type: 'Literal', value: element }
|
|
356
|
-
})
|
|
357
|
-
const params = pairs.filter(pair => pair.param).map(pair => pair.param)
|
|
358
|
-
.map(function (param) {
|
|
359
|
-
return { type: 'Identifier', name: param }
|
|
360
|
-
})
|
|
374
|
+
wrapWithDefine (dependencies) {
|
|
375
|
+
const elements = dependencies.map(pair => ({ type: 'Literal', value: pair.element }))
|
|
376
|
+
const params = dependencies.filter(pair => pair.param).map(pair => getIdentifier(pair.param))
|
|
361
377
|
this.wrap(body => {
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
this.getFunctionExpression(params, body)
|
|
365
|
-
])]
|
|
378
|
+
const callback = this.getFunctionExpression(params, body)
|
|
379
|
+
return [this.getDefine(elements.length > 0 ? [this.getArrayExpression(elements), callback] : [callback])]
|
|
366
380
|
})
|
|
367
381
|
}
|
|
368
382
|
}
|