Skip to content

Commit

Permalink
Fix ES2018 import() (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
jogibear9988 authored Nov 2, 2021
1 parent b7a1ed0 commit d6cc2ba
Show file tree
Hide file tree
Showing 9 changed files with 875 additions and 2,689 deletions.
7 changes: 7 additions & 0 deletions src/Esprima/Ast/Import.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@ namespace Esprima.Ast
{
public sealed class Import : Expression
{
public readonly Expression Source;

public Import() : base(Nodes.Import)
{
}

public Import(Expression source) : base(Nodes.Import)
{
Source = source;
}

public override NodeCollection ChildNodes => NodeCollection.Empty;

protected internal override void Accept(AstVisitor visitor)
Expand Down
20 changes: 18 additions & 2 deletions src/Esprima/JavascriptParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1511,8 +1511,24 @@ private bool MatchImportCall()
private Import ParseImportCall()
{
var node = CreateNode();
ExpectKeyword("import");
return Finalize(node, new Import());
ExpectKeyword("import");
Expect("(");

var source = this.parseAssignmentExpression();
if (!this.Match(")") && this._config.Tolerant)
{
this.TolerateUnexpectedToken(this.NextToken());
}
else
{
this.Expect(")");
if (this.Match(";"))
{
this.NextToken();
}
}

return Finalize(node, new Import(source));
}

private bool MatchImportMeta()
Expand Down
3 changes: 2 additions & 1 deletion src/Esprima/Utils/AstVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,8 @@ protected internal virtual void VisitExportSpecifier(ExportSpecifier exportSpeci
}

protected internal virtual void VisitImport(Import import)
{
{
Visit(import.Source);
}

protected internal virtual void VisitImportDeclaration(ImportDeclaration importDeclaration)
Expand Down
Loading

0 comments on commit d6cc2ba

Please sign in to comment.