@@ -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
}
@@ -1255,6 +1257,25 @@ export class Parser {
1255
1257
return args ;
1256
1258
}
1257
1259
1260
+ matchImportCall ( ) : boolean {
1261
+ let match = this . matchKeyword ( 'import' ) ;
1262
+ if ( match ) {
1263
+ const state = this . scanner . saveState ( ) ;
1264
+ this . scanner . scanComments ( ) ;
1265
+ const next = this . scanner . lex ( ) ;
1266
+ this . scanner . restoreState ( state ) ;
1267
+ match = ( next . type === Token . Punctuator ) && ( next . value === '(' ) ;
1268
+ }
1269
+
1270
+ return match ;
1271
+ }
1272
+
1273
+ parseImportCall ( ) : Node . Import {
1274
+ const node = this . createNode ( ) ;
1275
+ this . expectKeyword ( 'import' ) ;
1276
+ return this . finalize ( node , new Node . Import ( ) ) ;
1277
+ }
1278
+
1258
1279
parseLeftHandSideExpressionAllowCall ( ) : Node . Expression {
1259
1280
const startToken = this . lookahead ;
1260
1281
const maybeAsync = this . matchContextualKeyword ( 'async' ) ;
@@ -1287,6 +1308,9 @@ export class Parser {
1287
1308
this . context . isBindingElement = false ;
1288
1309
this . context . isAssignmentTarget = false ;
1289
1310
const args = asyncArrow ? this . parseAsyncArguments ( ) : this . parseArguments ( ) ;
1311
+ if ( expr . type === Syntax . Import && args . length !== 1 ) {
1312
+ this . tolerateError ( Messages . BadImportCallArity ) ;
1313
+ }
1290
1314
expr = this . finalize ( this . startNode ( startToken ) , new Node . CallExpression ( expr , args ) ) ;
1291
1315
if ( asyncArrow && this . match ( '=>' ) ) {
1292
1316
expr = {
@@ -1789,10 +1813,14 @@ export class Parser {
1789
1813
statement = this . parseExportDeclaration ( ) ;
1790
1814
break ;
1791
1815
case 'import' :
1792
- if ( ! this . context . isModule ) {
1793
- this . tolerateUnexpectedToken ( this . lookahead , Messages . IllegalImportDeclaration ) ;
1816
+ if ( this . matchImportCall ( ) ) {
1817
+ statement = this . parseExpressionStatement ( ) ;
1818
+ } else {
1819
+ if ( ! this . context . isModule ) {
1820
+ this . tolerateUnexpectedToken ( this . lookahead , Messages . IllegalImportDeclaration ) ;
1821
+ }
1822
+ statement = this . parseImportDeclaration ( ) ;
1794
1823
}
1795
- statement = this . parseImportDeclaration ( ) ;
1796
1824
break ;
1797
1825
case 'const' :
1798
1826
statement = this . parseLexicalDeclaration ( { inFor : false } ) ;
0 commit comments