Skip to content

Commit

Permalink
Remove import assertions feature (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
adams85 authored Feb 22, 2023
1 parent b71dafa commit 695825c
Show file tree
Hide file tree
Showing 28 changed files with 69 additions and 921 deletions.
14 changes: 5 additions & 9 deletions src/Esprima/Ast/ExportAllDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,28 @@

namespace Esprima.Ast;

[VisitableNode(ChildProperties = new[] { nameof(Exported), nameof(Source), nameof(Assertions) })]
[VisitableNode(ChildProperties = new[] { nameof(Exported), nameof(Source) })]
public sealed partial class ExportAllDeclaration : ExportDeclaration
{
private readonly NodeList<ImportAttribute> _assertions;

public ExportAllDeclaration(Literal source) : this(source, null, new NodeList<ImportAttribute>())
public ExportAllDeclaration(Literal source) : this(source, null)
{
}

public ExportAllDeclaration(Literal source, Expression? exported, in NodeList<ImportAttribute> assertions) : base(Nodes.ExportAllDeclaration)
public ExportAllDeclaration(Literal source, Expression? exported) : base(Nodes.ExportAllDeclaration)
{
Source = source;
Exported = exported;
_assertions = assertions;
}

public Literal Source { [MethodImpl(MethodImplOptions.AggressiveInlining)] get; }
/// <remarks>
/// <see cref="Identifier"/> | <see cref="Literal"/> (string)
/// </remarks>
public Expression? Exported { [MethodImpl(MethodImplOptions.AggressiveInlining)] get; }
public ref readonly NodeList<ImportAttribute> Assertions { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => ref _assertions; }

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private ExportAllDeclaration Rewrite(Expression? exported, Literal source, in NodeList<ImportAttribute> assertions)
private ExportAllDeclaration Rewrite(Expression? exported, Literal source)
{
return new ExportAllDeclaration(source, exported, assertions);
return new ExportAllDeclaration(source, exported);
}
}
12 changes: 4 additions & 8 deletions src/Esprima/Ast/ExportNamedDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@

namespace Esprima.Ast;

[VisitableNode(ChildProperties = new[] { nameof(Declaration), nameof(Specifiers), nameof(Source), nameof(Assertions) })]
[VisitableNode(ChildProperties = new[] { nameof(Declaration), nameof(Specifiers), nameof(Source) })]
public sealed partial class ExportNamedDeclaration : ExportDeclaration
{
private readonly NodeList<ExportSpecifier> _specifiers;
private readonly NodeList<ImportAttribute> _assertions;

public ExportNamedDeclaration(
Declaration? declaration,
in NodeList<ExportSpecifier> specifiers,
Literal? source,
in NodeList<ImportAttribute> assertions)
Literal? source)
: base(Nodes.ExportNamedDeclaration)
{
Declaration = declaration;
_specifiers = specifiers;
Source = source;
_assertions = assertions;
}

/// <remarks>
Expand All @@ -27,11 +24,10 @@ public ExportNamedDeclaration(
public Declaration? Declaration { [MethodImpl(MethodImplOptions.AggressiveInlining)] get; }
public ref readonly NodeList<ExportSpecifier> Specifiers { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => ref _specifiers; }
public Literal? Source { [MethodImpl(MethodImplOptions.AggressiveInlining)] get; }
public ref readonly NodeList<ImportAttribute> Assertions { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => ref _assertions; }

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private ExportNamedDeclaration Rewrite(Declaration? declaration, in NodeList<ExportSpecifier> specifiers, Literal? source, in NodeList<ImportAttribute> assertions)
private ExportNamedDeclaration Rewrite(Declaration? declaration, in NodeList<ExportSpecifier> specifiers, Literal? source)
{
return new ExportNamedDeclaration(declaration, specifiers, source, assertions);
return new ExportNamedDeclaration(declaration, specifiers, source);
}
}
14 changes: 4 additions & 10 deletions src/Esprima/Ast/Import.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,19 @@

namespace Esprima.Ast;

[VisitableNode(ChildProperties = new[] { nameof(Source), nameof(Attributes) })]
[VisitableNode(ChildProperties = new[] { nameof(Source) })]
public sealed partial class Import : Expression
{
public Import(Expression source) : this(source, null)
{
}

public Import(Expression source, Expression? attributes) : base(Nodes.Import)
public Import(Expression source) : base(Nodes.Import)
{
Source = source;
Attributes = attributes;
}

public Expression Source { [MethodImpl(MethodImplOptions.AggressiveInlining)] get; }
public Expression? Attributes { [MethodImpl(MethodImplOptions.AggressiveInlining)] get; }

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Import Rewrite(Expression source, Expression? attributes)
private Import Rewrite(Expression source)
{
return new Import(source, attributes);
return new Import(source);
}
}
25 changes: 0 additions & 25 deletions src/Esprima/Ast/ImportAttribute.cs

This file was deleted.

12 changes: 4 additions & 8 deletions src/Esprima/Ast/ImportDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,26 @@

namespace Esprima.Ast;

[VisitableNode(ChildProperties = new[] { nameof(Specifiers), nameof(Source), nameof(Assertions) })]
[VisitableNode(ChildProperties = new[] { nameof(Specifiers), nameof(Source) })]
public sealed partial class ImportDeclaration : Declaration
{
private readonly NodeList<ImportDeclarationSpecifier> _specifiers;
private readonly NodeList<ImportAttribute> _assertions;

public ImportDeclaration(
in NodeList<ImportDeclarationSpecifier> specifiers,
Literal source,
in NodeList<ImportAttribute> assertions)
Literal source)
: base(Nodes.ImportDeclaration)
{
_specifiers = specifiers;
Source = source;
_assertions = assertions;
}

public ref readonly NodeList<ImportDeclarationSpecifier> Specifiers { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => ref _specifiers; }
public Literal Source { [MethodImpl(MethodImplOptions.AggressiveInlining)] get; }
public ref readonly NodeList<ImportAttribute> Assertions { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => ref _assertions; }

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private ImportDeclaration Rewrite(in NodeList<ImportDeclarationSpecifier> specifiers, Literal source, in NodeList<ImportAttribute> assertions)
private ImportDeclaration Rewrite(in NodeList<ImportDeclarationSpecifier> specifiers, Literal source)
{
return new ImportDeclaration(specifiers, source, assertions);
return new ImportDeclaration(specifiers, source);
}
}
1 change: 0 additions & 1 deletion src/Esprima/Ast/Nodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ public enum Nodes
ImportSpecifier,
ImportDefaultSpecifier,
ImportNamespaceSpecifier,
ImportAttribute,
ImportDeclaration,
ExportSpecifier,
ExportNamedDeclaration,
Expand Down
85 changes: 6 additions & 79 deletions src/Esprima/JavascriptParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1710,15 +1710,6 @@ private Import ParseImportCall()

