Repository navigation
Expand file tree
/
Copy pathparser.go
More file actions
1006 lines (855 loc) · 22.7 KB
/
Copy pathparser.go
File metadata and controls
1006 lines (855 loc) · 22.7 KB
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Package jsimports extracts module imports from JavaScript/TypeScript/Flow
// source without building an AST. The scanner makes a single forward pass
// over the source bytes.
//
// Its primary job is context tracking. At every position, it knows whether
// it's in code, a string, a template literal, a comment, or a regex. Only
// in code context does it look for the `import`/`export`/`require` keywords,
// which makes it immune to import-like text inside strings and comments.
package jsimports
import (
"bytes"
)
// Kind describes the syntax through which a module was imported.
type Kind uint8
const (
// KindStatic is `import ... from "x"` or `import "x"`.
KindStatic Kind = iota
// KindDynamic is `import("x")`.
KindDynamic
// KindRequire is `require("x")`.
KindRequire
// KindRequireResolve is `require.resolve("x")`.
KindRequireResolve
// KindReExport is `export ... from "x"`.
KindReExport
// KindTypeStatic is a TS/Flow type-only import: (e.g. `import type ... from "x"`,
// `import typeof ... from "x"`, `import { type Foo } from "x"`). A mixed
// import like `import { type Foo, bar } from "x"` is recorded twice: once
// as KindStatic and once as KindTypeStatic.
KindTypeStatic
// KindTypeReExport is a TS type-only re-export: `export type { T } from "x"`
// or `export { type T } from "x"`. Mixed re-exports are recorded as both
// KindReExport and KindTypeReExport.
KindTypeReExport
)
func (k Kind) String() string {
switch k {
case KindStatic:
return "static"
case KindDynamic:
return "dynamic"
case KindRequire:
return "require"
case KindRequireResolve:
return "require.resolve"
case KindReExport:
return "re-export"
case KindTypeStatic:
return "type-static"
case KindTypeReExport:
return "type-re-export"
}
return "unknown"
}
// Import is a single module reference found in the source.
type Import struct {
// Specifier is the raw module path string (e.g. "./foo" or "lodash").
Specifier string
Kind Kind
// Line is the 1-based line of the import/export/require keyword.
Line int
}
// Diagnostic reports source the parser saw but could not statically analyze,
// such as `require(someVariable)` or `import('./' + name)`.
type Diagnostic struct {
Message string
Line int
}
// Result holds everything found in one source file.
type Result struct {
Imports []Import
Diagnostics []Diagnostic
}
// Parse scans src and returns the modules it imports. It never reads outside
// src and performs no I/O; the returned specifiers are copies, so src may be
// reused after Parse returns.
func Parse(src []byte) Result {
s := scanner{src: src, lineNum: 1}
// skip past BOM
if bytes.HasPrefix(src, utf8BOM) {
s.index = len(utf8BOM)
}
// skip past shebang line
if s.index+1 < len(src) && src[s.index] == '#' && src[s.index+1] == '!' {
if k := bytes.IndexByte(src[s.index:], '\n'); k >= 0 {
s.index += k
} else {
s.index = len(src)
}
}
s.scan()
return s.res
}
var (
utf8BOM = []byte{0xef, 0xbb, 0xbf}
starSlash = []byte("*/")
newline = []byte{'\n'}
)
// interesting marks the bytes the code-context loop has to stop at: quotes,
// slash (comment or regex), braces (template substitution tracking), and the
// first letters of import/export/require. Everything else is skipped.
var interesting [256]bool
// identChar marks bytes that can appear in an identifier. All bytes >= 0x80
// are treated as identifier characters, which conservatively prevents keyword
// matches adjacent to multi-byte runes.
var identChar [256]bool
func init() {
for _, char := range []byte{'\'', '"', '`', '/', '{', '}', 'i', 'r', 'e'} {
interesting[char] = true
}
for char := '0'; char <= '9'; char++ {
identChar[char] = true
}
for char := 'a'; char <= 'z'; char++ {
identChar[char] = true
}
for char := 'A'; char <= 'Z'; char++ {
identChar[char] = true
}
identChar['_'] = true
identChar['$'] = true
for char := 0x80; char < 256; char++ {
identChar[char] = true
}
}
type scanner struct {
src []byte
index int
// templates is the substitution stack: one entry per `${` we are inside,
// holding the count of unmatched '{' seen within that substitution so we
// know which '}' closes it.
templates []int
// line numbers are computed lazily, only when something is recorded:
// lineNum is the line at byte offset linePos.
linePos int
lineNum int
res Result
}
func (s *scanner) scan() {
src := s.src
srcLen := len(src)
for s.index < srcLen {
char := src[s.index]
if !interesting[char] {
s.index++
continue
}
switch char {
case '\'', '"':
s.index++
s.skipString(char)
case '`':
s.index++
s.scanTemplate()
case '/':
s.slash()
case '{':
if t := len(s.templates) - 1; t >= 0 {
s.templates[t]++
}
s.index++
case '}':
if t := len(s.templates) - 1; t >= 0 {
if s.templates[t] > 0 {
s.templates[t]--
s.index++
} else {
// end of a `${...}` substitution: back to template text
s.templates = s.templates[:t]
s.index++
s.scanTemplate()
}
} else {
s.index++
}
default: // 'i', 'r', 'e'
s.word()
}
}
}
// word is called at a byte that could start a keyword. It reads the whole
// identifier and dispatches if it is one of the three we care about.
func (s *scanner) word() {
startIdx := s.index
if !s.boundaryBefore(startIdx) {
// middle of a larger identifier, e.g. the 'i' in "main"
s.index++
return
}
switch string(s.readWord()) {
case "import":
if !s.precededByDot(startIdx) {
s.importKeyword(startIdx)
}
case "export":
if !s.precededByDot(startIdx) {
s.exportKeyword(startIdx)
}
case "require":
if !s.precededByDot(startIdx) {
s.requireKeyword(startIdx)
}
}
}
func (s *scanner) importKeyword(kwStartIdx int) {
s.skipWsComments()
if s.index >= len(s.src) {
return
}
switch char := s.src[s.index]; char {
case '(':
s.index++
s.callSpecifier(kwStartIdx, KindDynamic, true)
case '.':
// import.meta
s.index++
case '\'', '"':
// side-effect import: `import "x"`
if spec, ok := s.readString(char); ok {
s.record(spec, KindStatic, kwStartIdx)
} else {
s.diag("unterminated module specifier", kwStartIdx)
}
default:
s.staticImportClause(kwStartIdx)
}
}
// staticImportClause parses the bindings of `import <clause> from "x"`,
// starting just after the `import` keyword. It tracks whether the clause
// carries value bindings, type bindings, or both, and records the specifier
// accordingly (a mixed clause is recorded as both kinds).
func (s *scanner) staticImportClause(kwStartIdx int) {
src := s.src
typePrefix := false // `import type ...` / `import typeof ...`
hasValue := false
hasType := false
first := true
for {
s.skipWsComments()
if s.index >= len(src) {
s.diag("unterminated import statement", kwStartIdx)
return
}
switch char := src[s.index]; {
case char == '{':
s.index++
hv, ht, ok := s.scanBindingBraces()
if !ok {
s.diag("unterminated import statement", kwStartIdx)
return
}
hasValue = hasValue || hv
hasType = hasType || ht
case char == '*':
// namespace import
s.index++
hasValue = true
case char == ',':
s.index++
case identChar[char]:
wStart := s.index
switch string(s.readWord()) {
case "from":
s.skipWsComments()
if s.index < len(src) && (src[s.index] == '\'' || src[s.index] == '"') {
if spec, ok := s.readString(src[s.index]); ok {
s.recordTyped(spec, KindStatic, KindTypeStatic, kwStartIdx,
typePrefix, hasValue, hasType)
} else {
s.diag("unterminated module specifier", wStart)
}
return
}
// `from` used as a binding name; keep scanning
hasValue = true
case "type", "typeof":
// TS/Flow `import type ...` / `import typeof ...`
if first && s.typeOnlyClauseFollows() {
typePrefix = true
} else {
// a binding literally named `type`, e.g. `import type, {x}`
hasValue = true
}
case "as":
// alias keyword, not a binding
default:
// a default-import binding name
hasValue = true
}
default:
// '=' (TS import-equals), ';', or anything else that can't be
// part of an import clause
return
}
first = false
}
}
// recordTyped records a specifier under the value kind, the type kind, or
// both, based on what the clause contained. A `type` prefix makes the whole
// statement type-only regardless of the bindings.
func (s *scanner) recordTyped(spec []byte, valueKind, typeKind Kind, kwStartIdx int, typePrefix, hasValue, hasType bool) {
switch {
case typePrefix || (hasType && !hasValue):
s.record(spec, typeKind, kwStartIdx)
case hasType: // mixed, e.g. `import { type Foo, bar } from "x"`
s.record(spec, valueKind, kwStartIdx)
s.record(spec, typeKind, kwStartIdx)
default:
s.record(spec, valueKind, kwStartIdx)
}
}
// typeOnlyClauseFollows decides, positioned right after `import type`,
// whether this is a type-only import or `type` is being used as a regular
// binding name, as in `import type from 'x'` or `import type, {x}`.
// The position is restored before returning.
func (s *scanner) typeOnlyClauseFollows() bool {
save := s.index
defer func() { s.index = save }()
s.skipWsComments()
if s.index >= len(s.src) {
return false
}
switch char := s.src[s.index]; {
case char == ',':
return false
case char == '{' || char == '*':
return true
case identChar[char]:
if string(s.readWord()) == "from" {
s.skipWsComments()
return !(s.index < len(s.src) && (s.src[s.index] == '\'' || s.src[s.index] == '"'))
}
return true
}
return false
}
func (s *scanner) exportKeyword(kwStartIdx int) {
src := s.src
typePrefix := false // `export type { ... } from` / `export type * from`
hasValue := false
hasType := false
s.skipWsComments()
if s.index >= len(src) {
return
}
if identChar[src[s.index]] {
origIdx := s.index
if matched := s.trySkipWord("type"); !matched {
// export const/function/default/...: nothing to record.
return
}
s.skipWsComments()
if s.index >= len(src) || (src[s.index] != '{' && src[s.index] != '*') {
// TS type alias: `export type Foo = ...`
s.index = origIdx
return
}
typePrefix = true
}
switch src[s.index] {
case '{':
s.index++
hv, ht, ok := s.scanBindingBraces()
if !ok {
return
}
hasValue, hasType = hv, ht
s.skipWsComments()
if matched := s.trySkipWord("from"); !matched {
// plain `export { ... }` with no `from`
return
}
case '*':
s.index++
hasValue = true
s.skipWsComments()
origIdx := s.index
if string(s.readWord()) == "as" {
s.skipWsComments()
if s.index < len(src) && (src[s.index] == '\'' || src[s.index] == '"') {
// `export * as "string name" from "x"`
if _, ok := s.readString(src[s.index]); !ok {
return
}
} else {
s.readWord()
}
} else {
s.index = origIdx
}
s.skipWsComments()
if matched := s.trySkipWord("from"); !matched {
return
}
default:
return
}
s.skipWsComments()
if s.index >= len(src) || (src[s.index] != '\'' && src[s.index] != '"') {
s.diag("expected module specifier after `from`", s.index)
return
}
if spec, ok := s.readString(src[s.index]); ok {
s.recordTyped(spec, KindReExport, KindTypeReExport, kwStartIdx,
typePrefix, hasValue, hasType)
} else {
s.diag("unterminated module specifier", s.index)
}
}
func (s *scanner) requireKeyword(kwStartIdx int) {
src := s.src
kind := KindRequire
s.skipWsComments()
if s.index < len(src) && src[s.index] == '.' {
s.index++
s.skipWsComments()
if string(s.readWord()) != "resolve" {
// require.cache, require.main, ...
return
}
kind = KindRequireResolve
s.skipWsComments()
}
// optional-chained call: require?.("x")
if s.index+1 < len(src) && src[s.index] == '?' && src[s.index+1] == '.' {
s.index += 2
s.skipWsComments()
}
if s.index >= len(src) || src[s.index] != '(' {
return
}
s.index++
s.callSpecifier(kwStartIdx, kind, false)
}
// callSpecifier parses the argument of `import(...)`/`require(...)` starting
// just after the opening paren.
//
// It records an import only when the argument is a single string literal:
// the string must be followed by ')' (or ',' for dynamic import, which accepts
// an options object), which rejects concatenation like require('a' + b).
func (s *scanner) callSpecifier(kwStartIdx int, kind Kind, allowComma bool) {
src := s.src
s.skipWsComments()
if s.index >= len(src) {
return
}
char := src[s.index]
if char != '\'' && char != '"' && char != '`' {
s.diag("non-literal module path", s.index)
return
}
spec, ok := s.readString(char)
if !ok {
if char == '`' {
// template with substitutions; s.index was left at the backtick so
// the main loop rescans it with substitution tracking
s.diag("non-literal module path", s.index)
} else {
s.diag("unterminated module specifier", s.index)
}
return
}
s.skipWsComments()
if s.index < len(src) {
if char := src[s.index]; char == ')' || (allowComma && char == ',') {
s.record(spec, kind, kwStartIdx)
return
}
}
s.diag("non-literal module path", s.index)
}
// readString reads a string literal whose opening quote is at s.index. On
// success it returns the raw contents and leaves s.index after the closing
// quote.
//
// It fails on an unterminated string (s.index is left at the offending
// newline/EOF so the damage of unbalanced quotes is contained to one line)
// and on a template containing substitutions (s.index is left at the opening
// backtick so the main loop can rescan it as a template).
func (s *scanner) readString(quoteChar byte) ([]byte, bool) {
src := s.src
srcLen := len(src)
open := s.index
i := open + 1
for i < srcLen {
switch src[i] {
case '\\':
i += 2
case quoteChar:
s.index = i + 1
return src[open+1 : i], true
case '\n', '\r':
if quoteChar != '`' {
s.index = i
return nil, false
}
i++
case '$':
if quoteChar == '`' && i+1 < srcLen && src[i+1] == '{' {
s.index = open
return nil, false
}
i++
default:
i++
}
}
s.index = srcLen
return nil, false
}
// skipString consumes a string literal, starting just after the opening
// quote. Unterminated ' / " strings stop at the newline so a stray quote
// can't swallow the rest of the file.
func (s *scanner) skipString(quoteChar byte) {
src := s.src
srcLen := len(src)
for s.index < srcLen {
switch src[s.index] {
case '\\':
s.index += 2
case quoteChar:
s.index++
return
case '\n', '\r':
if quoteChar != '`' {
return
}
s.index++
default:
s.index++
}
}
}
// scanTemplate consumes template literal text, starting just after the
// opening backtick (or just after the '}' that closed a substitution).
//
// On `${` it pushes a substitution frame and returns so the main loop
// scans the substitution as code.
func (s *scanner) scanTemplate() {
src := s.src
srcLen := len(src)
for s.index < srcLen {
switch src[s.index] {
case '\\':
s.index += 2
case '`':
s.index++
return
case '$':
if s.index+1 < srcLen && src[s.index+1] == '{' {
s.templates = append(s.templates, 0)
s.index += 2
return
}
s.index++
default:
s.index++
}
}
}
// slash handles '/' in code context: line comment, block comment, regex
// literal, or division.
func (s *scanner) slash() {
src := s.src
srcLen := len(src)
if s.index+1 < srcLen {
switch src[s.index+1] {
case '/':
if k := bytes.IndexByte(src[s.index:], '\n'); k >= 0 {
s.index += k
} else {
s.index = srcLen
}
return
case '*':
if k := bytes.Index(src[s.index+2:], starSlash); k >= 0 {
s.index += 2 + k + 2
} else {
s.index = srcLen
}
return
}
}
if s.regexAllowed() {
s.skipRegex()
} else {
s.index++
}
}
// regexAllowed decides whether the '/' at s.index starts a regex literal or is a
// division operator, based on the last significant byte before it:
// - after a value (identifier, ')', ']') it's division
// - after an operator or a keyword like `return` it's a regex
//
// Misclassification is contained to one line because both skipRegex and skipString
// stop at newlines.
func (s *scanner) regexAllowed() bool {
src := s.src
i := s.index - 1
for i >= 0 && (src[i] == ' ' || src[i] == '\t' || src[i] == '\n' || src[i] == '\r') {
i--
}
if i < 0 {
return true
}
char := src[i]
if char == ')' || char == ']' {
return false
}
if identChar[char] {
j := i
for j >= 0 && identChar[src[j]] {
j--
}
switch string(src[j+1 : i+1]) {
case "return", "typeof", "instanceof", "new", "in", "of", "do",
"case", "else", "void", "delete", "throw", "yield", "await":
return true
}
return false
}
return true
}
// skipRegex consumes a regex literal starting at the '/' at s.index. '/' inside
// a character class is literal. Stops at a newline (regexes can't contain
// raw newlines, so we were wrong about it being a regex).
func (s *scanner) skipRegex() {
src := s.src
srcLen := len(src)
s.index++
inClass := false
for s.index < srcLen {
switch src[s.index] {
case '\\':
s.index += 2
case '[':
inClass = true
s.index++
case ']':
inClass = false
s.index++
case '/':
s.index++
if !inClass {
return
}
case '\n', '\r':
return
default:
s.index++
}
}
}
// scanBindingBraces consumes through the '}' closing an import/export
// clause, starting just after the '{'.
//
// Each binding is classified as a type binding (a leading `type`/`typeof`
// modifier, as in `{ type Foo, bar }`) or a value binding.
//
// A binding literally named `type` (e.g. `{ type }`, `{ type as alias }`,
// `{ type, x }`) is a value binding: `type` only acts as a modifier when
// followed by a binding name. Returns ok=false on EOF.
func (s *scanner) scanBindingBraces() (hasValue, hasType, ok bool) {
src := s.src
srcLen := len(src)
atBindingStart := true
for s.index < srcLen {
switch char := src[s.index]; {
case char == '}':
s.index++
return hasValue, hasType, true
case char == ',':
s.index++
atBindingStart = true
case char == '\'' || char == '"' || char == '`':
// string binding name, e.g. `{ "string name" as alias }`
s.index++
s.skipString(char)
if atBindingStart {
hasValue = true
atBindingStart = false
}
case char == '/':
if s.index+1 < srcLen && (src[s.index+1] == '/' || src[s.index+1] == '*') {
s.skipWsComments()
} else {
s.index++
}
case identChar[char]:
word := string(s.readWord())
if atBindingStart {
if (word == "type" || word == "typeof") && s.bindingNameFollows() {
hasType = true
} else {
hasValue = true
}
atBindingStart = false
}
default:
s.index++
}
}
return hasValue, hasType, false
}
// bindingNameFollows decides whether the `type` word just read inside import/
// export braces is a modifier (followed by a binding name) or itself a
// binding named `type` (followed by ',', '}', or `as`). The position is
// restored before returning.
func (s *scanner) bindingNameFollows() bool {
save := s.index
defer func() { s.index = save }()
s.skipWsComments()
if s.index >= len(s.src) {
return false
}
switch char := s.src[s.index]; {
case char == '\'' || char == '"':
return true
case identChar[char]:
return string(s.readWord()) != "as"
}
return false
}
// skipWsComments advances past whitespace (including the common multi-byte
// JS whitespace runes) and comments.
func (s *scanner) skipWsComments() {
src := s.src
srcLen := len(src)
for s.index < srcLen {
switch src[s.index] {
case ' ', '\t', '\n', '\r', '\v', '\f':
s.index++
case '/':
if s.index+1 < srcLen && src[s.index+1] == '/' {
if k := bytes.IndexByte(src[s.index:], '\n'); k >= 0 {
s.index += k
} else {
s.index = srcLen
}
} else if s.index+1 < srcLen && src[s.index+1] == '*' {
if k := bytes.Index(src[s.index+2:], starSlash); k >= 0 {
s.index += 2 + k + 2
} else {
s.index = srcLen
}
} else {
return
}
case 0xc2: // U+00A0 no-break space
if s.index+1 < srcLen && src[s.index+1] == 0xa0 {
s.index += 2
} else {
return
}
case 0xe2: // U+2028 / U+2029 line separators
if s.index+2 < srcLen && src[s.index+1] == 0x80 && (src[s.index+2] == 0xa8 || src[s.index+2] == 0xa9) {
s.index += 3
} else {
return
}
case 0xef: // U+FEFF zero-width no-break space
if s.index+2 < srcLen && src[s.index+1] == 0xbb && src[s.index+2] == 0xbf {
s.index += 3
} else {
return
}
default:
return
}
}
}
// readWord consumes and returns the identifier at s.index (empty if s.index isn't at
// an identifier character).
func (s *scanner) readWord() []byte {
src := s.src
srcLen := len(src)
startIdx := s.index
for s.index < srcLen && identChar[src[s.index]] {
s.index++
}
return src[startIdx:s.index]
}
// trySkipWord skips past the word at the current index and reports whether
// it matched the provided word. On a mismatch it rewinds the index back to
// where it was.
func (s *scanner) trySkipWord(word string) bool {
origIdx := s.index
matchedWord := string(s.readWord()) == word
if !matchedWord {
s.index = origIdx
}
return matchedWord
}
// boundaryBefore reports whether pos can start a keyword (i.e. the previous
// byte is not part of an identifier). Known multi-byte whitespace runes are
// accepted as boundaries even though their bytes look like identifier bytes.
func (s *scanner) boundaryBefore(pos int) bool {
if pos == 0 {
return true
}
src := s.src
prevChar := src[pos-1]
if !identChar[prevChar] {
return true
}
if prevChar < 0x80 {
return false
}
if prevChar == 0xa0 && pos >= 2 && src[pos-2] == 0xc2 {
return true // U+00A0
}
if (prevChar == 0xa8 || prevChar == 0xa9) && pos >= 3 && src[pos-3] == 0xe2 && src[pos-2] == 0x80 {
return true // U+2028 / U+2029
}
if prevChar == 0xbf && pos >= 3 && src[pos-3] == 0xef && src[pos-2] == 0xbb {
return true // U+FEFF
}
return false
}
// precededByDot reports whether the keyword at pos is a property access like
// `obj.require(...)`. A `...` spread does not count.
func (s *scanner) precededByDot(pos int) bool {
src := s.src
i := pos - 1
for i >= 0 && (src[i] == ' ' || src[i] == '\t' || src[i] == '\n' || src[i] == '\r') {
i--
}
return i >= 0 && src[i] == '.' && !(i > 0 && src[i-1] == '.')
}
func (s *scanner) record(spec []byte, kind Kind, pos int) {
s.res.Imports = append(s.res.Imports, Import{
Specifier: string(spec),
Kind: kind,
Line: s.lineAt(pos),
})
}
func (s *scanner) diag(msg string, pos int) {
s.res.Diagnostics = append(s.res.Diagnostics, Diagnostic{
Message: msg,
Line: s.lineAt(pos),
})
}
// lineAt returns the 1-based line at byte offset pos. Records happen in
// source order, so it counts newlines incrementally from the previous call.
func (s *scanner) lineAt(pos int) int {
if pos > len(s.src) {
pos = len(s.src)
}
if pos < s.linePos {
return 1 + bytes.Count(s.src[:pos], newline)