forked from RobotWebTools/ros3djs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
es6-transpiler.js
722 lines (638 loc) · 21 KB
/
es6-transpiler.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
const debug = require('debug')
const path = require('path')
debug.enable('ES6Transpiler:Error')
const colors = {
GRAY: 0,
RED: 1,
GREEN: 2,
YELLOW: 3,
BLUE: 4,
PURPLE: 5,
CYAN: 6,
WHITE: 7,
}
const stringifyObjects = (...args) => args
.map(arg => typeof arg === 'object'
? JSON.stringify(arg, null, 2)
: arg)
const logError = debug('ES6Transpiler:Error')
logError.log = (...args) => {
console.error(...stringifyObjects(...args))
throw new Error("Execution stopped because of an error, sea above.")
}
logError.color = colors.RED
const logWarning = debug('ES6Transpiler:Warning')
logWarning.log = (...args) => console.log(...stringifyObjects(...args))
logWarning.color = colors.YELLOW
const logInfo = debug('ES6Transpiler:Info')
logInfo.log = (...args) => console.log(...stringifyObjects(...args))
logInfo.color = colors.BLUE
const getColorCodes = (c, bold) => {
const boldCode = bold ? 1 : 5
const colorCode = c % 10
return {
start: `\u001b[3${c};${boldCode}m`,
finish: '\u001b[0m',
}
}
const logHeader = (color) => (...args) => {
const { start, finish } = getColorCodes(color)
console.log(start, ...args, finish)
}
const debugRules = {
logClasses: false,
logConstructors: false,
logExportsAsFound: false,
logExtensions: false,
logExternalDepsAsFound: false,
logExternalDepsAtEnd: false,
logFunctions: false,
logImportMarking: false,
logInheritanceAtEnd: false,
logInjectedExternalImports: false,
logInjectedInternalImports:false,
logInjectedSuperCalls: false,
logInternalDepsAsFound: false,
logInternalDepsAtEnd: false,
logSuperConstructorCalls: false,
logSuperMethodCalls: false,
}
const indent = ' '
// Track dependencies for validating transpilation output
const dependencies = {
internal: {},
track(file, dependency) {
// ignore self-dependencies
if (path.basename(file, '.js') === dependency) {
return
}
const deps = dependencies.internal
if (!deps[file]) {
deps[file] = []
}
if (!deps[file].includes(dependency)) {
if (debugRules.logInternalDepsAsFound) {
logInfo('depends on', dependency)
}
deps[file].push(dependency)
}
},
external: {},
trackExternal(file, dependency) {
const deps = dependencies.external
if (!deps[file]) {
deps[file] = []
}
if (!deps[file].includes(dependency)) {
if (debugRules.logExternalDepsAsFound) {
logInfo('depends on external dep', dependency)
}
deps[file].push(dependency)
}
},
internalToString() {
return JSON.stringify(dependencies.internal, null, 2)
},
externalToString() {
return JSON.stringify(dependencies.external, null, 2)
},
internalForFile(file) {
const deps = dependencies.internal
return deps[file]
},
externalForFile(file) {
const deps = dependencies.external
return deps[file]
},
}
// Track inheritance for class generation
const inheritance = {
index: {},
isClass(prop) {
return inheritance.index[prop] !== undefined
},
getParent(child) {
return inheritance.index[child]
},
track(child, parent) {
if (parent && !inheritance.index[child]) {
if (debugRules.logExtensions) {
logInfo(child, 'extends', parent)
}
inheritance.index[child] = parent
} else {
if (!inheritance.isClass(child)) {
if (debugRules.logClasses) {
logWarning(child, 'is a class')
}
inheritance.index[child] = null
}
}
},
toString() {
return JSON.stringify(inheritance.index, null, 2)
},
}
// Track exported properties for import injection
const exported = {
propIndex: {}, // exporter -> prop
track(exporter, prop) {
const props = exported.propIndex
const exporters = exported.exporterIndex
if (!props[exporter]) {
props[exporter] = []
}
if (props[exporter].includes(prop)) {
logError('property exported multiple times', { exporter, prop })
return
}
if (debugRules.logExportsAsFound) {
logInfo('exports', prop)
}
props[exporter].push(prop)
if (exporters[prop]) {
logError('prop', prop, 'already exported by', exporter)
return
}
exporters[prop] = exporter
},
exporterIndex: {}, // prop -> exporter
getExporter(prop) {
const exporters = exported.exporterIndex
const exporter = exporters[prop]
if (!exporter) {
logError('no export found for', prop, { exporters })
}
return exporter
},
}
const superConstructorCalls = {
index: {},
track(child, parent) {
if (superConstructorCalls.index[child]) {
logError('child', child, 'already called its super constructor:', parent)
return
}
superConstructorCalls.index[child] = parent
},
getParent(child) {
return superConstructorCalls.index[child]
},
}
const getFileClass = (filepath) => path.basename(filepath, '.js')
const isFileClass = (filepath, className) => getFileClass(filepath) === className
const transpile = {
// Replace initial ROS3D assignment
initialROS3DAssignment: [
// from
/var ROS3D = ROS3D \|\| \{\n REVISION \: '([0-9]+\.[0-9]+\.[0-9]+)'\n\};/m,
// to
(match, $1) => {
const revision = $1
return `export var REVISION = '${revision}';`
},
],
// Replace mutations with exported properties
exportedProperties: (filepath) => [
// from:
// ROS3D.MARKER_ARROW = 0;
/\nROS3D\.(\S*)\s*=\s*(.*)/g,
// to:
// export var MARKER_ARROW = 0;
(match, $1, $2) => {
const prop = $1
const rhs = $2
exported.track(filepath, prop)
return `\nexport var ${prop} = ${rhs}`
},
],
// Remove ROS3D prefix on internal dependencies
internalDependencies: (filepath) => [
// from:
// return ROS3D.findClosestPoint(axisRay, mpRay);
/(.*)ROS3D\.(\w+)(?!.*=.*)/g,
// to:
// return findClosestPoint(axisRay, mpRay);
(match, $1, $2) => {
const preStuffs = $1
const dep = $2
if (/^\s*(?:\*|\/\/)/.test(preStuffs)) {
// we're in a block comment, bail
return match
}
// track dependency on $1
dependencies.track(filepath, dep)
return `${preStuffs}${dep}`
}
],
// Track external dependencies for import injection
externalDependencies: (filepath) => [
/(.*)(THREE|EventEmitter2|ROSLIB|ColladaLoader|OBJLoader|MTLLoader|STLLoader).*/g,
(match, $preStuffs, $dep) => {
if (/^\s*(?:\*|\/\/)/.test($preStuffs)) {
// we're in a block comment, bail
return match
}
// track dependency on $1
dependencies.trackExternal(filepath, $dep)
return match
}
],
// Replace __proto__ mutation with class extension
buildInheritanceIndexViaProto: [
// from:
// ROS3D.PoseWithCovariance.prototype.__proto__ = THREE.Object3D.prototype;
/ROS3D\.(\w+)\.prototype\.__proto__ = (.*)\.prototype;[\r\n]?/g,
// to:
// set PoseWithCovariance to subclass from THREE.Object3D in inheritance index
(match, $1, $2) => {
// track $1 extends $2
inheritance.track($1, $2)
// remove it
return ''
}
],
// Replace __proto__ mutation with class extension
buildInheritanceIndexViaObjectAssign: [
// from:
// Object.assign(InteractiveMarker.prototype, THREE.EventDispatcher.prototype);
/Object\.assign\((\w+)\.prototype,\s*(.*)\.prototype\);/g,
// to:
// set InteractiveMarker to subclass from THREE.EventDispatcher in inheritance index
(match, $1, $2) => {
// track $1 extends $2
inheritance.track($1, $2)
// remove it
return ''
}
],
// Refactor methods
methods: [
// from:
// ROS3D.Arrow2.prototype.dispose = function () { ... };
/ROS3D\.(\w+)\.prototype\.(\w+)\s*=\s*function\s*|function\s+(\w+)/g,
// to:
// dispose() { ... };
(match, $1, $2, $3) => {
if ($1) {
// matches $1 and $2, replace with method
const $class = $1
const $method = $2
// bail, not our responsibility
if ($method === '__proto__') {
return match
}
return $method
} else {
// matches $3, log skeptically, but don't replace
if (debugRules.logFunctions) {
logInfo('found function declaration', { $3 })
}
return match
}
}
],
// Refactor constructor functions
constructors: (filepath, state = { foundConstructor: false }) => [
// from:
// ROS3D.Arrow2 = function(options) { ... };
/ROS3D\.(\w+)\s*=\s*function\s*\((.*)\)/g,
// to:
// constructor(options) { ... };
(match, $1, $2) => {
const isClass = isFileClass(filepath, $1)
// if (isClass1 !== isClass2) {
// logWarning('class mismatch', {
// filepath,
// $1,
// isInheritanceClass: isClass1,
// isFileClass: isClass2,
// })
// }
if (isClass) {
if (state.foundConstructor) {
logError('Already found a constructor in this file...', { match, $1, $2 })
}
state.foundConstructor = true
if (debugRules.logConstructors) {
logInfo('Found constructor', { match, $1, $2 })
}
const arguments = $2
return `constructor(${arguments})`
} else {
return match
}
}
],
// Refactor super calls
superCalls: (filepath, state = { superMethodCalls: {} }) => [
// from:
// constructor(options) {
// ...
// THREE.ArrowHelper.call(this, direction, origin, length, 0xff0000);
// ...
// };
/[\r\n]([\s]*)([\w.]+)\.call\((?:this|that)(.*)/g,
// to:
// constructor(options) {
// ...
// super(direction, origin, length, 0xff0000);
// ...
// }
(match, indent, $1, $2) => {
const child = getFileClass(filepath)
const parent = inheritance.getParent(child)
const callee = $1
const args = $2
.split(/,|\)/) // split args from leading comma and ending paren
.slice(1, -1) // slice out just the args
.join(',') // put them back into a string
.trim() // trim whitespace
if (!parent) {
logWarning('no parent found for super call disambiguation', { match, child, parent, callee, args })
}
if (callee === parent) {
// we got a super constructor call
if (debugRules.logSuperConstructorCalls) {
logInfo('found super constructor call', { match, child, parent, args })
}
superConstructorCalls.track(child, parent)
return `\n${indent}super(${args});`
} else {
// we got a super method call
const isMultipart = callee.includes('.')
const calleeParts = callee.split('.')
const hasTooManyParts = isMultipart && calleeParts.length > 3
const partsMatch = isMultipart && calleeParts[0] === child && calleeParts[1] === 'prototype'
if (hasTooManyParts) {
logError('super method call invalid, too many parts', { match, child, callee, args })
return match
}
if (!partsMatch) {
logError('super method call invalid, parts dont match', { match, child, parent, callee, args })
return match
}
const method = isMultipart
? calleeParts[2]
: callee
if (debugRules.logSuperMethodCalls) {
logInfo('found super method call', { match, child, parent, method, args })
}
if (state.superMethodCalls[method]) {
logWarning('multiple calls found to super method, refactor may be necessary', { child, method })
}
state.superMethodCalls[method] = true
return `\n${indent}super.${method}(${args});`
}
}
],
// Generate class wrappers using inheritance index
classes: (filepath, state = { isInClass: false, matchedFirstComment: false }) => [
// from:
// constructor(options) {
// ...
// }
//
// dispose() {
// ...
// }
// /.*(\*\/).*|[\r\n]+$(?:[\r\n]+$)+((?![\r\n]+))|.*/gm,
// /(\/\*\*(?:$|[.\r\n])*\*\/(?:$|[\s\r\n])*constructor\(.*)|[\r\n]+$(?:[\r\n]+$)+((?![\r\n]+))|.*/gm,
/((?:\/\*\*(?:(?:\*[^/]|[^*])+?)\*\/)(?:[\s\r\n])*constructor\s*\(.*)|$(?:[\r\n]$)*((?![\r\n]))|.+/gm,
// to:
// export class Arrow2 extends THREE.ArrowHelper {
// constructor(options) {
// ...
// }
//
// dispose() {
// ...
// }
// }
(match, $1, $2) => {
// $1 matches '/**' + anything not '*/' + '*/' + 'constructor('- aka a block comment followed by a constructor
// $2 matches the end of a line that isn't followed by a newline char - aka EOF
const isStart = $1 !== undefined && !state.matchedFirstComment
const isFinish = $2 !== undefined
const className = getFileClass(filepath)
const parent = inheritance.getParent(className)
if (isStart) {
const indentedMatch = indent + match.replace(/[\r\n]/g, `\n${indent}`)
state.matchedFirstComment = true
state.isInClass = true
// Track this as an export
exported.track(filepath, className)
if (parent) {
return `export class ${className} extends ${parent} {\n\n${indentedMatch}`
} else {
return `export class ${className} {\n\n${indentedMatch}`
}
}
if (state.isInClass) {
if (!isFinish) {
return `${indent}${match}`
} else {
state.isInClass = false
return `\n}\n`
}
}
return match
}
],
imports: (filepath, state = { isStart: false }) => [
/^/m,
(match) => {
let externalDeps = dependencies.externalForFile(filepath)
const internalDeps = dependencies.internalForFile(filepath)
let externalImports = ''
let internalImports = ''
if (externalDeps && externalDeps.length > 0) {
// Make sure these come after importing THREE
const threeExtensions = ['ColladaLoader', 'MTLLoader', 'OBJLoader', 'STLLoader']
const threeExtensionsUsed = []
threeExtensions.forEach(ext => {
if (externalDeps.includes(ext)) {
threeExtensionsUsed.push(ext)
}
})
if (threeExtensionsUsed.length > 0) {
// remove all special deps
externalDeps = externalDeps.filter(dep => !threeExtensionsUsed.includes(dep))
// reinsert after THREE
const threeIndex = externalDeps.indexOf('THREE')
if (threeIndex == -1) {
logError('found dependency on THREE extension without dependency on THREE itself!', { filepath, threeExtensionsUsed })
} else {
threeExtensionsUsed.forEach(ext => {
externalDeps.splice(threeIndex + 1, 0, ext)
})
}
}
const importStrings = externalDeps.map(dep => {
let importString
switch (dep) {
case 'THREE': {
const modulePath = 'shims/three/core.js'
const resolvedPath = path.relative(path.dirname(filepath), modulePath)
importString = `import THREE from '${resolvedPath}';`
break;
}
case 'ROSLIB': {
importString = "import * as ROSLIB from 'roslib';"
break;
}
case 'EventEmitter2': {
importString = "import EventEmitter2 from 'eventemitter2';"
break;
}
case 'ColladaLoader': {
const modulePath = 'shims/three/ColladaLoader.js'
const resolvedPath = path.relative(path.dirname(filepath), modulePath)
importString = `import '${resolvedPath}';`
break;
}
case 'MTLLoader': {
const modulePath = 'shims/three/MTLLoader.js'
const resolvedPath = path.relative(path.dirname(filepath), modulePath)
importString = `import '${resolvedPath}';`
break;
}
case 'OBJLoader': {
const modulePath = 'shims/three/OBJLoader.js'
const resolvedPath = path.relative(path.dirname(filepath), modulePath)
importString = `import '${resolvedPath}';`
break;
}
case 'STLLoader': {
const modulePath = 'shims/three/STLLoader.js'
const resolvedPath = path.relative(path.dirname(filepath), modulePath)
importString = `import '${resolvedPath}';`
break;
}
default: {
logError('unknown external dependency', dep, { filepath, externalDeps })
return undefined
}
}
if (importString) {
if (debugRules.logInjectedExternalImports) {
logInfo('injecting import statement:', importString)
}
}
return importString
})
externalImports = importStrings.length > 0
? importStrings.filter(s => !!s).join('\n') + '\n\n'
: ''
}
if (internalDeps) {
const exporters = {}
// build map of exporter -> dep
internalDeps.forEach(dep => {
const exporter = exported.getExporter(dep)
if (!exporter) {
logError('cant find exporter for', dep, { filepath })
return
}
// skip self exports
if (exporter === filepath) {
return
}
// build collection of deps for each exporter
if (!exporters[exporter]) {
exporters[exporter] = []
}
exporters[exporter].push(dep)
if (debugRules.logImportMarking) {
logInfo('marking import of', dep, 'from', exporter)
}
})
// inject imports grouped by exporter
const importStrings = Object.keys(exporters).map(exporter => {
const props = exporters[exporter]
const joinedProps = props.join(', ')
const modulePath = path
.relative(path.dirname(filepath), exporter)
.split(/\.js/)[0]
const internalModulePath = modulePath.startsWith('.')
? modulePath
: ['.', ...modulePath.split(path.sep)].join(path.sep)
const importString = `import { ${joinedProps} } from '${internalModulePath}'`
if (debugRules.logInjectedInternalImports) {
logInfo('injecting import statement:', importString)
}
return importString
})
internalImports = importStrings.length > 0
? importStrings.join('\n') + '\n\n'
: ''
}
const allImports = externalImports + internalImports
return allImports
}
],
injectMissingSuperCalls: (filepath) => [
/^(\s+?)(constructor.*)/gm,
(match, $indent, $constructor) => {
const className = getFileClass(filepath)
const parent = inheritance.getParent(className)
const hasCalledSuper = superConstructorCalls.getParent(className)
if (parent && !hasCalledSuper) {
// missing super() call
if (debugRules.logInjectedSuperCalls) {
logInfo('injecting missing super() call for', className, { match, parent })
}
return `${match}\n${$indent}${indent}super();`
}
return match
}
]
}
// Transpiles current src to ES6
const transpileToEs6 = function (content, filepath, grunt) {
logHeader(colors.YELLOW)('\nv -- Processing', filepath, '-- v')
let transpiled = content
// transpile content from current format to ES6
if (filepath === 'src/Ros3D.js') {
transpiled = transpiled
.replace(...transpile.initialROS3DAssignment)
}
// give replace function access to filepath
const transpileInternalDependencies = transpile.internalDependencies(filepath)
const transpileExternalDependencies = transpile.externalDependencies(filepath)
const transpileConstructors = transpile.constructors(filepath)
const transpileSuperCalls = transpile.superCalls(filepath)
const transpileClasses = transpile.classes(filepath)
const transpileExportedProperties= transpile.exportedProperties(filepath)
return transpiled
.replace(...transpileInternalDependencies)
.replace(...transpileExternalDependencies)
.replace(...transpile.buildInheritanceIndexViaProto)
.replace(...transpile.buildInheritanceIndexViaObjectAssign)
.replace(...transpile.methods)
.replace(...transpileConstructors)
.replace(...transpileSuperCalls)
.replace(...transpileClasses)
.replace(...transpileExportedProperties)
}
// Injects es6 imports based on dependency and export
// metadata gathered from transpilation
const injectImports = function (content, filepath, grunt) {
logHeader(colors.CYAN)('\nv -- Finalizing', filepath, '-- v')
const pathParts = filepath.split(path.sep)
const sourceFilePath = ['src', ...pathParts.slice(1)].join(path.sep)
let transpiled = content
// give replace function access to sourceFilePath
const transpileImports = transpile.imports(sourceFilePath)
const transpileInjectMissingSuperCalls = transpile.injectMissingSuperCalls(sourceFilePath)
return transpiled
.replace(...transpileImports)
.replace(...transpileInjectMissingSuperCalls)
}
module.exports = {
debugRules,
dependencies,
inheritance,
transpileToEs6,
injectImports,
}