-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrapper.js
163 lines (139 loc) · 4.69 KB
/
wrapper.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
"use strict";
const {existsSync} = require("fs");
const {dirname, resolve} = require("path");
/**
* TypeScript enums
*/
const JavaScriptKind = 1;
const ModuleExports = 2;
const ThisKeyword = 99;
const lazyDefinitions = ["defineLazyGetter", "defineLazyServiceGetter", "defineLazyPreferenceGetter"];
/**
* Patch the typescript namespace object with custom methods to handle JSMs.
*/
function patch(ts) {
if (ts._patched) {
return;
}
ts._patched = true;
// this.EXPORTED_SYMBOLS = ["Foo", "Bar"]
function isEXPORTED_SYMBOLS(expr) {
return ts.isBinaryExpression(expr) &&
ts.isPropertyAccessExpression(expr.left) &&
ts.idText(expr.left.name) === "EXPORTED_SYMBOLS";
}
// ["Foo", "Bar"] -> {Foo, Bar}
function transpileExports(expr) {
return ts.createObjectLiteral(expr.right.elements.map(e => {
let p = ts.createShorthandPropertyAssignment(e.text);
p.pos = e.pos;
p.end = e.end;
return p;
}));
}
// XPCOMUtils.defineLazyGetter(this, "foo", ...)
function isLazyDefinition(expr) {
return ts.isCallExpression(expr) &&
ts.isPropertyAccessExpression(expr.expression) &&
expr.expression.expression.escapedText === "XPCOMUtils" &&
lazyDefinitions.includes(expr.expression.name.escapedText) &&
expr.arguments && expr.arguments.length > 2 &&
expr.arguments[0].kind === ThisKeyword &&
ts.isStringLiteral(expr.arguments[1]);
}
// defineLazyGetter(this, "foo") -> var foo;
function transpileLazyDefinition(expr) {
let decl = ts.createVariableDeclaration(expr.arguments[1].text);
decl.name.pos = expr.arguments[1].pos + 2;
decl.name.end = expr.arguments[1].end - 1;
return decl;
}
let {getSpecialPropertyAssignmentKind} = ts;
ts.getSpecialPropertyAssignmentKind = function(expr) {
if (isEXPORTED_SYMBOLS(expr)) {
return ModuleExports;
}
return getSpecialPropertyAssignmentKind(expr);
}
let {bindSourceFile} = ts;
ts.bindSourceFile = function(file, ...args) {
let libDOM = file.fileName.endsWith("lib.dom.d.ts");
if (!file._patched && (libDOM || file.fileName.endsWith(".jsm"))) {
file._patched = true;
let varStatement;
let lazyVars = [];
for (let node of file.statements) {
if (ts.isExpressionStatement(node) && isEXPORTED_SYMBOLS(node.expression)) {
node.expression.right = transpileExports(node.expression);
}
if (ts.isVariableStatement(node)) {
varStatement = node;
}
if (ts.isExpressionStatement(node) && isLazyDefinition(node.expression)) {
lazyVars.push(transpileLazyDefinition(node.expression));
}
// Something something issue#.
if (libDOM && ts.isVariableStatement(node) &&
ts.idText(node.declarationList.declarations[0].name) === "MessageChannel") {
node.declarationList.declarations[0].name.escapedText += "Dummy";
}
}
// Add fake lazyVars to the var statement.
if (varStatement && lazyVars.length) {
let {declarations, _count = declarations.length} = varStatement.declarationList;
declarations.splice(_count, declarations.length, ...lazyVars);
varStatement.declarationList._count = _count;
}
}
return bindSourceFile(file, ...args);
}
let {ensureScriptKind} = ts;
ts.ensureScriptKind = (fileName, scriptKind) => {
if (fileName.endsWith(".jsm")) {
return JavaScriptKind;
}
return ensureScriptKind(fileName, scriptKind);
}
let {extensionFromPath} = ts;
ts.extensionFromPath = function(path) {
if (path.endsWith(".jsm")) {
return ".js";
}
return extensionFromPath(path);
}
let {createProgram} = ts;
ts.createProgram = function(...args) {
let host = args[0].host || args[2];
host.resolveModuleNames = function(moduleNames, containingFile) {
return moduleNames.map(name => {
let resolvedFileName = resolve(dirname(containingFile), name);
return existsSync(resolvedFileName) && {resolvedFileName};
});
}
return createProgram(...args);
};
};
/**
* @param {string} module
* Path to original Typescript module to load and patch.
* @param {string} [mainMethod]
* tsc.js runs the main method `executeCommandLine` before we have
* the chance to patch the `ts` object, so we need to intercept it.
*/
function load(module, mainMethod) {
if (!mainMethod) {
let ts = require(module);
patch(ts);
return ts;
}
Object.defineProperty(Object.prototype, mainMethod, {
set(value) {
patch(this);
delete Object.prototype[mainMethod];
this[mainMethod] = value;
},
configurable: true,
});
require(module);
}
module.exports = {load};