var source = ParseAssignmentExpression();

Expression? attributes = null;
if (Match(","))
{
NextToken();

if (!Match(")"))
attributes = ParseAssignmentExpression();
}

_context.IsAssignmentTarget = previousIsAssignmentTarget;

if (!this.Match(")") && _tolerant)
Expand All @@ -1732,7 +1723,7 @@ private Import ParseImportCall()
this.Expect(")");
}

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

private bool MatchImportMeta()
Expand Down Expand Up @@ -5050,66 +5041,6 @@ private Literal ParseModuleSpecifier()
return Finalize(node, new Literal((string) token.Value!, raw));
}

private ArrayList<ImportAttribute> ParseImportAttributes()
{
var attributes = new ArrayList<ImportAttribute>();
if (_lookahead.Value is not ("assert"))
{
return attributes;
}

NextToken();
Expect("{");

var parameterSet = new HashSet<string?>();
while (!Match("}"))
{
var importAttribute = ParseImportAttribute();

string? key = string.Empty;
switch (importAttribute.Key)
{
case Identifier identifier:
key = identifier.Name;
break;
case Literal literal:
key = literal.StringValue;
break;
}

if (!parameterSet.Add(key))
{
ThrowError(Messages.DuplicateAssertClauseProperty, key);
}

attributes.Add(importAttribute);
if (!Match("}"))
{
ExpectCommaSeparator();
}
}
Expect("}");
return attributes;
}

private ImportAttribute ParseImportAttribute()
{
var node = CreateNode();

Expression key = ParseObjectPropertyKey();
if (!Match(":"))
{
ThrowUnexpectedToken(NextToken());
}

NextToken();
var literalToken = NextToken();
var raw = GetTokenRaw(literalToken);
Literal value = Finalize(node, new Literal((string) literalToken.Value!, raw));

return Finalize(node, new ImportAttribute(key, value));
}

// import {<foo as bar>} ...;
private ImportSpecifier ParseImportSpecifier()
{
Expand Down Expand Up @@ -5260,10 +5191,9 @@ private ImportDeclaration ParseImportDeclaration()
src = ParseModuleSpecifier();
}

