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-280 Support unknown nodes in AST protobuf file #4786

Merged
merged 9 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions its/plugin/sonarlint-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<artifactId>javascript-it-plugin-sonarlint-tests</artifactId>
<name>JavaScript :: IT :: Plugin :: SonarLint Tests</name>
<packaging>pom</packaging>

<properties>
<surefire.argLine>-server</surefire.argLine>
Expand Down
29 changes: 23 additions & 6 deletions packages/jsts/src/parsers/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { debug } from '@sonar/shared';
const PATH_TO_PROTOFILE = path.join(__dirname, 'estree.proto');
const PROTO_ROOT = protobuf.loadSync(PATH_TO_PROTOFILE);
const NODE_TYPE = PROTO_ROOT.lookupType('Node');
const NODE_TYPE_ENUM = PROTO_ROOT.lookupEnum('NodeType');
export const NODE_TYPE_ENUM = PROTO_ROOT.lookupEnum('NodeType');

export function serializeInProtobuf(ast: AST.Program): Uint8Array {
const protobufAST = parseInProtobuf(ast);
Expand Down Expand Up @@ -56,11 +56,21 @@ export function visitNode(node: estree.BaseNodeWithoutComments | undefined | nul
return undefined;
}

return {
type: NODE_TYPE_ENUM.values[node.type + 'Type'],
loc: node.loc,
[lowerCaseFirstLetter(node.type)]: getProtobufShapeForNode(node),
};
const protoType = NODE_TYPE_ENUM.values[node.type + 'Type'];

if (typeof protoType !== 'undefined') {
return {
type: protoType,
loc: node.loc,
[lowerCaseFirstLetter(node.type)]: getProtobufShapeForNode(node),
};
} else {
return {
type: NODE_TYPE_ENUM.values['UnknownNodeType'],
loc: node.loc,
unknownNode: getProtobufShapeForNode(node),
};
}

function lowerCaseFirstLetter(str: string) {
return str.charAt(0).toLowerCase() + str.slice(1);
Expand Down Expand Up @@ -216,6 +226,7 @@ export function visitNode(node: estree.BaseNodeWithoutComments | undefined | nul
return visitFunctionExpression(node as estree.FunctionExpression);
default:
debug(`Unknown node type: ${node.type}`);
return visitUnknownNode(node);
}
}

Expand Down Expand Up @@ -763,4 +774,10 @@ export function visitNode(node: estree.BaseNodeWithoutComments | undefined | nul
async: node.async,
};
}

function visitUnknownNode(node: estree.BaseNodeWithoutComments) {
return {
astNodeType: node.type,
};
}
}
6 changes: 6 additions & 0 deletions packages/jsts/src/parsers/estree.proto
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ enum NodeType {
LiteralType = 68;
TemplateElementType = 69;
FunctionExpressionType = 70;
UnknownNodeType = 1000;
}
message Node {
NodeType type = 1;
Expand Down Expand Up @@ -163,6 +164,7 @@ message Node {
Literal literal = 71;
TemplateElement templateElement = 72;
FunctionExpression functionExpression = 73;
UnknownNode unknownNode = 1000;
}
}
message Program {
Expand Down Expand Up @@ -467,3 +469,7 @@ message FunctionExpression {
optional bool generator = 4;
optional bool async = 5;
}

message UnknownNode {
string astNodeType = 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

WDYT?

Suggested change
string astNodeType = 1;
string rawType = 1;

Copy link
Contributor

Choose a reason for hiding this comment

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

if you accept this change, you will have to adapt the other estree.proto files

Copy link
Contributor Author

Choose a reason for hiding this comment

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

btw, why so many repeated proto files? I think one should be source of truth

Copy link
Contributor

Choose a reason for hiding this comment

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

they are repeated because we didn't want to create coupling between the different modules. If we can find a better solution, it would be nice. See https://sonarsource.atlassian.net/browse/JS-197

Copy link
Contributor

Choose a reason for hiding this comment

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

As discussed, we will remove the deprecated scripts in tools/estree and only keep a script similar to copy-to-lib.js, named copy-to-plugin.js that copies the original in the bridge to the copy in the plugin.

We should also adapt the header comments in the protofile to identify:

  • which one is the original
  • which one the copy
  • by what script they are maintained in sync

}
13 changes: 13 additions & 0 deletions packages/jsts/tests/parsers/ast.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ import {
deserializeProtobuf,
parseInProtobuf,
serializeInProtobuf,
NODE_TYPE_ENUM,
} from '../../src/parsers';
import { JsTsAnalysisInput } from '../../src/analysis';
import fs from 'fs';

const parseFunctions = [
{
Expand Down Expand Up @@ -62,6 +64,17 @@ describe('ast', () => {
},
);
});
test('should encode unknown nodes', async () => {
const filePath = path.join(__dirname, 'fixtures', 'ast', 'unknownNode.ts');
const sc = await parseSourceCode(filePath, parsers.typescript);
const protoMessage = parseInProtobuf(sc.ast);
fs.writeFileSync('JS-285-reproducer-ts-proto.json', JSON.stringify(protoMessage, null, 2));
vdiez marked this conversation as resolved.
Show resolved Hide resolved
const serialized = serializeInProtobuf(sc.ast);
fs.writeFileSync('JS-285-reproducer-ts.proto', serialized);
vdiez marked this conversation as resolved.
Show resolved Hide resolved
expect((protoMessage as any).program.body[0].ifStatement.test.type).toEqual(
NODE_TYPE_ENUM.values['UnknownNodeType'],
);
});
});

