From 09650e4f62dfceaf6ba7668ee54ec02eb586f4ed Mon Sep 17 00:00:00 2001 From: Quentin Jaquier Date: Tue, 18 Jun 2024 12:04:48 +0200 Subject: [PATCH] JS-188 Directive can be used as a Statement --- .../plugins/javascript/api/estree/ESTree.java | 2 +- .../javascript/bridge/ESTreeFactoryTest.java | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/sonar-plugin/api/src/main/java/org/sonar/plugins/javascript/api/estree/ESTree.java b/sonar-plugin/api/src/main/java/org/sonar/plugins/javascript/api/estree/ESTree.java index a0807af5f75..08e18159850 100644 --- a/sonar-plugin/api/src/main/java/org/sonar/plugins/javascript/api/estree/ESTree.java +++ b/sonar-plugin/api/src/main/java/org/sonar/plugins/javascript/api/estree/ESTree.java @@ -104,7 +104,7 @@ public record ClassExpression(Location loc, Optional id, Optional label) implements Statement {} public record DebuggerStatement(Location loc) implements Statement {} - public record Directive(Location loc, Literal expression, String directive) implements DirectiveOrModuleDeclarationOrStatement {} + 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 exported, Literal source) implements ModuleDeclaration {} diff --git a/sonar-plugin/bridge/src/test/java/org/sonar/plugins/javascript/bridge/ESTreeFactoryTest.java b/sonar-plugin/bridge/src/test/java/org/sonar/plugins/javascript/bridge/ESTreeFactoryTest.java index c54b6326783..21003e2b923 100644 --- a/sonar-plugin/bridge/src/test/java/org/sonar/plugins/javascript/bridge/ESTreeFactoryTest.java +++ b/sonar-plugin/bridge/src/test/java/org/sonar/plugins/javascript/bridge/ESTreeFactoryTest.java @@ -562,6 +562,31 @@ void should_create_empty_statement_type() { assertThat(estree).isInstanceOf(ESTree.EmptyStatement.class); } + @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()