diff --git a/packages/jsts/src/parsers/ast.ts b/packages/jsts/src/parsers/ast.ts index 12f4ad3138a..e6509e301b7 100644 --- a/packages/jsts/src/parsers/ast.ts +++ b/packages/jsts/src/parsers/ast.ts @@ -452,8 +452,10 @@ export function visitNode(node: estree.BaseNodeWithoutComments | undefined | nul }; } - function visitStaticBlock(_node: estree.StaticBlock) { - return {}; + function visitStaticBlock(node: estree.StaticBlock) { + return { + body: node.body.map(visitNode), + }; } function visitPropertyDefinition(node: estree.PropertyDefinition) { diff --git a/packages/jsts/src/parsers/estree.proto b/packages/jsts/src/parsers/estree.proto index 2c1e7358a9e..1f1f2787db1 100644 --- a/packages/jsts/src/parsers/estree.proto +++ b/packages/jsts/src/parsers/estree.proto @@ -288,6 +288,7 @@ message ClassBody { repeated Node body = 1; } message StaticBlock { + repeated Node body = 1; } message PropertyDefinition { Node key = 1; diff --git a/packages/jsts/tests/parsers/fixtures/ast/base.js b/packages/jsts/tests/parsers/fixtures/ast/base.js index 9f6240fc105..be4e1c0ded1 100644 --- a/packages/jsts/tests/parsers/fixtures/ast/base.js +++ b/packages/jsts/tests/parsers/fixtures/ast/base.js @@ -365,3 +365,8 @@ num--; const [fooA, fooB] = foo; +class FooStatic { + static { + console.log('static block'); + } +} 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..2834ac1e13e 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 @@ -150,7 +150,7 @@ public record SequenceExpression(Location loc, List expressions) imp public record SimpleCallExpression(Location loc, boolean optional, ExpressionOrSuper callee, List arguments) implements CallExpression, ChainElement {} public record SimpleLiteral(Location loc, Object value, String raw) implements Literal {} public record SpreadElement(Location loc, Expression argument) implements ExpressionOrSpreadElement, PropertyOrSpreadElement {} - public record StaticBlock(Location loc) implements MethodDefinitionOrPropertyDefinitionOrStaticBlock, Statement {} + public record StaticBlock(Location loc, List body) implements MethodDefinitionOrPropertyDefinitionOrStaticBlock, Statement {} public record Super(Location loc) implements ExpressionOrSuper {} public record SwitchCase(Location loc, Optional test, List consequent) implements Node {} public record SwitchStatement(Location loc, Expression discriminant, List cases) implements Statement {} diff --git a/sonar-plugin/bridge/src/main/java/org/sonar/plugins/javascript/bridge/ESTreeFactory.java b/sonar-plugin/bridge/src/main/java/org/sonar/plugins/javascript/bridge/ESTreeFactory.java index 287b57e223d..1974f23bb97 100644 --- a/sonar-plugin/bridge/src/main/java/org/sonar/plugins/javascript/bridge/ESTreeFactory.java +++ b/sonar-plugin/bridge/src/main/java/org/sonar/plugins/javascript/bridge/ESTreeFactory.java @@ -77,6 +77,7 @@ import org.sonar.plugins.javascript.bridge.protobuf.SequenceExpression; import org.sonar.plugins.javascript.bridge.protobuf.SourceLocation; import org.sonar.plugins.javascript.bridge.protobuf.SpreadElement; +import org.sonar.plugins.javascript.bridge.protobuf.StaticBlock; import org.sonar.plugins.javascript.bridge.protobuf.SwitchCase; import org.sonar.plugins.javascript.bridge.protobuf.SwitchStatement; import org.sonar.plugins.javascript.bridge.protobuf.TaggedTemplateExpression; @@ -393,7 +394,8 @@ private static ESTree.ClassBody fromClassBodyType(Node node) { } private static ESTree.StaticBlock fromStaticBlockType(Node node) { - return new ESTree.StaticBlock(fromLocation(node.getLoc())); + StaticBlock staticBlock = node.getStaticBlock(); + return new ESTree.StaticBlock(fromLocation(node.getLoc()), from(staticBlock.getBodyList(), ESTree.Statement.class)); } private static ESTree.PropertyDefinition fromPropertyDefinitionType(Node node) { diff --git a/sonar-plugin/bridge/src/main/protobuf/estree.proto b/sonar-plugin/bridge/src/main/protobuf/estree.proto index 2c1e7358a9e..1f1f2787db1 100644 --- a/sonar-plugin/bridge/src/main/protobuf/estree.proto +++ b/sonar-plugin/bridge/src/main/protobuf/estree.proto @@ -288,6 +288,7 @@ message ClassBody { repeated Node body = 1; } message StaticBlock { + repeated Node body = 1; } message PropertyDefinition { Node key = 1; 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..68385ca5793 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 @@ -47,6 +47,7 @@ import org.sonar.plugins.javascript.bridge.protobuf.Position; import org.sonar.plugins.javascript.bridge.protobuf.Program; import org.sonar.plugins.javascript.bridge.protobuf.SourceLocation; +import org.sonar.plugins.javascript.bridge.protobuf.StaticBlock; import org.sonar.plugins.javascript.bridge.protobuf.UnaryExpression; import org.sonar.plugins.javascript.bridge.protobuf.UpdateExpression; import org.sonar.plugins.javascript.bridge.protobuf.WithStatement; @@ -562,6 +563,23 @@ void should_create_empty_statement_type() { assertThat(estree).isInstanceOf(ESTree.EmptyStatement.class); } + @Test + void should_create_static_block_type() { + StaticBlock staticBlock = StaticBlock.newBuilder() + .addBody(Node.newBuilder().setType(NodeType.EmptyStatementType).build()) + .build(); + Node protobufNode = Node.newBuilder() + .setType(NodeType.StaticBlockType) + .setStaticBlock(staticBlock) + .build(); + + ESTree.Node estree = ESTreeFactory.from(protobufNode, ESTree.Node.class); + assertThat(estree).isInstanceOfSatisfying(ESTree.StaticBlock.class, block -> { + assertThat(block.body().size()).isEqualTo(1); + assertThat(block.body().get(0)).isInstanceOf(ESTree.EmptyStatement.class); + }); + } + @Test void throw_exception_from_unrecognized_type() { Node protobufNode = Node.newBuilder() diff --git a/tools/estree/output/estree.proto b/tools/estree/output/estree.proto index 2c1e7358a9e..1f1f2787db1 100644 --- a/tools/estree/output/estree.proto +++ b/tools/estree/output/estree.proto @@ -288,6 +288,7 @@ message ClassBody { repeated Node body = 1; } message StaticBlock { + repeated Node body = 1; } message PropertyDefinition { Node key = 1;