/**
Expand Down
3 changes: 3 additions & 0 deletions packages/jsts/tests/parsers/fixtures/ast/unknownNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if ("ts" as Object) {
vdiez marked this conversation as resolved.
Show resolved Hide resolved
console.log("");
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ public record VariableDeclarator(Location loc, Pattern id, Optional<Expression>
public record WhileStatement(Location loc, Expression test, Statement body) implements Statement {}
public record WithStatement(Location loc, Expression object, Statement body) implements Statement {}
public record YieldExpression(Location loc, Optional<Expression> argument, boolean delegate) implements Expression {}
public record UnknownNode(Location loc, String astNodeType) implements Expression {}

public interface Operator {
String raw();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package org.sonar.plugins.javascript.api.estree;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Arrays;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class ESTreeTest {

@Test
void test() {

Class<?>[] classes = ESTree.class.getDeclaredClasses();
assertThat(classes).hasSize(106);
assertThat(classes).hasSize(107);

//filter all classes that are interface
var ifaceCount = Arrays.stream(classes).filter(Class::isInterface).count();
assertThat(ifaceCount).isEqualTo(25);

var recordCount = Arrays.stream(classes).filter(Class::isRecord).count();
assertThat(recordCount).isEqualTo(76);
assertThat(recordCount).isEqualTo(77);
}

@Test
Expand All @@ -32,7 +32,7 @@ void test_node_subclasses() {
void test_expression_subclasses() {
Class<?> sealedClass = ESTree.Expression.class;
Class<?>[] permittedSubclasses = sealedClass.getPermittedSubclasses();
assertThat(permittedSubclasses).hasSize(24);
assertThat(permittedSubclasses).hasSize(25);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
import org.sonar.plugins.javascript.bridge.protobuf.ThrowStatement;
import org.sonar.plugins.javascript.bridge.protobuf.TryStatement;
import org.sonar.plugins.javascript.bridge.protobuf.UnaryExpression;
import org.sonar.plugins.javascript.bridge.protobuf.UnknownNode;
import org.sonar.plugins.javascript.bridge.protobuf.UpdateExpression;
import org.sonar.plugins.javascript.bridge.protobuf.VariableDeclaration;
import org.sonar.plugins.javascript.bridge.protobuf.VariableDeclarator;
Expand Down Expand Up @@ -179,6 +180,7 @@ public static <T> T from(Node node, Class<T> clazz) {
case LiteralType -> fromLiteralType(node);
case TemplateElementType -> fromTemplateElementType(node);
case FunctionExpressionType -> fromFunctionExpressionType(node);
case UnknownNodeType -> fromUnknownNodeType(node);
case UNRECOGNIZED ->
throw new IllegalArgumentException("Unknown node type: " + node.getType() + " at " + node.getLoc());
};
Expand Down Expand Up @@ -724,4 +726,9 @@ private static ESTree.FunctionExpression fromFunctionExpressionType(Node node) {
functionExpression.getAsync());
}

private static ESTree.UnknownNode fromUnknownNodeType(Node node) {
UnknownNode unknownNode = node.getUnknownNode();
return new ESTree.UnknownNode(fromLocation(node.getLoc()), unknownNode.getAstNodeType());
}

}
6 changes: 6 additions & 0 deletions sonar-plugin/bridge/src/main/protobuf/estree.proto
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ enum NodeType {
LiteralType = 68;
TemplateElementType = 69;
FunctionExpressionType = 70;
UnknownNodeType = 1000;
}
message Node {
NodeType type = 1;
Expand Down Expand Up @@ -163,6 +164,7 @@ message Node {
Literal literal = 71;
TemplateElement templateElement = 72;
FunctionExpression functionExpression = 73;
UnknownNode unknownNode = 1000;
}
}
message Program {
Expand Down Expand Up @@ -467,3 +469,7 @@ message FunctionExpression {
optional bool generator = 4;
optional bool async = 5;
}

message UnknownNode {
string astNodeType = 1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
*/
package org.sonar.plugins.javascript.bridge;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
Expand Down Expand Up @@ -57,11 +60,30 @@
import org.sonar.plugins.javascript.bridge.protobuf.UpdateExpression;
import org.sonar.plugins.javascript.bridge.protobuf.WithStatement;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;

