@@ -37,8 +37,6 @@ const Import = 0;
37
37
const ExportAssign = 1 ;
38
38
const ExportStar = 2 ;
39
39
40
- const strictReserved = new Set ( [ 'implements' , 'interface' , 'let' , 'package' , 'private' , 'protected' , 'public' , 'static' , 'yield' , 'enum' ] ) ;
41
-
42
40
function parseCJS ( source , name = '@' ) {
43
41
resetState ( ) ;
44
42
try {
@@ -49,14 +47,39 @@ function parseCJS (source, name = '@') {
49
47
e . loc = pos ;
50
48
throw e ;
51
49
}
52
- const result = { exports : [ ..._exports ] . filter ( expt => ! unsafeGetters . has ( expt ) ) , reexports : [ ...reexports ] } ;
50
+ const result = { exports : [ ..._exports ] . filter ( expt => expt !== undefined && ! unsafeGetters . has ( expt ) ) , reexports : [ ...reexports ] . filter ( reexpt => reexpt !== undefined ) } ;
53
51
resetState ( ) ;
54
52
return result ;
55
53
}
56
54
57
- function addExport ( name ) {
58
- if ( ! strictReserved . has ( name ) )
59
- _exports . add ( name ) ;
55
+ function decode ( str ) {
56
+ if ( str [ 0 ] === '"' || str [ 0 ] === '\'' ) {
57
+ try {
58
+ const decoded = ( 0 , eval ) ( str ) ;
59
+ // Filter to exclude non-matching UTF-16 surrogate strings
60
+ for ( let i = 0 ; i < decoded . length ; i ++ ) {
61
+ const surrogatePrefix = decoded . charCodeAt ( i ) & 0xFC00 ;
62
+ if ( surrogatePrefix < 0xD800 ) {
63
+ // Not a surrogate
64
+ continue ;
65
+ }
66
+ else if ( surrogatePrefix === 0xD800 ) {
67
+ // Validate surrogate pair
68
+ if ( ( decoded . charCodeAt ( ++ i ) & 0xFC00 ) !== 0xDC00 )
69
+ return ;
70
+ }
71
+ else {
72
+ // Out-of-range surrogate code (above 0xD800)
73
+ return ;
74
+ }
75
+ }
76
+ return decoded ;
77
+ }
78
+ catch { }
79
+ }
80
+ else {
81
+ return str ;
82
+ }
60
83
}
61
84
62
85
function parseSource ( cjsSource ) {
@@ -173,10 +196,8 @@ function parseSource (cjsSource) {
173
196
// TODO: <!-- XML comment support
174
197
break ;
175
198
case 39 /*'*/ :
176
- singleQuoteString ( ) ;
177
- break ;
178
199
case 34 /*"*/ :
179
- doubleQuoteString ( ) ;
200
+ stringLiteral ( ch ) ;
180
201
break ;
181
202
case 47 /*/*/ : {
182
203
const next_ch = source . charCodeAt ( pos + 1 ) ;
@@ -329,11 +350,9 @@ function tryParseObjectDefineOrKeys (keys) {
329
350
pos ++ ;
330
351
ch = commentWhitespace ( ) ;
331
352
if ( ch !== 39 /*'*/ && ch !== 34 /*"*/ ) break ;
332
- let quot = ch ;
333
- const exportPos = ++ pos ;
334
- if ( ! identifier ( ) || source . charCodeAt ( pos ) !== quot ) break ;
335
- expt = source . slice ( exportPos , pos ) ;
336
- pos ++ ;
353
+ const exportPos = pos ;
354
+ stringLiteral ( ch ) ;
355
+ expt = source . slice ( exportPos , ++ pos ) ;
337
356
ch = commentWhitespace ( ) ;
338
357
if ( ch !== 44 /*,*/ ) break ;
339
358
pos ++ ;
@@ -360,7 +379,7 @@ function tryParseObjectDefineOrKeys (keys) {
360
379
pos += 5 ;
361
380
ch = commentWhitespace ( ) ;
362
381
if ( ch !== 58 /*:*/ ) break ;
363
- addExport ( expt ) ;
382
+ _exports . add ( decode ( expt ) ) ;
364
383
pos = revertPos ;
365
384
return ;
366
385
}
@@ -403,8 +422,7 @@ function tryParseObjectDefineOrKeys (keys) {
403
422
else if ( ch === 91 /*[*/ ) {
404
423
pos ++ ;
405
424
ch = commentWhitespace ( ) ;
406
- if ( ch === 39 /*'*/ ) singleQuoteString ( ) ;
407
- else if ( ch === 34 /*"*/ ) doubleQuoteString ( ) ;
425
+ if ( ch === 39 /*'*/ || ch === 34 /*"*/ ) stringLiteral ( ch ) ;
408
426
else break ;
409
427
pos ++ ;
410
428
ch = commentWhitespace ( ) ;
@@ -427,13 +445,13 @@ function tryParseObjectDefineOrKeys (keys) {
427
445
pos ++ ;
428
446
ch = commentWhitespace ( ) ;
429
447
if ( ch !== 41 /*)*/ ) break ;
430
- addExport ( expt ) ;
448
+ _exports . add ( decode ( expt ) ) ;
431
449
return ;
432
450
}
433
451
break ;
434
452
}
435
453
if ( expt ) {
436
- unsafeGetters . add ( expt ) ;
454
+ unsafeGetters . add ( decode ( expt ) ) ;
437
455
}
438
456
}
439
457
else if ( keys && ch === 107 /*k*/ && source . startsWith ( 'eys' , pos + 1 ) ) {
@@ -801,7 +819,7 @@ function tryParseObjectDefineOrKeys (keys) {
801
819
802
820
const starExportSpecifier = starExportMap [ id ] ;
803
821
if ( starExportSpecifier ) {
804
- reexports . add ( starExportSpecifier ) ;
822
+ reexports . add ( decode ( starExportSpecifier ) ) ;
805
823
pos = revertPos ;
806
824
return ;
807
825
}
@@ -863,7 +881,7 @@ function tryParseExportsDotAssign (assign) {
863
881
const endPos = pos ;
864
882
ch = commentWhitespace ( ) ;
865
883
if ( ch === 61 /*=*/ ) {
866
- addExport ( source . slice ( startPos , endPos ) ) ;
884
+ _exports . add ( decode ( source . slice ( startPos , endPos ) ) ) ;
867
885
return ;
868
886
}
869
887
}
@@ -874,19 +892,15 @@ function tryParseExportsDotAssign (assign) {
874
892
pos ++ ;
875
893
ch = commentWhitespace ( ) ;
876
894
if ( ch === 39 /*'*/ || ch === 34 /*"*/ ) {
877
- pos ++ ;
878
895
const startPos = pos ;
879
- if ( identifier ( ) && source . charCodeAt ( pos ) === ch ) {
880
- const endPos = pos ++ ;
881
- ch = commentWhitespace ( ) ;
882
- if ( ch !== 93 /*]*/ )
883
- break ;
884
- pos ++ ;
885
- ch = commentWhitespace ( ) ;
886
- if ( ch !== 61 /*=*/ )
887
- break ;
888
- addExport ( source . slice ( startPos , endPos ) ) ;
889
- }
896
+ stringLiteral ( ch ) ;
897
+ const endPos = ++ pos ;
898
+ ch = commentWhitespace ( ) ;
899
+ if ( ch !== 93 /*]*/ ) break ;
900
+ pos ++ ;
901
+ ch = commentWhitespace ( ) ;
902
+ if ( ch !== 61 /*=*/ ) break ;
903
+ _exports . add ( decode ( source . slice ( startPos , endPos ) ) ) ;
890
904
}
891
905
break ;
892
906
}
@@ -921,39 +935,21 @@ function tryParseRequire (requireType) {
921
935
if ( ch === 40 /*(*/ ) {
922
936
pos ++ ;
923
937
ch = commentWhitespace ( ) ;
924
- const reexportStart = pos + 1 ;
925
- if ( ch === 39 /*'*/ ) {
926
- singleQuoteString ( ) ;
927
- const reexportEnd = pos ++ ;
928
- ch = commentWhitespace ( ) ;
929
- if ( ch === 41 /*)*/ ) {
930
- switch ( requireType ) {
931
- case ExportAssign :
932
- reexports . add ( source . slice ( reexportStart , reexportEnd ) ) ;
933
- return true ;
934
- case ExportStar :
935
- reexports . add ( source . slice ( reexportStart , reexportEnd ) ) ;
936
- return true ;
937
- default :
938
- lastStarExportSpecifier = source . slice ( reexportStart , reexportEnd ) ;
939
- return true ;
940
- }
941
- }
942
- }
943
- else if ( ch === 34 /*"*/ ) {
944
- doubleQuoteString ( ) ;
945
- const reexportEnd = pos ++ ;
938
+ const reexportStart = pos ;
939
+ if ( ch === 39 /*'*/ || ch === 34 /*"*/ ) {
940
+ stringLiteral ( ch ) ;
941
+ const reexportEnd = ++ pos ;
946
942
ch = commentWhitespace ( ) ;
947
943
if ( ch === 41 /*)*/ ) {
948
944
switch ( requireType ) {
949
945
case ExportAssign :
950
- reexports . add ( source . slice ( reexportStart , reexportEnd ) ) ;
946
+ reexports . add ( decode ( source . slice ( reexportStart , reexportEnd ) ) ) ;
951
947
return true ;
952
948
case ExportStar :
953
- reexports . add ( source . slice ( reexportStart , reexportEnd ) ) ;
949
+ reexports . add ( decode ( source . slice ( reexportStart , reexportEnd ) ) ) ;
954
950
return true ;
955
951
default :
956
- lastStarExportSpecifier = source . slice ( reexportStart , reexportEnd ) ;
952
+ lastStarExportSpecifier = decode ( source . slice ( reexportStart , reexportEnd ) ) ;
957
953
return true ;
958
954
}
959
955
}
@@ -982,7 +978,7 @@ function tryParseLiteralExports () {
982
978
}
983
979
ch = source . charCodeAt ( pos ) ;
984
980
}
985
- addExport ( source . slice ( startPos , endPos ) ) ;
981
+ _exports . add ( decode ( source . slice ( startPos , endPos ) ) ) ;
986
982
}
987
983
else if ( ch === 46 /*.*/ && source . startsWith ( '..' , pos + 1 ) ) {
988
984
pos += 3 ;
@@ -996,21 +992,20 @@ function tryParseLiteralExports () {
996
992
ch = commentWhitespace ( ) ;
997
993
}
998
994
else if ( ch === 39 /*'*/ || ch === 34 /*"*/ ) {
999
- const startPos = ++ pos ;
1000
- if ( identifier ( ) && source . charCodeAt ( pos ) === ch ) {
1001
- const endPos = pos ++ ;
995
+ const startPos = pos ;
996
+ stringLiteral ( ch ) ;
997
+ const endPos = ++ pos ;
998
+ ch = commentWhitespace ( ) ;
999
+ if ( ch === 58 /*:*/ ) {
1000
+ pos ++ ;
1002
1001
ch = commentWhitespace ( ) ;
1003
- if ( ch === 58 /*:*/ ) {
1004
- pos ++ ;
1005
- ch = commentWhitespace ( ) ;
1006
- // nothing more complex than identifier expressions for now
1007
- if ( ! identifier ( ) ) {
1008
- pos = revertPos ;
1009
- return ;
1010
- }
1011
- ch = source . charCodeAt ( pos ) ;
1012
- addExport ( source . slice ( startPos , endPos ) ) ;
1002
+ // nothing more complex than identifier expressions for now
1003
+ if ( ! identifier ( ) ) {
1004
+ pos = revertPos ;
1005
+ return ;
1013
1006
}
1007
+ ch = source . charCodeAt ( pos ) ;
1008
+ _exports . add ( decode ( source . slice ( startPos , endPos ) ) ) ;
1014
1009
}
1015
1010
}
1016
1011
else {
@@ -1248,26 +1243,10 @@ function lineComment () {
1248
1243
}
1249
1244
}
1250
1245
1251
- function singleQuoteString ( ) {
1252
- while ( pos ++ < end ) {
1253
- let ch = source . charCodeAt ( pos ) ;
1254
- if ( ch === 39 /*'*/ )
1255
- return ;
1256
- if ( ch === 92 /*\*/ ) {
1257
- ch = source . charCodeAt ( ++ pos ) ;
1258
- if ( ch === 13 /*\r*/ && source . charCodeAt ( pos + 1 ) === 10 /*\n*/ )
1259
- pos ++ ;
1260
- }
1261
- else if ( isBr ( ch ) )
1262
- break ;
1263
- }
1264
- throw new Error ( 'Unterminated string.' ) ;
1265
- }
1266
-
1267
- function doubleQuoteString ( ) {
1246
+ function stringLiteral ( quote ) {
1268
1247
while ( pos ++ < end ) {
1269
1248
let ch = source . charCodeAt ( pos ) ;
1270
- if ( ch === 34 /*"*/ )
1249
+ if ( ch === quote )
1271
1250
return ;
1272
1251
if ( ch === 92 /*\*/ ) {
1273
1252
ch = source . charCodeAt ( ++ pos ) ;
0 commit comments