-
Notifications
You must be signed in to change notification settings - Fork 12.8k
/
Copy pathpreProcess.ts
452 lines (419 loc) · 17.3 KB
/
preProcess.ts
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
import {
FileReference,
isKeyword,
lastOrUndefined,
length,
noop,
PragmaContext,
PreProcessedFileInfo,
processCommentPragmas,
processPragmasIntoFields,
scanner,
ScriptTarget,
SyntaxKind,
} from "./_namespaces/ts.js";
export function preProcessFile(sourceText: string, readImportFiles = true, detectJavaScriptImports = false): PreProcessedFileInfo {
const pragmaContext: PragmaContext = {
languageVersion: ScriptTarget.ES5, // controls whether the token scanner considers unicode identifiers or not - shouldn't matter, since we're only using it for trivia
pragmas: undefined,
checkJsDirective: undefined,
referencedFiles: [],
typeReferenceDirectives: [],
libReferenceDirectives: [],
amdDependencies: [],
hasNoDefaultLib: undefined,
moduleName: undefined,
};
const importedFiles: FileReference[] = [];
let ambientExternalModules: { ref: FileReference; depth: number; }[] | undefined;
let lastToken: SyntaxKind;
let currentToken: SyntaxKind;
let braceNesting = 0;
// assume that text represent an external module if it contains at least one top level import/export
// ambient modules that are found inside external modules are interpreted as module augmentations
let externalModule = false;
function nextToken() {
lastToken = currentToken;
currentToken = scanner.scan();
if (currentToken === SyntaxKind.OpenBraceToken) {
braceNesting++;
}
else if (currentToken === SyntaxKind.CloseBraceToken) {
braceNesting--;
}
return currentToken;
}
function getFileReference() {
const fileName = scanner.getTokenValue();
const pos = scanner.getTokenStart();
return { fileName, pos, end: pos + fileName.length };
}
function recordAmbientExternalModule(): void {
if (!ambientExternalModules) {
ambientExternalModules = [];
}
ambientExternalModules.push({ ref: getFileReference(), depth: braceNesting });
}
function recordModuleName() {
importedFiles.push(getFileReference());
markAsExternalModuleIfTopLevel();
}
function markAsExternalModuleIfTopLevel() {
if (braceNesting === 0) {
externalModule = true;
}
}
/**
* Returns true if at least one token was consumed from the stream
*/
function tryConsumeDeclare(): boolean {
let token = scanner.getToken();
if (token === SyntaxKind.DeclareKeyword) {
// declare module "mod"
token = nextToken();
if (token === SyntaxKind.ModuleKeyword) {
token = nextToken();
if (token === SyntaxKind.StringLiteral) {
recordAmbientExternalModule();
}
}
return true;
}
return false;
}
/**
* Returns true if at least one token was consumed from the stream
*/
function tryConsumeImport(): boolean {
if (lastToken === SyntaxKind.DotToken) {
return false;
}
let token = scanner.getToken();
if (token === SyntaxKind.ImportKeyword) {
token = nextToken();
if (token === SyntaxKind.OpenParenToken) {
token = nextToken();
if (token === SyntaxKind.StringLiteral || token === SyntaxKind.NoSubstitutionTemplateLiteral) {
// import("mod");
recordModuleName();
return true;
}
}
else if (token === SyntaxKind.StringLiteral) {
// import "mod";
recordModuleName();
return true;
}
else {
if (token === SyntaxKind.TypeKeyword) {
const skipTypeKeyword = scanner.lookAhead(() => {
const token = scanner.scan();
return token !== SyntaxKind.FromKeyword && (
token === SyntaxKind.AsteriskToken ||
token === SyntaxKind.OpenBraceToken ||
token === SyntaxKind.Identifier ||
isKeyword(token)
);
});
if (skipTypeKeyword) {
token = nextToken();
}
}
if (token === SyntaxKind.Identifier || isKeyword(token)) {
token = nextToken();
if (token === SyntaxKind.FromKeyword) {
token = nextToken();
if (token === SyntaxKind.StringLiteral) {
// import d from "mod";
recordModuleName();
return true;
}
}
else if (token === SyntaxKind.EqualsToken) {
if (tryConsumeRequireCall(/*skipCurrentToken*/ true)) {
return true;
}
}
else if (token === SyntaxKind.CommaToken) {
// consume comma and keep going
token = nextToken();
}
else {
// unknown syntax
return true;
}
}
if (token === SyntaxKind.OpenBraceToken) {
token = nextToken();
// consume "{ a as B, c, d as D}" clauses
// make sure that it stops on EOF
while (token !== SyntaxKind.CloseBraceToken && token !== SyntaxKind.EndOfFileToken) {
token = nextToken();
}
if (token === SyntaxKind.CloseBraceToken) {
token = nextToken();
if (token === SyntaxKind.FromKeyword) {
token = nextToken();
if (token === SyntaxKind.StringLiteral) {
// import {a as A} from "mod";
// import d, {a, b as B} from "mod"
recordModuleName();
}
}
}
}
else if (token === SyntaxKind.AsteriskToken) {
token = nextToken();
if (token === SyntaxKind.AsKeyword) {
token = nextToken();
if (token === SyntaxKind.Identifier || isKeyword(token)) {
token = nextToken();
if (token === SyntaxKind.FromKeyword) {
token = nextToken();
if (token === SyntaxKind.StringLiteral) {
// import * as NS from "mod"
// import d, * as NS from "mod"
recordModuleName();
}
}
}
}
}
}
return true;
}
return false;
}
function tryConsumeExport(): boolean {
let token = scanner.getToken();
if (token === SyntaxKind.ExportKeyword) {
markAsExternalModuleIfTopLevel();
token = nextToken();
if (token === SyntaxKind.TypeKeyword) {
const skipTypeKeyword = scanner.lookAhead(() => {
const token = scanner.scan();
return token === SyntaxKind.AsteriskToken ||
token === SyntaxKind.OpenBraceToken;
});
if (skipTypeKeyword) {
token = nextToken();
}
}
if (token === SyntaxKind.OpenBraceToken) {
token = nextToken();
// consume "{ a as B, c, d as D}" clauses
// make sure it stops on EOF
while (token !== SyntaxKind.CloseBraceToken && token !== SyntaxKind.EndOfFileToken) {
token = nextToken();
}
if (token === SyntaxKind.CloseBraceToken) {
token = nextToken();
if (token === SyntaxKind.FromKeyword) {
token = nextToken();
if (token === SyntaxKind.StringLiteral) {
// export {a as A} from "mod";
// export {a, b as B} from "mod"
recordModuleName();
}
}
}
}
else if (token === SyntaxKind.AsteriskToken) {
token = nextToken();
if (token === SyntaxKind.FromKeyword) {
token = nextToken();
if (token === SyntaxKind.StringLiteral) {
// export * from "mod"
recordModuleName();
}
}
}
else if (token === SyntaxKind.ImportKeyword) {
token = nextToken();
if (token === SyntaxKind.TypeKeyword) {
const skipTypeKeyword = scanner.lookAhead(() => {
const token = scanner.scan();
return token === SyntaxKind.Identifier ||
isKeyword(token);
});
if (skipTypeKeyword) {
token = nextToken();
}
}
if (token === SyntaxKind.Identifier || isKeyword(token)) {
token = nextToken();
if (token === SyntaxKind.EqualsToken) {
if (tryConsumeRequireCall(/*skipCurrentToken*/ true)) {
return true;
}
}
}
}
return true;
}
return false;
}
function tryConsumeRequireCall(skipCurrentToken: boolean, allowTemplateLiterals = false): boolean {
let token = skipCurrentToken ? nextToken() : scanner.getToken();
if (token === SyntaxKind.RequireKeyword) {
token = nextToken();
if (token === SyntaxKind.OpenParenToken) {
token = nextToken();
if (
token === SyntaxKind.StringLiteral ||
allowTemplateLiterals && token === SyntaxKind.NoSubstitutionTemplateLiteral
) {
// require("mod");
recordModuleName();
}
}
return true;
}
return false;
}
function tryConsumeDefine(): boolean {
let token = scanner.getToken();
if (token === SyntaxKind.Identifier && scanner.getTokenValue() === "define") {
token = nextToken();
if (token !== SyntaxKind.OpenParenToken) {
return true;
}
token = nextToken();
if (token === SyntaxKind.StringLiteral || token === SyntaxKind.NoSubstitutionTemplateLiteral) {
// looks like define ("modname", ... - skip string literal and comma
token = nextToken();
if (token === SyntaxKind.CommaToken) {
token = nextToken();
}
else {
// unexpected token
return true;
}
}
// should be start of dependency list
if (token !== SyntaxKind.OpenBracketToken) {
return true;
}
// skip open bracket
token = nextToken();
// scan until ']' or EOF
while (token !== SyntaxKind.CloseBracketToken && token !== SyntaxKind.EndOfFileToken) {
// record string literals as module names
if (token === SyntaxKind.StringLiteral || token === SyntaxKind.NoSubstitutionTemplateLiteral) {
recordModuleName();
}
token = nextToken();
}
return true;
}
return false;
}
function processImports(): void {
scanner.setText(sourceText);
nextToken();
// Look for:
// import "mod";
// import d from "mod"
// import {a as A } from "mod";
// import * as NS from "mod"
// import d, {a, b as B} from "mod"
// import i = require("mod");
// import("mod");
// export * from "mod"
// export {a as b} from "mod"
// export import i = require("mod")
// (for JavaScript files) require("mod")
// Do not look for:
// AnySymbol.import("mod")
// AnySymbol.nested.import("mod")
while (true) {
if (scanner.getToken() === SyntaxKind.EndOfFileToken) {
break;
}
if (scanner.getToken() === SyntaxKind.TemplateHead) {
const stack = [scanner.getToken()];
loop:
while (length(stack)) {
const token = scanner.scan();
switch (token) {
case SyntaxKind.EndOfFileToken:
break loop;
case SyntaxKind.ImportKeyword:
tryConsumeImport();
break;
case SyntaxKind.TemplateHead:
stack.push(token);
break;
case SyntaxKind.OpenBraceToken:
if (length(stack)) {
stack.push(token);
}
break;
case SyntaxKind.CloseBraceToken:
if (length(stack)) {
if (lastOrUndefined(stack) === SyntaxKind.TemplateHead) {
if (scanner.reScanTemplateToken(/*isTaggedTemplate*/ false) === SyntaxKind.TemplateTail) {
stack.pop();
}
}
else {
stack.pop();
}
}
break;
}
}
nextToken();
}
// check if at least one of alternative have moved scanner forward
if (
tryConsumeDeclare() ||
tryConsumeImport() ||
tryConsumeExport() ||
(detectJavaScriptImports && (
tryConsumeRequireCall(/*skipCurrentToken*/ false, /*allowTemplateLiterals*/ true) ||
tryConsumeDefine()
))
) {
continue;
}
else {
nextToken();
}
}
scanner.setText(undefined);
}
if (readImportFiles) {
processImports();
}
processCommentPragmas(pragmaContext, sourceText);
processPragmasIntoFields(pragmaContext, noop);
if (externalModule) {
// for external modules module all nested ambient modules are augmentations
if (ambientExternalModules) {
// move all detected ambient modules to imported files since they need to be resolved
for (const decl of ambientExternalModules) {
importedFiles.push(decl.ref);
}
}
return { referencedFiles: pragmaContext.referencedFiles, typeReferenceDirectives: pragmaContext.typeReferenceDirectives, libReferenceDirectives: pragmaContext.libReferenceDirectives, importedFiles, isLibFile: !!pragmaContext.hasNoDefaultLib, ambientExternalModules: undefined };
}
else {
// for global scripts ambient modules still can have augmentations - look for ambient modules with depth > 0
let ambientModuleNames: string[] | undefined;
if (ambientExternalModules) {
for (const decl of ambientExternalModules) {
if (decl.depth === 0) {
if (!ambientModuleNames) {
ambientModuleNames = [];
}
ambientModuleNames.push(decl.ref.fileName);
}
else {
importedFiles.push(decl.ref);
}
}
}
return { referencedFiles: pragmaContext.referencedFiles, typeReferenceDirectives: pragmaContext.typeReferenceDirectives, libReferenceDirectives: pragmaContext.libReferenceDirectives, importedFiles, isLibFile: !!pragmaContext.hasNoDefaultLib, ambientExternalModules: ambientModuleNames };
}
}