class ESTreeFactoryTest {

@Test
void should_not_fail_with_unknown_nodes() throws IOException {
// the clear version of serialized.proto is `packages/jsts/tests/parsers/fixtures/ast/JS-285-reproducer.ts`,
vdiez marked this conversation as resolved.
Show resolved Hide resolved
// it was generated by writing to a file the serialized data in the test `packages/jsts/tests/parsers/ast.test.ts`
File file = Path.of("src", "test", "resources", "files", "unknown.proto").toFile();

Node node;
try (FileInputStream fis = new FileInputStream(file)) {
node = Node.parseFrom(fis);
}
ESTree.Node root = ESTreeFactory.from(node, ESTree.Node.class);
assertThat(root).isInstanceOf(ESTree.Program.class);
ESTree.Program program = (ESTree.Program) root;
assertThat(program.body()).hasSize(1);
assertThat(program.body().get(0)).isInstanceOfSatisfying(ESTree.IfStatement.class, ifStatement -> {
assertThat(ifStatement.test()).isInstanceOf(ESTree.UnknownNode.class);
ESTree.UnknownNode unknownNode = (ESTree.UnknownNode)ifStatement.test();
assertThat(unknownNode.astNodeType()).isEqualTo("TSAsExpression");

vdiez marked this conversation as resolved.
Show resolved Hide resolved
});
}

@Test
void should_create_nodes_from_serialized_data() throws IOException {
// the clear version of serialized.proto is `packages/jsts/tests/parsers/fixtures/ast/base.js`,
Expand Down
Binary file not shown.
6 changes: 6 additions & 0 deletions tools/estree/output/estree.proto
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ enum NodeType {
LiteralType = 68;
TemplateElementType = 69;
FunctionExpressionType = 70;
UnknownNodeType = 1000;
}
message Node {
NodeType type = 1;
Expand Down Expand Up @@ -163,6 +164,7 @@ message Node {
Literal literal = 71;
TemplateElement templateElement = 72;
FunctionExpression functionExpression = 73;
UnknownNode unknownNode = 1000;
}
}
message Program {
Expand Down Expand Up @@ -467,3 +469,7 @@ message FunctionExpression {
optional bool generator = 4;
optional bool async = 5;
}

message UnknownNode {
string astNodeType = 1;
}
Loading