@@ -680,6 +680,8 @@ export class Parser {
680
680
expr = this . finalize ( node , new Node . ThisExpression ( ) ) ;
681
681
} else if ( this . matchKeyword ( 'class' ) ) {
682
682
expr = this . parseClassExpression ( ) ;
683
+ } else if ( this . matchImportCall ( ) ) {
684
+ expr = this . parseImportCall ( ) ;
683
685
} else {
684
686
expr = this . throwUnexpectedToken ( this . nextToken ( ) ) ;
685
687
}
@@ -1216,6 +1218,8 @@ export class Parser {
1216
1218
} else {
1217
1219
this . throwUnexpectedToken ( this . lookahead ) ;
1218
1220
}
1221
+ } else if ( this . matchKeyword ( 'import' ) ) {
1222
+ this . throwUnexpectedToken ( this . lookahead ) ;
1219
1223
} else {
1220
1224
const callee = this . isolateCoverGrammar ( this . parseLeftHandSideExpression ) ;
1221
1225
const args = this . match ( '(' ) ? this . parseArguments ( ) : [ ] ;
@@ -1255,6 +1259,25 @@ export class Parser {
1255
1259
return args ;
1256
1260
}
1257
1261
1262
+ matchImportCall ( ) : boolean {
1263
+ let match = this . matchKeyword ( 'import' ) ;
1264
+ if ( match ) {
1265
+ const state = this . scanner . saveState ( ) ;
1266
+ this . scanner . scanComments ( ) ;
1267
+ const next = this . scanner . lex ( ) ;
1268
+ this . scanner . restoreState ( state ) ;
1269
+ match = ( next . type === Token . Punctuator ) && ( next . value === '(' ) ;
1270
+ }
1271
+
1272
+ return match ;
1273
+ }
1274
+
1275
+ parseImportCall ( ) : Node . Import {
1276
+ const node = this . createNode ( ) ;
1277
+ this . expectKeyword ( 'import' ) ;
1278
+ return this . finalize ( node , new Node . Import ( ) ) ;
1279
+ }
1280
+
1258
1281
parseLeftHandSideExpressionAllowCall ( ) : Node . Expression {
1259
1282
const startToken = this . lookahead ;
1260
1283
const maybeAsync = this . matchContextualKeyword ( 'async' ) ;
@@ -1287,6 +1310,9 @@ export class Parser {
1287
1310
this . context . isBindingElement = false ;
1288
1311
this . context . isAssignmentTarget = false ;
1289
1312
const args = asyncArrow ? this . parseAsyncArguments ( ) : this . parseArguments ( ) ;
1313
+ if ( expr . type === Syntax . Import && args . length !== 1 ) {
1314
+ this . tolerateError ( Messages . BadImportCallArity ) ;
1315
+ }
1290
1316
expr = this . finalize ( this . startNode ( startToken ) , new Node . CallExpression ( expr , args ) ) ;
1291
1317
if ( asyncArrow && this . match ( '=>' ) ) {
1292
1318
expr = {
@@ -1789,10 +1815,14 @@ export class Parser {
1789
1815
statement = this . parseExportDeclaration ( ) ;
1790
1816
break ;
1791
1817
case 'import' :
1792
- if ( ! this . context . isModule ) {
1793
- this . tolerateUnexpectedToken ( this . lookahead , Messages . IllegalImportDeclaration ) ;
1818
+ if ( this . matchImportCall ( ) ) {
1819
+ statement = this . parseExpressionStatement ( ) ;
1820
+ } else {
1821
+ if ( ! this . context . isModule ) {
1822
+ this . tolerateUnexpectedToken ( this . lookahead , Messages . IllegalImportDeclaration ) ;
1823
+ }
1824
+ statement = this . parseImportDeclaration ( ) ;
1794
1825
}
1795
- statement = this . parseImportDeclaration ( ) ;
1796
1826
break ;
1797
1827
case 'const' :
1798
1828
statement = this . parseLexicalDeclaration ( { inFor : false } ) ;
0 commit comments