forked from esnext/es6-module-transpiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
76 lines (64 loc) · 1.81 KB
/
utils.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
/* jshint node:true, undef:true, unused:true */
var recast = require('recast');
var esprima = require('esprima');
var proto = '__proto__';
function memo(object, property, getter) {
Object.defineProperty(object, property, {
get: function() {
this[property] = getter.call(this);
return this[property];
},
set: function(value) {
Object.defineProperty(this, property, {
value: value,
configurable: true,
writable: true
});
}
});
}
exports.memo = memo;
function startsWith(string, substring) {
return string.lastIndexOf(substring, 0) === 0;
}
exports.startsWith = startsWith;
function endsWith(string, substring) {
var expected = string.length - substring.length;
return string.indexOf(substring, expected) === expected;
}
exports.endsWith = endsWith;
function extend(subclass, superclass) {
subclass[proto] = superclass;
subclass.prototype = Object.create(superclass.prototype);
subclass.prototype.constructor = subclass;
}
exports.extend = extend;
function sourcePosition(mod, node) {
var loc = node && node.loc;
if (loc) {
return mod.relativePath + ':' + loc.start.line + ':' + (loc.start.column + 1);
} else {
return mod.relativePath;
}
}
exports.sourcePosition = sourcePosition;
function IFFE() {
if (!IFFE.AST) {
IFFE.AST = JSON.stringify(
recast.parse('(function(){}).call(this)', { esprima: esprima })
);
}
var result = JSON.parse(IFFE.AST);
var expression = result.program.body[0].expression;
var body = expression.callee.object.body.body;
var args = Array.prototype.slice.call(arguments);
args.forEach(function(arg) {
if (Object.prototype.toString.call(arg) === '[object Array]') {
body.push.apply(body, arg);
} else {
body.push(arg);
}
});
return expression;
}
exports.IFFE = IFFE;