Skip to content
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

JS-188 Directive can be used as a Statement #4742

Merged
merged 2 commits into from
Jun 18, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public record ClassExpression(Location loc, Optional<Identifier> id, Optional<Ex
public record ConditionalExpression(Location loc, Expression test, Expression alternate, Expression consequent) implements Expression {}
public record ContinueStatement(Location loc, Optional<Identifier> label) implements Statement {}
public record DebuggerStatement(Location loc) implements Statement {}
public record Directive(Location loc, Literal expression, String directive) implements DirectiveOrModuleDeclarationOrStatement {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we rename the top interface ModuleDeclarationOrStatement since Directive is a child of Statement?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This interface was introduced as the type of Program is:

export interface Program extends BaseNode {
    // ...
    body: Array<Directive | Statement | ModuleDeclaration>;
}

On one hand, we might want to match this definition, on the other, you are right that it is redundant. No strong feeling on this one, do you think that it is worth it?

Copy link
Contributor

Choose a reason for hiding this comment

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

Indeed, I'm not quite sure. How will it be used on the Sonar-Armor side?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As discussed, let's keep it as it is, and come back at it if there is a need.

public record Directive(Location loc, Literal expression, String directive) implements Statement {}
public record DoWhileStatement(Location loc, Statement body, Expression test) implements Statement {}
public record EmptyStatement(Location loc) implements Statement {}
public record ExportAllDeclaration(Location loc, Optional<IdentifierOrLiteral> exported, Literal source) implements ModuleDeclaration {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void test_expression_subclasses() {
void test_statement_subclasses() {
Class<?> sealedClass = ESTree.Statement.class;
Class<?>[] permittedSubclasses = sealedClass.getPermittedSubclasses();
assertThat(permittedSubclasses).hasSize(20);
assertThat(permittedSubclasses).hasSize(21);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,31 @@ void should_create_export_all_declaration_type_using_a_literal() {
});
}

@Test
void directive_can_be_in_block_statement() {
BlockStatement blockStatement = BlockStatement.newBuilder()
.addBody(Node.newBuilder()
.setType(NodeType.ExpressionStatementType)
.setExpressionStatement(ExpressionStatement.newBuilder()
.setDirective("directiveName")
.setExpression(Node.newBuilder().setType(NodeType.LiteralType).build())
.build())
.build())
.build();
Node protobufNode = Node.newBuilder()
.setType(NodeType.BlockStatementType)
.setBlockStatement(blockStatement)
.build();

ESTree.Node estree = ESTreeFactory.from(protobufNode, ESTree.Node.class);
assertThat(estree).isInstanceOfSatisfying(ESTree.BlockStatement.class, block -> {
assertThat(block.body()).hasSize(1);
assertThat(block.body().get(0)).isInstanceOfSatisfying(ESTree.Directive.class, directive -> {
assertThat(directive.directive()).isEqualTo("directiveName");
});
});
}

@Test
void throw_exception_from_unrecognized_type() {
Node protobufNode = Node.newBuilder()
Expand Down
Loading