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-193 Add missing body property to static block #4741

Merged
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
6 changes: 4 additions & 2 deletions packages/jsts/src/parsers/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions packages/jsts/src/parsers/estree.proto
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ message ClassBody {
repeated Node body = 1;
}
message StaticBlock {
repeated Node body = 1;
}
message PropertyDefinition {
Node key = 1;
Expand Down
5 changes: 5 additions & 0 deletions packages/jsts/tests/parsers/fixtures/ast/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,3 +365,8 @@ num--;

const [fooA, fooB] = foo;

class FooStatic {
static {
console.log('static block');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public record SequenceExpression(Location loc, List<Expression> expressions) imp
public record SimpleCallExpression(Location loc, boolean optional, ExpressionOrSuper callee, List<ExpressionOrSpreadElement> 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<Statement> body) implements MethodDefinitionOrPropertyDefinitionOrStaticBlock, Statement {}
public record Super(Location loc) implements ExpressionOrSuper {}
public record SwitchCase(Location loc, Optional<Expression> test, List<Statement> consequent) implements Node {}
public record SwitchStatement(Location loc, Expression discriminant, List<SwitchCase> cases) implements Statement {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions sonar-plugin/bridge/src/main/protobuf/estree.proto
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ message ClassBody {
repeated Node body = 1;
}
message StaticBlock {
repeated Node body = 1;
}
message PropertyDefinition {
Node key = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions tools/estree/output/estree.proto
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ message ClassBody {
repeated Node body = 1;
}
message StaticBlock {
repeated Node body = 1;
}
message PropertyDefinition {
Node key = 1;
Expand Down
Loading