forked from ternjs/tern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
requirejs.js
265 lines (237 loc) · 8.32 KB
/
requirejs.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
(function(mod) {
if (typeof exports == "object" && typeof module == "object") // CommonJS
return mod(require("../lib/infer"), require("../lib/tern"));
if (typeof define == "function" && define.amd) // AMD
return define(["../lib/infer", "../lib/tern"], mod);
mod(tern, tern);
})(function(infer, tern) {
"use strict";
function flattenPath(path) {
if (!/(^|\/)(\.\/|[^\/]+\/\.\.\/)/.test(path)) return path;
var parts = path.split("/");
for (var i = 0; i < parts.length; ++i) {
if (parts[i] == "." || !parts[i]) parts.splice(i--, 1);
else if (i && parts[i] == "..") parts.splice(i-- - 1, 2);
}
return parts.join("/");
}
function resolveName(name, data) {
var excl = name.indexOf("!");
if (excl > -1) name = name.slice(0, excl);
var opts = data.options;
var hasExt = /\.js$/.test(name);
if (hasExt || /^(?:\w+:|\/)/.test(name))
return name + (hasExt ? "" : ".js");
var base = opts.baseURL || "";
if (base && base.charAt(base.length - 1) != "/") base += "/";
if (opts.paths) {
var known = opts.paths[name];
if (known) return flattenPath(base + known + ".js");
var dir = name.match(/^([^\/]+)(\/.*)$/);
if (dir) {
var known = opts.paths[dir[1]];
if (known) return flattenPath(base + known + dir[2] + ".js");
}
}
return flattenPath(base + name + ".js");
}
function getRequire(data) {
if (!data.require) {
data.require = new infer.Fn("require", infer.ANull, [infer.cx().str], ["module"], new infer.AVal);
data.require.computeRet = function(_self, _args, argNodes) {
if (argNodes.length && argNodes[0].type == "Literal" && typeof argNodes[0].value == "string")
return getInterface(argNodes[0].value, data);
return infer.ANull;
};
}
return data.require;
}
function getInterface(name, data) {
if (name == "require") return getRequire(data);
if (name == "module") return infer.cx().definitions.requirejs.module;
if (data.options.override && Object.prototype.hasOwnProperty.call(data.options.override, name)) {
var over = data.options.override[name];
if (typeof over == "string" && over.charAt(0) == "=") return infer.def.parsePath(over.slice(1));
if (typeof over == "object") {
var known = getKnownModule(name, data);
if (known) return known;
var scope = data.interfaces[stripJSExt(name)] = new infer.Obj(null, stripJSExt(name));
infer.def.load(over, scope);
return scope;
}
name = over;
}
if (!/^(https?:|\/)|\.js$/.test(name))
name = resolveName(name, data);
name = flattenPath(name);
var known = getKnownModule(name, data);
if (!known) {
known = getModule(name, data);
data.server.addFile(name, null, data.currentFile);
}
return known;
}
function getKnownModule(name, data) {
return data.interfaces[stripJSExt(name)];
}
function getModule(name, data) {
var known = getKnownModule(name, data);
if (!known) {
known = data.interfaces[stripJSExt(name)] = new infer.AVal;
known.origin = name;
}
return known;
}
var EXPORT_OBJ_WEIGHT = 50;
function stripJSExt(f) {
return f.replace(/\.js$/, '');
}
var path = {
dirname: function(path) {
var lastSep = path.lastIndexOf("/");
return lastSep == -1 ? "" : path.slice(0, lastSep);
},
relative: function(from, to) {
if (to.indexOf(from) == 0) return to.slice(from.length);
else return to;
},
join: function(a, b) {
if (a && b) return a + "/" + b;
else return (a || "") + (b || "");
},
};
infer.registerFunction("requireJS", function(_self, args, argNodes) {
var server = infer.cx().parent, data = server && server._requireJS;
if (!data || !args.length) return infer.ANull;
var name = data.currentFile;
var out = getModule(name, data);
var deps = [], fn;
if (argNodes && args.length > 1) {
var node = argNodes[args.length == 2 ? 0 : 1];
var base = path.relative(server.options.projectDir, path.dirname(node.sourceFile.name));
if (node.type == "Literal" && typeof node.value == "string") {
deps.push(getInterface(path.join(base, node.value), data));
} else if (node.type == "ArrayExpression") for (var i = 0; i < node.elements.length; ++i) {
var elt = node.elements[i];
if (elt.type == "Literal" && typeof elt.value == "string") {
if (elt.value == "exports") {
var exports = new infer.Obj(true);
deps.push(exports);
out.addType(exports, EXPORT_OBJ_WEIGHT);
} else {
deps.push(getInterface(path.join(base, elt.value), data));
}
}
}
} else if (argNodes && args.length == 1 && argNodes[0].type == "FunctionExpression" && argNodes[0].params.length) {
// Simplified CommonJS call
var exports = new infer.Obj(true);
deps.push(getInterface("require", data), exports);
out.addType(exports, EXPORT_OBJ_WEIGHT);
fn = args[0];
}
if (!fn) {
fn = args[Math.min(args.length - 1, 2)];
if (!fn.isEmpty() && !fn.getFunctionType()) fn = null;
}
if (fn) fn.propagate(new infer.IsCallee(infer.ANull, deps, null, out));
else if (args.length) args[0].propagate(out);
return infer.ANull;
});
// Parse simple ObjectExpression AST nodes to their corresponding JavaScript objects.
function parseExprNode(node) {
switch (node.type) {
case "ArrayExpression":
return node.elements.map(parseExprNode);
case "Literal":
return node.value;
case "ObjectExpression":
var obj = {};
node.properties.forEach(function(prop) {
var key = prop.key.name || prop.key.value;
obj[key] = parseExprNode(prop.value);
});
return obj;
}
}
infer.registerFunction("requireJSConfig", function(_self, _args, argNodes) {
var server = infer.cx().parent, data = server && server._requireJS;
if (data && argNodes && argNodes.length && argNodes[0].type == "ObjectExpression") {
var config = parseExprNode(argNodes[0]);
for (var key in config) if (config.hasOwnProperty(key)) {
var value = config[key], exists = data.options[key];
if (!exists) {
data.options[key] = value;
} else if (key == "paths") {
for (var path in value) if (value.hasOwnProperty(path) && !data.options.paths[path])
data.options.paths[path] = value[path];
}
}
}
return infer.ANull;
});
function preCondenseReach(state) {
var interfaces = infer.cx().parent._requireJS.interfaces;
var rjs = state.roots["!requirejs"] = new infer.Obj(null);
for (var name in interfaces) {
var prop = rjs.defProp(name.replace(/\./g, "`"));
interfaces[name].propagate(prop);
prop.origin = interfaces[name].origin;
}
}
function postLoadDef(data) {
var cx = infer.cx(), interfaces = cx.definitions[data["!name"]]["!requirejs"];
var data = cx.parent._requireJS;
if (interfaces) for (var name in interfaces.props) {
interfaces.props[name].propagate(getInterface(name, data));
}
}
tern.registerPlugin("requirejs", function(server, options) {
server._requireJS = {
interfaces: Object.create(null),
options: options || {},
currentFile: null,
server: server
};
server.on("beforeLoad", function(file) {
this._requireJS.currentFile = file.name;
});
server.on("reset", function() {
this._requireJS.interfaces = Object.create(null);
this._requireJS.require = null;
});
return {
defs: defs,
passes: {
preCondenseReach: preCondenseReach,
postLoadDef: postLoadDef
},
};
});
var defs = {
"!name": "requirejs",
"!define": {
module: {
id: "string",
uri: "string",
config: "fn() -> ?",
exports: "?"
}
},
requirejs: {
"!type": "fn(deps: [string], callback: fn(), errback: fn()) -> !custom:requireJS",
onError: "fn(err: +Error)",
load: "fn(context: ?, moduleName: string, url: string)",
config: "fn(config: ?) -> !custom:requireJSConfig",
version: "string",
isBrowser: "bool"
},
require: "requirejs",
define: {
"!type": "fn(deps: [string], callback: fn()) -> !custom:requireJS",
amd: {
jQuery: "bool"
}
}
};
});