Skip to content

Support ES2020 export ns from #196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Esprima/Ast/ExportAllDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ namespace Esprima.Ast
public sealed class ExportAllDeclaration : ExportDeclaration
{
public readonly Literal Source;
public readonly Identifier? Exported;

public ExportAllDeclaration(Literal source) : base(Nodes.ExportAllDeclaration)
public ExportAllDeclaration(Literal source) : this(source, null)
{
}

public ExportAllDeclaration(Literal source, Identifier? exported) : base(Nodes.ExportAllDeclaration)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lahma would it be better to create another ctor so it's not a breaking change?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, as type is public and we don't have JS semantics where nothing matters having another constructor makes sense in minor release.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard to say if there's any dependency, but follows semantic versioning.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lahma would it be better to create another ctor so it's not a breaking change?

I added another commit to this PR that adds another constructor

{
Source = source;
Exported = exported;
}

public override NodeCollection ChildNodes => new NodeCollection(Source);
public override NodeCollection ChildNodes => new NodeCollection(Source, Exported);

protected internal override void Accept(AstVisitor visitor) => visitor.VisitExportAllDeclaration(this);
}
Expand Down
11 changes: 10 additions & 1 deletion src/Esprima/JavascriptParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4628,6 +4628,15 @@ private ExportDeclaration ParseExportDeclaration()
{
// export * from 'foo';
NextToken();

//export * as ns from 'foo'
Identifier? exported = null;
if (MatchContextualKeyword("as"))
{
NextToken();
exported = ParseIdentifierName();
}

if (!MatchContextualKeyword("from"))
{
var message = _lookahead.Value != null ? Messages.UnexpectedToken : Messages.MissingFromClause;
Expand All @@ -4636,7 +4645,7 @@ private ExportDeclaration ParseExportDeclaration()
NextToken();
var src = ParseModuleSpecifier();
ConsumeSemicolon();
exportDeclaration = Finalize(node, new ExportAllDeclaration(src));
exportDeclaration = Finalize(node, new ExportAllDeclaration(src, exported));

}
else if (_lookahead.Type == TokenType.Keyword)
Expand Down
3 changes: 3 additions & 0 deletions src/Esprima/Utils/AstJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,10 @@ protected internal override void VisitExportDefaultDeclaration(ExportDefaultDecl
protected internal override void VisitExportAllDeclaration(ExportAllDeclaration exportAllDeclaration)
{
using (StartNodeObject(exportAllDeclaration))
{
Member("source", exportAllDeclaration.Source);
Member("exported", exportAllDeclaration.Exported);
}
}

protected internal override void VisitExportNamedDeclaration(ExportNamedDeclaration exportNamedDeclaration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
}
}
},
"exported" : null,
"range": [
0,
20
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as someIdentifier from "someModule"
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
{
"type": "Program",
"body": [
{
"type": "ExportAllDeclaration",
"source": {
"type": "Literal",
"value": "someModule",
"raw": "\"someModule\"",
"range": [
32,
44
],
"loc": {
"start": {
"line": 1,
"column": 32
},
"end": {
"line": 1,
"column": 44
}
}
},
"exported": {
"type": "Identifier",
"name": "someIdentifier",
"range": [
12,
26
],
"loc": {
"start": {
"line": 1,
"column": 12
},
"end": {
"line": 1,
"column": 26
}
}
},
"range": [
0,
44
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 44
}
}
}
],
"sourceType": "module",
"range": [
0,
44
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 44
}
},
"tokens": [
{
"type": "Keyword",
"value": "export",
"range": [
0,
6
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 6
}
}
},
{
"type": "Punctuator",
"value": "*",
"range": [
7,
8
],
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 8
}
}
},
{
"type": "Identifier",
"value": "as",
"range": [
9,
11
],
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 11
}
}
},
{
"type": "Identifier",
"value": "someIdentifier",
"range": [
12,
26
],
"loc": {
"start": {
"line": 1,
"column": 12
},
"end": {
"line": 1,
"column": 26
}
}
},
{
"type": "Identifier",
"value": "from",
"range": [
27,
31
],
"loc": {
"start": {
"line": 1,
"column": 27
},
"end": {
"line": 1,
"column": 31
}
}
},
{
"type": "String",
"value": "\"someModule\"",
"range": [
32,
44
],
"loc": {
"start": {
"line": 1,
"column": 32
},
"end": {
"line": 1,
"column": 44
}
}
}
]
}