-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsImportParser.go
486 lines (411 loc) · 10.4 KB
/
jsImportParser.go
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
package jsImportParser
import (
"log"
"os"
"unicode/utf8"
)
var warnLog = log.New(os.Stderr, "[warning] ", 0)
var chars_as = []rune{'a', 's'}
var chars_export = []rune{'e', 'x', 'p', 'o', 'r', 't'}
var chars_from = []rune{'f', 'r', 'o', 'm'}
var chars_import = []rune{'i', 'm', 'p', 'o', 'r', 't'}
var chars_of = []rune{'o', 'f'}
var chars_require = []rune{'r', 'e', 'q', 'u', 'i', 'r', 'e'}
var chars_requireresolve = []rune{'r', 'e', 'q', 'u', 'i', 'r', 'e', '.', 'r', 'e', 's', 'o', 'l', 'v', 'e'}
var chars_type = []rune{'t', 'y', 'p', 'e'}
type stateStackItem struct {
currentChar rune
currentCharIsEscaped bool
currentLine int
isInsideString bool
pointer int
prevChar rune
prevCharIsEscaped bool
}
type lexer struct {
fileName string
source string
currentChar rune
currentCharIsEscaped bool
currentLine int
isInsideString bool
pointer int
prevChar rune
prevCharIsEscaped bool
stateStack []stateStackItem
}
// GetImportsFromFile takes the string contents of a file and the filename (for logging purposes)
// and returns a slice of strings containing the modules imported by the file
func GetImportsFromFile(contents string, fileName string) []string {
result := []string{}
l := lexer{
currentChar: -1,
currentCharIsEscaped: false,
currentLine: 1,
fileName: fileName,
isInsideString: false,
prevCharIsEscaped: false,
source: contents,
stateStack: []stateStackItem{},
}
l.step()
parseLoop:
for {
switch l.currentChar {
// EOF
case -1:
break parseLoop
case 'r':
var requireSrc string
var ok bool
l.pushState()
requireSrc, ok = l.processDynamicImportOrRequire(chars_requireresolve)
if !ok {
l.popState()
requireSrc, ok = l.processDynamicImportOrRequire(chars_require)
}
if ok {
result = append(result, requireSrc)
}
case 'i':
var importSrc string
var ok bool
l.pushState()
importSrc, ok = l.processDynamicImportOrRequire(chars_import)
if !ok {
l.popState()
importSrc, ok = l.processStaticImportStatement()
}
if ok {
result = append(result, importSrc)
}
case 'e':
if exportSrc, ok := l.processExportStatement(); ok {
// for simplicity, we consider `export ... from 'foo'`
// to just be an import of `foo`
result = append(result, exportSrc)
}
default:
l.step()
}
}
return result
}
func (l *lexer) processDynamicImportOrRequire(fnChars []rune) (string, bool) {
if l.prevChar != -1 && !isWhitespace(l.prevChar) && l.prevChar != ';' && l.prevChar != '=' && l.prevChar != '(' {
l.step()
return "", false
}
if ok := l.tryStepPastChars(fnChars); !ok {
return "", false
}
if l.currentChar != '(' {
return "", false
}
l.step()
l.stepPastWhitespace()
if !isQuote(l.currentChar) {
return "", false
}
if result, ok := l.getImportSrcString(); ok {
return result, true
}
return "", false
}
func (l *lexer) processStaticImportStatement() (string, bool) {
if l.prevChar != -1 && !isNewline(l.prevChar) && l.prevChar != ';' {
l.step()
return "", false
}
if ok := l.tryStepPastChars(chars_import); !ok {
return "", false
}
l.stepPastWhitespace()
if !isQuote(l.currentChar) {
// `import type ...`
if ok := l.tryStepPastChars(chars_type); ok {
// `import typeof ...`
l.tryStepPastChars(chars_of)
if isWhitespace(l.currentChar) {
// just ignore if the entire import is types
return "", false
}
}
l.pushState()
for {
if l.currentChar == ',' {
l.step()
l.stepPastWhitespace()
}
switch l.currentChar {
case -1:
warnLog.Printf("Invalid import in %s:%d", l.fileName, l.currentLine)
l.popState()
l.step()
return "", false
case '{':
if l.prevChar == '$' && !l.prevCharIsEscaped {
// unsupported import inside template string
l.step()
return "", false
} else {
l.stepUntil('}')
l.step()
}
case '*':
l.step()
l.stepPastWhitespace()
if ok := l.tryStepPastChars(chars_as); ok {
l.stepPastWhitespace()
} else {
// you can't do `import *` without ` as <something>`
warnLog.Printf("Invalid import in %s:%d", l.fileName, l.currentLine)
return "", false
}
// continue here so the `default` case will handle it in the next iteration
continue
default:
l.stepUntilWhitespaceOrComma()
}
l.stepPastWhitespace()
if l.currentChar != ',' {
break
}
}
if ok := l.tryStepPastChars(chars_from); !ok {
// import with no `from`
warnLog.Printf("Invalid import in %s:%d", l.fileName, l.currentLine)
return "", false
}
l.stepPastWhitespace()
}
if !isQuote(l.currentChar) {
warnLog.Printf("Invalid import in %s:%d", l.fileName, l.currentLine)
return "", false
}
if result, ok := l.getImportSrcString(); ok {
return result, true
}
return "", false
}
func (l *lexer) processExportStatement() (string, bool) {
if l.prevChar != -1 && !isNewline(l.prevChar) && l.prevChar != ';' {
l.step()
return "", false
}
if ok := l.tryStepPastChars(chars_export); !ok {
return "", false
}
l.stepPastWhitespace()
if ok := l.tryStepPastChars(chars_type); ok {
if isWhitespace(l.currentChar) {
// just ignore if the entire export is types
return "", false
}
}
switch l.currentChar {
case '{':
l.stepUntil('}')
l.step()
l.stepPastWhitespace()
case '*':
l.step()
l.stepPastWhitespace()
if ok := l.tryStepPastChars(chars_as); ok {
l.stepPastWhitespace()
l.stepUntilWhitespace()
l.stepPastWhitespace()
}
default:
// if it doesn't fit any of the above cases, it's not
// valid syntax for an export with `from` in it
return "", false
}
if ok := l.tryStepPastChars(chars_from); !ok {
// we're not interested if there's no `from`
return "", false
}
l.stepPastWhitespace()
if !isQuote(l.currentChar) {
warnLog.Printf("Invalid export in %s:%d", l.fileName, l.currentLine)
return "", false
}
if result, ok := l.getImportSrcString(); ok {
return result, true
}
return "", false
}
func (l *lexer) getImportSrcString() (string, bool) {
quoteChar := l.currentChar
result := ""
l.pushState()
l.isInsideString = true
l.step()
for !(l.currentChar == quoteChar && !l.currentCharIsEscaped) {
switch l.currentChar {
case -1:
l.popState()
warnLog.Printf("Invalid import in %s:%d", l.fileName, l.currentLine)
return "", false
// dynamic import paths aren't supported
case '{', '+':
l.popState()
warnLog.Printf("Unsupported dynamic import path in %s:%d", l.fileName, l.currentLine)
l.isInsideString = false
return "", false
default:
result += string(l.currentChar)
l.step()
}
}
l.step()
l.isInsideString = false
return result, true
}
// returns true only if it sees each of the provided chars along the way
func (l *lexer) tryStepPastChars(chars []rune) bool {
for _, char := range chars {
if l.currentChar != char {
return false
}
l.step()
}
return true
}
func (l *lexer) stepPastWhitespace() {
for isWhitespace(l.currentChar) {
l.step()
}
}
func (l *lexer) stepUntilWhitespace() {
for !isWhitespace(l.currentChar) {
l.step()
}
}
func (l *lexer) stepUntilWhitespaceOrComma() {
for !isWhitespace(l.currentChar) && l.currentChar != ',' {
l.step()
}
}
func (l *lexer) stepUntil(char rune) {
for l.currentChar != char && l.currentChar != -1 {
l.step()
}
}
// similar to unix `pushd`/`popd`
func (l *lexer) pushState() {
l.stateStack = append(l.stateStack, stateStackItem{
l.currentChar,
l.currentCharIsEscaped,
l.currentLine,
l.isInsideString,
l.pointer,
l.prevChar,
l.prevCharIsEscaped,
})
}
// similar to unix `pushd`/`popd`
func (l *lexer) popState() {
if len(l.stateStack) == 0 {
log.Fatal("Unable to 'popState' because the stack is empty")
}
lastStackIndex := len(l.stateStack) - 1
poppedState := l.stateStack[lastStackIndex]
l.stateStack = l.stateStack[:lastStackIndex]
l.currentChar = poppedState.currentChar
l.currentCharIsEscaped = poppedState.currentCharIsEscaped
l.currentLine = poppedState.currentLine
l.isInsideString = poppedState.isInsideString
l.pointer = poppedState.pointer
l.prevChar = poppedState.prevChar
l.prevCharIsEscaped = poppedState.prevCharIsEscaped
}
// step forward to the next char in the source file
//
// the `dryRun` param can be used to see what the next char is
// without actually taking a step
func (l *lexer) _step(dryRun ...bool) rune {
nextChar, width := utf8.DecodeRuneInString(l.source[l.pointer:])
// Use -1 to indicate the end of the file
if width == 0 {
nextChar = -1
}
if len(dryRun) == 0 || dryRun[0] == false {
if (nextChar == '\n' && l.prevChar != '\r') || nextChar == '\r' {
l.currentLine += 1
}
l.prevChar = l.currentChar
l.prevCharIsEscaped = l.currentCharIsEscaped
l.currentCharIsEscaped = nextChar != -1 && l.prevChar == '\\' && !l.currentCharIsEscaped
l.currentChar = nextChar
l.pointer += width
}
return nextChar
}
// Wrapper around the real `_step` fn that can skip past comments it comes
// across. This is needed because it's valid syntax to scatter comments
// among the keywords in an import/export, and we don't want to have to
// worry about running into comments when we're parsing them
func (l *lexer) step() {
l._step()
// auto-skip past comments as long as we're not inside a string
if !l.isInsideString {
commentLoop:
for {
switch l.currentChar {
case -1:
break commentLoop
// line comments
case '/':
if !l.currentCharIsEscaped && l._step(true) == '/' {
for !isNewline(l.currentChar) {
if l.currentChar == -1 {
break commentLoop
}
l._step()
}
l._step()
} else {
break commentLoop
}
// block comments
case '*':
if l.prevChar == '/' {
for !(l.currentChar == '/' && l.prevChar == '*') {
if l.currentChar == -1 {
break commentLoop
}
l._step()
}
l._step()
} else {
break commentLoop
}
default:
break commentLoop
}
}
}
}
func isQuote(input rune) bool {
return input == '\'' || input == '"' || input == '`'
}
func isWhitespace(input rune) bool {
return isNewline(input) || isSpaceOrTab(input)
}
func isNewline(input rune) bool {
switch input {
case '\r', '\n', '\u2028', '\u2029':
return true
default:
return false
}
}
func isSpaceOrTab(input rune) bool {
switch input {
case '\t', ' ':
return true
default:
return false
}
}