var attributes = ParseImportAttributes();
ConsumeSemicolon();

return Finalize(node, new ImportDeclaration(NodeList.From(ref specifiers), src, NodeList.From(ref attributes)));
return Finalize(node, new ImportDeclaration(NodeList.From(ref specifiers), src));
}

// https://tc39.github.io/ecma262/#sec-exports
Expand Down Expand Up @@ -5383,9 +5313,8 @@ private ExportDeclaration ParseExportDeclaration()

NextToken();
var src = ParseModuleSpecifier();
var attributes = ParseImportAttributes();
ConsumeSemicolon();
exportDeclaration = Finalize(node, new ExportAllDeclaration(src, exported, NodeList.From(ref attributes)));
exportDeclaration = Finalize(node, new ExportAllDeclaration(src, exported));
}
else if (_lookahead.Type == TokenType.Keyword)
{
Expand All @@ -5407,19 +5336,18 @@ private ExportDeclaration ParseExportDeclaration()
break;
}

exportDeclaration = Finalize(node, new ExportNamedDeclaration(declaration.As<Declaration>(), new NodeList<ExportSpecifier>(), null, new NodeList<ImportAttribute>()));
exportDeclaration = Finalize(node, new ExportNamedDeclaration(declaration.As<Declaration>(), new NodeList<ExportSpecifier>(), null));
}
else if (MatchAsyncFunction())
{
var declaration = ParseFunctionDeclaration();
exportDeclaration = Finalize(node, new ExportNamedDeclaration(declaration, new NodeList<ExportSpecifier>(), null, new NodeList<ImportAttribute>()));
exportDeclaration = Finalize(node, new ExportNamedDeclaration(declaration, new NodeList<ExportSpecifier>(), null));
}
else
{
var specifiers = new ArrayList<ExportSpecifier>();
Literal? source = null;
var isExportFromIdentifier = false;
ArrayList<ImportAttribute> attributes = new();

Expect("{");
while (!Match("}"))
Expand All @@ -5440,7 +5368,6 @@ private ExportDeclaration ParseExportDeclaration()
// export {foo} from 'foo';
NextToken();
source = ParseModuleSpecifier();
attributes = ParseImportAttributes();
ConsumeSemicolon();
}
else if (isExportFromIdentifier)
Expand All @@ -5455,7 +5382,7 @@ private ExportDeclaration ParseExportDeclaration()
ConsumeSemicolon();
}

exportDeclaration = Finalize(node, new ExportNamedDeclaration(null, NodeList.From(ref specifiers), source, NodeList.From(ref attributes)));
exportDeclaration = Finalize(node, new ExportNamedDeclaration(null, NodeList.From(ref specifiers), source));
}

return exportDeclaration;
Expand Down
1 change: 0 additions & 1 deletion src/Esprima/Messages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public static class Messages
public const string DuplicateConstructor = "A class may only have one constructor";
public const string DuplicateParameter = "Duplicate parameter name not allowed in this context";
public const string DuplicateProtoProperty = "Duplicate __proto__ fields are not allowed in object literals";
public const string DuplicateAssertClauseProperty = "Assert clause may not have duplicate keys {0}";
public const string ForInOfLoopInitializer = "'{0} loop variable declaration may not have an initializer";
public const string GeneratorInLegacyContext = "Generator declarations are not allowed in legacy contexts";
public const string IllegalBreak = "Illegal break statement";
Expand Down
13 changes: 0 additions & 13 deletions src/Esprima/Utils/AstToJavascriptConverter.Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,19 +221,6 @@ protected void VisitExpressionListItem(Expression expression, int index, int cou
_currentExpressionFlags = originalExpressionFlags;
}

private void VisitAssertions(in NodeList<ImportAttribute> assertions)
{
// https://github.com/tc39/proposal-import-assertions

Writer.WriteKeyword("assert", TokenFlags.SurroundingSpaceRecommended, ref _writeContext);

Writer.StartObject(assertions.Count, ref _writeContext);

VisitAuxiliaryNodeList(in assertions, separator: ",");

Writer.EndObject(assertions.Count, ref _writeContext);
}

private void VisitExportOrImportSpecifierIdentifier(Expression identifierExpression)
{
if (identifierExpression is Identifier { Name: "default" } identifier)
Expand Down
Loading

0 comments on commit 695825c

Please sign in to comment.