From 6ed99cff3b091ca86d368c7cf365e1f8e50a93da Mon Sep 17 00:00:00 2001 From: Tibor Blenessy Date: Tue, 4 Jun 2024 14:29:07 +0200 Subject: [PATCH 01/10] JS-158 Geneate Java API for ESTree --- .../plugins/javascript/api/estree/ESTree.java | 122 ++++++++++++++++++ .../javascript/api/estree/ESTreeTest.java | 15 +++ tools/estree/generate-java-ast.ts | 55 ++++++-- 3 files changed, 184 insertions(+), 8 deletions(-) create mode 100644 sonar-plugin/api/src/main/java/org/sonar/plugins/javascript/api/estree/ESTree.java create mode 100644 sonar-plugin/api/src/test/java/org/sonar/plugins/javascript/api/estree/ESTreeTest.java 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 new file mode 100644 index 00000000000..cf311ddb375 --- /dev/null +++ b/sonar-plugin/api/src/main/java/org/sonar/plugins/javascript/api/estree/ESTree.java @@ -0,0 +1,122 @@ +/* + * SonarQube JavaScript Plugin + * Copyright (C) 2011-2023 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.plugins.javascript.api.estree; + + +public class ESTree { + + private ESTree() { + // shouldn't be instantiated + } + + sealed interface Node { + + } + public record Program(String sourceType, Node body) implements Node {} + public record ModuleDeclaration(Node moduleDeclaration) implements Node {} + public record ExportAllDeclaration(Identifier exported, Literal source) implements Node {} + public record Literal(Node literal) implements Node {} + public record BigIntLiteral(int value, String bigint, String raw) implements Node {} + public record SimpleLiteral(Node value, String raw) implements Node {} + public record Identifier(String name) implements Node {} + public record ExportDefaultDeclaration(Node declaration) implements Node {} + public record Expression(Node expression) implements Node {} + public record YieldExpression(Expression argument, boolean delegate) implements Node {} + public record UpdateExpression(UpdateOperator operator, Expression argument, boolean prefix) implements Node {} + public record UpdateOperator(String updateOperator) implements Node {} + public record UnaryExpression(UnaryOperator operator, boolean prefix, Expression argument) implements Node {} + public record UnaryOperator(String unaryOperator) implements Node {} + public record ThisExpression() implements Node {} + public record TemplateLiteral(Node quasis, Node expressions) implements Node {} + public record TaggedTemplateExpression(Expression tag, TemplateLiteral quasi) implements Node {} + public record SequenceExpression(Node expressions) implements Node {} + public record ObjectExpression(Node properties) implements Node {} + public record SpreadElement(Expression argument) implements Node {} + public record Property(Node key, Node value, String kind, boolean method, boolean shorthand, boolean computed) implements Node {} + public record Pattern(Node pattern) implements Node {} + public record AssignmentPattern(Pattern left, Expression right) implements Node {} + public record RestElement(Pattern argument) implements Node {} + public record ArrayPattern(Node elements) implements Node {} + public record ObjectPattern(Node properties) implements Node {} + public record AssignmentProperty(Pattern value, String kind, boolean method, Node key, boolean shorthand, boolean computed) implements Node {} + public record PrivateIdentifier(String name) implements Node {} + public record NewExpression(Node callee, Node arguments) implements Node {} + public record Super() implements Node {} + public record MetaProperty(Identifier meta, Identifier property) implements Node {} + public record MemberExpression(Node object, Node property, boolean computed, boolean optional) implements Node {} + public record LogicalExpression(LogicalOperator operator, Expression left, Expression right) implements Node {} + public record LogicalOperator(String logicalOperator) implements Node {} + public record ImportExpression(Expression source) implements Node {} + public record FunctionExpression(Identifier id, BlockStatement body, Node params, boolean generator, boolean async) implements Node {} + public record BlockStatement(Node body) implements Node {} + public record ConditionalExpression(Expression test, Expression alternate, Expression consequent) implements Node {} + public record ClassExpression(Identifier id, Expression superClass, ClassBody body) implements Node {} + public record ClassBody(Node body) implements Node {} + public record StaticBlock() implements Node {} + public record PropertyDefinition(Node key, Expression value, boolean computed, boolean isStatic) implements Node {} + public record MethodDefinition(Node key, FunctionExpression value, String kind, boolean computed, boolean isStatic) implements Node {} + public record ChainExpression(ChainElement expression) implements Node {} + public record ChainElement(Node chainElement) implements Node {} + public record SimpleCallExpression(boolean optional, Node callee, Node arguments) implements Node {} + public record CallExpression(Node callExpression) implements Node {} + public record BinaryExpression(BinaryOperator operator, Expression left, Expression right) implements Node {} + public record BinaryOperator(String binaryOperator) implements Node {} + public record AwaitExpression(Expression argument) implements Node {} + public record AssignmentExpression(AssignmentOperator operator, Node left, Expression right) implements Node {} + public record AssignmentOperator(String assignmentOperator) implements Node {} + public record ArrowFunctionExpression(boolean expression, Node body, Node params, boolean generator, boolean async) implements Node {} + public record ArrayExpression(Node elements) implements Node {} + public record MaybeNamedClassDeclaration(Identifier id, Expression superClass, ClassBody body) implements Node {} + public record MaybeNamedFunctionDeclaration(Identifier id, BlockStatement body, Node params, boolean generator, boolean async) implements Node {} + public record ExportNamedDeclaration(Declaration declaration, Node specifiers, Literal source) implements Node {} + public record ExportSpecifier(Identifier exported, Identifier local) implements Node {} + public record Declaration(Node declaration) implements Node {} + public record ClassDeclaration(Identifier id, Expression superClass, ClassBody body) implements Node {} + public record VariableDeclaration(Node declarations, String kind) implements Node {} + public record VariableDeclarator(Pattern id, Expression init) implements Node {} + public record FunctionDeclaration(Identifier id, BlockStatement body, Node params, boolean generator, boolean async) implements Node {} + public record ImportDeclaration(Node specifiers, Literal source) implements Node {} + public record ImportNamespaceSpecifier(Identifier local) implements Node {} + public record ImportDefaultSpecifier(Identifier local) implements Node {} + public record ImportSpecifier(Identifier imported, Identifier local) implements Node {} + public record Statement(Node statement) implements Node {} + public record ForOfStatement(boolean await, Node left, Expression right, Statement body) implements Node {} + public record ForInStatement(Node left, Expression right, Statement body) implements Node {} + public record ForStatement(Node init, Expression test, Expression update, Statement body) implements Node {} + public record DoWhileStatement(Statement body, Expression test) implements Node {} + public record WhileStatement(Expression test, Statement body) implements Node {} + public record TryStatement(BlockStatement block, CatchClause handler, BlockStatement finalizer) implements Node {} + public record CatchClause(Pattern param, BlockStatement body) implements Node {} + public record ThrowStatement(Expression argument) implements Node {} + public record SwitchStatement(Expression discriminant, Node cases) implements Node {} + public record SwitchCase(Expression test, Node consequent) implements Node {} + public record IfStatement(Expression test, Statement consequent, Statement alternate) implements Node {} + public record ContinueStatement(Identifier label) implements Node {} + public record BreakStatement(Identifier label) implements Node {} + public record LabeledStatement(Identifier label, Statement body) implements Node {} + public record ReturnStatement(Expression argument) implements Node {} + public record WithStatement(Expression object, Statement body) implements Node {} + public record DebuggerStatement() implements Node {} + public record EmptyStatement() implements Node {} + public record ExpressionStatement(Expression expression) implements Node {} + public record Directive(Literal expression, String directive) implements Node {} +} + diff --git a/sonar-plugin/api/src/test/java/org/sonar/plugins/javascript/api/estree/ESTreeTest.java b/sonar-plugin/api/src/test/java/org/sonar/plugins/javascript/api/estree/ESTreeTest.java new file mode 100644 index 00000000000..2d96a975230 --- /dev/null +++ b/sonar-plugin/api/src/test/java/org/sonar/plugins/javascript/api/estree/ESTreeTest.java @@ -0,0 +1,15 @@ +package org.sonar.plugins.javascript.api.estree; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class ESTreeTest { + + @Test + void test() { + var p = new ESTree.Program("", null); + + } + +} diff --git a/tools/estree/generate-java-ast.ts b/tools/estree/generate-java-ast.ts index 204f179a304..3b6977ae532 100644 --- a/tools/estree/generate-java-ast.ts +++ b/tools/estree/generate-java-ast.ts @@ -19,7 +19,7 @@ */ import fs from 'node:fs'; -import { ESTreeNode } from './get-estree-nodes'; +import { ESTreeNode, NodeField } from './get-estree-nodes'; const HEADER = `/* * SonarQube JavaScript Plugin @@ -42,16 +42,55 @@ const HEADER = `/* */ `; -export function writeJavaClassesToDir(messages: Record, output: string) { - for (const [name, message] of Object.entries(messages)) { - const filename = `${output}/${name}.java`; - const content = `${HEADER} +export function writeJavaClassesToDir(nodes: Record, output: string) { + const records = []; + for (const [name, node] of Object.entries(nodes)) { + const fields = []; + for (const field of node.fields) { + fields.push(`${javaType(field)} ${javaName(field.name)}`); + } + records.push(` public record ${name}(${fields.join(', ')}) implements Node {}`); + } + + const estree = `${HEADER} package org.sonar.plugins.javascript.api.estree; -public record ${name}( - ) { + +public class ESTree { + + private ESTree() { + // shouldn't be instantiated + } + + sealed interface Node { + + } +${records.join('\n')} } + `; - fs.writeFileSync(filename, content, 'utf-8'); + fs.writeFileSync('output/ESTree.java', estree, 'utf-8'); +} + +function javaType(field: NodeField) { + const { fieldValue } = field; + if ('type' in fieldValue) { + switch (fieldValue.type) { + case 'string': + return 'String'; + case 'int32': + return 'int'; + case 'bool': + return 'boolean'; + } + return fieldValue.type; + } + return 'Node'; +} + +function javaName(name: string) { + if (name === 'static') { + return 'isStatic'; } + return name; } From 9a6da5433a72634eac9ef5af8e4b56a26ce5b7fc Mon Sep 17 00:00:00 2001 From: Tibor Blenessy Date: Tue, 4 Jun 2024 15:00:31 +0200 Subject: [PATCH 02/10] add shared field --- .../plugins/javascript/api/estree/ESTree.java | 185 +++++++++--------- .../javascript/api/estree/ESTreeTest.java | 7 +- tools/estree/generate-java-ast.ts | 31 ++- 3 files changed, 125 insertions(+), 98 deletions(-) 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 cf311ddb375..d0637eb3bbe 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 @@ -20,6 +20,7 @@ package org.sonar.plugins.javascript.api.estree; +import java.util.List; public class ESTree { @@ -28,95 +29,101 @@ private ESTree() { } sealed interface Node { - + + String type(); + Location loc(); } - public record Program(String sourceType, Node body) implements Node {} - public record ModuleDeclaration(Node moduleDeclaration) implements Node {} - public record ExportAllDeclaration(Identifier exported, Literal source) implements Node {} - public record Literal(Node literal) implements Node {} - public record BigIntLiteral(int value, String bigint, String raw) implements Node {} - public record SimpleLiteral(Node value, String raw) implements Node {} - public record Identifier(String name) implements Node {} - public record ExportDefaultDeclaration(Node declaration) implements Node {} - public record Expression(Node expression) implements Node {} - public record YieldExpression(Expression argument, boolean delegate) implements Node {} - public record UpdateExpression(UpdateOperator operator, Expression argument, boolean prefix) implements Node {} - public record UpdateOperator(String updateOperator) implements Node {} - public record UnaryExpression(UnaryOperator operator, boolean prefix, Expression argument) implements Node {} - public record UnaryOperator(String unaryOperator) implements Node {} - public record ThisExpression() implements Node {} - public record TemplateLiteral(Node quasis, Node expressions) implements Node {} - public record TaggedTemplateExpression(Expression tag, TemplateLiteral quasi) implements Node {} - public record SequenceExpression(Node expressions) implements Node {} - public record ObjectExpression(Node properties) implements Node {} - public record SpreadElement(Expression argument) implements Node {} - public record Property(Node key, Node value, String kind, boolean method, boolean shorthand, boolean computed) implements Node {} - public record Pattern(Node pattern) implements Node {} - public record AssignmentPattern(Pattern left, Expression right) implements Node {} - public record RestElement(Pattern argument) implements Node {} - public record ArrayPattern(Node elements) implements Node {} - public record ObjectPattern(Node properties) implements Node {} - public record AssignmentProperty(Pattern value, String kind, boolean method, Node key, boolean shorthand, boolean computed) implements Node {} - public record PrivateIdentifier(String name) implements Node {} - public record NewExpression(Node callee, Node arguments) implements Node {} - public record Super() implements Node {} - public record MetaProperty(Identifier meta, Identifier property) implements Node {} - public record MemberExpression(Node object, Node property, boolean computed, boolean optional) implements Node {} - public record LogicalExpression(LogicalOperator operator, Expression left, Expression right) implements Node {} - public record LogicalOperator(String logicalOperator) implements Node {} - public record ImportExpression(Expression source) implements Node {} - public record FunctionExpression(Identifier id, BlockStatement body, Node params, boolean generator, boolean async) implements Node {} - public record BlockStatement(Node body) implements Node {} - public record ConditionalExpression(Expression test, Expression alternate, Expression consequent) implements Node {} - public record ClassExpression(Identifier id, Expression superClass, ClassBody body) implements Node {} - public record ClassBody(Node body) implements Node {} - public record StaticBlock() implements Node {} - public record PropertyDefinition(Node key, Expression value, boolean computed, boolean isStatic) implements Node {} - public record MethodDefinition(Node key, FunctionExpression value, String kind, boolean computed, boolean isStatic) implements Node {} - public record ChainExpression(ChainElement expression) implements Node {} - public record ChainElement(Node chainElement) implements Node {} - public record SimpleCallExpression(boolean optional, Node callee, Node arguments) implements Node {} - public record CallExpression(Node callExpression) implements Node {} - public record BinaryExpression(BinaryOperator operator, Expression left, Expression right) implements Node {} - public record BinaryOperator(String binaryOperator) implements Node {} - public record AwaitExpression(Expression argument) implements Node {} - public record AssignmentExpression(AssignmentOperator operator, Node left, Expression right) implements Node {} - public record AssignmentOperator(String assignmentOperator) implements Node {} - public record ArrowFunctionExpression(boolean expression, Node body, Node params, boolean generator, boolean async) implements Node {} - public record ArrayExpression(Node elements) implements Node {} - public record MaybeNamedClassDeclaration(Identifier id, Expression superClass, ClassBody body) implements Node {} - public record MaybeNamedFunctionDeclaration(Identifier id, BlockStatement body, Node params, boolean generator, boolean async) implements Node {} - public record ExportNamedDeclaration(Declaration declaration, Node specifiers, Literal source) implements Node {} - public record ExportSpecifier(Identifier exported, Identifier local) implements Node {} - public record Declaration(Node declaration) implements Node {} - public record ClassDeclaration(Identifier id, Expression superClass, ClassBody body) implements Node {} - public record VariableDeclaration(Node declarations, String kind) implements Node {} - public record VariableDeclarator(Pattern id, Expression init) implements Node {} - public record FunctionDeclaration(Identifier id, BlockStatement body, Node params, boolean generator, boolean async) implements Node {} - public record ImportDeclaration(Node specifiers, Literal source) implements Node {} - public record ImportNamespaceSpecifier(Identifier local) implements Node {} - public record ImportDefaultSpecifier(Identifier local) implements Node {} - public record ImportSpecifier(Identifier imported, Identifier local) implements Node {} - public record Statement(Node statement) implements Node {} - public record ForOfStatement(boolean await, Node left, Expression right, Statement body) implements Node {} - public record ForInStatement(Node left, Expression right, Statement body) implements Node {} - public record ForStatement(Node init, Expression test, Expression update, Statement body) implements Node {} - public record DoWhileStatement(Statement body, Expression test) implements Node {} - public record WhileStatement(Expression test, Statement body) implements Node {} - public record TryStatement(BlockStatement block, CatchClause handler, BlockStatement finalizer) implements Node {} - public record CatchClause(Pattern param, BlockStatement body) implements Node {} - public record ThrowStatement(Expression argument) implements Node {} - public record SwitchStatement(Expression discriminant, Node cases) implements Node {} - public record SwitchCase(Expression test, Node consequent) implements Node {} - public record IfStatement(Expression test, Statement consequent, Statement alternate) implements Node {} - public record ContinueStatement(Identifier label) implements Node {} - public record BreakStatement(Identifier label) implements Node {} - public record LabeledStatement(Identifier label, Statement body) implements Node {} - public record ReturnStatement(Expression argument) implements Node {} - public record WithStatement(Expression object, Statement body) implements Node {} - public record DebuggerStatement() implements Node {} - public record EmptyStatement() implements Node {} - public record ExpressionStatement(Expression expression) implements Node {} - public record Directive(Literal expression, String directive) implements Node {} + + public record Location(int startLine, int startCol, int endLine, int endCol) {} + + + public record Program(String type, Location loc, String sourceType, List body) implements Node {} + public record ModuleDeclaration(String type, Location loc, Node moduleDeclaration) implements Node {} + public record ExportAllDeclaration(String type, Location loc, Identifier exported, Literal source) implements Node {} + public record Literal(String type, Location loc, Node literal) implements Node {} + public record BigIntLiteral(String type, Location loc, int value, String bigint, String raw) implements Node {} + public record SimpleLiteral(String type, Location loc, Node value, String raw) implements Node {} + public record Identifier(String type, Location loc, String name) implements Node {} + public record ExportDefaultDeclaration(String type, Location loc, Node declaration) implements Node {} + public record Expression(String type, Location loc, Node expression) implements Node {} + public record YieldExpression(String type, Location loc, Expression argument, boolean delegate) implements Node {} + public record UpdateExpression(String type, Location loc, UpdateOperator operator, Expression argument, boolean prefix) implements Node {} + public record UpdateOperator(String type, Location loc, String updateOperator) implements Node {} + public record UnaryExpression(String type, Location loc, UnaryOperator operator, boolean prefix, Expression argument) implements Node {} + public record UnaryOperator(String type, Location loc, String unaryOperator) implements Node {} + public record ThisExpression(String type, Location loc) implements Node {} + public record TemplateLiteral(String type, Location loc, List quasis, List expressions) implements Node {} + public record TaggedTemplateExpression(String type, Location loc, Expression tag, TemplateLiteral quasi) implements Node {} + public record SequenceExpression(String type, Location loc, List expressions) implements Node {} + public record ObjectExpression(String type, Location loc, List properties) implements Node {} + public record SpreadElement(String type, Location loc, Expression argument) implements Node {} + public record Property(String type, Location loc, Node key, Node value, String kind, boolean method, boolean shorthand, boolean computed) implements Node {} + public record Pattern(String type, Location loc, Node pattern) implements Node {} + public record AssignmentPattern(String type, Location loc, Pattern left, Expression right) implements Node {} + public record RestElement(String type, Location loc, Pattern argument) implements Node {} + public record ArrayPattern(String type, Location loc, List elements) implements Node {} + public record ObjectPattern(String type, Location loc, List properties) implements Node {} + public record AssignmentProperty(String type, Location loc, Pattern value, String kind, boolean method, Node key, boolean shorthand, boolean computed) implements Node {} + public record PrivateIdentifier(String type, Location loc, String name) implements Node {} + public record NewExpression(String type, Location loc, Node callee, List arguments) implements Node {} + public record Super(String type, Location loc) implements Node {} + public record MetaProperty(String type, Location loc, Identifier meta, Identifier property) implements Node {} + public record MemberExpression(String type, Location loc, Node object, Node property, boolean computed, boolean optional) implements Node {} + public record LogicalExpression(String type, Location loc, LogicalOperator operator, Expression left, Expression right) implements Node {} + public record LogicalOperator(String type, Location loc, String logicalOperator) implements Node {} + public record ImportExpression(String type, Location loc, Expression source) implements Node {} + public record FunctionExpression(String type, Location loc, Identifier id, BlockStatement body, List params, boolean generator, boolean async) implements Node {} + public record BlockStatement(String type, Location loc, List body) implements Node {} + public record ConditionalExpression(String type, Location loc, Expression test, Expression alternate, Expression consequent) implements Node {} + public record ClassExpression(String type, Location loc, Identifier id, Expression superClass, ClassBody body) implements Node {} + public record ClassBody(String type, Location loc, List body) implements Node {} + public record StaticBlock(String type, Location loc) implements Node {} + public record PropertyDefinition(String type, Location loc, Node key, Expression value, boolean computed, boolean isStatic) implements Node {} + public record MethodDefinition(String type, Location loc, Node key, FunctionExpression value, String kind, boolean computed, boolean isStatic) implements Node {} + public record ChainExpression(String type, Location loc, ChainElement expression) implements Node {} + public record ChainElement(String type, Location loc, Node chainElement) implements Node {} + public record SimpleCallExpression(String type, Location loc, boolean optional, Node callee, List arguments) implements Node {} + public record CallExpression(String type, Location loc, Node callExpression) implements Node {} + public record BinaryExpression(String type, Location loc, BinaryOperator operator, Expression left, Expression right) implements Node {} + public record BinaryOperator(String type, Location loc, String binaryOperator) implements Node {} + public record AwaitExpression(String type, Location loc, Expression argument) implements Node {} + public record AssignmentExpression(String type, Location loc, AssignmentOperator operator, Node left, Expression right) implements Node {} + public record AssignmentOperator(String type, Location loc, String assignmentOperator) implements Node {} + public record ArrowFunctionExpression(String type, Location loc, boolean expression, Node body, List params, boolean generator, boolean async) implements Node {} + public record ArrayExpression(String type, Location loc, List elements) implements Node {} + public record MaybeNamedClassDeclaration(String type, Location loc, Identifier id, Expression superClass, ClassBody body) implements Node {} + public record MaybeNamedFunctionDeclaration(String type, Location loc, Identifier id, BlockStatement body, List params, boolean generator, boolean async) implements Node {} + public record ExportNamedDeclaration(String type, Location loc, Declaration declaration, List specifiers, Literal source) implements Node {} + public record ExportSpecifier(String type, Location loc, Identifier exported, Identifier local) implements Node {} + public record Declaration(String type, Location loc, Node declaration) implements Node {} + public record ClassDeclaration(String type, Location loc, Identifier id, Expression superClass, ClassBody body) implements Node {} + public record VariableDeclaration(String type, Location loc, List declarations, String kind) implements Node {} + public record VariableDeclarator(String type, Location loc, Pattern id, Expression init) implements Node {} + public record FunctionDeclaration(String type, Location loc, Identifier id, BlockStatement body, List params, boolean generator, boolean async) implements Node {} + public record ImportDeclaration(String type, Location loc, List specifiers, Literal source) implements Node {} + public record ImportNamespaceSpecifier(String type, Location loc, Identifier local) implements Node {} + public record ImportDefaultSpecifier(String type, Location loc, Identifier local) implements Node {} + public record ImportSpecifier(String type, Location loc, Identifier imported, Identifier local) implements Node {} + public record Statement(String type, Location loc, Node statement) implements Node {} + public record ForOfStatement(String type, Location loc, boolean await, Node left, Expression right, Statement body) implements Node {} + public record ForInStatement(String type, Location loc, Node left, Expression right, Statement body) implements Node {} + public record ForStatement(String type, Location loc, Node init, Expression test, Expression update, Statement body) implements Node {} + public record DoWhileStatement(String type, Location loc, Statement body, Expression test) implements Node {} + public record WhileStatement(String type, Location loc, Expression test, Statement body) implements Node {} + public record TryStatement(String type, Location loc, BlockStatement block, CatchClause handler, BlockStatement finalizer) implements Node {} + public record CatchClause(String type, Location loc, Pattern param, BlockStatement body) implements Node {} + public record ThrowStatement(String type, Location loc, Expression argument) implements Node {} + public record SwitchStatement(String type, Location loc, Expression discriminant, List cases) implements Node {} + public record SwitchCase(String type, Location loc, Expression test, List consequent) implements Node {} + public record IfStatement(String type, Location loc, Expression test, Statement consequent, Statement alternate) implements Node {} + public record ContinueStatement(String type, Location loc, Identifier label) implements Node {} + public record BreakStatement(String type, Location loc, Identifier label) implements Node {} + public record LabeledStatement(String type, Location loc, Identifier label, Statement body) implements Node {} + public record ReturnStatement(String type, Location loc, Expression argument) implements Node {} + public record WithStatement(String type, Location loc, Expression object, Statement body) implements Node {} + public record DebuggerStatement(String type, Location loc) implements Node {} + public record EmptyStatement(String type, Location loc) implements Node {} + public record ExpressionStatement(String type, Location loc, Expression expression) implements Node {} + public record Directive(String type, Location loc, Literal expression, String directive) implements Node {} } diff --git a/sonar-plugin/api/src/test/java/org/sonar/plugins/javascript/api/estree/ESTreeTest.java b/sonar-plugin/api/src/test/java/org/sonar/plugins/javascript/api/estree/ESTreeTest.java index 2d96a975230..ce71c1ec178 100644 --- a/sonar-plugin/api/src/test/java/org/sonar/plugins/javascript/api/estree/ESTreeTest.java +++ b/sonar-plugin/api/src/test/java/org/sonar/plugins/javascript/api/estree/ESTreeTest.java @@ -3,13 +3,18 @@ import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; +import org.sonar.plugins.javascript.api.estree.ESTree.Node; +import org.sonar.plugins.javascript.api.estree.ESTree.Program; class ESTreeTest { @Test void test() { - var p = new ESTree.Program("", null); + Node p = new Program("", null); + switch (p) { + + } } } diff --git a/tools/estree/generate-java-ast.ts b/tools/estree/generate-java-ast.ts index 3b6977ae532..c820d80f99a 100644 --- a/tools/estree/generate-java-ast.ts +++ b/tools/estree/generate-java-ast.ts @@ -19,7 +19,7 @@ */ import fs from 'node:fs'; -import { ESTreeNode, NodeField } from './get-estree-nodes'; +import { ESTreeNode, NodeField, NodeFieldValue } from './get-estree-nodes'; const HEADER = `/* * SonarQube JavaScript Plugin @@ -42,12 +42,23 @@ const HEADER = `/* */ `; +const NODE_INTERFACE = `sealed interface Node { + + String type(); + Location loc(); + } + + public record Location(int startLine, int startCol, int endLine, int endCol) {} +`; + +const SHARED_FIELDS = ['String type', 'Location loc']; + export function writeJavaClassesToDir(nodes: Record, output: string) { const records = []; for (const [name, node] of Object.entries(nodes)) { - const fields = []; + const fields = [...SHARED_FIELDS]; for (const field of node.fields) { - fields.push(`${javaType(field)} ${javaName(field.name)}`); + fields.push(`${javaType(field.fieldValue)} ${javaName(field.name)}`); } records.push(` public record ${name}(${fields.join(', ')}) implements Node {}`); } @@ -55,6 +66,7 @@ export function writeJavaClassesToDir(nodes: Record, output: const estree = `${HEADER} package org.sonar.plugins.javascript.api.estree; +import java.util.List; public class ESTree { @@ -62,9 +74,8 @@ public class ESTree { // shouldn't be instantiated } - sealed interface Node { - - } + ${NODE_INTERFACE} + ${records.join('\n')} } @@ -72,8 +83,7 @@ ${records.join('\n')} fs.writeFileSync('output/ESTree.java', estree, 'utf-8'); } -function javaType(field: NodeField) { - const { fieldValue } = field; +function javaType(fieldValue: NodeFieldValue): string { if ('type' in fieldValue) { switch (fieldValue.type) { case 'string': @@ -82,9 +92,14 @@ function javaType(field: NodeField) { return 'int'; case 'bool': return 'boolean'; + case 'BaseNodeWithoutComments': + return 'Node'; } return fieldValue.type; } + if ('elementValue' in fieldValue) { + return `List<${javaType(fieldValue.elementValue)}>`; + } return 'Node'; } From 36bb842c4c0c04ff5939792cc6e3a9a7f2d9ab78 Mon Sep 17 00:00:00 2001 From: Tibor Blenessy Date: Thu, 6 Jun 2024 09:46:29 +0200 Subject: [PATCH 03/10] wip --- .../plugins/javascript/api/estree/ESTree.java | 186 ++--- .../javascript/api/estree/ESTreeTest.java | 4 - sonar-plugin/bridge/pom.xml | 2 +- .../javascript/bridge/ESTreeFactory.java | 35 + .../bridge/src/main/protobuf/estree.proto | 719 ++++++++---------- tools/estree/generate-java-ast.ts | 48 +- tools/estree/generate-proto-file.ts | 39 +- tools/estree/get-estree-nodes.ts | 19 + tools/estree/main.ts | 2 +- tools/estree/output/ESTree.java | 131 ++++ tools/estree/output/estreeProto.proto | 692 +++++++++++++++++ 11 files changed, 1329 insertions(+), 548 deletions(-) create mode 100644 sonar-plugin/bridge/src/main/java/org/sonar/plugins/javascript/bridge/ESTreeFactory.java create mode 100644 tools/estree/output/ESTree.java create mode 100644 tools/estree/output/estreeProto.proto 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 d0637eb3bbe..dc183537583 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 @@ -28,102 +28,104 @@ private ESTree() { // shouldn't be instantiated } - sealed interface Node { + public sealed interface Node { - String type(); Location loc(); } - public record Location(int startLine, int startCol, int endLine, int endCol) {} - + public record Position(int line, int column) {} + public record Location(Position start, Position end) {} + - public record Program(String type, Location loc, String sourceType, List body) implements Node {} - public record ModuleDeclaration(String type, Location loc, Node moduleDeclaration) implements Node {} - public record ExportAllDeclaration(String type, Location loc, Identifier exported, Literal source) implements Node {} - public record Literal(String type, Location loc, Node literal) implements Node {} - public record BigIntLiteral(String type, Location loc, int value, String bigint, String raw) implements Node {} - public record SimpleLiteral(String type, Location loc, Node value, String raw) implements Node {} - public record Identifier(String type, Location loc, String name) implements Node {} - public record ExportDefaultDeclaration(String type, Location loc, Node declaration) implements Node {} - public record Expression(String type, Location loc, Node expression) implements Node {} - public record YieldExpression(String type, Location loc, Expression argument, boolean delegate) implements Node {} - public record UpdateExpression(String type, Location loc, UpdateOperator operator, Expression argument, boolean prefix) implements Node {} - public record UpdateOperator(String type, Location loc, String updateOperator) implements Node {} - public record UnaryExpression(String type, Location loc, UnaryOperator operator, boolean prefix, Expression argument) implements Node {} - public record UnaryOperator(String type, Location loc, String unaryOperator) implements Node {} - public record ThisExpression(String type, Location loc) implements Node {} - public record TemplateLiteral(String type, Location loc, List quasis, List expressions) implements Node {} - public record TaggedTemplateExpression(String type, Location loc, Expression tag, TemplateLiteral quasi) implements Node {} - public record SequenceExpression(String type, Location loc, List expressions) implements Node {} - public record ObjectExpression(String type, Location loc, List properties) implements Node {} - public record SpreadElement(String type, Location loc, Expression argument) implements Node {} - public record Property(String type, Location loc, Node key, Node value, String kind, boolean method, boolean shorthand, boolean computed) implements Node {} - public record Pattern(String type, Location loc, Node pattern) implements Node {} - public record AssignmentPattern(String type, Location loc, Pattern left, Expression right) implements Node {} - public record RestElement(String type, Location loc, Pattern argument) implements Node {} - public record ArrayPattern(String type, Location loc, List elements) implements Node {} - public record ObjectPattern(String type, Location loc, List properties) implements Node {} - public record AssignmentProperty(String type, Location loc, Pattern value, String kind, boolean method, Node key, boolean shorthand, boolean computed) implements Node {} - public record PrivateIdentifier(String type, Location loc, String name) implements Node {} - public record NewExpression(String type, Location loc, Node callee, List arguments) implements Node {} - public record Super(String type, Location loc) implements Node {} - public record MetaProperty(String type, Location loc, Identifier meta, Identifier property) implements Node {} - public record MemberExpression(String type, Location loc, Node object, Node property, boolean computed, boolean optional) implements Node {} - public record LogicalExpression(String type, Location loc, LogicalOperator operator, Expression left, Expression right) implements Node {} - public record LogicalOperator(String type, Location loc, String logicalOperator) implements Node {} - public record ImportExpression(String type, Location loc, Expression source) implements Node {} - public record FunctionExpression(String type, Location loc, Identifier id, BlockStatement body, List params, boolean generator, boolean async) implements Node {} - public record BlockStatement(String type, Location loc, List body) implements Node {} - public record ConditionalExpression(String type, Location loc, Expression test, Expression alternate, Expression consequent) implements Node {} - public record ClassExpression(String type, Location loc, Identifier id, Expression superClass, ClassBody body) implements Node {} - public record ClassBody(String type, Location loc, List body) implements Node {} - public record StaticBlock(String type, Location loc) implements Node {} - public record PropertyDefinition(String type, Location loc, Node key, Expression value, boolean computed, boolean isStatic) implements Node {} - public record MethodDefinition(String type, Location loc, Node key, FunctionExpression value, String kind, boolean computed, boolean isStatic) implements Node {} - public record ChainExpression(String type, Location loc, ChainElement expression) implements Node {} - public record ChainElement(String type, Location loc, Node chainElement) implements Node {} - public record SimpleCallExpression(String type, Location loc, boolean optional, Node callee, List arguments) implements Node {} - public record CallExpression(String type, Location loc, Node callExpression) implements Node {} - public record BinaryExpression(String type, Location loc, BinaryOperator operator, Expression left, Expression right) implements Node {} - public record BinaryOperator(String type, Location loc, String binaryOperator) implements Node {} - public record AwaitExpression(String type, Location loc, Expression argument) implements Node {} - public record AssignmentExpression(String type, Location loc, AssignmentOperator operator, Node left, Expression right) implements Node {} - public record AssignmentOperator(String type, Location loc, String assignmentOperator) implements Node {} - public record ArrowFunctionExpression(String type, Location loc, boolean expression, Node body, List params, boolean generator, boolean async) implements Node {} - public record ArrayExpression(String type, Location loc, List elements) implements Node {} - public record MaybeNamedClassDeclaration(String type, Location loc, Identifier id, Expression superClass, ClassBody body) implements Node {} - public record MaybeNamedFunctionDeclaration(String type, Location loc, Identifier id, BlockStatement body, List params, boolean generator, boolean async) implements Node {} - public record ExportNamedDeclaration(String type, Location loc, Declaration declaration, List specifiers, Literal source) implements Node {} - public record ExportSpecifier(String type, Location loc, Identifier exported, Identifier local) implements Node {} - public record Declaration(String type, Location loc, Node declaration) implements Node {} - public record ClassDeclaration(String type, Location loc, Identifier id, Expression superClass, ClassBody body) implements Node {} - public record VariableDeclaration(String type, Location loc, List declarations, String kind) implements Node {} - public record VariableDeclarator(String type, Location loc, Pattern id, Expression init) implements Node {} - public record FunctionDeclaration(String type, Location loc, Identifier id, BlockStatement body, List params, boolean generator, boolean async) implements Node {} - public record ImportDeclaration(String type, Location loc, List specifiers, Literal source) implements Node {} - public record ImportNamespaceSpecifier(String type, Location loc, Identifier local) implements Node {} - public record ImportDefaultSpecifier(String type, Location loc, Identifier local) implements Node {} - public record ImportSpecifier(String type, Location loc, Identifier imported, Identifier local) implements Node {} - public record Statement(String type, Location loc, Node statement) implements Node {} - public record ForOfStatement(String type, Location loc, boolean await, Node left, Expression right, Statement body) implements Node {} - public record ForInStatement(String type, Location loc, Node left, Expression right, Statement body) implements Node {} - public record ForStatement(String type, Location loc, Node init, Expression test, Expression update, Statement body) implements Node {} - public record DoWhileStatement(String type, Location loc, Statement body, Expression test) implements Node {} - public record WhileStatement(String type, Location loc, Expression test, Statement body) implements Node {} - public record TryStatement(String type, Location loc, BlockStatement block, CatchClause handler, BlockStatement finalizer) implements Node {} - public record CatchClause(String type, Location loc, Pattern param, BlockStatement body) implements Node {} - public record ThrowStatement(String type, Location loc, Expression argument) implements Node {} - public record SwitchStatement(String type, Location loc, Expression discriminant, List cases) implements Node {} - public record SwitchCase(String type, Location loc, Expression test, List consequent) implements Node {} - public record IfStatement(String type, Location loc, Expression test, Statement consequent, Statement alternate) implements Node {} - public record ContinueStatement(String type, Location loc, Identifier label) implements Node {} - public record BreakStatement(String type, Location loc, Identifier label) implements Node {} - public record LabeledStatement(String type, Location loc, Identifier label, Statement body) implements Node {} - public record ReturnStatement(String type, Location loc, Expression argument) implements Node {} - public record WithStatement(String type, Location loc, Expression object, Statement body) implements Node {} - public record DebuggerStatement(String type, Location loc) implements Node {} - public record EmptyStatement(String type, Location loc) implements Node {} - public record ExpressionStatement(String type, Location loc, Expression expression) implements Node {} - public record Directive(String type, Location loc, Literal expression, String directive) implements Node {} + public record ArrayExpression(Location loc, List elements) implements Expression {} + public record ArrayPattern(Location loc, List elements) implements Pattern {} + public record ArrowFunctionExpression(Location loc, boolean expression, Node body, List params, boolean generator, boolean async) implements Expression {} + public record AssignmentExpression(Location loc, AssignmentOperator operator, Node left, Expression right) implements Expression {} + public record AssignmentOperator(Location loc, String assignmentOperator) implements Node {} + public record AssignmentPattern(Location loc, Pattern left, Expression right) implements Pattern {} + public record AssignmentProperty(Location loc, Pattern value, String kind, boolean method, Node key, boolean shorthand, boolean computed) implements Node {} + public record AwaitExpression(Location loc, Expression argument) implements Expression {} + public record BigIntLiteral(Location loc, int value, String bigint, String raw) implements Literal {} + public record BinaryExpression(Location loc, BinaryOperator operator, Expression left, Expression right) implements Expression {} + public record BinaryOperator(Location loc, String binaryOperator) implements Node {} + public record BlockStatement(Location loc, List body) implements Statement {} + public record BreakStatement(Location loc, Identifier label) implements Statement {} + public sealed interface CallExpression extends Node {} + public record CatchClause(Location loc, Pattern param, BlockStatement body) implements Node {} + public sealed interface ChainElement extends Node {} + public record ChainExpression(Location loc, ChainElement expression) implements Expression {} + public record ClassBody(Location loc, List body) implements Node {} + public record ClassDeclaration(Location loc, Identifier id, Expression superClass, ClassBody body) implements Declaration {} + public record ClassExpression(Location loc, Identifier id, Expression superClass, ClassBody body) implements Expression {} + public record ConditionalExpression(Location loc, Expression test, Expression alternate, Expression consequent) implements Expression {} + public record ContinueStatement(Location loc, Identifier label) implements Statement {} + public record DebuggerStatement(Location loc) implements Statement {} + public sealed interface Declaration extends Node {} + public record Directive(Location loc, Literal expression, String directive) implements Node {} + public record DoWhileStatement(Location loc, Statement body, Expression test) implements Statement {} + public record EmptyStatement(Location loc) implements Statement {} + public record ExportAllDeclaration(Location loc, Identifier exported, Literal source) implements ModuleDeclaration {} + public sealed interface ExportDefaultDeclaration extends Node {} + public record ExportNamedDeclaration(Location loc, Declaration declaration, List specifiers, Literal source) implements ModuleDeclaration {} + public record ExportSpecifier(Location loc, Identifier exported, Identifier local) implements Node {} + public sealed interface Expression extends Node {} + public record ExpressionStatement(Location loc, Expression expression) implements Statement {} + public record ForInStatement(Location loc, Node left, Expression right, Statement body) implements Statement {} + public record ForOfStatement(Location loc, boolean await, Node left, Expression right, Statement body) implements Statement {} + public record ForStatement(Location loc, Node init, Expression test, Expression update, Statement body) implements Statement {} + public record FunctionDeclaration(Location loc, Identifier id, BlockStatement body, List params, boolean generator, boolean async) implements Declaration {} + public record FunctionExpression(Location loc, Identifier id, BlockStatement body, List params, boolean generator, boolean async) implements Expression {} + public record Identifier(Location loc, String name) implements Pattern {} + public record IfStatement(Location loc, Expression test, Statement consequent, Statement alternate) implements Statement {} + public record ImportDeclaration(Location loc, List specifiers, Literal source) implements ModuleDeclaration {} + public record ImportDefaultSpecifier(Location loc, Identifier local) implements Node {} + public record ImportExpression(Location loc, Expression source) implements Expression {} + public record ImportNamespaceSpecifier(Location loc, Identifier local) implements Node {} + public record ImportSpecifier(Location loc, Identifier imported, Identifier local) implements Node {} + public record LabeledStatement(Location loc, Identifier label, Statement body) implements Statement {} + public sealed interface Literal extends Node {} + public record LogicalExpression(Location loc, LogicalOperator operator, Expression left, Expression right) implements Expression {} + public record LogicalOperator(Location loc, String logicalOperator) implements Node {} + public record MaybeNamedClassDeclaration(Location loc, Identifier id, Expression superClass, ClassBody body) implements ExportDefaultDeclaration {} + public record MaybeNamedFunctionDeclaration(Location loc, Identifier id, BlockStatement body, List params, boolean generator, boolean async) implements ExportDefaultDeclaration {} + public record MemberExpression(Location loc, Node object, Node property, boolean computed, boolean optional) implements Pattern {} + public record MetaProperty(Location loc, Identifier meta, Identifier property) implements Expression {} + public record MethodDefinition(Location loc, Node key, FunctionExpression value, String kind, boolean computed, boolean isStatic) implements Node {} + public sealed interface ModuleDeclaration extends Node {} + public record NewExpression(Location loc, Node callee, List arguments) implements Expression {} + public record ObjectExpression(Location loc, List properties) implements Expression {} + public record ObjectPattern(Location loc, List properties) implements Pattern {} + public sealed interface Pattern extends Node {} + public record PrivateIdentifier(Location loc, String name) implements Node {} + public record Program(Location loc, String sourceType, List body) implements Node {} + public record Property(Location loc, Node key, Node value, String kind, boolean method, boolean shorthand, boolean computed) implements Node {} + public record PropertyDefinition(Location loc, Node key, Expression value, boolean computed, boolean isStatic) implements Node {} + public record RegExpLiteral(Location loc, String pattern, String flags, String raw) implements Literal {} + public record RestElement(Location loc, Pattern argument) implements Pattern {} + public record ReturnStatement(Location loc, Expression argument) implements Statement {} + public record SequenceExpression(Location loc, List expressions) implements Expression {} + public record SimpleCallExpression(Location loc, boolean optional, Node callee, List arguments) implements ChainElement {} + public record SimpleLiteral(Location loc, Node value, String raw) implements Literal {} + public record SpreadElement(Location loc, Expression argument) implements Node {} + public sealed interface Statement extends Node {} + public record StaticBlock(Location loc) implements Statement {} + public record Super(Location loc) implements Node {} + public record SwitchCase(Location loc, Expression test, List consequent) implements Node {} + public record SwitchStatement(Location loc, Expression discriminant, List cases) implements Statement {} + public record TaggedTemplateExpression(Location loc, Expression tag, TemplateLiteral quasi) implements Expression {} + public record TemplateElement(Location loc, boolean tail, String cooked, String raw) implements Node {} + public record TemplateLiteral(Location loc, List quasis, List expressions) implements Expression {} + public record ThisExpression(Location loc) implements Expression {} + public record ThrowStatement(Location loc, Expression argument) implements Statement {} + public record TryStatement(Location loc, BlockStatement block, CatchClause handler, BlockStatement finalizer) implements Statement {} + public record UnaryExpression(Location loc, UnaryOperator operator, boolean prefix, Expression argument) implements Expression {} + public record UnaryOperator(Location loc, String unaryOperator) implements Node {} + public record UpdateExpression(Location loc, UpdateOperator operator, Expression argument, boolean prefix) implements Expression {} + public record UpdateOperator(Location loc, String updateOperator) implements Node {} + public record VariableDeclaration(Location loc, List declarations, String kind) implements Declaration {} + public record VariableDeclarator(Location loc, Pattern id, Expression init) implements Node {} + 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, Expression argument, boolean delegate) implements Expression {} } diff --git a/sonar-plugin/api/src/test/java/org/sonar/plugins/javascript/api/estree/ESTreeTest.java b/sonar-plugin/api/src/test/java/org/sonar/plugins/javascript/api/estree/ESTreeTest.java index ce71c1ec178..b49d8fd5465 100644 --- a/sonar-plugin/api/src/test/java/org/sonar/plugins/javascript/api/estree/ESTreeTest.java +++ b/sonar-plugin/api/src/test/java/org/sonar/plugins/javascript/api/estree/ESTreeTest.java @@ -10,11 +10,7 @@ class ESTreeTest { @Test void test() { - Node p = new Program("", null); - switch (p) { - - } } } diff --git a/sonar-plugin/bridge/pom.xml b/sonar-plugin/bridge/pom.xml index b55385101f7..99b36cbac7f 100644 --- a/sonar-plugin/bridge/pom.xml +++ b/sonar-plugin/bridge/pom.xml @@ -122,7 +122,7 @@ com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier} ${project.basedir}/src/main/protobuf - ${project.build.directory}/generated-sources/protobuf + ${project.build.directory}/generated-sources 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 new file mode 100644 index 00000000000..016840f1227 --- /dev/null +++ b/sonar-plugin/bridge/src/main/java/org/sonar/plugins/javascript/bridge/ESTreeFactory.java @@ -0,0 +1,35 @@ +package org.sonar.plugins.javascript.bridge; + +import java.util.List; +import java.util.stream.Collectors; +import org.sonar.plugins.javascript.api.estree.ESTree; +import org.sonar.plugins.javascript.bridge.protobuf.Node; +import org.sonar.plugins.javascript.bridge.protobuf.SourceLocation; + +public class ESTreeFactory { + + + ESTree.Node from(Node node) { + switch (node.getType()) { + case ProgramType: + return fromProgram(node); + default: + throw new IllegalArgumentException("Unsupported node type: " + node.getType()); + } + } + + ESTree.Program fromProgram(Node node) { + var program = node.getProgram(); + return new ESTree.Program(ESTree.NodeType.Program, fromLocation(node.getLoc()), program.getSourceType(), from(program.getBodyList())); + } + + private List from(List bodyList) { + return bodyList.stream().map(this::from).collect(Collectors.toList()); + } + + + private static ESTree.Location fromLocation(SourceLocation location) { + return new ESTree.Location(location.getStart().getLine(), location.getStart().getEnd(), location.getEnd().getLine(), location.getEnd().getEnd()); + } + +} diff --git a/sonar-plugin/bridge/src/main/protobuf/estree.proto b/sonar-plugin/bridge/src/main/protobuf/estree.proto index af0b10181fc..753d6f4bce1 100644 --- a/sonar-plugin/bridge/src/main/protobuf/estree.proto +++ b/sonar-plugin/bridge/src/main/protobuf/estree.proto @@ -1,28 +1,182 @@ syntax = "proto3"; // Generated for @types/estree version: 1.0.5 +option java_package="org.sonar.plugins.javascript.bridge.protobuf"; +option java_multiple_files = true; + +message SourceLocation { + string source = 1; + Position start = 2; + Position end = 3; +} +message Position { + int32 line = 1; + int32 end = 2; +} + +enum NodeType { + ProgramType = 0; + ExportAllDeclarationType = 2; + BigIntLiteralType = 3; + SimpleLiteralType = 4; + IdentifierType = 5; + ExportDefaultDeclarationType = 6; + YieldExpressionType = 7; + UpdateExpressionType = 8; + UnaryExpressionType = 9; + ThisExpressionType = 10; + TemplateLiteralType = 11; + TaggedTemplateExpressionType = 12; + SequenceExpressionType = 13; + ObjectExpressionType = 14; + SpreadElementType = 15; + PropertyType = 16; + AssignmentPatternType = 17; + RestElementType = 18; + ArrayPatternType = 19; + ObjectPatternType = 20; + PrivateIdentifierType = 21; + NewExpressionType = 22; + SuperType = 23; + MetaPropertyType = 24; + MemberExpressionType = 25; + LogicalExpressionType = 26; + ImportExpressionType = 27; + BlockStatementType = 28; + ConditionalExpressionType = 29; + ClassExpressionType = 30; + ClassBodyType = 31; + StaticBlockType = 32; + PropertyDefinitionType = 33; + MethodDefinitionType = 34; + ChainExpressionType = 35; + SimpleCallExpressionType = 36; + BinaryExpressionType = 37; + AwaitExpressionType = 38; + AssignmentExpressionType = 39; + ArrowFunctionExpressionType = 40; + ArrayExpressionType = 41; + MaybeNamedClassDeclarationType = 42; + MaybeNamedFunctionDeclarationType = 43; + ExportNamedDeclarationType = 44; + ExportSpecifierType = 45; + VariableDeclarationType = 46; + VariableDeclaratorType = 47; + ImportDeclarationType = 48; + ImportNamespaceSpecifierType = 49; + ImportDefaultSpecifierType = 50; + ImportSpecifierType = 51; + ForOfStatementType = 52; + ForInStatementType = 53; + ForStatementType = 54; + DoWhileStatementType = 55; + WhileStatementType = 56; + TryStatementType = 57; + CatchClauseType = 58; + ThrowStatementType = 59; + SwitchStatementType = 60; + SwitchCaseType = 61; + IfStatementType = 62; + ContinueStatementType = 63; + BreakStatementType = 64; + LabeledStatementType = 65; + ReturnStatementType = 66; + WithStatementType = 67; + DebuggerStatementType = 68; + EmptyStatementType = 69; + ExpressionStatementType = 70; + DirectiveType = 71; + RegExpLiteralType = 72; + TemplateElementType = 73; + FunctionExpressionType = 74; +} +message Node { + NodeType type = 1; + SourceLocation loc = 2; + oneof node { + Program program = 3; + ExportAllDeclaration exportAllDeclaration = 4; + BigIntLiteral bigIntLiteral = 5; + SimpleLiteral simpleLiteral = 6; + Identifier identifier = 7; + ExportDefaultDeclaration exportDefaultDeclaration = 8; + YieldExpression yieldExpression = 9; + UpdateExpression updateExpression = 10; + UnaryExpression unaryExpression = 11; + ThisExpression thisExpression = 12; + TemplateLiteral templateLiteral = 13; + TaggedTemplateExpression taggedTemplateExpression = 14; + SequenceExpression sequenceExpression = 15; + ObjectExpression objectExpression = 16; + SpreadElement spreadElement = 17; + Property property = 18; + AssignmentPattern assignmentPattern = 19; + RestElement restElement = 20; + ArrayPattern arrayPattern = 21; + ObjectPattern objectPattern = 22; + PrivateIdentifier privateIdentifier = 23; + NewExpression newExpression = 24; + Super super = 25; + MetaProperty metaProperty = 26; + MemberExpression memberExpression = 27; + LogicalExpression logicalExpression = 28; + ImportExpression importExpression = 29; + BlockStatement blockStatement = 30; + ConditionalExpression conditionalExpression = 31; + ClassExpression classExpression = 32; + ClassBody classBody = 33; + StaticBlock staticBlock = 34; + PropertyDefinition propertyDefinition = 35; + MethodDefinition methodDefinition = 36; + ChainExpression chainExpression = 37; + SimpleCallExpression simpleCallExpression = 38; + BinaryExpression binaryExpression = 39; + AwaitExpression awaitExpression = 40; + AssignmentExpression assignmentExpression = 41; + ArrowFunctionExpression arrowFunctionExpression = 42; + ArrayExpression arrayExpression = 43; + MaybeNamedClassDeclaration maybeNamedClassDeclaration = 44; + MaybeNamedFunctionDeclaration maybeNamedFunctionDeclaration = 45; + ExportNamedDeclaration exportNamedDeclaration = 46; + ExportSpecifier exportSpecifier = 47; + VariableDeclaration variableDeclaration = 48; + VariableDeclarator variableDeclarator = 49; + ImportDeclaration importDeclaration = 50; + ImportNamespaceSpecifier importNamespaceSpecifier = 51; + ImportDefaultSpecifier importDefaultSpecifier = 52; + ImportSpecifier importSpecifier = 53; + ForOfStatement forOfStatement = 54; + ForInStatement forInStatement = 55; + ForStatement forStatement = 56; + DoWhileStatement doWhileStatement = 57; + WhileStatement whileStatement = 58; + TryStatement tryStatement = 59; + CatchClause catchClause = 60; + ThrowStatement throwStatement = 61; + SwitchStatement switchStatement = 62; + SwitchCase switchCase = 63; + IfStatement ifStatement = 64; + ContinueStatement continueStatement = 65; + BreakStatement breakStatement = 66; + LabeledStatement labeledStatement = 67; + ReturnStatement returnStatement = 68; + WithStatement withStatement = 69; + DebuggerStatement debuggerStatement = 70; + EmptyStatement emptyStatement = 71; + ExpressionStatement expressionStatement = 72; + Directive directive = 73; + RegExpLiteral regExpLiteral = 74; + TemplateElement templateElement = 75; + FunctionExpression functionExpression = 76; + } +} message Program { string sourceType = 1; - repeated BaseNodeWithoutComments body = 2; -} -message ModuleDeclaration { - oneof moduleDeclaration { - ImportDeclaration moduleDeclaration_importDeclaration = 1; - ExportNamedDeclaration moduleDeclaration_exportNamedDeclaration = 2; - ExportDefaultDeclaration moduleDeclaration_exportDefaultDeclaration = 3; - ExportAllDeclaration moduleDeclaration_exportAllDeclaration = 4; - } + repeated Node body = 2; } message ExportAllDeclaration { - Identifier exported = 1; - Literal source = 2; -} -message Literal { - oneof literal { - SimpleLiteral literal_simpleLiteral = 1; - RegExpLiteral literal_regExpLiteral = 2; - BigIntLiteral literal_bigIntLiteral = 3; - } + Node exported = 1; + Node source = 2; } message BigIntLiteral { int32 value = 1; @@ -41,440 +195,270 @@ message Identifier { string name = 1; } message ExportDefaultDeclaration { - oneof declaration { - MaybeNamedFunctionDeclaration declaration_maybeNamedFunctionDeclaration = 1; - MaybeNamedClassDeclaration declaration_maybeNamedClassDeclaration = 2; - Expression declaration_expression = 3; - } -} -message Expression { - oneof expression { - ArrayExpression expression_arrayExpression = 1; - ArrowFunctionExpression expression_arrowFunctionExpression = 2; - AssignmentExpression expression_assignmentExpression = 3; - AwaitExpression expression_awaitExpression = 4; - BinaryExpression expression_binaryExpression = 5; - CallExpression expression_callExpression = 6; - ChainExpression expression_chainExpression = 7; - ClassExpression expression_classExpression = 8; - ConditionalExpression expression_conditionalExpression = 9; - FunctionExpression expression_functionExpression = 10; - Identifier expression_identifier = 11; - ImportExpression expression_importExpression = 12; - Literal expression_literal = 13; - LogicalExpression expression_logicalExpression = 14; - MemberExpression expression_memberExpression = 15; - MetaProperty expression_metaProperty = 16; - NewExpression expression_newExpression = 17; - ObjectExpression expression_objectExpression = 18; - SequenceExpression expression_sequenceExpression = 19; - TaggedTemplateExpression expression_taggedTemplateExpression = 20; - TemplateLiteral expression_templateLiteral = 21; - ThisExpression expression_thisExpression = 22; - UnaryExpression expression_unaryExpression = 23; - UpdateExpression expression_updateExpression = 24; - YieldExpression expression_yieldExpression = 25; - } + Node declaration = 1; } message YieldExpression { - Expression argument = 1; + Node argument = 1; bool delegate = 2; } message UpdateExpression { - UpdateOperator operator = 1; - Expression argument = 2; + string operator = 1; + Node argument = 2; bool prefix = 3; } -message UpdateOperator { - string updateOperator = 1; -} message UnaryExpression { - UnaryOperator operator = 1; + string operator = 1; bool prefix = 2; - Expression argument = 3; -} -message UnaryOperator { - string unaryOperator = 1; + Node argument = 3; } message ThisExpression { } message TemplateLiteral { - repeated TemplateElement quasis = 1; - repeated Expression expressions = 2; + repeated Node quasis = 1; + repeated Node expressions = 2; } message TaggedTemplateExpression { - Expression tag = 1; - TemplateLiteral quasi = 2; + Node tag = 1; + Node quasi = 2; } message SequenceExpression { - repeated Expression expressions = 1; + repeated Node expressions = 1; } message ObjectExpression { - repeated BaseNodeWithoutComments properties = 1; + repeated Node properties = 1; } message SpreadElement { - Expression argument = 1; + Node argument = 1; } message Property { - oneof key { - Expression key_expression = 1; - PrivateIdentifier key_privateIdentifier = 2; - } - oneof value { - Expression value_expression = 3; - Pattern value_pattern = 4; - } - string kind = 5; - bool method = 6; - bool shorthand = 7; - bool computed = 8; -} -message Pattern { - oneof pattern { - Identifier pattern_identifier = 1; - ObjectPattern pattern_objectPattern = 2; - ArrayPattern pattern_arrayPattern = 3; - RestElement pattern_restElement = 4; - AssignmentPattern pattern_assignmentPattern = 5; - MemberExpression pattern_memberExpression = 6; - } + Node key = 1; + Node value = 2; + string kind = 3; + bool method = 4; + bool shorthand = 5; + bool computed = 6; } message AssignmentPattern { - Pattern left = 1; - Expression right = 2; + Node left = 1; + Node right = 2; } message RestElement { - Pattern argument = 1; + Node argument = 1; } message ArrayPattern { - repeated Pattern elements = 1; + repeated Node elements = 1; } message ObjectPattern { - repeated BaseNodeWithoutComments properties = 1; -} -message AssignmentProperty { - Pattern value = 1; - string kind = 2; - bool method = 3; - oneof key { - Expression key_expression = 4; - PrivateIdentifier key_privateIdentifier = 5; - } - bool shorthand = 6; - bool computed = 7; + repeated Node properties = 1; } message PrivateIdentifier { string name = 1; } message NewExpression { - oneof callee { - Expression callee_expression = 1; - Super callee_super = 2; - } - repeated BaseNodeWithoutComments arguments = 3; + Node callee = 1; + repeated Node arguments = 2; } message Super { } message MetaProperty { - Identifier meta = 1; - Identifier property = 2; + Node meta = 1; + Node property = 2; } message MemberExpression { - oneof object { - Expression object_expression = 1; - Super object_super = 2; - } - oneof property { - Expression property_expression = 3; - PrivateIdentifier property_privateIdentifier = 4; - } - bool computed = 5; - bool optional = 6; + Node object = 1; + Node property = 2; + bool computed = 3; + bool optional = 4; } message LogicalExpression { - LogicalOperator operator = 1; - Expression left = 2; - Expression right = 3; -} -message LogicalOperator { - string logicalOperator = 1; + Node operator = 1; + Node left = 2; + Node right = 3; } message ImportExpression { - Expression source = 1; -} -message FunctionExpression { - Identifier id = 1; - BlockStatement body = 2; - repeated Pattern params = 3; - bool generator = 4; - bool async = 5; + Node source = 1; } message BlockStatement { - repeated Statement body = 1; + repeated Node body = 1; } message ConditionalExpression { - Expression test = 1; - Expression alternate = 2; - Expression consequent = 3; + Node test = 1; + Node alternate = 2; + Node consequent = 3; } message ClassExpression { - Identifier id = 1; - Expression superClass = 2; - ClassBody body = 3; + Node id = 1; + Node superClass = 2; + Node body = 3; } message ClassBody { - repeated BaseNodeWithoutComments body = 1; + repeated Node body = 1; } message StaticBlock { } message PropertyDefinition { - oneof key { - Expression key_expression = 1; - PrivateIdentifier key_privateIdentifier = 2; - } - Expression value = 3; - bool computed = 4; - bool static = 5; + Node key = 1; + Node value = 2; + bool computed = 3; + bool static = 4; } message MethodDefinition { - oneof key { - Expression key_expression = 1; - PrivateIdentifier key_privateIdentifier = 2; - } - FunctionExpression value = 3; - string kind = 4; - bool computed = 5; - bool static = 6; + Node key = 1; + Node value = 2; + string kind = 3; + bool computed = 4; + bool static = 5; } message ChainExpression { - ChainElement expression = 1; -} -message ChainElement { - oneof chainElement { - SimpleCallExpression chainElement_simpleCallExpression = 1; - MemberExpression chainElement_memberExpression = 2; - } + Node expression = 1; } message SimpleCallExpression { bool optional = 1; - oneof callee { - Expression callee_expression = 2; - Super callee_super = 3; - } - repeated BaseNodeWithoutComments arguments = 4; -} -message CallExpression { - oneof callExpression { - SimpleCallExpression callExpression_simpleCallExpression = 1; - NewExpression callExpression_newExpression = 2; - } + Node callee = 2; + repeated Node arguments = 3; } message BinaryExpression { - BinaryOperator operator = 1; - Expression left = 2; - Expression right = 3; -} -message BinaryOperator { - string binaryOperator = 1; + string operator = 1; + Node left = 2; + Node right = 3; } message AwaitExpression { - Expression argument = 1; + Node argument = 1; } message AssignmentExpression { - AssignmentOperator operator = 1; - oneof left { - Pattern left_pattern = 2; - MemberExpression left_memberExpression = 3; - } - Expression right = 4; -} -message AssignmentOperator { - string assignmentOperator = 1; + string operator = 1; + Node left = 2; + Node right = 3; } message ArrowFunctionExpression { bool expression = 1; - oneof body { - BlockStatement body_blockStatement = 2; - Expression body_expression = 3; - } - repeated Pattern params = 4; - bool generator = 5; - bool async = 6; + Node body = 2; + repeated Node params = 3; + bool generator = 4; + bool async = 5; } message ArrayExpression { - repeated BaseNodeWithoutComments elements = 1; + repeated Node elements = 1; } message MaybeNamedClassDeclaration { - Identifier id = 1; - Expression superClass = 2; - ClassBody body = 3; + Node id = 1; + Node superClass = 2; + Node body = 3; } message MaybeNamedFunctionDeclaration { - Identifier id = 1; - BlockStatement body = 2; - repeated Pattern params = 3; + Node id = 1; + Node body = 2; + repeated Node params = 3; bool generator = 4; bool async = 5; } message ExportNamedDeclaration { - Declaration declaration = 1; - repeated ExportSpecifier specifiers = 2; - Literal source = 3; + Node declaration = 1; + repeated Node specifiers = 2; + Node source = 3; } message ExportSpecifier { - Identifier exported = 1; - Identifier local = 2; -} -message Declaration { - oneof declaration { - FunctionDeclaration declaration_functionDeclaration = 1; - VariableDeclaration declaration_variableDeclaration = 2; - ClassDeclaration declaration_classDeclaration = 3; - } -} -message ClassDeclaration { - Identifier id = 1; - Expression superClass = 2; - ClassBody body = 3; + Node exported = 1; + Node local = 2; } message VariableDeclaration { - repeated VariableDeclarator declarations = 1; + repeated Node declarations = 1; string kind = 2; } message VariableDeclarator { - Pattern id = 1; - Expression init = 2; -} -message FunctionDeclaration { - Identifier id = 1; - BlockStatement body = 2; - repeated Pattern params = 3; - bool generator = 4; - bool async = 5; + Node id = 1; + Node init = 2; } message ImportDeclaration { - repeated BaseNodeWithoutComments specifiers = 1; - Literal source = 2; + repeated Node specifiers = 1; + Node source = 2; } message ImportNamespaceSpecifier { - Identifier local = 1; + Node local = 1; } message ImportDefaultSpecifier { - Identifier local = 1; + Node local = 1; } message ImportSpecifier { - Identifier imported = 1; - Identifier local = 2; -} -message Statement { - oneof statement { - ExpressionStatement statement_expressionStatement = 1; - BlockStatement statement_blockStatement = 2; - StaticBlock statement_staticBlock = 3; - EmptyStatement statement_emptyStatement = 4; - DebuggerStatement statement_debuggerStatement = 5; - WithStatement statement_withStatement = 6; - ReturnStatement statement_returnStatement = 7; - LabeledStatement statement_labeledStatement = 8; - BreakStatement statement_breakStatement = 9; - ContinueStatement statement_continueStatement = 10; - IfStatement statement_ifStatement = 11; - SwitchStatement statement_switchStatement = 12; - ThrowStatement statement_throwStatement = 13; - TryStatement statement_tryStatement = 14; - WhileStatement statement_whileStatement = 15; - DoWhileStatement statement_doWhileStatement = 16; - ForStatement statement_forStatement = 17; - ForInStatement statement_forInStatement = 18; - ForOfStatement statement_forOfStatement = 19; - Declaration statement_declaration = 20; - } + Node imported = 1; + Node local = 2; } message ForOfStatement { bool await = 1; - oneof left { - VariableDeclaration left_variableDeclaration = 2; - Pattern left_pattern = 3; - } - Expression right = 4; - Statement body = 5; + Node left = 2; + Node right = 3; + Node body = 4; } message ForInStatement { - oneof left { - VariableDeclaration left_variableDeclaration = 1; - Pattern left_pattern = 2; - } - Expression right = 3; - Statement body = 4; + Node left = 1; + Node right = 2; + Node body = 3; } message ForStatement { - oneof init { - VariableDeclaration init_variableDeclaration = 1; - Expression init_expression = 2; - } - Expression test = 3; - Expression update = 4; - Statement body = 5; + Node init = 1; + Node test = 2; + Node update = 3; + Node body = 4; } message DoWhileStatement { - Statement body = 1; - Expression test = 2; + Node body = 1; + Node test = 2; } message WhileStatement { - Expression test = 1; - Statement body = 2; + Node test = 1; + Node body = 2; } message TryStatement { - BlockStatement block = 1; - CatchClause handler = 2; - BlockStatement finalizer = 3; + Node block = 1; + Node handler = 2; + Node finalizer = 3; } message CatchClause { - Pattern param = 1; - BlockStatement body = 2; + Node param = 1; + Node body = 2; } message ThrowStatement { - Expression argument = 1; + Node argument = 1; } message SwitchStatement { - Expression discriminant = 1; - repeated SwitchCase cases = 2; + Node discriminant = 1; + repeated Node cases = 2; } message SwitchCase { - Expression test = 1; - repeated Statement consequent = 2; + Node test = 1; + repeated Node consequent = 2; } message IfStatement { - Expression test = 1; - Statement consequent = 2; - Statement alternate = 3; + Node test = 1; + Node consequent = 2; + Node alternate = 3; } message ContinueStatement { - Identifier label = 1; + Node label = 1; } message BreakStatement { - Identifier label = 1; + Node label = 1; } message LabeledStatement { - Identifier label = 1; - Statement body = 2; + Node label = 1; + Node body = 2; } message ReturnStatement { - Expression argument = 1; + Node argument = 1; } message WithStatement { - Expression object = 1; - Statement body = 2; + Node object = 1; + Node body = 2; } message DebuggerStatement { } message EmptyStatement { } message ExpressionStatement { - Expression expression = 1; + Node expression = 1; } message Directive { - Literal expression = 1; + Node expression = 1; string directive = 2; } message RegExpLiteral { @@ -487,108 +471,11 @@ message TemplateElement { string cooked = 2; string raw = 3; } -message BaseNodeWithoutComments { - string type = 1; - SourceLocation loc = 2; - oneof node { - Program node_program = 3; - ModuleDeclaration node_moduleDeclaration = 4; - ExportAllDeclaration node_exportAllDeclaration = 5; - Literal node_literal = 6; - BigIntLiteral node_bigIntLiteral = 7; - SimpleLiteral node_simpleLiteral = 8; - Identifier node_identifier = 9; - ExportDefaultDeclaration node_exportDefaultDeclaration = 10; - Expression node_expression = 11; - YieldExpression node_yieldExpression = 12; - UpdateExpression node_updateExpression = 13; - UpdateOperator node_updateOperator = 14; - UnaryExpression node_unaryExpression = 15; - UnaryOperator node_unaryOperator = 16; - ThisExpression node_thisExpression = 17; - TemplateLiteral node_templateLiteral = 18; - TaggedTemplateExpression node_taggedTemplateExpression = 19; - SequenceExpression node_sequenceExpression = 20; - ObjectExpression node_objectExpression = 21; - SpreadElement node_spreadElement = 22; - Property node_property = 23; - Pattern node_pattern = 24; - AssignmentPattern node_assignmentPattern = 25; - RestElement node_restElement = 26; - ArrayPattern node_arrayPattern = 27; - ObjectPattern node_objectPattern = 28; - AssignmentProperty node_assignmentProperty = 29; - PrivateIdentifier node_privateIdentifier = 30; - NewExpression node_newExpression = 31; - Super node_super = 32; - MetaProperty node_metaProperty = 33; - MemberExpression node_memberExpression = 34; - LogicalExpression node_logicalExpression = 35; - LogicalOperator node_logicalOperator = 36; - ImportExpression node_importExpression = 37; - FunctionExpression node_functionExpression = 38; - BlockStatement node_blockStatement = 39; - ConditionalExpression node_conditionalExpression = 40; - ClassExpression node_classExpression = 41; - ClassBody node_classBody = 42; - StaticBlock node_staticBlock = 43; - PropertyDefinition node_propertyDefinition = 44; - MethodDefinition node_methodDefinition = 45; - ChainExpression node_chainExpression = 46; - ChainElement node_chainElement = 47; - SimpleCallExpression node_simpleCallExpression = 48; - CallExpression node_callExpression = 49; - BinaryExpression node_binaryExpression = 50; - BinaryOperator node_binaryOperator = 51; - AwaitExpression node_awaitExpression = 52; - AssignmentExpression node_assignmentExpression = 53; - AssignmentOperator node_assignmentOperator = 54; - ArrowFunctionExpression node_arrowFunctionExpression = 55; - ArrayExpression node_arrayExpression = 56; - MaybeNamedClassDeclaration node_maybeNamedClassDeclaration = 57; - MaybeNamedFunctionDeclaration node_maybeNamedFunctionDeclaration = 58; - ExportNamedDeclaration node_exportNamedDeclaration = 59; - ExportSpecifier node_exportSpecifier = 60; - Declaration node_declaration = 61; - ClassDeclaration node_classDeclaration = 62; - VariableDeclaration node_variableDeclaration = 63; - VariableDeclarator node_variableDeclarator = 64; - FunctionDeclaration node_functionDeclaration = 65; - ImportDeclaration node_importDeclaration = 66; - ImportNamespaceSpecifier node_importNamespaceSpecifier = 67; - ImportDefaultSpecifier node_importDefaultSpecifier = 68; - ImportSpecifier node_importSpecifier = 69; - Statement node_statement = 70; - ForOfStatement node_forOfStatement = 71; - ForInStatement node_forInStatement = 72; - ForStatement node_forStatement = 73; - DoWhileStatement node_doWhileStatement = 74; - WhileStatement node_whileStatement = 75; - TryStatement node_tryStatement = 76; - CatchClause node_catchClause = 77; - ThrowStatement node_throwStatement = 78; - SwitchStatement node_switchStatement = 79; - SwitchCase node_switchCase = 80; - IfStatement node_ifStatement = 81; - ContinueStatement node_continueStatement = 82; - BreakStatement node_breakStatement = 83; - LabeledStatement node_labeledStatement = 84; - ReturnStatement node_returnStatement = 85; - WithStatement node_withStatement = 86; - DebuggerStatement node_debuggerStatement = 87; - EmptyStatement node_emptyStatement = 88; - ExpressionStatement node_expressionStatement = 89; - Directive node_directive = 90; - RegExpLiteral node_regExpLiteral = 91; - TemplateElement node_templateElement = 92; - } -} -message SourceLocation { - string source = 1; - Position start = 2; - Position end = 3; + +message FunctionExpression { + Node id = 1; + Node body = 2; + repeated Node params = 3; + bool generator = 4; + bool async = 5; } -message Position { - int32 line = 1; - int32 end = 2; -} \ No newline at end of file diff --git a/tools/estree/generate-java-ast.ts b/tools/estree/generate-java-ast.ts index c820d80f99a..8fd094d1c28 100644 --- a/tools/estree/generate-java-ast.ts +++ b/tools/estree/generate-java-ast.ts @@ -42,25 +42,47 @@ const HEADER = `/* */ `; -const NODE_INTERFACE = `sealed interface Node { +const NODE_INTERFACE = `public sealed interface Node { - String type(); Location loc(); } - public record Location(int startLine, int startCol, int endLine, int endCol) {} + public record Position(int line, int column) {} + public record Location(Position start, Position end) {} `; -const SHARED_FIELDS = ['String type', 'Location loc']; +const SHARED_FIELDS = ['Location loc']; export function writeJavaClassesToDir(nodes: Record, output: string) { const records = []; - for (const [name, node] of Object.entries(nodes)) { - const fields = [...SHARED_FIELDS]; - for (const field of node.fields) { - fields.push(`${javaType(field.fieldValue)} ${javaName(field.name)}`); + const entries = Object.entries(nodes).sort(([a], [b]) => (a < b ? -1 : 1)); + const ifaces = entries + .filter(([, node]) => node.fields.length === 1 && 'unionElements' in node.fields[0].fieldValue) + .map(([name]) => name); + + const impl = new Map(); + ifaces.forEach(iface => { + // @ts-ignore + const union = nodes[iface].fields[0].fieldValue.unionElements.map(e => + upperCaseFirstLetter(e.name), + ); + for (const u of union) { + impl.set(u, iface); + } + }); + + for (const [name, node] of entries) { + if (ifaces.includes(name)) { + records.push(` public sealed interface ${name} extends Node {}`); + } else { + const fields = [...SHARED_FIELDS]; + for (const field of node.fields) { + fields.push(`${javaType(field.fieldValue)} ${javaName(field.name)}`); + } + records.push( + ` public record ${name}(${fields.join(', ')}) implements ${impl.has(name) ? impl.get(name) : 'Node'} {}`, + ); } - records.push(` public record ${name}(${fields.join(', ')}) implements Node {}`); } const estree = `${HEADER} @@ -74,13 +96,13 @@ public class ESTree { // shouldn't be instantiated } - ${NODE_INTERFACE} + ${NODE_INTERFACE} ${records.join('\n')} } `; - fs.writeFileSync('output/ESTree.java', estree, 'utf-8'); + fs.writeFileSync(output, estree, 'utf-8'); } function javaType(fieldValue: NodeFieldValue): string { @@ -109,3 +131,7 @@ function javaName(name: string) { } return name; } + +function upperCaseFirstLetter(s: string) { + return s.charAt(0).toUpperCase() + s.slice(1); +} diff --git a/tools/estree/generate-proto-file.ts b/tools/estree/generate-proto-file.ts index 7089848f187..cb21f532a2b 100644 --- a/tools/estree/generate-proto-file.ts +++ b/tools/estree/generate-proto-file.ts @@ -30,25 +30,6 @@ const packageJson = require(path.join('..', '..', 'package.json')); const typesVersion = packageJson.devDependencies['@types/estree']; export function addHandWrittenMessages(messages: Record) { - // Create node manually for 'RegExpLiteral' and 'TemplateElement'. - messages['RegExpLiteral'] = { - name: 'RegExpLiteral', - fields: [ - { name: 'pattern', fieldValue: { type: 'string' } }, - { name: 'flags', fieldValue: { type: 'string' } }, - { name: 'raw', fieldValue: { type: 'string' } }, - ], - }; - - messages['TemplateElement'] = { - name: 'TemplateElement', - fields: [ - { name: 'tail', fieldValue: { type: 'bool' } }, - { name: 'cooked', fieldValue: { type: 'string' } }, - { name: 'raw', fieldValue: { type: 'string' } }, - ], - }; - // We create manually the top level node "BaseNodeWithoutComments", holding all the other nodes. The name is taken directly from the index.d.ts file. // While we could generate this node with the same logic as the one used for all nodes, we do it manually as there would be too many edge cases to handle. const allNodeTypesAsFields = Object.keys(messages).map(nodeType => { @@ -86,8 +67,8 @@ export function addHandWrittenMessages(messages: Record) { export function writeMessagesToDir(messages: Record, outputDir: string) { addHandWrittenMessages(messages); fs.writeFileSync( - path.join(outputDir, 'estree.proto'), - addPrefix(translateToProtoFormat(messages)), + path.join(outputDir, 'estreeProto.proto'), + addPrefix(translateToProtoFormat(messages), messages), ); /** * Translate the messages to a protobuf file format. @@ -122,7 +103,19 @@ export function writeMessagesToDir(messages: Record, outputD return lines.join('\n'); } - function addPrefix(protoData: string) { - return `syntax = "proto3";\n// Generated for @types/estree version: ${typesVersion}\n\n${protoData}`; + function addPrefix(protoData: string, messages: Record) { + return `syntax = "proto3"; +// Generated for @types/estree version: ${typesVersion} +option java_package="org.sonar.plugins.javascript.bridge.protobuf"; +option java_multiple_files = true; + +enum NodeType { +${Object.keys(messages) + .sort() + .map((n, i) => `${n} = ${i}`) + .join(';\n')} +} + +${protoData}`; } } diff --git a/tools/estree/get-estree-nodes.ts b/tools/estree/get-estree-nodes.ts index 723bafda399..bb507b6b515 100644 --- a/tools/estree/get-estree-nodes.ts +++ b/tools/estree/get-estree-nodes.ts @@ -49,6 +49,25 @@ export function getEstreeNodes(file: ts.SourceFile) { const messages: Record = {}; + // Create node manually for 'RegExpLiteral' and 'TemplateElement'. + messages['RegExpLiteral'] = { + name: 'RegExpLiteral', + fields: [ + { name: 'pattern', fieldValue: { type: 'string' } }, + { name: 'flags', fieldValue: { type: 'string' } }, + { name: 'raw', fieldValue: { type: 'string' } }, + ], + }; + + messages['TemplateElement'] = { + name: 'TemplateElement', + fields: [ + { name: 'tail', fieldValue: { type: 'bool' } }, + { name: 'cooked', fieldValue: { type: 'string' } }, + { name: 'raw', fieldValue: { type: 'string' } }, + ], + }; + const requestedTypes: string[] = ['Program']; while (requestedTypes.length) { const requestedType = requestedTypes.pop()!; diff --git a/tools/estree/main.ts b/tools/estree/main.ts index 4ac4caea6a7..4babf79c984 100644 --- a/tools/estree/main.ts +++ b/tools/estree/main.ts @@ -40,7 +40,7 @@ fs.mkdirSync(output, { recursive: true }); if (arg === 'proto') { writeMessagesToDir(nodes, output); } else if (arg === 'java') { - writeJavaClassesToDir(nodes, output); + writeJavaClassesToDir(nodes, path.join(output, 'ESTree.java')); } else { console.error('Error: Argument should be "proto" or "java"'); process.exit(1); // Exit with a failure code diff --git a/tools/estree/output/ESTree.java b/tools/estree/output/ESTree.java new file mode 100644 index 00000000000..dc183537583 --- /dev/null +++ b/tools/estree/output/ESTree.java @@ -0,0 +1,131 @@ +/* + * SonarQube JavaScript Plugin + * Copyright (C) 2011-2023 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.plugins.javascript.api.estree; + +import java.util.List; + +public class ESTree { + + private ESTree() { + // shouldn't be instantiated + } + + public sealed interface Node { + + Location loc(); + } + + public record Position(int line, int column) {} + public record Location(Position start, Position end) {} + + + public record ArrayExpression(Location loc, List elements) implements Expression {} + public record ArrayPattern(Location loc, List elements) implements Pattern {} + public record ArrowFunctionExpression(Location loc, boolean expression, Node body, List params, boolean generator, boolean async) implements Expression {} + public record AssignmentExpression(Location loc, AssignmentOperator operator, Node left, Expression right) implements Expression {} + public record AssignmentOperator(Location loc, String assignmentOperator) implements Node {} + public record AssignmentPattern(Location loc, Pattern left, Expression right) implements Pattern {} + public record AssignmentProperty(Location loc, Pattern value, String kind, boolean method, Node key, boolean shorthand, boolean computed) implements Node {} + public record AwaitExpression(Location loc, Expression argument) implements Expression {} + public record BigIntLiteral(Location loc, int value, String bigint, String raw) implements Literal {} + public record BinaryExpression(Location loc, BinaryOperator operator, Expression left, Expression right) implements Expression {} + public record BinaryOperator(Location loc, String binaryOperator) implements Node {} + public record BlockStatement(Location loc, List body) implements Statement {} + public record BreakStatement(Location loc, Identifier label) implements Statement {} + public sealed interface CallExpression extends Node {} + public record CatchClause(Location loc, Pattern param, BlockStatement body) implements Node {} + public sealed interface ChainElement extends Node {} + public record ChainExpression(Location loc, ChainElement expression) implements Expression {} + public record ClassBody(Location loc, List body) implements Node {} + public record ClassDeclaration(Location loc, Identifier id, Expression superClass, ClassBody body) implements Declaration {} + public record ClassExpression(Location loc, Identifier id, Expression superClass, ClassBody body) implements Expression {} + public record ConditionalExpression(Location loc, Expression test, Expression alternate, Expression consequent) implements Expression {} + public record ContinueStatement(Location loc, Identifier label) implements Statement {} + public record DebuggerStatement(Location loc) implements Statement {} + public sealed interface Declaration extends Node {} + public record Directive(Location loc, Literal expression, String directive) implements Node {} + public record DoWhileStatement(Location loc, Statement body, Expression test) implements Statement {} + public record EmptyStatement(Location loc) implements Statement {} + public record ExportAllDeclaration(Location loc, Identifier exported, Literal source) implements ModuleDeclaration {} + public sealed interface ExportDefaultDeclaration extends Node {} + public record ExportNamedDeclaration(Location loc, Declaration declaration, List specifiers, Literal source) implements ModuleDeclaration {} + public record ExportSpecifier(Location loc, Identifier exported, Identifier local) implements Node {} + public sealed interface Expression extends Node {} + public record ExpressionStatement(Location loc, Expression expression) implements Statement {} + public record ForInStatement(Location loc, Node left, Expression right, Statement body) implements Statement {} + public record ForOfStatement(Location loc, boolean await, Node left, Expression right, Statement body) implements Statement {} + public record ForStatement(Location loc, Node init, Expression test, Expression update, Statement body) implements Statement {} + public record FunctionDeclaration(Location loc, Identifier id, BlockStatement body, List params, boolean generator, boolean async) implements Declaration {} + public record FunctionExpression(Location loc, Identifier id, BlockStatement body, List params, boolean generator, boolean async) implements Expression {} + public record Identifier(Location loc, String name) implements Pattern {} + public record IfStatement(Location loc, Expression test, Statement consequent, Statement alternate) implements Statement {} + public record ImportDeclaration(Location loc, List specifiers, Literal source) implements ModuleDeclaration {} + public record ImportDefaultSpecifier(Location loc, Identifier local) implements Node {} + public record ImportExpression(Location loc, Expression source) implements Expression {} + public record ImportNamespaceSpecifier(Location loc, Identifier local) implements Node {} + public record ImportSpecifier(Location loc, Identifier imported, Identifier local) implements Node {} + public record LabeledStatement(Location loc, Identifier label, Statement body) implements Statement {} + public sealed interface Literal extends Node {} + public record LogicalExpression(Location loc, LogicalOperator operator, Expression left, Expression right) implements Expression {} + public record LogicalOperator(Location loc, String logicalOperator) implements Node {} + public record MaybeNamedClassDeclaration(Location loc, Identifier id, Expression superClass, ClassBody body) implements ExportDefaultDeclaration {} + public record MaybeNamedFunctionDeclaration(Location loc, Identifier id, BlockStatement body, List params, boolean generator, boolean async) implements ExportDefaultDeclaration {} + public record MemberExpression(Location loc, Node object, Node property, boolean computed, boolean optional) implements Pattern {} + public record MetaProperty(Location loc, Identifier meta, Identifier property) implements Expression {} + public record MethodDefinition(Location loc, Node key, FunctionExpression value, String kind, boolean computed, boolean isStatic) implements Node {} + public sealed interface ModuleDeclaration extends Node {} + public record NewExpression(Location loc, Node callee, List arguments) implements Expression {} + public record ObjectExpression(Location loc, List properties) implements Expression {} + public record ObjectPattern(Location loc, List properties) implements Pattern {} + public sealed interface Pattern extends Node {} + public record PrivateIdentifier(Location loc, String name) implements Node {} + public record Program(Location loc, String sourceType, List body) implements Node {} + public record Property(Location loc, Node key, Node value, String kind, boolean method, boolean shorthand, boolean computed) implements Node {} + public record PropertyDefinition(Location loc, Node key, Expression value, boolean computed, boolean isStatic) implements Node {} + public record RegExpLiteral(Location loc, String pattern, String flags, String raw) implements Literal {} + public record RestElement(Location loc, Pattern argument) implements Pattern {} + public record ReturnStatement(Location loc, Expression argument) implements Statement {} + public record SequenceExpression(Location loc, List expressions) implements Expression {} + public record SimpleCallExpression(Location loc, boolean optional, Node callee, List arguments) implements ChainElement {} + public record SimpleLiteral(Location loc, Node value, String raw) implements Literal {} + public record SpreadElement(Location loc, Expression argument) implements Node {} + public sealed interface Statement extends Node {} + public record StaticBlock(Location loc) implements Statement {} + public record Super(Location loc) implements Node {} + public record SwitchCase(Location loc, Expression test, List consequent) implements Node {} + public record SwitchStatement(Location loc, Expression discriminant, List cases) implements Statement {} + public record TaggedTemplateExpression(Location loc, Expression tag, TemplateLiteral quasi) implements Expression {} + public record TemplateElement(Location loc, boolean tail, String cooked, String raw) implements Node {} + public record TemplateLiteral(Location loc, List quasis, List expressions) implements Expression {} + public record ThisExpression(Location loc) implements Expression {} + public record ThrowStatement(Location loc, Expression argument) implements Statement {} + public record TryStatement(Location loc, BlockStatement block, CatchClause handler, BlockStatement finalizer) implements Statement {} + public record UnaryExpression(Location loc, UnaryOperator operator, boolean prefix, Expression argument) implements Expression {} + public record UnaryOperator(Location loc, String unaryOperator) implements Node {} + public record UpdateExpression(Location loc, UpdateOperator operator, Expression argument, boolean prefix) implements Expression {} + public record UpdateOperator(Location loc, String updateOperator) implements Node {} + public record VariableDeclaration(Location loc, List declarations, String kind) implements Declaration {} + public record VariableDeclarator(Location loc, Pattern id, Expression init) implements Node {} + 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, Expression argument, boolean delegate) implements Expression {} +} + diff --git a/tools/estree/output/estreeProto.proto b/tools/estree/output/estreeProto.proto new file mode 100644 index 00000000000..65ff2aedf55 --- /dev/null +++ b/tools/estree/output/estreeProto.proto @@ -0,0 +1,692 @@ +syntax = "proto3"; +// Generated for @types/estree version: 1.0.5 +option java_package="org.sonar.plugins.javascript.bridge.protobuf"; +option java_multiple_files = true; + +enum NodeType { +ArrayExpression = 0; +ArrayPattern = 1; +ArrowFunctionExpression = 2; +AssignmentExpression = 3; +AssignmentOperator = 4; +AssignmentPattern = 5; +AssignmentProperty = 6; +AwaitExpression = 7; +BaseNodeWithoutComments = 8; +BigIntLiteral = 9; +BinaryExpression = 10; +BinaryOperator = 11; +BlockStatement = 12; +BreakStatement = 13; +CallExpression = 14; +CatchClause = 15; +ChainElement = 16; +ChainExpression = 17; +ClassBody = 18; +ClassDeclaration = 19; +ClassExpression = 20; +ConditionalExpression = 21; +ContinueStatement = 22; +DebuggerStatement = 23; +Declaration = 24; +Directive = 25; +DoWhileStatement = 26; +EmptyStatement = 27; +ExportAllDeclaration = 28; +ExportDefaultDeclaration = 29; +ExportNamedDeclaration = 30; +ExportSpecifier = 31; +Expression = 32; +ExpressionStatement = 33; +ForInStatement = 34; +ForOfStatement = 35; +ForStatement = 36; +FunctionDeclaration = 37; +FunctionExpression = 38; +Identifier = 39; +IfStatement = 40; +ImportDeclaration = 41; +ImportDefaultSpecifier = 42; +ImportExpression = 43; +ImportNamespaceSpecifier = 44; +ImportSpecifier = 45; +LabeledStatement = 46; +Literal = 47; +LogicalExpression = 48; +LogicalOperator = 49; +MaybeNamedClassDeclaration = 50; +MaybeNamedFunctionDeclaration = 51; +MemberExpression = 52; +MetaProperty = 53; +MethodDefinition = 54; +ModuleDeclaration = 55; +NewExpression = 56; +ObjectExpression = 57; +ObjectPattern = 58; +Pattern = 59; +Position = 60; +PrivateIdentifier = 61; +Program = 62; +Property = 63; +PropertyDefinition = 64; +RegExpLiteral = 65; +RestElement = 66; +ReturnStatement = 67; +SequenceExpression = 68; +SimpleCallExpression = 69; +SimpleLiteral = 70; +SourceLocation = 71; +SpreadElement = 72; +Statement = 73; +StaticBlock = 74; +Super = 75; +SwitchCase = 76; +SwitchStatement = 77; +TaggedTemplateExpression = 78; +TemplateElement = 79; +TemplateLiteral = 80; +ThisExpression = 81; +ThrowStatement = 82; +TryStatement = 83; +UnaryExpression = 84; +UnaryOperator = 85; +UpdateExpression = 86; +UpdateOperator = 87; +VariableDeclaration = 88; +VariableDeclarator = 89; +WhileStatement = 90; +WithStatement = 91; +YieldExpression = 92 +} + +message RegExpLiteral { + string pattern = 1; + string flags = 2; + string raw = 3; +} +message TemplateElement { + bool tail = 1; + string cooked = 2; + string raw = 3; +} +message Program { + string sourceType = 1; + repeated BaseNodeWithoutComments body = 2; +} +message ModuleDeclaration { + oneof moduleDeclaration { + ImportDeclaration moduleDeclaration_importDeclaration = 1; + ExportNamedDeclaration moduleDeclaration_exportNamedDeclaration = 2; + ExportDefaultDeclaration moduleDeclaration_exportDefaultDeclaration = 3; + ExportAllDeclaration moduleDeclaration_exportAllDeclaration = 4; + } +} +message ExportAllDeclaration { + Identifier exported = 1; + Literal source = 2; +} +message Literal { + oneof literal { + SimpleLiteral literal_simpleLiteral = 1; + RegExpLiteral literal_regExpLiteral = 2; + BigIntLiteral literal_bigIntLiteral = 3; + } +} +message BigIntLiteral { + int32 value = 1; + string bigint = 2; + string raw = 3; +} +message SimpleLiteral { + oneof value { + string value_string = 1; + bool value_boolean = 2; + int32 value_number = 3; + } + string raw = 4; +} +message Identifier { + string name = 1; +} +message ExportDefaultDeclaration { + oneof declaration { + MaybeNamedFunctionDeclaration declaration_maybeNamedFunctionDeclaration = 1; + MaybeNamedClassDeclaration declaration_maybeNamedClassDeclaration = 2; + Expression declaration_expression = 3; + } +} +message Expression { + oneof expression { + ArrayExpression expression_arrayExpression = 1; + ArrowFunctionExpression expression_arrowFunctionExpression = 2; + AssignmentExpression expression_assignmentExpression = 3; + AwaitExpression expression_awaitExpression = 4; + BinaryExpression expression_binaryExpression = 5; + CallExpression expression_callExpression = 6; + ChainExpression expression_chainExpression = 7; + ClassExpression expression_classExpression = 8; + ConditionalExpression expression_conditionalExpression = 9; + FunctionExpression expression_functionExpression = 10; + Identifier expression_identifier = 11; + ImportExpression expression_importExpression = 12; + Literal expression_literal = 13; + LogicalExpression expression_logicalExpression = 14; + MemberExpression expression_memberExpression = 15; + MetaProperty expression_metaProperty = 16; + NewExpression expression_newExpression = 17; + ObjectExpression expression_objectExpression = 18; + SequenceExpression expression_sequenceExpression = 19; + TaggedTemplateExpression expression_taggedTemplateExpression = 20; + TemplateLiteral expression_templateLiteral = 21; + ThisExpression expression_thisExpression = 22; + UnaryExpression expression_unaryExpression = 23; + UpdateExpression expression_updateExpression = 24; + YieldExpression expression_yieldExpression = 25; + } +} +message YieldExpression { + Expression argument = 1; + bool delegate = 2; +} +message UpdateExpression { + UpdateOperator operator = 1; + Expression argument = 2; + bool prefix = 3; +} +message UpdateOperator { + string updateOperator = 1; +} +message UnaryExpression { + UnaryOperator operator = 1; + bool prefix = 2; + Expression argument = 3; +} +message UnaryOperator { + string unaryOperator = 1; +} +message ThisExpression { +} +message TemplateLiteral { + repeated TemplateElement quasis = 1; + repeated Expression expressions = 2; +} +message TaggedTemplateExpression { + Expression tag = 1; + TemplateLiteral quasi = 2; +} +message SequenceExpression { + repeated Expression expressions = 1; +} +message ObjectExpression { + repeated BaseNodeWithoutComments properties = 1; +} +message SpreadElement { + Expression argument = 1; +} +message Property { + oneof key { + Expression key_expression = 1; + PrivateIdentifier key_privateIdentifier = 2; + } + oneof value { + Expression value_expression = 3; + Pattern value_pattern = 4; + } + string kind = 5; + bool method = 6; + bool shorthand = 7; + bool computed = 8; +} +message Pattern { + oneof pattern { + Identifier pattern_identifier = 1; + ObjectPattern pattern_objectPattern = 2; + ArrayPattern pattern_arrayPattern = 3; + RestElement pattern_restElement = 4; + AssignmentPattern pattern_assignmentPattern = 5; + MemberExpression pattern_memberExpression = 6; + } +} +message AssignmentPattern { + Pattern left = 1; + Expression right = 2; +} +message RestElement { + Pattern argument = 1; +} +message ArrayPattern { + repeated Pattern elements = 1; +} +message ObjectPattern { + repeated BaseNodeWithoutComments properties = 1; +} +message AssignmentProperty { + Pattern value = 1; + string kind = 2; + bool method = 3; + oneof key { + Expression key_expression = 4; + PrivateIdentifier key_privateIdentifier = 5; + } + bool shorthand = 6; + bool computed = 7; +} +message PrivateIdentifier { + string name = 1; +} +message NewExpression { + oneof callee { + Expression callee_expression = 1; + Super callee_super = 2; + } + repeated BaseNodeWithoutComments arguments = 3; +} +message Super { +} +message MetaProperty { + Identifier meta = 1; + Identifier property = 2; +} +message MemberExpression { + oneof object { + Expression object_expression = 1; + Super object_super = 2; + } + oneof property { + Expression property_expression = 3; + PrivateIdentifier property_privateIdentifier = 4; + } + bool computed = 5; + bool optional = 6; +} +message LogicalExpression { + LogicalOperator operator = 1; + Expression left = 2; + Expression right = 3; +} +message LogicalOperator { + string logicalOperator = 1; +} +message ImportExpression { + Expression source = 1; +} +message FunctionExpression { + Identifier id = 1; + BlockStatement body = 2; + repeated Pattern params = 3; + bool generator = 4; + bool async = 5; +} +message BlockStatement { + repeated Statement body = 1; +} +message ConditionalExpression { + Expression test = 1; + Expression alternate = 2; + Expression consequent = 3; +} +message ClassExpression { + Identifier id = 1; + Expression superClass = 2; + ClassBody body = 3; +} +message ClassBody { + repeated BaseNodeWithoutComments body = 1; +} +message StaticBlock { +} +message PropertyDefinition { + oneof key { + Expression key_expression = 1; + PrivateIdentifier key_privateIdentifier = 2; + } + Expression value = 3; + bool computed = 4; + bool static = 5; +} +message MethodDefinition { + oneof key { + Expression key_expression = 1; + PrivateIdentifier key_privateIdentifier = 2; + } + FunctionExpression value = 3; + string kind = 4; + bool computed = 5; + bool static = 6; +} +message ChainExpression { + ChainElement expression = 1; +} +message ChainElement { + oneof chainElement { + SimpleCallExpression chainElement_simpleCallExpression = 1; + MemberExpression chainElement_memberExpression = 2; + } +} +message SimpleCallExpression { + bool optional = 1; + oneof callee { + Expression callee_expression = 2; + Super callee_super = 3; + } + repeated BaseNodeWithoutComments arguments = 4; +} +message CallExpression { + oneof callExpression { + SimpleCallExpression callExpression_simpleCallExpression = 1; + NewExpression callExpression_newExpression = 2; + } +} +message BinaryExpression { + BinaryOperator operator = 1; + Expression left = 2; + Expression right = 3; +} +message BinaryOperator { + string binaryOperator = 1; +} +message AwaitExpression { + Expression argument = 1; +} +message AssignmentExpression { + AssignmentOperator operator = 1; + oneof left { + Pattern left_pattern = 2; + MemberExpression left_memberExpression = 3; + } + Expression right = 4; +} +message AssignmentOperator { + string assignmentOperator = 1; +} +message ArrowFunctionExpression { + bool expression = 1; + oneof body { + BlockStatement body_blockStatement = 2; + Expression body_expression = 3; + } + repeated Pattern params = 4; + bool generator = 5; + bool async = 6; +} +message ArrayExpression { + repeated BaseNodeWithoutComments elements = 1; +} +message MaybeNamedClassDeclaration { + Identifier id = 1; + Expression superClass = 2; + ClassBody body = 3; +} +message MaybeNamedFunctionDeclaration { + Identifier id = 1; + BlockStatement body = 2; + repeated Pattern params = 3; + bool generator = 4; + bool async = 5; +} +message ExportNamedDeclaration { + Declaration declaration = 1; + repeated ExportSpecifier specifiers = 2; + Literal source = 3; +} +message ExportSpecifier { + Identifier exported = 1; + Identifier local = 2; +} +message Declaration { + oneof declaration { + FunctionDeclaration declaration_functionDeclaration = 1; + VariableDeclaration declaration_variableDeclaration = 2; + ClassDeclaration declaration_classDeclaration = 3; + } +} +message ClassDeclaration { + Identifier id = 1; + Expression superClass = 2; + ClassBody body = 3; +} +message VariableDeclaration { + repeated VariableDeclarator declarations = 1; + string kind = 2; +} +message VariableDeclarator { + Pattern id = 1; + Expression init = 2; +} +message FunctionDeclaration { + Identifier id = 1; + BlockStatement body = 2; + repeated Pattern params = 3; + bool generator = 4; + bool async = 5; +} +message ImportDeclaration { + repeated BaseNodeWithoutComments specifiers = 1; + Literal source = 2; +} +message ImportNamespaceSpecifier { + Identifier local = 1; +} +message ImportDefaultSpecifier { + Identifier local = 1; +} +message ImportSpecifier { + Identifier imported = 1; + Identifier local = 2; +} +message Statement { + oneof statement { + ExpressionStatement statement_expressionStatement = 1; + BlockStatement statement_blockStatement = 2; + StaticBlock statement_staticBlock = 3; + EmptyStatement statement_emptyStatement = 4; + DebuggerStatement statement_debuggerStatement = 5; + WithStatement statement_withStatement = 6; + ReturnStatement statement_returnStatement = 7; + LabeledStatement statement_labeledStatement = 8; + BreakStatement statement_breakStatement = 9; + ContinueStatement statement_continueStatement = 10; + IfStatement statement_ifStatement = 11; + SwitchStatement statement_switchStatement = 12; + ThrowStatement statement_throwStatement = 13; + TryStatement statement_tryStatement = 14; + WhileStatement statement_whileStatement = 15; + DoWhileStatement statement_doWhileStatement = 16; + ForStatement statement_forStatement = 17; + ForInStatement statement_forInStatement = 18; + ForOfStatement statement_forOfStatement = 19; + Declaration statement_declaration = 20; + } +} +message ForOfStatement { + bool await = 1; + oneof left { + VariableDeclaration left_variableDeclaration = 2; + Pattern left_pattern = 3; + } + Expression right = 4; + Statement body = 5; +} +message ForInStatement { + oneof left { + VariableDeclaration left_variableDeclaration = 1; + Pattern left_pattern = 2; + } + Expression right = 3; + Statement body = 4; +} +message ForStatement { + oneof init { + VariableDeclaration init_variableDeclaration = 1; + Expression init_expression = 2; + } + Expression test = 3; + Expression update = 4; + Statement body = 5; +} +message DoWhileStatement { + Statement body = 1; + Expression test = 2; +} +message WhileStatement { + Expression test = 1; + Statement body = 2; +} +message TryStatement { + BlockStatement block = 1; + CatchClause handler = 2; + BlockStatement finalizer = 3; +} +message CatchClause { + Pattern param = 1; + BlockStatement body = 2; +} +message ThrowStatement { + Expression argument = 1; +} +message SwitchStatement { + Expression discriminant = 1; + repeated SwitchCase cases = 2; +} +message SwitchCase { + Expression test = 1; + repeated Statement consequent = 2; +} +message IfStatement { + Expression test = 1; + Statement consequent = 2; + Statement alternate = 3; +} +message ContinueStatement { + Identifier label = 1; +} +message BreakStatement { + Identifier label = 1; +} +message LabeledStatement { + Identifier label = 1; + Statement body = 2; +} +message ReturnStatement { + Expression argument = 1; +} +message WithStatement { + Expression object = 1; + Statement body = 2; +} +message DebuggerStatement { +} +message EmptyStatement { +} +message ExpressionStatement { + Expression expression = 1; +} +message Directive { + Literal expression = 1; + string directive = 2; +} +message BaseNodeWithoutComments { + string type = 1; + SourceLocation loc = 2; + oneof node { + RegExpLiteral node_regExpLiteral = 3; + TemplateElement node_templateElement = 4; + Program node_program = 5; + ModuleDeclaration node_moduleDeclaration = 6; + ExportAllDeclaration node_exportAllDeclaration = 7; + Literal node_literal = 8; + BigIntLiteral node_bigIntLiteral = 9; + SimpleLiteral node_simpleLiteral = 10; + Identifier node_identifier = 11; + ExportDefaultDeclaration node_exportDefaultDeclaration = 12; + Expression node_expression = 13; + YieldExpression node_yieldExpression = 14; + UpdateExpression node_updateExpression = 15; + UpdateOperator node_updateOperator = 16; + UnaryExpression node_unaryExpression = 17; + UnaryOperator node_unaryOperator = 18; + ThisExpression node_thisExpression = 19; + TemplateLiteral node_templateLiteral = 20; + TaggedTemplateExpression node_taggedTemplateExpression = 21; + SequenceExpression node_sequenceExpression = 22; + ObjectExpression node_objectExpression = 23; + SpreadElement node_spreadElement = 24; + Property node_property = 25; + Pattern node_pattern = 26; + AssignmentPattern node_assignmentPattern = 27; + RestElement node_restElement = 28; + ArrayPattern node_arrayPattern = 29; + ObjectPattern node_objectPattern = 30; + AssignmentProperty node_assignmentProperty = 31; + PrivateIdentifier node_privateIdentifier = 32; + NewExpression node_newExpression = 33; + Super node_super = 34; + MetaProperty node_metaProperty = 35; + MemberExpression node_memberExpression = 36; + LogicalExpression node_logicalExpression = 37; + LogicalOperator node_logicalOperator = 38; + ImportExpression node_importExpression = 39; + FunctionExpression node_functionExpression = 40; + BlockStatement node_blockStatement = 41; + ConditionalExpression node_conditionalExpression = 42; + ClassExpression node_classExpression = 43; + ClassBody node_classBody = 44; + StaticBlock node_staticBlock = 45; + PropertyDefinition node_propertyDefinition = 46; + MethodDefinition node_methodDefinition = 47; + ChainExpression node_chainExpression = 48; + ChainElement node_chainElement = 49; + SimpleCallExpression node_simpleCallExpression = 50; + CallExpression node_callExpression = 51; + BinaryExpression node_binaryExpression = 52; + BinaryOperator node_binaryOperator = 53; + AwaitExpression node_awaitExpression = 54; + AssignmentExpression node_assignmentExpression = 55; + AssignmentOperator node_assignmentOperator = 56; + ArrowFunctionExpression node_arrowFunctionExpression = 57; + ArrayExpression node_arrayExpression = 58; + MaybeNamedClassDeclaration node_maybeNamedClassDeclaration = 59; + MaybeNamedFunctionDeclaration node_maybeNamedFunctionDeclaration = 60; + ExportNamedDeclaration node_exportNamedDeclaration = 61; + ExportSpecifier node_exportSpecifier = 62; + Declaration node_declaration = 63; + ClassDeclaration node_classDeclaration = 64; + VariableDeclaration node_variableDeclaration = 65; + VariableDeclarator node_variableDeclarator = 66; + FunctionDeclaration node_functionDeclaration = 67; + ImportDeclaration node_importDeclaration = 68; + ImportNamespaceSpecifier node_importNamespaceSpecifier = 69; + ImportDefaultSpecifier node_importDefaultSpecifier = 70; + ImportSpecifier node_importSpecifier = 71; + Statement node_statement = 72; + ForOfStatement node_forOfStatement = 73; + ForInStatement node_forInStatement = 74; + ForStatement node_forStatement = 75; + DoWhileStatement node_doWhileStatement = 76; + WhileStatement node_whileStatement = 77; + TryStatement node_tryStatement = 78; + CatchClause node_catchClause = 79; + ThrowStatement node_throwStatement = 80; + SwitchStatement node_switchStatement = 81; + SwitchCase node_switchCase = 82; + IfStatement node_ifStatement = 83; + ContinueStatement node_continueStatement = 84; + BreakStatement node_breakStatement = 85; + LabeledStatement node_labeledStatement = 86; + ReturnStatement node_returnStatement = 87; + WithStatement node_withStatement = 88; + DebuggerStatement node_debuggerStatement = 89; + EmptyStatement node_emptyStatement = 90; + ExpressionStatement node_expressionStatement = 91; + Directive node_directive = 92; + } +} +message SourceLocation { + string source = 1; + Position start = 2; + Position end = 3; +} +message Position { + int32 line = 1; + int32 end = 2; +} \ No newline at end of file From a7fc49a2b70903a0a247203beea2c191dc1caffe Mon Sep 17 00:00:00 2001 From: Tibor Blenessy Date: Thu, 6 Jun 2024 17:03:30 +0200 Subject: [PATCH 04/10] add interfaces to ast nodes --- .../plugins/javascript/api/estree/ESTree.java | 97 ++++++++----- .../javascript/api/estree/ESTreeTest.java | 35 ++++- .../javascript/bridge/ESTreeFactory.java | 6 +- tools/estree/generate-java-ast.ts | 134 +++++++++++++++--- tools/estree/output/ESTree.java | 97 ++++++++----- 5 files changed, 273 insertions(+), 96 deletions(-) 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 dc183537583..791bc2f0ded 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 @@ -22,24 +22,62 @@ import java.util.List; +/** + This file is generated. Do not modify it manually. Look at tools/estree instead. + + This is !EXPERIMENTAL UNSUPPORTED INTERNAL API! It can be modified or removed without prior notice. +*/ public class ESTree { private ESTree() { - // shouldn't be instantiated + // shouldn't be instantiated, used only as a namespace } - public sealed interface Node { - + public sealed interface Node { Location loc(); } public record Position(int line, int column) {} public record Location(Position start, Position end) {} + public sealed interface CallExpression extends Node { + Node callee(); + List arguments(); + } + public sealed interface ChainElement extends Node { + boolean optional(); + } + public sealed interface Declaration extends Node { + + } + public sealed interface ExportDefaultDeclaration extends Node { + + } + public sealed interface Expression extends Node { + + } + public sealed interface HasBody extends Node { + Node body(); + } + public sealed interface HasTest extends Node { + Expression test(); + } + public sealed interface Literal extends Node { + String raw(); + } + public sealed interface ModuleDeclaration extends Node { + + } + public sealed interface Pattern extends Node { + + } + public sealed interface Statement extends Node { + + } public record ArrayExpression(Location loc, List elements) implements Expression {} public record ArrayPattern(Location loc, List elements) implements Pattern {} - public record ArrowFunctionExpression(Location loc, boolean expression, Node body, List params, boolean generator, boolean async) implements Expression {} + public record ArrowFunctionExpression(Location loc, boolean expression, Node body, List params, boolean generator, boolean async) implements Expression, HasBody {} public record AssignmentExpression(Location loc, AssignmentOperator operator, Node left, Expression right) implements Expression {} public record AssignmentOperator(Location loc, String assignmentOperator) implements Node {} public record AssignmentPattern(Location loc, Pattern left, Expression right) implements Pattern {} @@ -50,52 +88,44 @@ public record BinaryExpression(Location loc, BinaryOperator operator, Expression public record BinaryOperator(Location loc, String binaryOperator) implements Node {} public record BlockStatement(Location loc, List body) implements Statement {} public record BreakStatement(Location loc, Identifier label) implements Statement {} - public sealed interface CallExpression extends Node {} - public record CatchClause(Location loc, Pattern param, BlockStatement body) implements Node {} - public sealed interface ChainElement extends Node {} + public record CatchClause(Location loc, Pattern param, BlockStatement body) implements Node, HasBody {} public record ChainExpression(Location loc, ChainElement expression) implements Expression {} public record ClassBody(Location loc, List body) implements Node {} - public record ClassDeclaration(Location loc, Identifier id, Expression superClass, ClassBody body) implements Declaration {} - public record ClassExpression(Location loc, Identifier id, Expression superClass, ClassBody body) implements Expression {} - public record ConditionalExpression(Location loc, Expression test, Expression alternate, Expression consequent) implements Expression {} + public record ClassDeclaration(Location loc, Identifier id, Expression superClass, ClassBody body) implements Declaration, HasBody {} + public record ClassExpression(Location loc, Identifier id, Expression superClass, ClassBody body) implements Expression, HasBody {} + public record ConditionalExpression(Location loc, Expression test, Expression alternate, Expression consequent) implements Expression, HasTest {} public record ContinueStatement(Location loc, Identifier label) implements Statement {} public record DebuggerStatement(Location loc) implements Statement {} - public sealed interface Declaration extends Node {} public record Directive(Location loc, Literal expression, String directive) implements Node {} - public record DoWhileStatement(Location loc, Statement body, Expression test) implements Statement {} + public record DoWhileStatement(Location loc, Statement body, Expression test) implements Statement, HasBody, HasTest {} public record EmptyStatement(Location loc) implements Statement {} public record ExportAllDeclaration(Location loc, Identifier exported, Literal source) implements ModuleDeclaration {} - public sealed interface ExportDefaultDeclaration extends Node {} public record ExportNamedDeclaration(Location loc, Declaration declaration, List specifiers, Literal source) implements ModuleDeclaration {} public record ExportSpecifier(Location loc, Identifier exported, Identifier local) implements Node {} - public sealed interface Expression extends Node {} public record ExpressionStatement(Location loc, Expression expression) implements Statement {} - public record ForInStatement(Location loc, Node left, Expression right, Statement body) implements Statement {} - public record ForOfStatement(Location loc, boolean await, Node left, Expression right, Statement body) implements Statement {} - public record ForStatement(Location loc, Node init, Expression test, Expression update, Statement body) implements Statement {} - public record FunctionDeclaration(Location loc, Identifier id, BlockStatement body, List params, boolean generator, boolean async) implements Declaration {} - public record FunctionExpression(Location loc, Identifier id, BlockStatement body, List params, boolean generator, boolean async) implements Expression {} - public record Identifier(Location loc, String name) implements Pattern {} - public record IfStatement(Location loc, Expression test, Statement consequent, Statement alternate) implements Statement {} + public record ForInStatement(Location loc, Node left, Expression right, Statement body) implements Statement, HasBody {} + public record ForOfStatement(Location loc, boolean await, Node left, Expression right, Statement body) implements Statement, HasBody {} + public record ForStatement(Location loc, Node init, Expression test, Expression update, Statement body) implements Statement, HasBody, HasTest {} + public record FunctionDeclaration(Location loc, Identifier id, BlockStatement body, List params, boolean generator, boolean async) implements Declaration, HasBody {} + public record FunctionExpression(Location loc, Identifier id, BlockStatement body, List params, boolean generator, boolean async) implements Expression, HasBody {} + public record Identifier(Location loc, String name) implements Expression, Pattern {} + public record IfStatement(Location loc, Expression test, Statement consequent, Statement alternate) implements Statement, HasTest {} public record ImportDeclaration(Location loc, List specifiers, Literal source) implements ModuleDeclaration {} public record ImportDefaultSpecifier(Location loc, Identifier local) implements Node {} public record ImportExpression(Location loc, Expression source) implements Expression {} public record ImportNamespaceSpecifier(Location loc, Identifier local) implements Node {} public record ImportSpecifier(Location loc, Identifier imported, Identifier local) implements Node {} - public record LabeledStatement(Location loc, Identifier label, Statement body) implements Statement {} - public sealed interface Literal extends Node {} + public record LabeledStatement(Location loc, Identifier label, Statement body) implements Statement, HasBody {} public record LogicalExpression(Location loc, LogicalOperator operator, Expression left, Expression right) implements Expression {} public record LogicalOperator(Location loc, String logicalOperator) implements Node {} - public record MaybeNamedClassDeclaration(Location loc, Identifier id, Expression superClass, ClassBody body) implements ExportDefaultDeclaration {} - public record MaybeNamedFunctionDeclaration(Location loc, Identifier id, BlockStatement body, List params, boolean generator, boolean async) implements ExportDefaultDeclaration {} - public record MemberExpression(Location loc, Node object, Node property, boolean computed, boolean optional) implements Pattern {} + public record MaybeNamedClassDeclaration(Location loc, Identifier id, Expression superClass, ClassBody body) implements ExportDefaultDeclaration, HasBody {} + public record MaybeNamedFunctionDeclaration(Location loc, Identifier id, BlockStatement body, List params, boolean generator, boolean async) implements ExportDefaultDeclaration, HasBody {} + public record MemberExpression(Location loc, Node object, Node property, boolean computed, boolean optional) implements ChainElement, Expression, Pattern {} public record MetaProperty(Location loc, Identifier meta, Identifier property) implements Expression {} public record MethodDefinition(Location loc, Node key, FunctionExpression value, String kind, boolean computed, boolean isStatic) implements Node {} - public sealed interface ModuleDeclaration extends Node {} - public record NewExpression(Location loc, Node callee, List arguments) implements Expression {} + public record NewExpression(Location loc, Node callee, List arguments) implements CallExpression, Expression {} public record ObjectExpression(Location loc, List properties) implements Expression {} public record ObjectPattern(Location loc, List properties) implements Pattern {} - public sealed interface Pattern extends Node {} public record PrivateIdentifier(Location loc, String name) implements Node {} public record Program(Location loc, String sourceType, List body) implements Node {} public record Property(Location loc, Node key, Node value, String kind, boolean method, boolean shorthand, boolean computed) implements Node {} @@ -104,13 +134,12 @@ public record RegExpLiteral(Location loc, String pattern, String flags, String r public record RestElement(Location loc, Pattern argument) implements Pattern {} public record ReturnStatement(Location loc, Expression argument) implements Statement {} public record SequenceExpression(Location loc, List expressions) implements Expression {} - public record SimpleCallExpression(Location loc, boolean optional, Node callee, List arguments) implements ChainElement {} + public record SimpleCallExpression(Location loc, boolean optional, Node callee, List arguments) implements CallExpression, ChainElement {} public record SimpleLiteral(Location loc, Node value, String raw) implements Literal {} public record SpreadElement(Location loc, Expression argument) implements Node {} - public sealed interface Statement extends Node {} public record StaticBlock(Location loc) implements Statement {} public record Super(Location loc) implements Node {} - public record SwitchCase(Location loc, Expression test, List consequent) implements Node {} + public record SwitchCase(Location loc, Expression test, List consequent) implements Node, HasTest {} public record SwitchStatement(Location loc, Expression discriminant, List cases) implements Statement {} public record TaggedTemplateExpression(Location loc, Expression tag, TemplateLiteral quasi) implements Expression {} public record TemplateElement(Location loc, boolean tail, String cooked, String raw) implements Node {} @@ -124,8 +153,8 @@ public record UpdateExpression(Location loc, UpdateOperator operator, Expression public record UpdateOperator(Location loc, String updateOperator) implements Node {} public record VariableDeclaration(Location loc, List declarations, String kind) implements Declaration {} public record VariableDeclarator(Location loc, Pattern id, Expression init) implements Node {} - public record WhileStatement(Location loc, Expression test, Statement body) implements Statement {} - public record WithStatement(Location loc, Expression object, Statement body) implements Statement {} + public record WhileStatement(Location loc, Expression test, Statement body) implements Statement, HasBody, HasTest {} + public record WithStatement(Location loc, Expression object, Statement body) implements Statement, HasBody {} public record YieldExpression(Location loc, Expression argument, boolean delegate) implements Expression {} } diff --git a/sonar-plugin/api/src/test/java/org/sonar/plugins/javascript/api/estree/ESTreeTest.java b/sonar-plugin/api/src/test/java/org/sonar/plugins/javascript/api/estree/ESTreeTest.java index b49d8fd5465..924066fd9b2 100644 --- a/sonar-plugin/api/src/test/java/org/sonar/plugins/javascript/api/estree/ESTreeTest.java +++ b/sonar-plugin/api/src/test/java/org/sonar/plugins/javascript/api/estree/ESTreeTest.java @@ -1,16 +1,45 @@ package org.sonar.plugins.javascript.api.estree; -import static org.junit.jupiter.api.Assertions.*; +import static org.assertj.core.api.Assertions.assertThat; +import java.util.Arrays; import org.junit.jupiter.api.Test; -import org.sonar.plugins.javascript.api.estree.ESTree.Node; -import org.sonar.plugins.javascript.api.estree.ESTree.Program; class ESTreeTest { @Test void test() { + Class[] classes = ESTree.class.getDeclaredClasses(); + assertThat(classes).hasSize(95); + + //filter all classes that are interface + var ifaceCount = Arrays.stream(classes).filter(Class::isInterface).count(); + assertThat(ifaceCount).isEqualTo(12); + + var recordCount = Arrays.stream(classes).filter(Class::isRecord).count(); + assertThat(recordCount).isEqualTo(83); + } + + @Test + void test_node_subclasses() { + Class sealedClass = ESTree.Node.class; + Class[] permittedSubclasses = sealedClass.getPermittedSubclasses(); + assertThat(permittedSubclasses).hasSize(34); + } + + @Test + void test_expression_subclasses() { + Class sealedClass = ESTree.Expression.class; + Class[] permittedSubclasses = sealedClass.getPermittedSubclasses(); + assertThat(permittedSubclasses).hasSize(23); + } + + @Test + void test_statement_subclasses() { + Class sealedClass = ESTree.Statement.class; + Class[] permittedSubclasses = sealedClass.getPermittedSubclasses(); + assertThat(permittedSubclasses).hasSize(19); } } 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 016840f1227..22235b330cf 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 @@ -8,7 +8,6 @@ public class ESTreeFactory { - ESTree.Node from(Node node) { switch (node.getType()) { case ProgramType: @@ -20,7 +19,7 @@ ESTree.Node from(Node node) { ESTree.Program fromProgram(Node node) { var program = node.getProgram(); - return new ESTree.Program(ESTree.NodeType.Program, fromLocation(node.getLoc()), program.getSourceType(), from(program.getBodyList())); + return new ESTree.Program(fromLocation(node.getLoc()), program.getSourceType(), from(program.getBodyList())); } private List from(List bodyList) { @@ -29,7 +28,8 @@ private List from(List bodyList) { private static ESTree.Location fromLocation(SourceLocation location) { - return new ESTree.Location(location.getStart().getLine(), location.getStart().getEnd(), location.getEnd().getLine(), location.getEnd().getEnd()); + return new ESTree.Location(new ESTree.Position(location.getStart().getLine(), location.getStart().getEnd()), + new ESTree.Position(location.getEnd().getLine(), location.getEnd().getEnd())); } } diff --git a/tools/estree/generate-java-ast.ts b/tools/estree/generate-java-ast.ts index 8fd094d1c28..d0c1a8d8962 100644 --- a/tools/estree/generate-java-ast.ts +++ b/tools/estree/generate-java-ast.ts @@ -19,7 +19,12 @@ */ import fs from 'node:fs'; -import { ESTreeNode, NodeField, NodeFieldValue } from './get-estree-nodes'; +import { + ArrayLikeFieldValue, + ESTreeNode, + NodeFieldValue, + UnionFieldValue, +} from './get-estree-nodes'; const HEADER = `/* * SonarQube JavaScript Plugin @@ -42,8 +47,7 @@ const HEADER = `/* */ `; -const NODE_INTERFACE = `public sealed interface Node { - +const NODE_INTERFACE = `public sealed interface Node { Location loc(); } @@ -53,34 +57,87 @@ const NODE_INTERFACE = `public sealed interface Node { const SHARED_FIELDS = ['Location loc']; +function isUnionNode(node: ESTreeNode) { + return node.fields.length === 1 && 'unionElements' in node.fields[0].fieldValue; +} + export function writeJavaClassesToDir(nodes: Record, output: string) { - const records = []; + // adding some hardcoded interfaces which allow to treat loops in the AST consistently + nodes['HasBody'] = { + name: 'HasBody', + fields: [ + { + name: 'body', + fieldValue: { type: 'Node' }, + }, + ], + }; + + nodes['HasTest'] = { + name: 'HasTest', + fields: [ + { + name: 'test', + fieldValue: { type: 'Expression' }, + }, + ], + }; const entries = Object.entries(nodes).sort(([a], [b]) => (a < b ? -1 : 1)); - const ifaces = entries - .filter(([, node]) => node.fields.length === 1 && 'unionElements' in node.fields[0].fieldValue) - .map(([name]) => name); - - const impl = new Map(); - ifaces.forEach(iface => { - // @ts-ignore - const union = nodes[iface].fields[0].fieldValue.unionElements.map(e => - upperCaseFirstLetter(e.name), - ); - for (const u of union) { - impl.set(u, iface); - } - }); + const ifaces = entries.filter(([, n]) => isUnionNode(n)).map(([name]) => name); + ifaces.push('HasBody', 'HasTest'); + const impl = new Map(); + ifaces + .filter(i => isUnionNode(nodes[i])) + .forEach(iface => { + //@ts-ignore + const union = nodes[iface].fields[0].fieldValue.unionElements.map(e => + upperCaseFirstLetter(e.name), + ); + for (const u of union) { + if (impl.has(u)) { + impl.set(u, impl.get(u)!.concat(iface)); + } else { + impl.set(u, [iface]); + } + } + }); + + nodes['Literal'].fields = [ + { + name: 'raw', + fieldValue: { type: 'string' }, + }, + ]; + nodes['ChainElement'].fields = [ + { + name: 'optional', + fieldValue: { type: 'bool' }, + }, + ]; + nodes['CallExpression'].fields = [ + { + name: 'callee', + fieldValue: { type: 'Node' }, // more precise type is Expression | Super, but we can't represent union types in Java easily + }, + { + name: 'arguments', + fieldValue: { type: `List` }, + }, + ]; + + const records = []; + const ifaceSrc = []; for (const [name, node] of entries) { if (ifaces.includes(name)) { - records.push(` public sealed interface ${name} extends Node {}`); + ifaceSrc.push(` public sealed interface ${name} extends Node {\n${ifaceFields(node)}\n }`); } else { const fields = [...SHARED_FIELDS]; for (const field of node.fields) { fields.push(`${javaType(field.fieldValue)} ${javaName(field.name)}`); } records.push( - ` public record ${name}(${fields.join(', ')}) implements ${impl.has(name) ? impl.get(name) : 'Node'} {}`, + ` public record ${name}(${fields.join(', ')}) implements ${implementsClause(impl, node)} {}`, ); } } @@ -90,13 +147,19 @@ package org.sonar.plugins.javascript.api.estree; import java.util.List; +/** + This file is generated. Do not modify it manually. Look at tools/estree instead. + + This is !EXPERIMENTAL UNSUPPORTED INTERNAL API! It can be modified or removed without prior notice. +*/ public class ESTree { private ESTree() { - // shouldn't be instantiated + // shouldn't be instantiated, used only as a namespace } ${NODE_INTERFACE} +${ifaceSrc.join('\n')} ${records.join('\n')} } @@ -119,7 +182,7 @@ function javaType(fieldValue: NodeFieldValue): string { } return fieldValue.type; } - if ('elementValue' in fieldValue) { + if (isArray(fieldValue)) { return `List<${javaType(fieldValue.elementValue)}>`; } return 'Node'; @@ -135,3 +198,30 @@ function javaName(name: string) { function upperCaseFirstLetter(s: string) { return s.charAt(0).toUpperCase() + s.slice(1); } + +function ifaceFields(node: ESTreeNode) { + return node.fields + .filter(f => !('unionElements' in f.fieldValue)) + .map(f => ` ${javaType(f.fieldValue)} ${javaName(f.name)}();`) + .join('\n'); +} + +function implementsClause(impl: Map, node: ESTreeNode) { + const ifaces = []; + if (impl.has(node.name)) { + ifaces.push(...impl.get(node.name)!); + } else { + ifaces.push('Node'); + } + if (node.fields.some(f => f.name === 'body' && !isArray(f.fieldValue))) { + ifaces.push('HasBody'); + } + if (node.fields.some(f => f.name === 'test')) { + ifaces.push('HasTest'); + } + return ifaces.join(', '); +} + +function isArray(fieldValue: NodeFieldValue): fieldValue is ArrayLikeFieldValue { + return 'elementValue' in fieldValue; +} diff --git a/tools/estree/output/ESTree.java b/tools/estree/output/ESTree.java index dc183537583..791bc2f0ded 100644 --- a/tools/estree/output/ESTree.java +++ b/tools/estree/output/ESTree.java @@ -22,24 +22,62 @@ import java.util.List; +/** + This file is generated. Do not modify it manually. Look at tools/estree instead. + + This is !EXPERIMENTAL UNSUPPORTED INTERNAL API! It can be modified or removed without prior notice. +*/ public class ESTree { private ESTree() { - // shouldn't be instantiated + // shouldn't be instantiated, used only as a namespace } - public sealed interface Node { - + public sealed interface Node { Location loc(); } public record Position(int line, int column) {} public record Location(Position start, Position end) {} + public sealed interface CallExpression extends Node { + Node callee(); + List arguments(); + } + public sealed interface ChainElement extends Node { + boolean optional(); + } + public sealed interface Declaration extends Node { + + } + public sealed interface ExportDefaultDeclaration extends Node { + + } + public sealed interface Expression extends Node { + + } + public sealed interface HasBody extends Node { + Node body(); + } + public sealed interface HasTest extends Node { + Expression test(); + } + public sealed interface Literal extends Node { + String raw(); + } + public sealed interface ModuleDeclaration extends Node { + + } + public sealed interface Pattern extends Node { + + } + public sealed interface Statement extends Node { + + } public record ArrayExpression(Location loc, List elements) implements Expression {} public record ArrayPattern(Location loc, List elements) implements Pattern {} - public record ArrowFunctionExpression(Location loc, boolean expression, Node body, List params, boolean generator, boolean async) implements Expression {} + public record ArrowFunctionExpression(Location loc, boolean expression, Node body, List params, boolean generator, boolean async) implements Expression, HasBody {} public record AssignmentExpression(Location loc, AssignmentOperator operator, Node left, Expression right) implements Expression {} public record AssignmentOperator(Location loc, String assignmentOperator) implements Node {} public record AssignmentPattern(Location loc, Pattern left, Expression right) implements Pattern {} @@ -50,52 +88,44 @@ public record BinaryExpression(Location loc, BinaryOperator operator, Expression public record BinaryOperator(Location loc, String binaryOperator) implements Node {} public record BlockStatement(Location loc, List body) implements Statement {} public record BreakStatement(Location loc, Identifier label) implements Statement {} - public sealed interface CallExpression extends Node {} - public record CatchClause(Location loc, Pattern param, BlockStatement body) implements Node {} - public sealed interface ChainElement extends Node {} + public record CatchClause(Location loc, Pattern param, BlockStatement body) implements Node, HasBody {} public record ChainExpression(Location loc, ChainElement expression) implements Expression {} public record ClassBody(Location loc, List body) implements Node {} - public record ClassDeclaration(Location loc, Identifier id, Expression superClass, ClassBody body) implements Declaration {} - public record ClassExpression(Location loc, Identifier id, Expression superClass, ClassBody body) implements Expression {} - public record ConditionalExpression(Location loc, Expression test, Expression alternate, Expression consequent) implements Expression {} + public record ClassDeclaration(Location loc, Identifier id, Expression superClass, ClassBody body) implements Declaration, HasBody {} + public record ClassExpression(Location loc, Identifier id, Expression superClass, ClassBody body) implements Expression, HasBody {} + public record ConditionalExpression(Location loc, Expression test, Expression alternate, Expression consequent) implements Expression, HasTest {} public record ContinueStatement(Location loc, Identifier label) implements Statement {} public record DebuggerStatement(Location loc) implements Statement {} - public sealed interface Declaration extends Node {} public record Directive(Location loc, Literal expression, String directive) implements Node {} - public record DoWhileStatement(Location loc, Statement body, Expression test) implements Statement {} + public record DoWhileStatement(Location loc, Statement body, Expression test) implements Statement, HasBody, HasTest {} public record EmptyStatement(Location loc) implements Statement {} public record ExportAllDeclaration(Location loc, Identifier exported, Literal source) implements ModuleDeclaration {} - public sealed interface ExportDefaultDeclaration extends Node {} public record ExportNamedDeclaration(Location loc, Declaration declaration, List specifiers, Literal source) implements ModuleDeclaration {} public record ExportSpecifier(Location loc, Identifier exported, Identifier local) implements Node {} - public sealed interface Expression extends Node {} public record ExpressionStatement(Location loc, Expression expression) implements Statement {} - public record ForInStatement(Location loc, Node left, Expression right, Statement body) implements Statement {} - public record ForOfStatement(Location loc, boolean await, Node left, Expression right, Statement body) implements Statement {} - public record ForStatement(Location loc, Node init, Expression test, Expression update, Statement body) implements Statement {} - public record FunctionDeclaration(Location loc, Identifier id, BlockStatement body, List params, boolean generator, boolean async) implements Declaration {} - public record FunctionExpression(Location loc, Identifier id, BlockStatement body, List params, boolean generator, boolean async) implements Expression {} - public record Identifier(Location loc, String name) implements Pattern {} - public record IfStatement(Location loc, Expression test, Statement consequent, Statement alternate) implements Statement {} + public record ForInStatement(Location loc, Node left, Expression right, Statement body) implements Statement, HasBody {} + public record ForOfStatement(Location loc, boolean await, Node left, Expression right, Statement body) implements Statement, HasBody {} + public record ForStatement(Location loc, Node init, Expression test, Expression update, Statement body) implements Statement, HasBody, HasTest {} + public record FunctionDeclaration(Location loc, Identifier id, BlockStatement body, List params, boolean generator, boolean async) implements Declaration, HasBody {} + public record FunctionExpression(Location loc, Identifier id, BlockStatement body, List params, boolean generator, boolean async) implements Expression, HasBody {} + public record Identifier(Location loc, String name) implements Expression, Pattern {} + public record IfStatement(Location loc, Expression test, Statement consequent, Statement alternate) implements Statement, HasTest {} public record ImportDeclaration(Location loc, List specifiers, Literal source) implements ModuleDeclaration {} public record ImportDefaultSpecifier(Location loc, Identifier local) implements Node {} public record ImportExpression(Location loc, Expression source) implements Expression {} public record ImportNamespaceSpecifier(Location loc, Identifier local) implements Node {} public record ImportSpecifier(Location loc, Identifier imported, Identifier local) implements Node {} - public record LabeledStatement(Location loc, Identifier label, Statement body) implements Statement {} - public sealed interface Literal extends Node {} + public record LabeledStatement(Location loc, Identifier label, Statement body) implements Statement, HasBody {} public record LogicalExpression(Location loc, LogicalOperator operator, Expression left, Expression right) implements Expression {} public record LogicalOperator(Location loc, String logicalOperator) implements Node {} - public record MaybeNamedClassDeclaration(Location loc, Identifier id, Expression superClass, ClassBody body) implements ExportDefaultDeclaration {} - public record MaybeNamedFunctionDeclaration(Location loc, Identifier id, BlockStatement body, List params, boolean generator, boolean async) implements ExportDefaultDeclaration {} - public record MemberExpression(Location loc, Node object, Node property, boolean computed, boolean optional) implements Pattern {} + public record MaybeNamedClassDeclaration(Location loc, Identifier id, Expression superClass, ClassBody body) implements ExportDefaultDeclaration, HasBody {} + public record MaybeNamedFunctionDeclaration(Location loc, Identifier id, BlockStatement body, List params, boolean generator, boolean async) implements ExportDefaultDeclaration, HasBody {} + public record MemberExpression(Location loc, Node object, Node property, boolean computed, boolean optional) implements ChainElement, Expression, Pattern {} public record MetaProperty(Location loc, Identifier meta, Identifier property) implements Expression {} public record MethodDefinition(Location loc, Node key, FunctionExpression value, String kind, boolean computed, boolean isStatic) implements Node {} - public sealed interface ModuleDeclaration extends Node {} - public record NewExpression(Location loc, Node callee, List arguments) implements Expression {} + public record NewExpression(Location loc, Node callee, List arguments) implements CallExpression, Expression {} public record ObjectExpression(Location loc, List properties) implements Expression {} public record ObjectPattern(Location loc, List properties) implements Pattern {} - public sealed interface Pattern extends Node {} public record PrivateIdentifier(Location loc, String name) implements Node {} public record Program(Location loc, String sourceType, List body) implements Node {} public record Property(Location loc, Node key, Node value, String kind, boolean method, boolean shorthand, boolean computed) implements Node {} @@ -104,13 +134,12 @@ public record RegExpLiteral(Location loc, String pattern, String flags, String r public record RestElement(Location loc, Pattern argument) implements Pattern {} public record ReturnStatement(Location loc, Expression argument) implements Statement {} public record SequenceExpression(Location loc, List expressions) implements Expression {} - public record SimpleCallExpression(Location loc, boolean optional, Node callee, List arguments) implements ChainElement {} + public record SimpleCallExpression(Location loc, boolean optional, Node callee, List arguments) implements CallExpression, ChainElement {} public record SimpleLiteral(Location loc, Node value, String raw) implements Literal {} public record SpreadElement(Location loc, Expression argument) implements Node {} - public sealed interface Statement extends Node {} public record StaticBlock(Location loc) implements Statement {} public record Super(Location loc) implements Node {} - public record SwitchCase(Location loc, Expression test, List consequent) implements Node {} + public record SwitchCase(Location loc, Expression test, List consequent) implements Node, HasTest {} public record SwitchStatement(Location loc, Expression discriminant, List cases) implements Statement {} public record TaggedTemplateExpression(Location loc, Expression tag, TemplateLiteral quasi) implements Expression {} public record TemplateElement(Location loc, boolean tail, String cooked, String raw) implements Node {} @@ -124,8 +153,8 @@ public record UpdateExpression(Location loc, UpdateOperator operator, Expression public record UpdateOperator(Location loc, String updateOperator) implements Node {} public record VariableDeclaration(Location loc, List declarations, String kind) implements Declaration {} public record VariableDeclarator(Location loc, Pattern id, Expression init) implements Node {} - public record WhileStatement(Location loc, Expression test, Statement body) implements Statement {} - public record WithStatement(Location loc, Expression object, Statement body) implements Statement {} + public record WhileStatement(Location loc, Expression test, Statement body) implements Statement, HasBody, HasTest {} + public record WithStatement(Location loc, Expression object, Statement body) implements Statement, HasBody {} public record YieldExpression(Location loc, Expression argument, boolean delegate) implements Expression {} } From 438b36bcf051069341064f70ed8e9267ca1f0703 Mon Sep 17 00:00:00 2001 From: Tibor Blenessy Date: Thu, 6 Jun 2024 17:26:53 +0200 Subject: [PATCH 05/10] remove unrelated files --- .../javascript/bridge/ESTreeFactory.java | 35 - .../bridge/src/main/protobuf/estree.proto | 719 ++++++++++-------- tools/estree/generate-proto-file.ts | 20 +- tools/estree/output/estreeProto.proto | 692 ----------------- 4 files changed, 420 insertions(+), 1046 deletions(-) delete mode 100644 sonar-plugin/bridge/src/main/java/org/sonar/plugins/javascript/bridge/ESTreeFactory.java delete mode 100644 tools/estree/output/estreeProto.proto 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 deleted file mode 100644 index 22235b330cf..00000000000 --- a/sonar-plugin/bridge/src/main/java/org/sonar/plugins/javascript/bridge/ESTreeFactory.java +++ /dev/null @@ -1,35 +0,0 @@ -package org.sonar.plugins.javascript.bridge; - -import java.util.List; -import java.util.stream.Collectors; -import org.sonar.plugins.javascript.api.estree.ESTree; -import org.sonar.plugins.javascript.bridge.protobuf.Node; -import org.sonar.plugins.javascript.bridge.protobuf.SourceLocation; - -public class ESTreeFactory { - - ESTree.Node from(Node node) { - switch (node.getType()) { - case ProgramType: - return fromProgram(node); - default: - throw new IllegalArgumentException("Unsupported node type: " + node.getType()); - } - } - - ESTree.Program fromProgram(Node node) { - var program = node.getProgram(); - return new ESTree.Program(fromLocation(node.getLoc()), program.getSourceType(), from(program.getBodyList())); - } - - private List from(List bodyList) { - return bodyList.stream().map(this::from).collect(Collectors.toList()); - } - - - private static ESTree.Location fromLocation(SourceLocation location) { - return new ESTree.Location(new ESTree.Position(location.getStart().getLine(), location.getStart().getEnd()), - new ESTree.Position(location.getEnd().getLine(), location.getEnd().getEnd())); - } - -} diff --git a/sonar-plugin/bridge/src/main/protobuf/estree.proto b/sonar-plugin/bridge/src/main/protobuf/estree.proto index 753d6f4bce1..83294f27eaf 100644 --- a/sonar-plugin/bridge/src/main/protobuf/estree.proto +++ b/sonar-plugin/bridge/src/main/protobuf/estree.proto @@ -1,182 +1,28 @@ syntax = "proto3"; // Generated for @types/estree version: 1.0.5 -option java_package="org.sonar.plugins.javascript.bridge.protobuf"; -option java_multiple_files = true; - -message SourceLocation { - string source = 1; - Position start = 2; - Position end = 3; -} -message Position { - int32 line = 1; - int32 end = 2; -} - -enum NodeType { - ProgramType = 0; - ExportAllDeclarationType = 2; - BigIntLiteralType = 3; - SimpleLiteralType = 4; - IdentifierType = 5; - ExportDefaultDeclarationType = 6; - YieldExpressionType = 7; - UpdateExpressionType = 8; - UnaryExpressionType = 9; - ThisExpressionType = 10; - TemplateLiteralType = 11; - TaggedTemplateExpressionType = 12; - SequenceExpressionType = 13; - ObjectExpressionType = 14; - SpreadElementType = 15; - PropertyType = 16; - AssignmentPatternType = 17; - RestElementType = 18; - ArrayPatternType = 19; - ObjectPatternType = 20; - PrivateIdentifierType = 21; - NewExpressionType = 22; - SuperType = 23; - MetaPropertyType = 24; - MemberExpressionType = 25; - LogicalExpressionType = 26; - ImportExpressionType = 27; - BlockStatementType = 28; - ConditionalExpressionType = 29; - ClassExpressionType = 30; - ClassBodyType = 31; - StaticBlockType = 32; - PropertyDefinitionType = 33; - MethodDefinitionType = 34; - ChainExpressionType = 35; - SimpleCallExpressionType = 36; - BinaryExpressionType = 37; - AwaitExpressionType = 38; - AssignmentExpressionType = 39; - ArrowFunctionExpressionType = 40; - ArrayExpressionType = 41; - MaybeNamedClassDeclarationType = 42; - MaybeNamedFunctionDeclarationType = 43; - ExportNamedDeclarationType = 44; - ExportSpecifierType = 45; - VariableDeclarationType = 46; - VariableDeclaratorType = 47; - ImportDeclarationType = 48; - ImportNamespaceSpecifierType = 49; - ImportDefaultSpecifierType = 50; - ImportSpecifierType = 51; - ForOfStatementType = 52; - ForInStatementType = 53; - ForStatementType = 54; - DoWhileStatementType = 55; - WhileStatementType = 56; - TryStatementType = 57; - CatchClauseType = 58; - ThrowStatementType = 59; - SwitchStatementType = 60; - SwitchCaseType = 61; - IfStatementType = 62; - ContinueStatementType = 63; - BreakStatementType = 64; - LabeledStatementType = 65; - ReturnStatementType = 66; - WithStatementType = 67; - DebuggerStatementType = 68; - EmptyStatementType = 69; - ExpressionStatementType = 70; - DirectiveType = 71; - RegExpLiteralType = 72; - TemplateElementType = 73; - FunctionExpressionType = 74; -} -message Node { - NodeType type = 1; - SourceLocation loc = 2; - oneof node { - Program program = 3; - ExportAllDeclaration exportAllDeclaration = 4; - BigIntLiteral bigIntLiteral = 5; - SimpleLiteral simpleLiteral = 6; - Identifier identifier = 7; - ExportDefaultDeclaration exportDefaultDeclaration = 8; - YieldExpression yieldExpression = 9; - UpdateExpression updateExpression = 10; - UnaryExpression unaryExpression = 11; - ThisExpression thisExpression = 12; - TemplateLiteral templateLiteral = 13; - TaggedTemplateExpression taggedTemplateExpression = 14; - SequenceExpression sequenceExpression = 15; - ObjectExpression objectExpression = 16; - SpreadElement spreadElement = 17; - Property property = 18; - AssignmentPattern assignmentPattern = 19; - RestElement restElement = 20; - ArrayPattern arrayPattern = 21; - ObjectPattern objectPattern = 22; - PrivateIdentifier privateIdentifier = 23; - NewExpression newExpression = 24; - Super super = 25; - MetaProperty metaProperty = 26; - MemberExpression memberExpression = 27; - LogicalExpression logicalExpression = 28; - ImportExpression importExpression = 29; - BlockStatement blockStatement = 30; - ConditionalExpression conditionalExpression = 31; - ClassExpression classExpression = 32; - ClassBody classBody = 33; - StaticBlock staticBlock = 34; - PropertyDefinition propertyDefinition = 35; - MethodDefinition methodDefinition = 36; - ChainExpression chainExpression = 37; - SimpleCallExpression simpleCallExpression = 38; - BinaryExpression binaryExpression = 39; - AwaitExpression awaitExpression = 40; - AssignmentExpression assignmentExpression = 41; - ArrowFunctionExpression arrowFunctionExpression = 42; - ArrayExpression arrayExpression = 43; - MaybeNamedClassDeclaration maybeNamedClassDeclaration = 44; - MaybeNamedFunctionDeclaration maybeNamedFunctionDeclaration = 45; - ExportNamedDeclaration exportNamedDeclaration = 46; - ExportSpecifier exportSpecifier = 47; - VariableDeclaration variableDeclaration = 48; - VariableDeclarator variableDeclarator = 49; - ImportDeclaration importDeclaration = 50; - ImportNamespaceSpecifier importNamespaceSpecifier = 51; - ImportDefaultSpecifier importDefaultSpecifier = 52; - ImportSpecifier importSpecifier = 53; - ForOfStatement forOfStatement = 54; - ForInStatement forInStatement = 55; - ForStatement forStatement = 56; - DoWhileStatement doWhileStatement = 57; - WhileStatement whileStatement = 58; - TryStatement tryStatement = 59; - CatchClause catchClause = 60; - ThrowStatement throwStatement = 61; - SwitchStatement switchStatement = 62; - SwitchCase switchCase = 63; - IfStatement ifStatement = 64; - ContinueStatement continueStatement = 65; - BreakStatement breakStatement = 66; - LabeledStatement labeledStatement = 67; - ReturnStatement returnStatement = 68; - WithStatement withStatement = 69; - DebuggerStatement debuggerStatement = 70; - EmptyStatement emptyStatement = 71; - ExpressionStatement expressionStatement = 72; - Directive directive = 73; - RegExpLiteral regExpLiteral = 74; - TemplateElement templateElement = 75; - FunctionExpression functionExpression = 76; - } -} message Program { string sourceType = 1; - repeated Node body = 2; + repeated BaseNodeWithoutComments body = 2; +} +message ModuleDeclaration { + oneof moduleDeclaration { + ImportDeclaration moduleDeclaration_importDeclaration = 1; + ExportNamedDeclaration moduleDeclaration_exportNamedDeclaration = 2; + ExportDefaultDeclaration moduleDeclaration_exportDefaultDeclaration = 3; + ExportAllDeclaration moduleDeclaration_exportAllDeclaration = 4; + } } message ExportAllDeclaration { - Node exported = 1; - Node source = 2; + Identifier exported = 1; + Literal source = 2; +} +message Literal { + oneof literal { + SimpleLiteral literal_simpleLiteral = 1; + RegExpLiteral literal_regExpLiteral = 2; + BigIntLiteral literal_bigIntLiteral = 3; + } } message BigIntLiteral { int32 value = 1; @@ -195,270 +41,440 @@ message Identifier { string name = 1; } message ExportDefaultDeclaration { - Node declaration = 1; + oneof declaration { + MaybeNamedFunctionDeclaration declaration_maybeNamedFunctionDeclaration = 1; + MaybeNamedClassDeclaration declaration_maybeNamedClassDeclaration = 2; + Expression declaration_expression = 3; + } +} +message Expression { + oneof expression { + ArrayExpression expression_arrayExpression = 1; + ArrowFunctionExpression expression_arrowFunctionExpression = 2; + AssignmentExpression expression_assignmentExpression = 3; + AwaitExpression expression_awaitExpression = 4; + BinaryExpression expression_binaryExpression = 5; + CallExpression expression_callExpression = 6; + ChainExpression expression_chainExpression = 7; + ClassExpression expression_classExpression = 8; + ConditionalExpression expression_conditionalExpression = 9; + FunctionExpression expression_functionExpression = 10; + Identifier expression_identifier = 11; + ImportExpression expression_importExpression = 12; + Literal expression_literal = 13; + LogicalExpression expression_logicalExpression = 14; + MemberExpression expression_memberExpression = 15; + MetaProperty expression_metaProperty = 16; + NewExpression expression_newExpression = 17; + ObjectExpression expression_objectExpression = 18; + SequenceExpression expression_sequenceExpression = 19; + TaggedTemplateExpression expression_taggedTemplateExpression = 20; + TemplateLiteral expression_templateLiteral = 21; + ThisExpression expression_thisExpression = 22; + UnaryExpression expression_unaryExpression = 23; + UpdateExpression expression_updateExpression = 24; + YieldExpression expression_yieldExpression = 25; + } } message YieldExpression { - Node argument = 1; + Expression argument = 1; bool delegate = 2; } message UpdateExpression { - string operator = 1; - Node argument = 2; + UpdateOperator operator = 1; + Expression argument = 2; bool prefix = 3; } +message UpdateOperator { + string updateOperator = 1; +} message UnaryExpression { - string operator = 1; + UnaryOperator operator = 1; bool prefix = 2; - Node argument = 3; + Expression argument = 3; +} +message UnaryOperator { + string unaryOperator = 1; } message ThisExpression { } message TemplateLiteral { - repeated Node quasis = 1; - repeated Node expressions = 2; + repeated TemplateElement quasis = 1; + repeated Expression expressions = 2; } message TaggedTemplateExpression { - Node tag = 1; - Node quasi = 2; + Expression tag = 1; + TemplateLiteral quasi = 2; } message SequenceExpression { - repeated Node expressions = 1; + repeated Expression expressions = 1; } message ObjectExpression { - repeated Node properties = 1; + repeated BaseNodeWithoutComments properties = 1; } message SpreadElement { - Node argument = 1; + Expression argument = 1; } message Property { - Node key = 1; - Node value = 2; - string kind = 3; - bool method = 4; - bool shorthand = 5; - bool computed = 6; + oneof key { + Expression key_expression = 1; + PrivateIdentifier key_privateIdentifier = 2; + } + oneof value { + Expression value_expression = 3; + Pattern value_pattern = 4; + } + string kind = 5; + bool method = 6; + bool shorthand = 7; + bool computed = 8; +} +message Pattern { + oneof pattern { + Identifier pattern_identifier = 1; + ObjectPattern pattern_objectPattern = 2; + ArrayPattern pattern_arrayPattern = 3; + RestElement pattern_restElement = 4; + AssignmentPattern pattern_assignmentPattern = 5; + MemberExpression pattern_memberExpression = 6; + } } message AssignmentPattern { - Node left = 1; - Node right = 2; + Pattern left = 1; + Expression right = 2; } message RestElement { - Node argument = 1; + Pattern argument = 1; } message ArrayPattern { - repeated Node elements = 1; + repeated Pattern elements = 1; } message ObjectPattern { - repeated Node properties = 1; + repeated BaseNodeWithoutComments properties = 1; +} +message AssignmentProperty { + Pattern value = 1; + string kind = 2; + bool method = 3; + oneof key { + Expression key_expression = 4; + PrivateIdentifier key_privateIdentifier = 5; + } + bool shorthand = 6; + bool computed = 7; } message PrivateIdentifier { string name = 1; } message NewExpression { - Node callee = 1; - repeated Node arguments = 2; + oneof callee { + Expression callee_expression = 1; + Super callee_super = 2; + } + repeated BaseNodeWithoutComments arguments = 3; } message Super { } message MetaProperty { - Node meta = 1; - Node property = 2; + Identifier meta = 1; + Identifier property = 2; } message MemberExpression { - Node object = 1; - Node property = 2; - bool computed = 3; - bool optional = 4; + oneof object { + Expression object_expression = 1; + Super object_super = 2; + } + oneof property { + Expression property_expression = 3; + PrivateIdentifier property_privateIdentifier = 4; + } + bool computed = 5; + bool optional = 6; } message LogicalExpression { - Node operator = 1; - Node left = 2; - Node right = 3; + LogicalOperator operator = 1; + Expression left = 2; + Expression right = 3; +} +message LogicalOperator { + string logicalOperator = 1; } message ImportExpression { - Node source = 1; + Expression source = 1; +} +message FunctionExpression { + Identifier id = 1; + BlockStatement body = 2; + repeated Pattern params = 3; + bool generator = 4; + bool async = 5; } message BlockStatement { - repeated Node body = 1; + repeated Statement body = 1; } message ConditionalExpression { - Node test = 1; - Node alternate = 2; - Node consequent = 3; + Expression test = 1; + Expression alternate = 2; + Expression consequent = 3; } message ClassExpression { - Node id = 1; - Node superClass = 2; - Node body = 3; + Identifier id = 1; + Expression superClass = 2; + ClassBody body = 3; } message ClassBody { - repeated Node body = 1; + repeated BaseNodeWithoutComments body = 1; } message StaticBlock { } message PropertyDefinition { - Node key = 1; - Node value = 2; - bool computed = 3; - bool static = 4; -} -message MethodDefinition { - Node key = 1; - Node value = 2; - string kind = 3; + oneof key { + Expression key_expression = 1; + PrivateIdentifier key_privateIdentifier = 2; + } + Expression value = 3; bool computed = 4; bool static = 5; } +message MethodDefinition { + oneof key { + Expression key_expression = 1; + PrivateIdentifier key_privateIdentifier = 2; + } + FunctionExpression value = 3; + string kind = 4; + bool computed = 5; + bool static = 6; +} message ChainExpression { - Node expression = 1; + ChainElement expression = 1; +} +message ChainElement { + oneof chainElement { + SimpleCallExpression chainElement_simpleCallExpression = 1; + MemberExpression chainElement_memberExpression = 2; + } } message SimpleCallExpression { bool optional = 1; - Node callee = 2; - repeated Node arguments = 3; + oneof callee { + Expression callee_expression = 2; + Super callee_super = 3; + } + repeated BaseNodeWithoutComments arguments = 4; +} +message CallExpression { + oneof callExpression { + SimpleCallExpression callExpression_simpleCallExpression = 1; + NewExpression callExpression_newExpression = 2; + } } message BinaryExpression { - string operator = 1; - Node left = 2; - Node right = 3; + BinaryOperator operator = 1; + Expression left = 2; + Expression right = 3; +} +message BinaryOperator { + string binaryOperator = 1; } message AwaitExpression { - Node argument = 1; + Expression argument = 1; } message AssignmentExpression { - string operator = 1; - Node left = 2; - Node right = 3; + AssignmentOperator operator = 1; + oneof left { + Pattern left_pattern = 2; + MemberExpression left_memberExpression = 3; + } + Expression right = 4; +} +message AssignmentOperator { + string assignmentOperator = 1; } message ArrowFunctionExpression { bool expression = 1; - Node body = 2; - repeated Node params = 3; - bool generator = 4; - bool async = 5; + oneof body { + BlockStatement body_blockStatement = 2; + Expression body_expression = 3; + } + repeated Pattern params = 4; + bool generator = 5; + bool async = 6; } message ArrayExpression { - repeated Node elements = 1; + repeated BaseNodeWithoutComments elements = 1; } message MaybeNamedClassDeclaration { - Node id = 1; - Node superClass = 2; - Node body = 3; + Identifier id = 1; + Expression superClass = 2; + ClassBody body = 3; } message MaybeNamedFunctionDeclaration { - Node id = 1; - Node body = 2; - repeated Node params = 3; + Identifier id = 1; + BlockStatement body = 2; + repeated Pattern params = 3; bool generator = 4; bool async = 5; } message ExportNamedDeclaration { - Node declaration = 1; - repeated Node specifiers = 2; - Node source = 3; + Declaration declaration = 1; + repeated ExportSpecifier specifiers = 2; + Literal source = 3; } message ExportSpecifier { - Node exported = 1; - Node local = 2; + Identifier exported = 1; + Identifier local = 2; +} +message Declaration { + oneof declaration { + FunctionDeclaration declaration_functionDeclaration = 1; + VariableDeclaration declaration_variableDeclaration = 2; + ClassDeclaration declaration_classDeclaration = 3; + } +} +message ClassDeclaration { + Identifier id = 1; + Expression superClass = 2; + ClassBody body = 3; } message VariableDeclaration { - repeated Node declarations = 1; + repeated VariableDeclarator declarations = 1; string kind = 2; } message VariableDeclarator { - Node id = 1; - Node init = 2; + Pattern id = 1; + Expression init = 2; +} +message FunctionDeclaration { + Identifier id = 1; + BlockStatement body = 2; + repeated Pattern params = 3; + bool generator = 4; + bool async = 5; } message ImportDeclaration { - repeated Node specifiers = 1; - Node source = 2; + repeated BaseNodeWithoutComments specifiers = 1; + Literal source = 2; } message ImportNamespaceSpecifier { - Node local = 1; + Identifier local = 1; } message ImportDefaultSpecifier { - Node local = 1; + Identifier local = 1; } message ImportSpecifier { - Node imported = 1; - Node local = 2; + Identifier imported = 1; + Identifier local = 2; +} +message Statement { + oneof statement { + ExpressionStatement statement_expressionStatement = 1; + BlockStatement statement_blockStatement = 2; + StaticBlock statement_staticBlock = 3; + EmptyStatement statement_emptyStatement = 4; + DebuggerStatement statement_debuggerStatement = 5; + WithStatement statement_withStatement = 6; + ReturnStatement statement_returnStatement = 7; + LabeledStatement statement_labeledStatement = 8; + BreakStatement statement_breakStatement = 9; + ContinueStatement statement_continueStatement = 10; + IfStatement statement_ifStatement = 11; + SwitchStatement statement_switchStatement = 12; + ThrowStatement statement_throwStatement = 13; + TryStatement statement_tryStatement = 14; + WhileStatement statement_whileStatement = 15; + DoWhileStatement statement_doWhileStatement = 16; + ForStatement statement_forStatement = 17; + ForInStatement statement_forInStatement = 18; + ForOfStatement statement_forOfStatement = 19; + Declaration statement_declaration = 20; + } } message ForOfStatement { bool await = 1; - Node left = 2; - Node right = 3; - Node body = 4; + oneof left { + VariableDeclaration left_variableDeclaration = 2; + Pattern left_pattern = 3; + } + Expression right = 4; + Statement body = 5; } message ForInStatement { - Node left = 1; - Node right = 2; - Node body = 3; + oneof left { + VariableDeclaration left_variableDeclaration = 1; + Pattern left_pattern = 2; + } + Expression right = 3; + Statement body = 4; } message ForStatement { - Node init = 1; - Node test = 2; - Node update = 3; - Node body = 4; + oneof init { + VariableDeclaration init_variableDeclaration = 1; + Expression init_expression = 2; + } + Expression test = 3; + Expression update = 4; + Statement body = 5; } message DoWhileStatement { - Node body = 1; - Node test = 2; + Statement body = 1; + Expression test = 2; } message WhileStatement { - Node test = 1; - Node body = 2; + Expression test = 1; + Statement body = 2; } message TryStatement { - Node block = 1; - Node handler = 2; - Node finalizer = 3; + BlockStatement block = 1; + CatchClause handler = 2; + BlockStatement finalizer = 3; } message CatchClause { - Node param = 1; - Node body = 2; + Pattern param = 1; + BlockStatement body = 2; } message ThrowStatement { - Node argument = 1; + Expression argument = 1; } message SwitchStatement { - Node discriminant = 1; - repeated Node cases = 2; + Expression discriminant = 1; + repeated SwitchCase cases = 2; } message SwitchCase { - Node test = 1; - repeated Node consequent = 2; + Expression test = 1; + repeated Statement consequent = 2; } message IfStatement { - Node test = 1; - Node consequent = 2; - Node alternate = 3; + Expression test = 1; + Statement consequent = 2; + Statement alternate = 3; } message ContinueStatement { - Node label = 1; + Identifier label = 1; } message BreakStatement { - Node label = 1; + Identifier label = 1; } message LabeledStatement { - Node label = 1; - Node body = 2; + Identifier label = 1; + Statement body = 2; } message ReturnStatement { - Node argument = 1; + Expression argument = 1; } message WithStatement { - Node object = 1; - Node body = 2; + Expression object = 1; + Statement body = 2; } message DebuggerStatement { } message EmptyStatement { } message ExpressionStatement { - Node expression = 1; + Expression expression = 1; } message Directive { - Node expression = 1; + Literal expression = 1; string directive = 2; } message RegExpLiteral { @@ -471,11 +487,108 @@ message TemplateElement { string cooked = 2; string raw = 3; } - -message FunctionExpression { - Node id = 1; - Node body = 2; - repeated Node params = 3; - bool generator = 4; - bool async = 5; +message BaseNodeWithoutComments { + string type = 1; + SourceLocation loc = 2; + oneof node { + Program node_program = 3; + ModuleDeclaration node_moduleDeclaration = 4; + ExportAllDeclaration node_exportAllDeclaration = 5; + Literal node_literal = 6; + BigIntLiteral node_bigIntLiteral = 7; + SimpleLiteral node_simpleLiteral = 8; + Identifier node_identifier = 9; + ExportDefaultDeclaration node_exportDefaultDeclaration = 10; + Expression node_expression = 11; + YieldExpression node_yieldExpression = 12; + UpdateExpression node_updateExpression = 13; + UpdateOperator node_updateOperator = 14; + UnaryExpression node_unaryExpression = 15; + UnaryOperator node_unaryOperator = 16; + ThisExpression node_thisExpression = 17; + TemplateLiteral node_templateLiteral = 18; + TaggedTemplateExpression node_taggedTemplateExpression = 19; + SequenceExpression node_sequenceExpression = 20; + ObjectExpression node_objectExpression = 21; + SpreadElement node_spreadElement = 22; + Property node_property = 23; + Pattern node_pattern = 24; + AssignmentPattern node_assignmentPattern = 25; + RestElement node_restElement = 26; + ArrayPattern node_arrayPattern = 27; + ObjectPattern node_objectPattern = 28; + AssignmentProperty node_assignmentProperty = 29; + PrivateIdentifier node_privateIdentifier = 30; + NewExpression node_newExpression = 31; + Super node_super = 32; + MetaProperty node_metaProperty = 33; + MemberExpression node_memberExpression = 34; + LogicalExpression node_logicalExpression = 35; + LogicalOperator node_logicalOperator = 36; + ImportExpression node_importExpression = 37; + FunctionExpression node_functionExpression = 38; + BlockStatement node_blockStatement = 39; + ConditionalExpression node_conditionalExpression = 40; + ClassExpression node_classExpression = 41; + ClassBody node_classBody = 42; + StaticBlock node_staticBlock = 43; + PropertyDefinition node_propertyDefinition = 44; + MethodDefinition node_methodDefinition = 45; + ChainExpression node_chainExpression = 46; + ChainElement node_chainElement = 47; + SimpleCallExpression node_simpleCallExpression = 48; + CallExpression node_callExpression = 49; + BinaryExpression node_binaryExpression = 50; + BinaryOperator node_binaryOperator = 51; + AwaitExpression node_awaitExpression = 52; + AssignmentExpression node_assignmentExpression = 53; + AssignmentOperator node_assignmentOperator = 54; + ArrowFunctionExpression node_arrowFunctionExpression = 55; + ArrayExpression node_arrayExpression = 56; + MaybeNamedClassDeclaration node_maybeNamedClassDeclaration = 57; + MaybeNamedFunctionDeclaration node_maybeNamedFunctionDeclaration = 58; + ExportNamedDeclaration node_exportNamedDeclaration = 59; + ExportSpecifier node_exportSpecifier = 60; + Declaration node_declaration = 61; + ClassDeclaration node_classDeclaration = 62; + VariableDeclaration node_variableDeclaration = 63; + VariableDeclarator node_variableDeclarator = 64; + FunctionDeclaration node_functionDeclaration = 65; + ImportDeclaration node_importDeclaration = 66; + ImportNamespaceSpecifier node_importNamespaceSpecifier = 67; + ImportDefaultSpecifier node_importDefaultSpecifier = 68; + ImportSpecifier node_importSpecifier = 69; + Statement node_statement = 70; + ForOfStatement node_forOfStatement = 71; + ForInStatement node_forInStatement = 72; + ForStatement node_forStatement = 73; + DoWhileStatement node_doWhileStatement = 74; + WhileStatement node_whileStatement = 75; + TryStatement node_tryStatement = 76; + CatchClause node_catchClause = 77; + ThrowStatement node_throwStatement = 78; + SwitchStatement node_switchStatement = 79; + SwitchCase node_switchCase = 80; + IfStatement node_ifStatement = 81; + ContinueStatement node_continueStatement = 82; + BreakStatement node_breakStatement = 83; + LabeledStatement node_labeledStatement = 84; + ReturnStatement node_returnStatement = 85; + WithStatement node_withStatement = 86; + DebuggerStatement node_debuggerStatement = 87; + EmptyStatement node_emptyStatement = 88; + ExpressionStatement node_expressionStatement = 89; + Directive node_directive = 90; + RegExpLiteral node_regExpLiteral = 91; + TemplateElement node_templateElement = 92; + } +} +message SourceLocation { + string source = 1; + Position start = 2; + Position end = 3; +} +message Position { + int32 line = 1; + int32 end = 2; } diff --git a/tools/estree/generate-proto-file.ts b/tools/estree/generate-proto-file.ts index cb21f532a2b..bf6b463c861 100644 --- a/tools/estree/generate-proto-file.ts +++ b/tools/estree/generate-proto-file.ts @@ -67,8 +67,8 @@ export function addHandWrittenMessages(messages: Record) { export function writeMessagesToDir(messages: Record, outputDir: string) { addHandWrittenMessages(messages); fs.writeFileSync( - path.join(outputDir, 'estreeProto.proto'), - addPrefix(translateToProtoFormat(messages), messages), + path.join(outputDir, 'estree.proto'), + addPrefix(translateToProtoFormat(messages)), ); /** * Translate the messages to a protobuf file format. @@ -103,19 +103,7 @@ export function writeMessagesToDir(messages: Record, outputD return lines.join('\n'); } - function addPrefix(protoData: string, messages: Record) { - return `syntax = "proto3"; -// Generated for @types/estree version: ${typesVersion} -option java_package="org.sonar.plugins.javascript.bridge.protobuf"; -option java_multiple_files = true; - -enum NodeType { -${Object.keys(messages) - .sort() - .map((n, i) => `${n} = ${i}`) - .join(';\n')} -} - -${protoData}`; + function addPrefix(protoData: string) { + return `syntax = "proto3";\n// Generated for @types/estree version: ${typesVersion}\n\n${protoData}`; } } diff --git a/tools/estree/output/estreeProto.proto b/tools/estree/output/estreeProto.proto deleted file mode 100644 index 65ff2aedf55..00000000000 --- a/tools/estree/output/estreeProto.proto +++ /dev/null @@ -1,692 +0,0 @@ -syntax = "proto3"; -// Generated for @types/estree version: 1.0.5 -option java_package="org.sonar.plugins.javascript.bridge.protobuf"; -option java_multiple_files = true; - -enum NodeType { -ArrayExpression = 0; -ArrayPattern = 1; -ArrowFunctionExpression = 2; -AssignmentExpression = 3; -AssignmentOperator = 4; -AssignmentPattern = 5; -AssignmentProperty = 6; -AwaitExpression = 7; -BaseNodeWithoutComments = 8; -BigIntLiteral = 9; -BinaryExpression = 10; -BinaryOperator = 11; -BlockStatement = 12; -BreakStatement = 13; -CallExpression = 14; -CatchClause = 15; -ChainElement = 16; -ChainExpression = 17; -ClassBody = 18; -ClassDeclaration = 19; -ClassExpression = 20; -ConditionalExpression = 21; -ContinueStatement = 22; -DebuggerStatement = 23; -Declaration = 24; -Directive = 25; -DoWhileStatement = 26; -EmptyStatement = 27; -ExportAllDeclaration = 28; -ExportDefaultDeclaration = 29; -ExportNamedDeclaration = 30; -ExportSpecifier = 31; -Expression = 32; -ExpressionStatement = 33; -ForInStatement = 34; -ForOfStatement = 35; -ForStatement = 36; -FunctionDeclaration = 37; -FunctionExpression = 38; -Identifier = 39; -IfStatement = 40; -ImportDeclaration = 41; -ImportDefaultSpecifier = 42; -ImportExpression = 43; -ImportNamespaceSpecifier = 44; -ImportSpecifier = 45; -LabeledStatement = 46; -Literal = 47; -LogicalExpression = 48; -LogicalOperator = 49; -MaybeNamedClassDeclaration = 50; -MaybeNamedFunctionDeclaration = 51; -MemberExpression = 52; -MetaProperty = 53; -MethodDefinition = 54; -ModuleDeclaration = 55; -NewExpression = 56; -ObjectExpression = 57; -ObjectPattern = 58; -Pattern = 59; -Position = 60; -PrivateIdentifier = 61; -Program = 62; -Property = 63; -PropertyDefinition = 64; -RegExpLiteral = 65; -RestElement = 66; -ReturnStatement = 67; -SequenceExpression = 68; -SimpleCallExpression = 69; -SimpleLiteral = 70; -SourceLocation = 71; -SpreadElement = 72; -Statement = 73; -StaticBlock = 74; -Super = 75; -SwitchCase = 76; -SwitchStatement = 77; -TaggedTemplateExpression = 78; -TemplateElement = 79; -TemplateLiteral = 80; -ThisExpression = 81; -ThrowStatement = 82; -TryStatement = 83; -UnaryExpression = 84; -UnaryOperator = 85; -UpdateExpression = 86; -UpdateOperator = 87; -VariableDeclaration = 88; -VariableDeclarator = 89; -WhileStatement = 90; -WithStatement = 91; -YieldExpression = 92 -} - -message RegExpLiteral { - string pattern = 1; - string flags = 2; - string raw = 3; -} -message TemplateElement { - bool tail = 1; - string cooked = 2; - string raw = 3; -} -message Program { - string sourceType = 1; - repeated BaseNodeWithoutComments body = 2; -} -message ModuleDeclaration { - oneof moduleDeclaration { - ImportDeclaration moduleDeclaration_importDeclaration = 1; - ExportNamedDeclaration moduleDeclaration_exportNamedDeclaration = 2; - ExportDefaultDeclaration moduleDeclaration_exportDefaultDeclaration = 3; - ExportAllDeclaration moduleDeclaration_exportAllDeclaration = 4; - } -} -message ExportAllDeclaration { - Identifier exported = 1; - Literal source = 2; -} -message Literal { - oneof literal { - SimpleLiteral literal_simpleLiteral = 1; - RegExpLiteral literal_regExpLiteral = 2; - BigIntLiteral literal_bigIntLiteral = 3; - } -} -message BigIntLiteral { - int32 value = 1; - string bigint = 2; - string raw = 3; -} -message SimpleLiteral { - oneof value { - string value_string = 1; - bool value_boolean = 2; - int32 value_number = 3; - } - string raw = 4; -} -message Identifier { - string name = 1; -} -message ExportDefaultDeclaration { - oneof declaration { - MaybeNamedFunctionDeclaration declaration_maybeNamedFunctionDeclaration = 1; - MaybeNamedClassDeclaration declaration_maybeNamedClassDeclaration = 2; - Expression declaration_expression = 3; - } -} -message Expression { - oneof expression { - ArrayExpression expression_arrayExpression = 1; - ArrowFunctionExpression expression_arrowFunctionExpression = 2; - AssignmentExpression expression_assignmentExpression = 3; - AwaitExpression expression_awaitExpression = 4; - BinaryExpression expression_binaryExpression = 5; - CallExpression expression_callExpression = 6; - ChainExpression expression_chainExpression = 7; - ClassExpression expression_classExpression = 8; - ConditionalExpression expression_conditionalExpression = 9; - FunctionExpression expression_functionExpression = 10; - Identifier expression_identifier = 11; - ImportExpression expression_importExpression = 12; - Literal expression_literal = 13; - LogicalExpression expression_logicalExpression = 14; - MemberExpression expression_memberExpression = 15; - MetaProperty expression_metaProperty = 16; - NewExpression expression_newExpression = 17; - ObjectExpression expression_objectExpression = 18; - SequenceExpression expression_sequenceExpression = 19; - TaggedTemplateExpression expression_taggedTemplateExpression = 20; - TemplateLiteral expression_templateLiteral = 21; - ThisExpression expression_thisExpression = 22; - UnaryExpression expression_unaryExpression = 23; - UpdateExpression expression_updateExpression = 24; - YieldExpression expression_yieldExpression = 25; - } -} -message YieldExpression { - Expression argument = 1; - bool delegate = 2; -} -message UpdateExpression { - UpdateOperator operator = 1; - Expression argument = 2; - bool prefix = 3; -} -message UpdateOperator { - string updateOperator = 1; -} -message UnaryExpression { - UnaryOperator operator = 1; - bool prefix = 2; - Expression argument = 3; -} -message UnaryOperator { - string unaryOperator = 1; -} -message ThisExpression { -} -message TemplateLiteral { - repeated TemplateElement quasis = 1; - repeated Expression expressions = 2; -} -message TaggedTemplateExpression { - Expression tag = 1; - TemplateLiteral quasi = 2; -} -message SequenceExpression { - repeated Expression expressions = 1; -} -message ObjectExpression { - repeated BaseNodeWithoutComments properties = 1; -} -message SpreadElement { - Expression argument = 1; -} -message Property { - oneof key { - Expression key_expression = 1; - PrivateIdentifier key_privateIdentifier = 2; - } - oneof value { - Expression value_expression = 3; - Pattern value_pattern = 4; - } - string kind = 5; - bool method = 6; - bool shorthand = 7; - bool computed = 8; -} -message Pattern { - oneof pattern { - Identifier pattern_identifier = 1; - ObjectPattern pattern_objectPattern = 2; - ArrayPattern pattern_arrayPattern = 3; - RestElement pattern_restElement = 4; - AssignmentPattern pattern_assignmentPattern = 5; - MemberExpression pattern_memberExpression = 6; - } -} -message AssignmentPattern { - Pattern left = 1; - Expression right = 2; -} -message RestElement { - Pattern argument = 1; -} -message ArrayPattern { - repeated Pattern elements = 1; -} -message ObjectPattern { - repeated BaseNodeWithoutComments properties = 1; -} -message AssignmentProperty { - Pattern value = 1; - string kind = 2; - bool method = 3; - oneof key { - Expression key_expression = 4; - PrivateIdentifier key_privateIdentifier = 5; - } - bool shorthand = 6; - bool computed = 7; -} -message PrivateIdentifier { - string name = 1; -} -message NewExpression { - oneof callee { - Expression callee_expression = 1; - Super callee_super = 2; - } - repeated BaseNodeWithoutComments arguments = 3; -} -message Super { -} -message MetaProperty { - Identifier meta = 1; - Identifier property = 2; -} -message MemberExpression { - oneof object { - Expression object_expression = 1; - Super object_super = 2; - } - oneof property { - Expression property_expression = 3; - PrivateIdentifier property_privateIdentifier = 4; - } - bool computed = 5; - bool optional = 6; -} -message LogicalExpression { - LogicalOperator operator = 1; - Expression left = 2; - Expression right = 3; -} -message LogicalOperator { - string logicalOperator = 1; -} -message ImportExpression { - Expression source = 1; -} -message FunctionExpression { - Identifier id = 1; - BlockStatement body = 2; - repeated Pattern params = 3; - bool generator = 4; - bool async = 5; -} -message BlockStatement { - repeated Statement body = 1; -} -message ConditionalExpression { - Expression test = 1; - Expression alternate = 2; - Expression consequent = 3; -} -message ClassExpression { - Identifier id = 1; - Expression superClass = 2; - ClassBody body = 3; -} -message ClassBody { - repeated BaseNodeWithoutComments body = 1; -} -message StaticBlock { -} -message PropertyDefinition { - oneof key { - Expression key_expression = 1; - PrivateIdentifier key_privateIdentifier = 2; - } - Expression value = 3; - bool computed = 4; - bool static = 5; -} -message MethodDefinition { - oneof key { - Expression key_expression = 1; - PrivateIdentifier key_privateIdentifier = 2; - } - FunctionExpression value = 3; - string kind = 4; - bool computed = 5; - bool static = 6; -} -message ChainExpression { - ChainElement expression = 1; -} -message ChainElement { - oneof chainElement { - SimpleCallExpression chainElement_simpleCallExpression = 1; - MemberExpression chainElement_memberExpression = 2; - } -} -message SimpleCallExpression { - bool optional = 1; - oneof callee { - Expression callee_expression = 2; - Super callee_super = 3; - } - repeated BaseNodeWithoutComments arguments = 4; -} -message CallExpression { - oneof callExpression { - SimpleCallExpression callExpression_simpleCallExpression = 1; - NewExpression callExpression_newExpression = 2; - } -} -message BinaryExpression { - BinaryOperator operator = 1; - Expression left = 2; - Expression right = 3; -} -message BinaryOperator { - string binaryOperator = 1; -} -message AwaitExpression { - Expression argument = 1; -} -message AssignmentExpression { - AssignmentOperator operator = 1; - oneof left { - Pattern left_pattern = 2; - MemberExpression left_memberExpression = 3; - } - Expression right = 4; -} -message AssignmentOperator { - string assignmentOperator = 1; -} -message ArrowFunctionExpression { - bool expression = 1; - oneof body { - BlockStatement body_blockStatement = 2; - Expression body_expression = 3; - } - repeated Pattern params = 4; - bool generator = 5; - bool async = 6; -} -message ArrayExpression { - repeated BaseNodeWithoutComments elements = 1; -} -message MaybeNamedClassDeclaration { - Identifier id = 1; - Expression superClass = 2; - ClassBody body = 3; -} -message MaybeNamedFunctionDeclaration { - Identifier id = 1; - BlockStatement body = 2; - repeated Pattern params = 3; - bool generator = 4; - bool async = 5; -} -message ExportNamedDeclaration { - Declaration declaration = 1; - repeated ExportSpecifier specifiers = 2; - Literal source = 3; -} -message ExportSpecifier { - Identifier exported = 1; - Identifier local = 2; -} -message Declaration { - oneof declaration { - FunctionDeclaration declaration_functionDeclaration = 1; - VariableDeclaration declaration_variableDeclaration = 2; - ClassDeclaration declaration_classDeclaration = 3; - } -} -message ClassDeclaration { - Identifier id = 1; - Expression superClass = 2; - ClassBody body = 3; -} -message VariableDeclaration { - repeated VariableDeclarator declarations = 1; - string kind = 2; -} -message VariableDeclarator { - Pattern id = 1; - Expression init = 2; -} -message FunctionDeclaration { - Identifier id = 1; - BlockStatement body = 2; - repeated Pattern params = 3; - bool generator = 4; - bool async = 5; -} -message ImportDeclaration { - repeated BaseNodeWithoutComments specifiers = 1; - Literal source = 2; -} -message ImportNamespaceSpecifier { - Identifier local = 1; -} -message ImportDefaultSpecifier { - Identifier local = 1; -} -message ImportSpecifier { - Identifier imported = 1; - Identifier local = 2; -} -message Statement { - oneof statement { - ExpressionStatement statement_expressionStatement = 1; - BlockStatement statement_blockStatement = 2; - StaticBlock statement_staticBlock = 3; - EmptyStatement statement_emptyStatement = 4; - DebuggerStatement statement_debuggerStatement = 5; - WithStatement statement_withStatement = 6; - ReturnStatement statement_returnStatement = 7; - LabeledStatement statement_labeledStatement = 8; - BreakStatement statement_breakStatement = 9; - ContinueStatement statement_continueStatement = 10; - IfStatement statement_ifStatement = 11; - SwitchStatement statement_switchStatement = 12; - ThrowStatement statement_throwStatement = 13; - TryStatement statement_tryStatement = 14; - WhileStatement statement_whileStatement = 15; - DoWhileStatement statement_doWhileStatement = 16; - ForStatement statement_forStatement = 17; - ForInStatement statement_forInStatement = 18; - ForOfStatement statement_forOfStatement = 19; - Declaration statement_declaration = 20; - } -} -message ForOfStatement { - bool await = 1; - oneof left { - VariableDeclaration left_variableDeclaration = 2; - Pattern left_pattern = 3; - } - Expression right = 4; - Statement body = 5; -} -message ForInStatement { - oneof left { - VariableDeclaration left_variableDeclaration = 1; - Pattern left_pattern = 2; - } - Expression right = 3; - Statement body = 4; -} -message ForStatement { - oneof init { - VariableDeclaration init_variableDeclaration = 1; - Expression init_expression = 2; - } - Expression test = 3; - Expression update = 4; - Statement body = 5; -} -message DoWhileStatement { - Statement body = 1; - Expression test = 2; -} -message WhileStatement { - Expression test = 1; - Statement body = 2; -} -message TryStatement { - BlockStatement block = 1; - CatchClause handler = 2; - BlockStatement finalizer = 3; -} -message CatchClause { - Pattern param = 1; - BlockStatement body = 2; -} -message ThrowStatement { - Expression argument = 1; -} -message SwitchStatement { - Expression discriminant = 1; - repeated SwitchCase cases = 2; -} -message SwitchCase { - Expression test = 1; - repeated Statement consequent = 2; -} -message IfStatement { - Expression test = 1; - Statement consequent = 2; - Statement alternate = 3; -} -message ContinueStatement { - Identifier label = 1; -} -message BreakStatement { - Identifier label = 1; -} -message LabeledStatement { - Identifier label = 1; - Statement body = 2; -} -message ReturnStatement { - Expression argument = 1; -} -message WithStatement { - Expression object = 1; - Statement body = 2; -} -message DebuggerStatement { -} -message EmptyStatement { -} -message ExpressionStatement { - Expression expression = 1; -} -message Directive { - Literal expression = 1; - string directive = 2; -} -message BaseNodeWithoutComments { - string type = 1; - SourceLocation loc = 2; - oneof node { - RegExpLiteral node_regExpLiteral = 3; - TemplateElement node_templateElement = 4; - Program node_program = 5; - ModuleDeclaration node_moduleDeclaration = 6; - ExportAllDeclaration node_exportAllDeclaration = 7; - Literal node_literal = 8; - BigIntLiteral node_bigIntLiteral = 9; - SimpleLiteral node_simpleLiteral = 10; - Identifier node_identifier = 11; - ExportDefaultDeclaration node_exportDefaultDeclaration = 12; - Expression node_expression = 13; - YieldExpression node_yieldExpression = 14; - UpdateExpression node_updateExpression = 15; - UpdateOperator node_updateOperator = 16; - UnaryExpression node_unaryExpression = 17; - UnaryOperator node_unaryOperator = 18; - ThisExpression node_thisExpression = 19; - TemplateLiteral node_templateLiteral = 20; - TaggedTemplateExpression node_taggedTemplateExpression = 21; - SequenceExpression node_sequenceExpression = 22; - ObjectExpression node_objectExpression = 23; - SpreadElement node_spreadElement = 24; - Property node_property = 25; - Pattern node_pattern = 26; - AssignmentPattern node_assignmentPattern = 27; - RestElement node_restElement = 28; - ArrayPattern node_arrayPattern = 29; - ObjectPattern node_objectPattern = 30; - AssignmentProperty node_assignmentProperty = 31; - PrivateIdentifier node_privateIdentifier = 32; - NewExpression node_newExpression = 33; - Super node_super = 34; - MetaProperty node_metaProperty = 35; - MemberExpression node_memberExpression = 36; - LogicalExpression node_logicalExpression = 37; - LogicalOperator node_logicalOperator = 38; - ImportExpression node_importExpression = 39; - FunctionExpression node_functionExpression = 40; - BlockStatement node_blockStatement = 41; - ConditionalExpression node_conditionalExpression = 42; - ClassExpression node_classExpression = 43; - ClassBody node_classBody = 44; - StaticBlock node_staticBlock = 45; - PropertyDefinition node_propertyDefinition = 46; - MethodDefinition node_methodDefinition = 47; - ChainExpression node_chainExpression = 48; - ChainElement node_chainElement = 49; - SimpleCallExpression node_simpleCallExpression = 50; - CallExpression node_callExpression = 51; - BinaryExpression node_binaryExpression = 52; - BinaryOperator node_binaryOperator = 53; - AwaitExpression node_awaitExpression = 54; - AssignmentExpression node_assignmentExpression = 55; - AssignmentOperator node_assignmentOperator = 56; - ArrowFunctionExpression node_arrowFunctionExpression = 57; - ArrayExpression node_arrayExpression = 58; - MaybeNamedClassDeclaration node_maybeNamedClassDeclaration = 59; - MaybeNamedFunctionDeclaration node_maybeNamedFunctionDeclaration = 60; - ExportNamedDeclaration node_exportNamedDeclaration = 61; - ExportSpecifier node_exportSpecifier = 62; - Declaration node_declaration = 63; - ClassDeclaration node_classDeclaration = 64; - VariableDeclaration node_variableDeclaration = 65; - VariableDeclarator node_variableDeclarator = 66; - FunctionDeclaration node_functionDeclaration = 67; - ImportDeclaration node_importDeclaration = 68; - ImportNamespaceSpecifier node_importNamespaceSpecifier = 69; - ImportDefaultSpecifier node_importDefaultSpecifier = 70; - ImportSpecifier node_importSpecifier = 71; - Statement node_statement = 72; - ForOfStatement node_forOfStatement = 73; - ForInStatement node_forInStatement = 74; - ForStatement node_forStatement = 75; - DoWhileStatement node_doWhileStatement = 76; - WhileStatement node_whileStatement = 77; - TryStatement node_tryStatement = 78; - CatchClause node_catchClause = 79; - ThrowStatement node_throwStatement = 80; - SwitchStatement node_switchStatement = 81; - SwitchCase node_switchCase = 82; - IfStatement node_ifStatement = 83; - ContinueStatement node_continueStatement = 84; - BreakStatement node_breakStatement = 85; - LabeledStatement node_labeledStatement = 86; - ReturnStatement node_returnStatement = 87; - WithStatement node_withStatement = 88; - DebuggerStatement node_debuggerStatement = 89; - EmptyStatement node_emptyStatement = 90; - ExpressionStatement node_expressionStatement = 91; - Directive node_directive = 92; - } -} -message SourceLocation { - string source = 1; - Position start = 2; - Position end = 3; -} -message Position { - int32 line = 1; - int32 end = 2; -} \ No newline at end of file From a7132c382db67580d3e90c9e92c09c8557a18461 Mon Sep 17 00:00:00 2001 From: Tibor Blenessy Date: Thu, 6 Jun 2024 19:08:10 +0200 Subject: [PATCH 06/10] remove output --- tools/estree/output/ESTree.java | 160 --------- tools/estree/output/estree.proto | 594 ------------------------------- 2 files changed, 754 deletions(-) delete mode 100644 tools/estree/output/ESTree.java delete mode 100644 tools/estree/output/estree.proto diff --git a/tools/estree/output/ESTree.java b/tools/estree/output/ESTree.java deleted file mode 100644 index 791bc2f0ded..00000000000 --- a/tools/estree/output/ESTree.java +++ /dev/null @@ -1,160 +0,0 @@ -/* - * SonarQube JavaScript Plugin - * Copyright (C) 2011-2023 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.sonar.plugins.javascript.api.estree; - -import java.util.List; - -/** - This file is generated. Do not modify it manually. Look at tools/estree instead. - - This is !EXPERIMENTAL UNSUPPORTED INTERNAL API! It can be modified or removed without prior notice. -*/ -public class ESTree { - - private ESTree() { - // shouldn't be instantiated, used only as a namespace - } - - public sealed interface Node { - Location loc(); - } - - public record Position(int line, int column) {} - public record Location(Position start, Position end) {} - - public sealed interface CallExpression extends Node { - Node callee(); - List arguments(); - } - public sealed interface ChainElement extends Node { - boolean optional(); - } - public sealed interface Declaration extends Node { - - } - public sealed interface ExportDefaultDeclaration extends Node { - - } - public sealed interface Expression extends Node { - - } - public sealed interface HasBody extends Node { - Node body(); - } - public sealed interface HasTest extends Node { - Expression test(); - } - public sealed interface Literal extends Node { - String raw(); - } - public sealed interface ModuleDeclaration extends Node { - - } - public sealed interface Pattern extends Node { - - } - public sealed interface Statement extends Node { - - } - - public record ArrayExpression(Location loc, List elements) implements Expression {} - public record ArrayPattern(Location loc, List elements) implements Pattern {} - public record ArrowFunctionExpression(Location loc, boolean expression, Node body, List params, boolean generator, boolean async) implements Expression, HasBody {} - public record AssignmentExpression(Location loc, AssignmentOperator operator, Node left, Expression right) implements Expression {} - public record AssignmentOperator(Location loc, String assignmentOperator) implements Node {} - public record AssignmentPattern(Location loc, Pattern left, Expression right) implements Pattern {} - public record AssignmentProperty(Location loc, Pattern value, String kind, boolean method, Node key, boolean shorthand, boolean computed) implements Node {} - public record AwaitExpression(Location loc, Expression argument) implements Expression {} - public record BigIntLiteral(Location loc, int value, String bigint, String raw) implements Literal {} - public record BinaryExpression(Location loc, BinaryOperator operator, Expression left, Expression right) implements Expression {} - public record BinaryOperator(Location loc, String binaryOperator) implements Node {} - public record BlockStatement(Location loc, List body) implements Statement {} - public record BreakStatement(Location loc, Identifier label) implements Statement {} - public record CatchClause(Location loc, Pattern param, BlockStatement body) implements Node, HasBody {} - public record ChainExpression(Location loc, ChainElement expression) implements Expression {} - public record ClassBody(Location loc, List body) implements Node {} - public record ClassDeclaration(Location loc, Identifier id, Expression superClass, ClassBody body) implements Declaration, HasBody {} - public record ClassExpression(Location loc, Identifier id, Expression superClass, ClassBody body) implements Expression, HasBody {} - public record ConditionalExpression(Location loc, Expression test, Expression alternate, Expression consequent) implements Expression, HasTest {} - public record ContinueStatement(Location loc, Identifier label) implements Statement {} - public record DebuggerStatement(Location loc) implements Statement {} - public record Directive(Location loc, Literal expression, String directive) implements Node {} - public record DoWhileStatement(Location loc, Statement body, Expression test) implements Statement, HasBody, HasTest {} - public record EmptyStatement(Location loc) implements Statement {} - public record ExportAllDeclaration(Location loc, Identifier exported, Literal source) implements ModuleDeclaration {} - public record ExportNamedDeclaration(Location loc, Declaration declaration, List specifiers, Literal source) implements ModuleDeclaration {} - public record ExportSpecifier(Location loc, Identifier exported, Identifier local) implements Node {} - public record ExpressionStatement(Location loc, Expression expression) implements Statement {} - public record ForInStatement(Location loc, Node left, Expression right, Statement body) implements Statement, HasBody {} - public record ForOfStatement(Location loc, boolean await, Node left, Expression right, Statement body) implements Statement, HasBody {} - public record ForStatement(Location loc, Node init, Expression test, Expression update, Statement body) implements Statement, HasBody, HasTest {} - public record FunctionDeclaration(Location loc, Identifier id, BlockStatement body, List params, boolean generator, boolean async) implements Declaration, HasBody {} - public record FunctionExpression(Location loc, Identifier id, BlockStatement body, List params, boolean generator, boolean async) implements Expression, HasBody {} - public record Identifier(Location loc, String name) implements Expression, Pattern {} - public record IfStatement(Location loc, Expression test, Statement consequent, Statement alternate) implements Statement, HasTest {} - public record ImportDeclaration(Location loc, List specifiers, Literal source) implements ModuleDeclaration {} - public record ImportDefaultSpecifier(Location loc, Identifier local) implements Node {} - public record ImportExpression(Location loc, Expression source) implements Expression {} - public record ImportNamespaceSpecifier(Location loc, Identifier local) implements Node {} - public record ImportSpecifier(Location loc, Identifier imported, Identifier local) implements Node {} - public record LabeledStatement(Location loc, Identifier label, Statement body) implements Statement, HasBody {} - public record LogicalExpression(Location loc, LogicalOperator operator, Expression left, Expression right) implements Expression {} - public record LogicalOperator(Location loc, String logicalOperator) implements Node {} - public record MaybeNamedClassDeclaration(Location loc, Identifier id, Expression superClass, ClassBody body) implements ExportDefaultDeclaration, HasBody {} - public record MaybeNamedFunctionDeclaration(Location loc, Identifier id, BlockStatement body, List params, boolean generator, boolean async) implements ExportDefaultDeclaration, HasBody {} - public record MemberExpression(Location loc, Node object, Node property, boolean computed, boolean optional) implements ChainElement, Expression, Pattern {} - public record MetaProperty(Location loc, Identifier meta, Identifier property) implements Expression {} - public record MethodDefinition(Location loc, Node key, FunctionExpression value, String kind, boolean computed, boolean isStatic) implements Node {} - public record NewExpression(Location loc, Node callee, List arguments) implements CallExpression, Expression {} - public record ObjectExpression(Location loc, List properties) implements Expression {} - public record ObjectPattern(Location loc, List properties) implements Pattern {} - public record PrivateIdentifier(Location loc, String name) implements Node {} - public record Program(Location loc, String sourceType, List body) implements Node {} - public record Property(Location loc, Node key, Node value, String kind, boolean method, boolean shorthand, boolean computed) implements Node {} - public record PropertyDefinition(Location loc, Node key, Expression value, boolean computed, boolean isStatic) implements Node {} - public record RegExpLiteral(Location loc, String pattern, String flags, String raw) implements Literal {} - public record RestElement(Location loc, Pattern argument) implements Pattern {} - public record ReturnStatement(Location loc, Expression argument) implements Statement {} - public record SequenceExpression(Location loc, List expressions) implements Expression {} - public record SimpleCallExpression(Location loc, boolean optional, Node callee, List arguments) implements CallExpression, ChainElement {} - public record SimpleLiteral(Location loc, Node value, String raw) implements Literal {} - public record SpreadElement(Location loc, Expression argument) implements Node {} - public record StaticBlock(Location loc) implements Statement {} - public record Super(Location loc) implements Node {} - public record SwitchCase(Location loc, Expression test, List consequent) implements Node, HasTest {} - public record SwitchStatement(Location loc, Expression discriminant, List cases) implements Statement {} - public record TaggedTemplateExpression(Location loc, Expression tag, TemplateLiteral quasi) implements Expression {} - public record TemplateElement(Location loc, boolean tail, String cooked, String raw) implements Node {} - public record TemplateLiteral(Location loc, List quasis, List expressions) implements Expression {} - public record ThisExpression(Location loc) implements Expression {} - public record ThrowStatement(Location loc, Expression argument) implements Statement {} - public record TryStatement(Location loc, BlockStatement block, CatchClause handler, BlockStatement finalizer) implements Statement {} - public record UnaryExpression(Location loc, UnaryOperator operator, boolean prefix, Expression argument) implements Expression {} - public record UnaryOperator(Location loc, String unaryOperator) implements Node {} - public record UpdateExpression(Location loc, UpdateOperator operator, Expression argument, boolean prefix) implements Expression {} - public record UpdateOperator(Location loc, String updateOperator) implements Node {} - public record VariableDeclaration(Location loc, List declarations, String kind) implements Declaration {} - public record VariableDeclarator(Location loc, Pattern id, Expression init) implements Node {} - public record WhileStatement(Location loc, Expression test, Statement body) implements Statement, HasBody, HasTest {} - public record WithStatement(Location loc, Expression object, Statement body) implements Statement, HasBody {} - public record YieldExpression(Location loc, Expression argument, boolean delegate) implements Expression {} -} - diff --git a/tools/estree/output/estree.proto b/tools/estree/output/estree.proto deleted file mode 100644 index af0b10181fc..00000000000 --- a/tools/estree/output/estree.proto +++ /dev/null @@ -1,594 +0,0 @@ -syntax = "proto3"; -// Generated for @types/estree version: 1.0.5 - -message Program { - string sourceType = 1; - repeated BaseNodeWithoutComments body = 2; -} -message ModuleDeclaration { - oneof moduleDeclaration { - ImportDeclaration moduleDeclaration_importDeclaration = 1; - ExportNamedDeclaration moduleDeclaration_exportNamedDeclaration = 2; - ExportDefaultDeclaration moduleDeclaration_exportDefaultDeclaration = 3; - ExportAllDeclaration moduleDeclaration_exportAllDeclaration = 4; - } -} -message ExportAllDeclaration { - Identifier exported = 1; - Literal source = 2; -} -message Literal { - oneof literal { - SimpleLiteral literal_simpleLiteral = 1; - RegExpLiteral literal_regExpLiteral = 2; - BigIntLiteral literal_bigIntLiteral = 3; - } -} -message BigIntLiteral { - int32 value = 1; - string bigint = 2; - string raw = 3; -} -message SimpleLiteral { - oneof value { - string value_string = 1; - bool value_boolean = 2; - int32 value_number = 3; - } - string raw = 4; -} -message Identifier { - string name = 1; -} -message ExportDefaultDeclaration { - oneof declaration { - MaybeNamedFunctionDeclaration declaration_maybeNamedFunctionDeclaration = 1; - MaybeNamedClassDeclaration declaration_maybeNamedClassDeclaration = 2; - Expression declaration_expression = 3; - } -} -message Expression { - oneof expression { - ArrayExpression expression_arrayExpression = 1; - ArrowFunctionExpression expression_arrowFunctionExpression = 2; - AssignmentExpression expression_assignmentExpression = 3; - AwaitExpression expression_awaitExpression = 4; - BinaryExpression expression_binaryExpression = 5; - CallExpression expression_callExpression = 6; - ChainExpression expression_chainExpression = 7; - ClassExpression expression_classExpression = 8; - ConditionalExpression expression_conditionalExpression = 9; - FunctionExpression expression_functionExpression = 10; - Identifier expression_identifier = 11; - ImportExpression expression_importExpression = 12; - Literal expression_literal = 13; - LogicalExpression expression_logicalExpression = 14; - MemberExpression expression_memberExpression = 15; - MetaProperty expression_metaProperty = 16; - NewExpression expression_newExpression = 17; - ObjectExpression expression_objectExpression = 18; - SequenceExpression expression_sequenceExpression = 19; - TaggedTemplateExpression expression_taggedTemplateExpression = 20; - TemplateLiteral expression_templateLiteral = 21; - ThisExpression expression_thisExpression = 22; - UnaryExpression expression_unaryExpression = 23; - UpdateExpression expression_updateExpression = 24; - YieldExpression expression_yieldExpression = 25; - } -} -message YieldExpression { - Expression argument = 1; - bool delegate = 2; -} -message UpdateExpression { - UpdateOperator operator = 1; - Expression argument = 2; - bool prefix = 3; -} -message UpdateOperator { - string updateOperator = 1; -} -message UnaryExpression { - UnaryOperator operator = 1; - bool prefix = 2; - Expression argument = 3; -} -message UnaryOperator { - string unaryOperator = 1; -} -message ThisExpression { -} -message TemplateLiteral { - repeated TemplateElement quasis = 1; - repeated Expression expressions = 2; -} -message TaggedTemplateExpression { - Expression tag = 1; - TemplateLiteral quasi = 2; -} -message SequenceExpression { - repeated Expression expressions = 1; -} -message ObjectExpression { - repeated BaseNodeWithoutComments properties = 1; -} -message SpreadElement { - Expression argument = 1; -} -message Property { - oneof key { - Expression key_expression = 1; - PrivateIdentifier key_privateIdentifier = 2; - } - oneof value { - Expression value_expression = 3; - Pattern value_pattern = 4; - } - string kind = 5; - bool method = 6; - bool shorthand = 7; - bool computed = 8; -} -message Pattern { - oneof pattern { - Identifier pattern_identifier = 1; - ObjectPattern pattern_objectPattern = 2; - ArrayPattern pattern_arrayPattern = 3; - RestElement pattern_restElement = 4; - AssignmentPattern pattern_assignmentPattern = 5; - MemberExpression pattern_memberExpression = 6; - } -} -message AssignmentPattern { - Pattern left = 1; - Expression right = 2; -} -message RestElement { - Pattern argument = 1; -} -message ArrayPattern { - repeated Pattern elements = 1; -} -message ObjectPattern { - repeated BaseNodeWithoutComments properties = 1; -} -message AssignmentProperty { - Pattern value = 1; - string kind = 2; - bool method = 3; - oneof key { - Expression key_expression = 4; - PrivateIdentifier key_privateIdentifier = 5; - } - bool shorthand = 6; - bool computed = 7; -} -message PrivateIdentifier { - string name = 1; -} -message NewExpression { - oneof callee { - Expression callee_expression = 1; - Super callee_super = 2; - } - repeated BaseNodeWithoutComments arguments = 3; -} -message Super { -} -message MetaProperty { - Identifier meta = 1; - Identifier property = 2; -} -message MemberExpression { - oneof object { - Expression object_expression = 1; - Super object_super = 2; - } - oneof property { - Expression property_expression = 3; - PrivateIdentifier property_privateIdentifier = 4; - } - bool computed = 5; - bool optional = 6; -} -message LogicalExpression { - LogicalOperator operator = 1; - Expression left = 2; - Expression right = 3; -} -message LogicalOperator { - string logicalOperator = 1; -} -message ImportExpression { - Expression source = 1; -} -message FunctionExpression { - Identifier id = 1; - BlockStatement body = 2; - repeated Pattern params = 3; - bool generator = 4; - bool async = 5; -} -message BlockStatement { - repeated Statement body = 1; -} -message ConditionalExpression { - Expression test = 1; - Expression alternate = 2; - Expression consequent = 3; -} -message ClassExpression { - Identifier id = 1; - Expression superClass = 2; - ClassBody body = 3; -} -message ClassBody { - repeated BaseNodeWithoutComments body = 1; -} -message StaticBlock { -} -message PropertyDefinition { - oneof key { - Expression key_expression = 1; - PrivateIdentifier key_privateIdentifier = 2; - } - Expression value = 3; - bool computed = 4; - bool static = 5; -} -message MethodDefinition { - oneof key { - Expression key_expression = 1; - PrivateIdentifier key_privateIdentifier = 2; - } - FunctionExpression value = 3; - string kind = 4; - bool computed = 5; - bool static = 6; -} -message ChainExpression { - ChainElement expression = 1; -} -message ChainElement { - oneof chainElement { - SimpleCallExpression chainElement_simpleCallExpression = 1; - MemberExpression chainElement_memberExpression = 2; - } -} -message SimpleCallExpression { - bool optional = 1; - oneof callee { - Expression callee_expression = 2; - Super callee_super = 3; - } - repeated BaseNodeWithoutComments arguments = 4; -} -message CallExpression { - oneof callExpression { - SimpleCallExpression callExpression_simpleCallExpression = 1; - NewExpression callExpression_newExpression = 2; - } -} -message BinaryExpression { - BinaryOperator operator = 1; - Expression left = 2; - Expression right = 3; -} -message BinaryOperator { - string binaryOperator = 1; -} -message AwaitExpression { - Expression argument = 1; -} -message AssignmentExpression { - AssignmentOperator operator = 1; - oneof left { - Pattern left_pattern = 2; - MemberExpression left_memberExpression = 3; - } - Expression right = 4; -} -message AssignmentOperator { - string assignmentOperator = 1; -} -message ArrowFunctionExpression { - bool expression = 1; - oneof body { - BlockStatement body_blockStatement = 2; - Expression body_expression = 3; - } - repeated Pattern params = 4; - bool generator = 5; - bool async = 6; -} -message ArrayExpression { - repeated BaseNodeWithoutComments elements = 1; -} -message MaybeNamedClassDeclaration { - Identifier id = 1; - Expression superClass = 2; - ClassBody body = 3; -} -message MaybeNamedFunctionDeclaration { - Identifier id = 1; - BlockStatement body = 2; - repeated Pattern params = 3; - bool generator = 4; - bool async = 5; -} -message ExportNamedDeclaration { - Declaration declaration = 1; - repeated ExportSpecifier specifiers = 2; - Literal source = 3; -} -message ExportSpecifier { - Identifier exported = 1; - Identifier local = 2; -} -message Declaration { - oneof declaration { - FunctionDeclaration declaration_functionDeclaration = 1; - VariableDeclaration declaration_variableDeclaration = 2; - ClassDeclaration declaration_classDeclaration = 3; - } -} -message ClassDeclaration { - Identifier id = 1; - Expression superClass = 2; - ClassBody body = 3; -} -message VariableDeclaration { - repeated VariableDeclarator declarations = 1; - string kind = 2; -} -message VariableDeclarator { - Pattern id = 1; - Expression init = 2; -} -message FunctionDeclaration { - Identifier id = 1; - BlockStatement body = 2; - repeated Pattern params = 3; - bool generator = 4; - bool async = 5; -} -message ImportDeclaration { - repeated BaseNodeWithoutComments specifiers = 1; - Literal source = 2; -} -message ImportNamespaceSpecifier { - Identifier local = 1; -} -message ImportDefaultSpecifier { - Identifier local = 1; -} -message ImportSpecifier { - Identifier imported = 1; - Identifier local = 2; -} -message Statement { - oneof statement { - ExpressionStatement statement_expressionStatement = 1; - BlockStatement statement_blockStatement = 2; - StaticBlock statement_staticBlock = 3; - EmptyStatement statement_emptyStatement = 4; - DebuggerStatement statement_debuggerStatement = 5; - WithStatement statement_withStatement = 6; - ReturnStatement statement_returnStatement = 7; - LabeledStatement statement_labeledStatement = 8; - BreakStatement statement_breakStatement = 9; - ContinueStatement statement_continueStatement = 10; - IfStatement statement_ifStatement = 11; - SwitchStatement statement_switchStatement = 12; - ThrowStatement statement_throwStatement = 13; - TryStatement statement_tryStatement = 14; - WhileStatement statement_whileStatement = 15; - DoWhileStatement statement_doWhileStatement = 16; - ForStatement statement_forStatement = 17; - ForInStatement statement_forInStatement = 18; - ForOfStatement statement_forOfStatement = 19; - Declaration statement_declaration = 20; - } -} -message ForOfStatement { - bool await = 1; - oneof left { - VariableDeclaration left_variableDeclaration = 2; - Pattern left_pattern = 3; - } - Expression right = 4; - Statement body = 5; -} -message ForInStatement { - oneof left { - VariableDeclaration left_variableDeclaration = 1; - Pattern left_pattern = 2; - } - Expression right = 3; - Statement body = 4; -} -message ForStatement { - oneof init { - VariableDeclaration init_variableDeclaration = 1; - Expression init_expression = 2; - } - Expression test = 3; - Expression update = 4; - Statement body = 5; -} -message DoWhileStatement { - Statement body = 1; - Expression test = 2; -} -message WhileStatement { - Expression test = 1; - Statement body = 2; -} -message TryStatement { - BlockStatement block = 1; - CatchClause handler = 2; - BlockStatement finalizer = 3; -} -message CatchClause { - Pattern param = 1; - BlockStatement body = 2; -} -message ThrowStatement { - Expression argument = 1; -} -message SwitchStatement { - Expression discriminant = 1; - repeated SwitchCase cases = 2; -} -message SwitchCase { - Expression test = 1; - repeated Statement consequent = 2; -} -message IfStatement { - Expression test = 1; - Statement consequent = 2; - Statement alternate = 3; -} -message ContinueStatement { - Identifier label = 1; -} -message BreakStatement { - Identifier label = 1; -} -message LabeledStatement { - Identifier label = 1; - Statement body = 2; -} -message ReturnStatement { - Expression argument = 1; -} -message WithStatement { - Expression object = 1; - Statement body = 2; -} -message DebuggerStatement { -} -message EmptyStatement { -} -message ExpressionStatement { - Expression expression = 1; -} -message Directive { - Literal expression = 1; - string directive = 2; -} -message RegExpLiteral { - string pattern = 1; - string flags = 2; - string raw = 3; -} -message TemplateElement { - bool tail = 1; - string cooked = 2; - string raw = 3; -} -message BaseNodeWithoutComments { - string type = 1; - SourceLocation loc = 2; - oneof node { - Program node_program = 3; - ModuleDeclaration node_moduleDeclaration = 4; - ExportAllDeclaration node_exportAllDeclaration = 5; - Literal node_literal = 6; - BigIntLiteral node_bigIntLiteral = 7; - SimpleLiteral node_simpleLiteral = 8; - Identifier node_identifier = 9; - ExportDefaultDeclaration node_exportDefaultDeclaration = 10; - Expression node_expression = 11; - YieldExpression node_yieldExpression = 12; - UpdateExpression node_updateExpression = 13; - UpdateOperator node_updateOperator = 14; - UnaryExpression node_unaryExpression = 15; - UnaryOperator node_unaryOperator = 16; - ThisExpression node_thisExpression = 17; - TemplateLiteral node_templateLiteral = 18; - TaggedTemplateExpression node_taggedTemplateExpression = 19; - SequenceExpression node_sequenceExpression = 20; - ObjectExpression node_objectExpression = 21; - SpreadElement node_spreadElement = 22; - Property node_property = 23; - Pattern node_pattern = 24; - AssignmentPattern node_assignmentPattern = 25; - RestElement node_restElement = 26; - ArrayPattern node_arrayPattern = 27; - ObjectPattern node_objectPattern = 28; - AssignmentProperty node_assignmentProperty = 29; - PrivateIdentifier node_privateIdentifier = 30; - NewExpression node_newExpression = 31; - Super node_super = 32; - MetaProperty node_metaProperty = 33; - MemberExpression node_memberExpression = 34; - LogicalExpression node_logicalExpression = 35; - LogicalOperator node_logicalOperator = 36; - ImportExpression node_importExpression = 37; - FunctionExpression node_functionExpression = 38; - BlockStatement node_blockStatement = 39; - ConditionalExpression node_conditionalExpression = 40; - ClassExpression node_classExpression = 41; - ClassBody node_classBody = 42; - StaticBlock node_staticBlock = 43; - PropertyDefinition node_propertyDefinition = 44; - MethodDefinition node_methodDefinition = 45; - ChainExpression node_chainExpression = 46; - ChainElement node_chainElement = 47; - SimpleCallExpression node_simpleCallExpression = 48; - CallExpression node_callExpression = 49; - BinaryExpression node_binaryExpression = 50; - BinaryOperator node_binaryOperator = 51; - AwaitExpression node_awaitExpression = 52; - AssignmentExpression node_assignmentExpression = 53; - AssignmentOperator node_assignmentOperator = 54; - ArrowFunctionExpression node_arrowFunctionExpression = 55; - ArrayExpression node_arrayExpression = 56; - MaybeNamedClassDeclaration node_maybeNamedClassDeclaration = 57; - MaybeNamedFunctionDeclaration node_maybeNamedFunctionDeclaration = 58; - ExportNamedDeclaration node_exportNamedDeclaration = 59; - ExportSpecifier node_exportSpecifier = 60; - Declaration node_declaration = 61; - ClassDeclaration node_classDeclaration = 62; - VariableDeclaration node_variableDeclaration = 63; - VariableDeclarator node_variableDeclarator = 64; - FunctionDeclaration node_functionDeclaration = 65; - ImportDeclaration node_importDeclaration = 66; - ImportNamespaceSpecifier node_importNamespaceSpecifier = 67; - ImportDefaultSpecifier node_importDefaultSpecifier = 68; - ImportSpecifier node_importSpecifier = 69; - Statement node_statement = 70; - ForOfStatement node_forOfStatement = 71; - ForInStatement node_forInStatement = 72; - ForStatement node_forStatement = 73; - DoWhileStatement node_doWhileStatement = 74; - WhileStatement node_whileStatement = 75; - TryStatement node_tryStatement = 76; - CatchClause node_catchClause = 77; - ThrowStatement node_throwStatement = 78; - SwitchStatement node_switchStatement = 79; - SwitchCase node_switchCase = 80; - IfStatement node_ifStatement = 81; - ContinueStatement node_continueStatement = 82; - BreakStatement node_breakStatement = 83; - LabeledStatement node_labeledStatement = 84; - ReturnStatement node_returnStatement = 85; - WithStatement node_withStatement = 86; - DebuggerStatement node_debuggerStatement = 87; - EmptyStatement node_emptyStatement = 88; - ExpressionStatement node_expressionStatement = 89; - Directive node_directive = 90; - RegExpLiteral node_regExpLiteral = 91; - TemplateElement node_templateElement = 92; - } -} -message SourceLocation { - string source = 1; - Position start = 2; - Position end = 3; -} -message Position { - int32 line = 1; - int32 end = 2; -} \ No newline at end of file From 10319b26c8b67a598abda33bcc9794d1610c34ae Mon Sep 17 00:00:00 2001 From: Tibor Blenessy Date: Fri, 7 Jun 2024 09:34:11 +0200 Subject: [PATCH 07/10] narrower types - wip --- .../plugins/javascript/api/estree/ESTree.java | 107 +++++++------- tools/estree/generate-java-ast.ts | 138 +++++++++--------- tools/estree/get-estree-nodes.ts | 4 +- 3 files changed, 126 insertions(+), 123 deletions(-) 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 791bc2f0ded..a7e39b18bca 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 @@ -55,12 +55,6 @@ public sealed interface ExportDefaultDeclaration extends Node { } public sealed interface Expression extends Node { - } - public sealed interface HasBody extends Node { - Node body(); - } - public sealed interface HasTest extends Node { - Expression test(); } public sealed interface Literal extends Node { String raw(); @@ -74,72 +68,87 @@ public sealed interface Pattern extends Node { public sealed interface Statement extends Node { } + + public sealed interface ExpressionOrSpreadElement extends Node {} + public sealed interface BlockStatementOrExpression extends Node {} + public sealed interface MemberExpressionOrPattern extends Node {} + public sealed interface ExpressionOrPrivateIdentifier extends Node {} + public sealed interface MethodDefinitionOrPropertyDefinitionOrStaticBlock extends Node {} + public sealed interface PatternOrVariableDeclaration extends Node {} + public sealed interface ExpressionOrVariableDeclaration extends Node {} + public sealed interface ImportDefaultSpecifierOrImportNamespaceSpecifierOrImportSpecifier extends Node {} + public sealed interface ExpressionOrSuper extends Node {} + public sealed interface PropertyOrSpreadElement extends Node {} + public sealed interface AssignmentPropertyOrRestElement extends Node {} + public sealed interface DirectiveOrModuleDeclarationOrStatement extends Node {} + public sealed interface ExpressionOrPattern extends Node {} + public sealed interface BooleanOrNumberOrString extends Node {} - public record ArrayExpression(Location loc, List elements) implements Expression {} + public record ArrayExpression(Location loc, List elements) implements Expression {} public record ArrayPattern(Location loc, List elements) implements Pattern {} - public record ArrowFunctionExpression(Location loc, boolean expression, Node body, List params, boolean generator, boolean async) implements Expression, HasBody {} - public record AssignmentExpression(Location loc, AssignmentOperator operator, Node left, Expression right) implements Expression {} + public record ArrowFunctionExpression(Location loc, boolean expression, BlockStatementOrExpression body, List params, boolean generator, boolean async) implements Expression {} + public record AssignmentExpression(Location loc, AssignmentOperator operator, MemberExpressionOrPattern left, Expression right) implements Expression {} public record AssignmentOperator(Location loc, String assignmentOperator) implements Node {} public record AssignmentPattern(Location loc, Pattern left, Expression right) implements Pattern {} - public record AssignmentProperty(Location loc, Pattern value, String kind, boolean method, Node key, boolean shorthand, boolean computed) implements Node {} + public record AssignmentProperty(Location loc, Pattern value, String kind, boolean method, ExpressionOrPrivateIdentifier key, boolean shorthand, boolean computed) implements Node {} public record AwaitExpression(Location loc, Expression argument) implements Expression {} public record BigIntLiteral(Location loc, int value, String bigint, String raw) implements Literal {} public record BinaryExpression(Location loc, BinaryOperator operator, Expression left, Expression right) implements Expression {} public record BinaryOperator(Location loc, String binaryOperator) implements Node {} - public record BlockStatement(Location loc, List body) implements Statement {} + public record BlockStatement(Location loc, List body) implements BlockStatementOrExpression, Statement {} public record BreakStatement(Location loc, Identifier label) implements Statement {} - public record CatchClause(Location loc, Pattern param, BlockStatement body) implements Node, HasBody {} + public record CatchClause(Location loc, Pattern param, BlockStatement body) implements Node {} public record ChainExpression(Location loc, ChainElement expression) implements Expression {} - public record ClassBody(Location loc, List body) implements Node {} - public record ClassDeclaration(Location loc, Identifier id, Expression superClass, ClassBody body) implements Declaration, HasBody {} - public record ClassExpression(Location loc, Identifier id, Expression superClass, ClassBody body) implements Expression, HasBody {} - public record ConditionalExpression(Location loc, Expression test, Expression alternate, Expression consequent) implements Expression, HasTest {} + public record ClassBody(Location loc, List body) implements Node {} + public record ClassDeclaration(Location loc, Identifier id, Expression superClass, ClassBody body) implements Declaration {} + public record ClassExpression(Location loc, Identifier id, Expression superClass, ClassBody body) implements Expression {} + public record ConditionalExpression(Location loc, Expression test, Expression alternate, Expression consequent) implements Expression {} public record ContinueStatement(Location loc, Identifier label) implements Statement {} public record DebuggerStatement(Location loc) implements Statement {} public record Directive(Location loc, Literal expression, String directive) implements Node {} - public record DoWhileStatement(Location loc, Statement body, Expression test) implements Statement, HasBody, HasTest {} + public record DoWhileStatement(Location loc, Statement body, Expression test) implements Statement {} public record EmptyStatement(Location loc) implements Statement {} public record ExportAllDeclaration(Location loc, Identifier exported, Literal source) implements ModuleDeclaration {} public record ExportNamedDeclaration(Location loc, Declaration declaration, List specifiers, Literal source) implements ModuleDeclaration {} public record ExportSpecifier(Location loc, Identifier exported, Identifier local) implements Node {} public record ExpressionStatement(Location loc, Expression expression) implements Statement {} - public record ForInStatement(Location loc, Node left, Expression right, Statement body) implements Statement, HasBody {} - public record ForOfStatement(Location loc, boolean await, Node left, Expression right, Statement body) implements Statement, HasBody {} - public record ForStatement(Location loc, Node init, Expression test, Expression update, Statement body) implements Statement, HasBody, HasTest {} - public record FunctionDeclaration(Location loc, Identifier id, BlockStatement body, List params, boolean generator, boolean async) implements Declaration, HasBody {} - public record FunctionExpression(Location loc, Identifier id, BlockStatement body, List params, boolean generator, boolean async) implements Expression, HasBody {} + public record ForInStatement(Location loc, PatternOrVariableDeclaration left, Expression right, Statement body) implements Statement {} + public record ForOfStatement(Location loc, boolean await, PatternOrVariableDeclaration left, Expression right, Statement body) implements Statement {} + public record ForStatement(Location loc, ExpressionOrVariableDeclaration init, Expression test, Expression update, Statement body) implements Statement {} + public record FunctionDeclaration(Location loc, Identifier id, BlockStatement body, List params, boolean generator, boolean async) implements Declaration {} + public record FunctionExpression(Location loc, Identifier id, BlockStatement body, List params, boolean generator, boolean async) implements Expression {} public record Identifier(Location loc, String name) implements Expression, Pattern {} - public record IfStatement(Location loc, Expression test, Statement consequent, Statement alternate) implements Statement, HasTest {} - public record ImportDeclaration(Location loc, List specifiers, Literal source) implements ModuleDeclaration {} - public record ImportDefaultSpecifier(Location loc, Identifier local) implements Node {} + public record IfStatement(Location loc, Expression test, Statement consequent, Statement alternate) implements Statement {} + public record ImportDeclaration(Location loc, List specifiers, Literal source) implements ModuleDeclaration {} + public record ImportDefaultSpecifier(Location loc, Identifier local) implements ImportDefaultSpecifierOrImportNamespaceSpecifierOrImportSpecifier {} public record ImportExpression(Location loc, Expression source) implements Expression {} - public record ImportNamespaceSpecifier(Location loc, Identifier local) implements Node {} - public record ImportSpecifier(Location loc, Identifier imported, Identifier local) implements Node {} - public record LabeledStatement(Location loc, Identifier label, Statement body) implements Statement, HasBody {} + public record ImportNamespaceSpecifier(Location loc, Identifier local) implements ImportDefaultSpecifierOrImportNamespaceSpecifierOrImportSpecifier {} + public record ImportSpecifier(Location loc, Identifier imported, Identifier local) implements ImportDefaultSpecifierOrImportNamespaceSpecifierOrImportSpecifier {} + public record LabeledStatement(Location loc, Identifier label, Statement body) implements Statement {} public record LogicalExpression(Location loc, LogicalOperator operator, Expression left, Expression right) implements Expression {} public record LogicalOperator(Location loc, String logicalOperator) implements Node {} - public record MaybeNamedClassDeclaration(Location loc, Identifier id, Expression superClass, ClassBody body) implements ExportDefaultDeclaration, HasBody {} - public record MaybeNamedFunctionDeclaration(Location loc, Identifier id, BlockStatement body, List params, boolean generator, boolean async) implements ExportDefaultDeclaration, HasBody {} - public record MemberExpression(Location loc, Node object, Node property, boolean computed, boolean optional) implements ChainElement, Expression, Pattern {} + public record MaybeNamedClassDeclaration(Location loc, Identifier id, Expression superClass, ClassBody body) implements ExportDefaultDeclaration {} + public record MaybeNamedFunctionDeclaration(Location loc, Identifier id, BlockStatement body, List params, boolean generator, boolean async) implements ExportDefaultDeclaration {} + public record MemberExpression(Location loc, ExpressionOrSuper object, ExpressionOrPrivateIdentifier property, boolean computed, boolean optional) implements MemberExpressionOrPattern, ChainElement, Expression, Pattern {} public record MetaProperty(Location loc, Identifier meta, Identifier property) implements Expression {} - public record MethodDefinition(Location loc, Node key, FunctionExpression value, String kind, boolean computed, boolean isStatic) implements Node {} - public record NewExpression(Location loc, Node callee, List arguments) implements CallExpression, Expression {} - public record ObjectExpression(Location loc, List properties) implements Expression {} - public record ObjectPattern(Location loc, List properties) implements Pattern {} - public record PrivateIdentifier(Location loc, String name) implements Node {} - public record Program(Location loc, String sourceType, List body) implements Node {} - public record Property(Location loc, Node key, Node value, String kind, boolean method, boolean shorthand, boolean computed) implements Node {} - public record PropertyDefinition(Location loc, Node key, Expression value, boolean computed, boolean isStatic) implements Node {} + public record MethodDefinition(Location loc, ExpressionOrPrivateIdentifier key, FunctionExpression value, String kind, boolean computed, boolean isStatic) implements MethodDefinitionOrPropertyDefinitionOrStaticBlock {} + public record NewExpression(Location loc, ExpressionOrSuper callee, List arguments) implements CallExpression, Expression {} + public record ObjectExpression(Location loc, List properties) implements Expression {} + public record ObjectPattern(Location loc, List properties) implements Pattern {} + public record PrivateIdentifier(Location loc, String name) implements ExpressionOrPrivateIdentifier {} + public record Program(Location loc, String sourceType, List body) implements Node {} + public record Property(Location loc, ExpressionOrPrivateIdentifier key, ExpressionOrPattern value, String kind, boolean method, boolean shorthand, boolean computed) implements PropertyOrSpreadElement {} + public record PropertyDefinition(Location loc, ExpressionOrPrivateIdentifier key, Expression value, boolean computed, boolean isStatic) implements MethodDefinitionOrPropertyDefinitionOrStaticBlock {} public record RegExpLiteral(Location loc, String pattern, String flags, String raw) implements Literal {} - public record RestElement(Location loc, Pattern argument) implements Pattern {} + public record RestElement(Location loc, Pattern argument) implements AssignmentPropertyOrRestElement, Pattern {} public record ReturnStatement(Location loc, Expression argument) implements Statement {} public record SequenceExpression(Location loc, List expressions) implements Expression {} - public record SimpleCallExpression(Location loc, boolean optional, Node callee, List arguments) implements CallExpression, ChainElement {} - public record SimpleLiteral(Location loc, Node value, String raw) implements Literal {} - public record SpreadElement(Location loc, Expression argument) implements Node {} - public record StaticBlock(Location loc) implements Statement {} - public record Super(Location loc) implements Node {} - public record SwitchCase(Location loc, Expression test, List consequent) implements Node, HasTest {} + public record SimpleCallExpression(Location loc, boolean optional, ExpressionOrSuper callee, List arguments) implements CallExpression, ChainElement {} + public record SimpleLiteral(Location loc, BooleanOrNumberOrString 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 Super(Location loc) implements ExpressionOrSuper {} + public record SwitchCase(Location loc, Expression test, List consequent) implements Node {} public record SwitchStatement(Location loc, Expression discriminant, List cases) implements Statement {} public record TaggedTemplateExpression(Location loc, Expression tag, TemplateLiteral quasi) implements Expression {} public record TemplateElement(Location loc, boolean tail, String cooked, String raw) implements Node {} @@ -151,10 +160,10 @@ public record UnaryExpression(Location loc, UnaryOperator operator, boolean pref public record UnaryOperator(Location loc, String unaryOperator) implements Node {} public record UpdateExpression(Location loc, UpdateOperator operator, Expression argument, boolean prefix) implements Expression {} public record UpdateOperator(Location loc, String updateOperator) implements Node {} - public record VariableDeclaration(Location loc, List declarations, String kind) implements Declaration {} + public record VariableDeclaration(Location loc, List declarations, String kind) implements ExpressionOrVariableDeclaration, PatternOrVariableDeclaration, Declaration {} public record VariableDeclarator(Location loc, Pattern id, Expression init) implements Node {} - public record WhileStatement(Location loc, Expression test, Statement body) implements Statement, HasBody, HasTest {} - public record WithStatement(Location loc, Expression object, Statement body) implements Statement, HasBody {} + 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, Expression argument, boolean delegate) implements Expression {} } diff --git a/tools/estree/generate-java-ast.ts b/tools/estree/generate-java-ast.ts index d0c1a8d8962..98c83d0ecfb 100644 --- a/tools/estree/generate-java-ast.ts +++ b/tools/estree/generate-java-ast.ts @@ -62,29 +62,8 @@ function isUnionNode(node: ESTreeNode) { } export function writeJavaClassesToDir(nodes: Record, output: string) { - // adding some hardcoded interfaces which allow to treat loops in the AST consistently - nodes['HasBody'] = { - name: 'HasBody', - fields: [ - { - name: 'body', - fieldValue: { type: 'Node' }, - }, - ], - }; - - nodes['HasTest'] = { - name: 'HasTest', - fields: [ - { - name: 'test', - fieldValue: { type: 'Expression' }, - }, - ], - }; const entries = Object.entries(nodes).sort(([a], [b]) => (a < b ? -1 : 1)); const ifaces = entries.filter(([, n]) => isUnionNode(n)).map(([name]) => name); - ifaces.push('HasBody', 'HasTest'); const impl = new Map(); ifaces @@ -128,6 +107,7 @@ export function writeJavaClassesToDir(nodes: Record, output: const records = []; const ifaceSrc = []; + const unionIfaces: string[] = []; for (const [name, node] of entries) { if (ifaces.includes(name)) { ifaceSrc.push(` public sealed interface ${name} extends Node {\n${ifaceFields(node)}\n }`); @@ -142,6 +122,64 @@ export function writeJavaClassesToDir(nodes: Record, output: } } + function javaType(fieldValue: NodeFieldValue): string { + if ('type' in fieldValue) { + switch (fieldValue.type) { + case 'string': + return 'String'; + case 'int32': + return 'int'; + case 'bool': + return 'boolean'; + case 'BaseNodeWithoutComments': + return 'Node'; + } + return fieldValue.type; + } + if (isArray(fieldValue)) { + return `List<${javaType(fieldValue.elementValue)}>`; + } + if ('unionElements' in fieldValue) { + let unionIface = fieldValue.unionElements + .map(e => upperCaseFirstLetter(e.name)) + .sort() + .join('Or'); + fieldValue.unionElements.forEach(e => + impl.set( + upperCaseFirstLetter(e.name), + [unionIface].concat(impl.get(upperCaseFirstLetter(e.name)) || []), + ), + ); + unionIfaces.push(unionIface); + return unionIface; + } + return 'Node'; + } + + function javaName(name: string) { + if (name === 'static') { + return 'isStatic'; + } + return name; + } + + function ifaceFields(node: ESTreeNode) { + return node.fields + .filter(f => !('unionElements' in f.fieldValue)) + .map(f => ` ${javaType(f.fieldValue)} ${javaName(f.name)}();`) + .join('\n'); + } + + function implementsClause(impl: Map, node: ESTreeNode) { + const ifaces = []; + if (impl.has(node.name)) { + ifaces.push(...impl.get(node.name)!); + } else { + ifaces.push('Node'); + } + return unique(ifaces).join(', '); + } + const estree = `${HEADER} package org.sonar.plugins.javascript.api.estree; @@ -160,6 +198,10 @@ public class ESTree { ${NODE_INTERFACE} ${ifaceSrc.join('\n')} + +${unique(unionIfaces) + .map(i => ` public sealed interface ${i} extends Node {}`) + .join('\n')} ${records.join('\n')} } @@ -168,60 +210,14 @@ ${records.join('\n')} fs.writeFileSync(output, estree, 'utf-8'); } -function javaType(fieldValue: NodeFieldValue): string { - if ('type' in fieldValue) { - switch (fieldValue.type) { - case 'string': - return 'String'; - case 'int32': - return 'int'; - case 'bool': - return 'boolean'; - case 'BaseNodeWithoutComments': - return 'Node'; - } - return fieldValue.type; - } - if (isArray(fieldValue)) { - return `List<${javaType(fieldValue.elementValue)}>`; - } - return 'Node'; -} - -function javaName(name: string) { - if (name === 'static') { - return 'isStatic'; - } - return name; -} - function upperCaseFirstLetter(s: string) { return s.charAt(0).toUpperCase() + s.slice(1); } -function ifaceFields(node: ESTreeNode) { - return node.fields - .filter(f => !('unionElements' in f.fieldValue)) - .map(f => ` ${javaType(f.fieldValue)} ${javaName(f.name)}();`) - .join('\n'); -} - -function implementsClause(impl: Map, node: ESTreeNode) { - const ifaces = []; - if (impl.has(node.name)) { - ifaces.push(...impl.get(node.name)!); - } else { - ifaces.push('Node'); - } - if (node.fields.some(f => f.name === 'body' && !isArray(f.fieldValue))) { - ifaces.push('HasBody'); - } - if (node.fields.some(f => f.name === 'test')) { - ifaces.push('HasTest'); - } - return ifaces.join(', '); -} - function isArray(fieldValue: NodeFieldValue): fieldValue is ArrayLikeFieldValue { return 'elementValue' in fieldValue; } + +function unique(arr: T[]): T[] { + return arr.filter((v, i, self) => self.indexOf(v) === i); +} diff --git a/tools/estree/get-estree-nodes.ts b/tools/estree/get-estree-nodes.ts index bb507b6b515..7451b924e3d 100644 --- a/tools/estree/get-estree-nodes.ts +++ b/tools/estree/get-estree-nodes.ts @@ -187,9 +187,7 @@ export function getEstreeNodes(file: ts.SourceFile) { if (name === 'Array' && typeNode.typeArguments?.length === 1) { // The type is of shape "Array", we want to generate repeated A return { - elementValue: flattenRepeatedNodeInOneOf( - getFieldValueFromType(typeNode.typeArguments[0]), - ), + elementValue: getFieldValueFromType(typeNode.typeArguments[0]), }; } return requestType(typeNode.typeName.getText(file)); From 09818193937231071aba832c08defa3126f9f1ae Mon Sep 17 00:00:00 2001 From: Tibor Blenessy Date: Fri, 7 Jun 2024 09:37:25 +0200 Subject: [PATCH 08/10] tweaks --- .../plugins/javascript/api/estree/ESTree.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) 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 a7e39b18bca..132789e3ed6 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 @@ -41,8 +41,8 @@ public record Position(int line, int column) {} public record Location(Position start, Position end) {} public sealed interface CallExpression extends Node { - Node callee(); - List arguments(); + ExpressionOrSuper callee(); + List arguments(); } public sealed interface ChainElement extends Node { boolean optional(); @@ -53,7 +53,7 @@ public sealed interface Declaration extends Node { public sealed interface ExportDefaultDeclaration extends Node { } - public sealed interface Expression extends Node { + public sealed interface Expression extends ExpressionOrPattern, ExpressionOrSuper, ExpressionOrPrivateIdentifier, ExpressionOrSpreadElement, ExpressionOrVariableDeclaration { } public sealed interface Literal extends Node { @@ -62,10 +62,10 @@ public sealed interface Literal extends Node { public sealed interface ModuleDeclaration extends Node { } - public sealed interface Pattern extends Node { + public sealed interface Pattern extends ExpressionOrPattern { } - public sealed interface Statement extends Node { + public sealed interface Statement extends DirectiveOrModuleDeclarationOrStatement { } @@ -82,8 +82,7 @@ public sealed interface PropertyOrSpreadElement extends Node {} public sealed interface AssignmentPropertyOrRestElement extends Node {} public sealed interface DirectiveOrModuleDeclarationOrStatement extends Node {} public sealed interface ExpressionOrPattern extends Node {} - public sealed interface BooleanOrNumberOrString extends Node {} - + public record ArrayExpression(Location loc, List elements) implements Expression {} public record ArrayPattern(Location loc, List elements) implements Pattern {} public record ArrowFunctionExpression(Location loc, boolean expression, BlockStatementOrExpression body, List params, boolean generator, boolean async) implements Expression {} @@ -144,7 +143,7 @@ public record RestElement(Location loc, Pattern argument) implements AssignmentP public record ReturnStatement(Location loc, Expression argument) implements Statement {} public record SequenceExpression(Location loc, List expressions) implements Expression {} public record SimpleCallExpression(Location loc, boolean optional, ExpressionOrSuper callee, List arguments) implements CallExpression, ChainElement {} - public record SimpleLiteral(Location loc, BooleanOrNumberOrString value, String raw) implements Literal {} + public record SimpleLiteral(Location loc, String 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 Super(Location loc) implements ExpressionOrSuper {} From 4f660d22243feecb1516cda76de43b76472981ec Mon Sep 17 00:00:00 2001 From: Tibor Blenessy Date: Fri, 7 Jun 2024 13:32:09 +0200 Subject: [PATCH 09/10] operators as enums --- .../plugins/javascript/api/estree/ESTree.java | 139 ++++++++++++++-- tools/estree/generate-java-ast.ts | 151 +++++++++++++++++- 2 files changed, 269 insertions(+), 21 deletions(-) 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 132789e3ed6..ad043948125 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 @@ -40,29 +40,29 @@ public sealed interface Node { public record Position(int line, int column) {} public record Location(Position start, Position end) {} - public sealed interface CallExpression extends Node { + public sealed interface CallExpression extends Expression { ExpressionOrSuper callee(); List arguments(); } public sealed interface ChainElement extends Node { boolean optional(); } - public sealed interface Declaration extends Node { + public sealed interface Declaration extends Statement { } - public sealed interface ExportDefaultDeclaration extends Node { + public sealed interface ExportDefaultDeclaration extends ModuleDeclaration { } - public sealed interface Expression extends ExpressionOrPattern, ExpressionOrSuper, ExpressionOrPrivateIdentifier, ExpressionOrSpreadElement, ExpressionOrVariableDeclaration { + public sealed interface Expression extends ExpressionOrSpreadElement, ExpressionOrSuper, ExpressionOrPrivateIdentifier, ExpressionOrPattern, ExpressionOrVariableDeclaration, BlockStatementOrExpression, ExportDefaultDeclaration { } - public sealed interface Literal extends Node { + public sealed interface Literal extends Expression { String raw(); } - public sealed interface ModuleDeclaration extends Node { + public sealed interface ModuleDeclaration extends DirectiveOrModuleDeclarationOrStatement { } - public sealed interface Pattern extends ExpressionOrPattern { + public sealed interface Pattern extends ExpressionOrPattern, PatternOrVariableDeclaration, MemberExpressionOrPattern { } public sealed interface Statement extends DirectiveOrModuleDeclarationOrStatement { @@ -82,18 +82,16 @@ public sealed interface PropertyOrSpreadElement extends Node {} public sealed interface AssignmentPropertyOrRestElement extends Node {} public sealed interface DirectiveOrModuleDeclarationOrStatement extends Node {} public sealed interface ExpressionOrPattern extends Node {} - + public record ArrayExpression(Location loc, List elements) implements Expression {} public record ArrayPattern(Location loc, List elements) implements Pattern {} public record ArrowFunctionExpression(Location loc, boolean expression, BlockStatementOrExpression body, List params, boolean generator, boolean async) implements Expression {} public record AssignmentExpression(Location loc, AssignmentOperator operator, MemberExpressionOrPattern left, Expression right) implements Expression {} - public record AssignmentOperator(Location loc, String assignmentOperator) implements Node {} public record AssignmentPattern(Location loc, Pattern left, Expression right) implements Pattern {} public record AssignmentProperty(Location loc, Pattern value, String kind, boolean method, ExpressionOrPrivateIdentifier key, boolean shorthand, boolean computed) implements Node {} public record AwaitExpression(Location loc, Expression argument) implements Expression {} public record BigIntLiteral(Location loc, int value, String bigint, String raw) implements Literal {} public record BinaryExpression(Location loc, BinaryOperator operator, Expression left, Expression right) implements Expression {} - public record BinaryOperator(Location loc, String binaryOperator) implements Node {} public record BlockStatement(Location loc, List body) implements BlockStatementOrExpression, Statement {} public record BreakStatement(Location loc, Identifier label) implements Statement {} public record CatchClause(Location loc, Pattern param, BlockStatement body) implements Node {} @@ -125,7 +123,6 @@ public record ImportNamespaceSpecifier(Location loc, Identifier local) implement public record ImportSpecifier(Location loc, Identifier imported, Identifier local) implements ImportDefaultSpecifierOrImportNamespaceSpecifierOrImportSpecifier {} public record LabeledStatement(Location loc, Identifier label, Statement body) implements Statement {} public record LogicalExpression(Location loc, LogicalOperator operator, Expression left, Expression right) implements Expression {} - public record LogicalOperator(Location loc, String logicalOperator) implements Node {} public record MaybeNamedClassDeclaration(Location loc, Identifier id, Expression superClass, ClassBody body) implements ExportDefaultDeclaration {} public record MaybeNamedFunctionDeclaration(Location loc, Identifier id, BlockStatement body, List params, boolean generator, boolean async) implements ExportDefaultDeclaration {} public record MemberExpression(Location loc, ExpressionOrSuper object, ExpressionOrPrivateIdentifier property, boolean computed, boolean optional) implements MemberExpressionOrPattern, ChainElement, Expression, Pattern {} @@ -143,7 +140,7 @@ public record RestElement(Location loc, Pattern argument) implements AssignmentP public record ReturnStatement(Location loc, Expression argument) implements Statement {} public record SequenceExpression(Location loc, List expressions) implements Expression {} public record SimpleCallExpression(Location loc, boolean optional, ExpressionOrSuper callee, List arguments) implements CallExpression, ChainElement {} - public record SimpleLiteral(Location loc, String value, String raw) implements Literal {} + public record SimpleLiteral(Location loc, Node 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 Super(Location loc) implements ExpressionOrSuper {} @@ -156,13 +153,127 @@ public record ThisExpression(Location loc) implements Expression {} public record ThrowStatement(Location loc, Expression argument) implements Statement {} public record TryStatement(Location loc, BlockStatement block, CatchClause handler, BlockStatement finalizer) implements Statement {} public record UnaryExpression(Location loc, UnaryOperator operator, boolean prefix, Expression argument) implements Expression {} - public record UnaryOperator(Location loc, String unaryOperator) implements Node {} public record UpdateExpression(Location loc, UpdateOperator operator, Expression argument, boolean prefix) implements Expression {} - public record UpdateOperator(Location loc, String updateOperator) implements Node {} public record VariableDeclaration(Location loc, List declarations, String kind) implements ExpressionOrVariableDeclaration, PatternOrVariableDeclaration, Declaration {} public record VariableDeclarator(Location loc, Pattern id, Expression init) implements Node {} 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, Expression argument, boolean delegate) implements Expression {} + + public interface Operator { + String raw(); + } + + public enum UnaryOperator implements Operator { + MINUS("-"), PLUS("+"), LOGICAL_NOT("!"), BITWISE_NOT("~"), TYPEOF("typeof"), VOID("void"), DELETE("delete"); + + private final String raw; + + UnaryOperator(String raw) { + this.raw = raw; + } + + @Override + public String raw() { + return raw; + } + } + + public enum BinaryOperator implements Operator { + EQUAL("=="), + NOT_EQUAL("!="), + STRICT_EQUAL("==="), + STRICT_NOT_EQUAL("!=="), + LESS_THAN("<"), + LESS_THAN_OR_EQUAL("<="), + GREATER_THAN(">"), + GREATER_THAN_OR_EQUAL(">="), + LEFT_SHIFT("<<"), + RIGHT_SHIFT(">>"), + UNSIGNED_RIGHT_SHIFT(">>>"), + PLUS("+"), + MINUS("-"), + MULTIPLY("*"), + DIVIDE("/"), + MODULO("%"), + EXPONENTIATION("**"), + BITWISE_AND("&"), + BITWISE_OR("|"), + BITWISE_XOR("^"), + IN("in"), + INSTANCEOF("instanceof"); + + private final String raw; + + BinaryOperator(String raw) { + this.raw = raw; + } + + @Override + public String raw() { + return raw; + } + } + + public enum LogicalOperator implements Operator { + AND("&&"), OR("||"), NULLISH_COALESCING("??"); + + private final String raw; + + LogicalOperator(String raw) { + this.raw = raw; + } + + @Override + public String raw() { + return raw; + } + } + + public enum AssignmentOperator implements Operator { + ASSIGN("="), + PLUS_ASSIGN("+="), + MINUS_ASSIGN("-="), + MULTIPLY_ASSIGN("*="), + DIVIDE_ASSIGN("/="), + MODULO_ASSIGN("%="), + EXPONENTIATION_ASSIGN("**="), + LEFT_SHIFT_ASSIGN("<<="), + RIGHT_SHIFT_ASSIGN(">>="), + UNSIGNED_RIGHT_SHIFT_ASSIGN(">>>="), + BITWISE_OR_ASSIGN("|="), + BITWISE_XOR_ASSIGN("^="), + BITWISE_AND_ASSIGN("&="), + LOGICAL_OR_ASSIGN("||="), + LOGICAL_AND_ASSIGN("&&="), + NULLISH_COALESCING_ASSIGN("??=") + ; + + private final String raw; + + AssignmentOperator(String raw) { + this.raw = raw; + } + + @Override + public String raw() { + return raw; + } + } + + public enum UpdateOperator implements Operator { + INCREMENT("++"), DECREMENT("--"); + + private final String raw; + + UpdateOperator(String raw) { + this.raw = raw; + } + + @Override + public String raw() { + return raw; + } + } } diff --git a/tools/estree/generate-java-ast.ts b/tools/estree/generate-java-ast.ts index 98c83d0ecfb..5cbe667757a 100644 --- a/tools/estree/generate-java-ast.ts +++ b/tools/estree/generate-java-ast.ts @@ -22,6 +22,7 @@ import fs from 'node:fs'; import { ArrayLikeFieldValue, ESTreeNode, + NodeField, NodeFieldValue, UnionFieldValue, } from './get-estree-nodes'; @@ -57,11 +58,141 @@ const NODE_INTERFACE = `public sealed interface Node { const SHARED_FIELDS = ['Location loc']; +const OPERATORS = ` public interface Operator { + String raw(); + } + + public enum UnaryOperator implements Operator { + MINUS("-"), PLUS("+"), LOGICAL_NOT("!"), BITWISE_NOT("~"), TYPEOF("typeof"), VOID("void"), DELETE("delete"); + + private final String raw; + + UnaryOperator(String raw) { + this.raw = raw; + } + + @Override + public String raw() { + return raw; + } + } + + public enum BinaryOperator implements Operator { + EQUAL("=="), + NOT_EQUAL("!="), + STRICT_EQUAL("==="), + STRICT_NOT_EQUAL("!=="), + LESS_THAN("<"), + LESS_THAN_OR_EQUAL("<="), + GREATER_THAN(">"), + GREATER_THAN_OR_EQUAL(">="), + LEFT_SHIFT("<<"), + RIGHT_SHIFT(">>"), + UNSIGNED_RIGHT_SHIFT(">>>"), + PLUS("+"), + MINUS("-"), + MULTIPLY("*"), + DIVIDE("/"), + MODULO("%"), + EXPONENTIATION("**"), + BITWISE_AND("&"), + BITWISE_OR("|"), + BITWISE_XOR("^"), + IN("in"), + INSTANCEOF("instanceof"); + + private final String raw; + + BinaryOperator(String raw) { + this.raw = raw; + } + + @Override + public String raw() { + return raw; + } + } + + public enum LogicalOperator implements Operator { + AND("&&"), OR("||"), NULLISH_COALESCING("??"); + + private final String raw; + + LogicalOperator(String raw) { + this.raw = raw; + } + + @Override + public String raw() { + return raw; + } + } + + public enum AssignmentOperator implements Operator { + ASSIGN("="), + PLUS_ASSIGN("+="), + MINUS_ASSIGN("-="), + MULTIPLY_ASSIGN("*="), + DIVIDE_ASSIGN("/="), + MODULO_ASSIGN("%="), + EXPONENTIATION_ASSIGN("**="), + LEFT_SHIFT_ASSIGN("<<="), + RIGHT_SHIFT_ASSIGN(">>="), + UNSIGNED_RIGHT_SHIFT_ASSIGN(">>>="), + BITWISE_OR_ASSIGN("|="), + BITWISE_XOR_ASSIGN("^="), + BITWISE_AND_ASSIGN("&="), + LOGICAL_OR_ASSIGN("||="), + LOGICAL_AND_ASSIGN("&&="), + NULLISH_COALESCING_ASSIGN("??=") + ; + + private final String raw; + + AssignmentOperator(String raw) { + this.raw = raw; + } + + @Override + public String raw() { + return raw; + } + } + + public enum UpdateOperator implements Operator { + INCREMENT("++"), DECREMENT("--"); + + private final String raw; + + UpdateOperator(String raw) { + this.raw = raw; + } + + @Override + public String raw() { + return raw; + } + }`; + function isUnionNode(node: ESTreeNode) { return node.fields.length === 1 && 'unionElements' in node.fields[0].fieldValue; } +function isPrimitive(f: NodeField) { + const fv = f.fieldValue; + return 'type' in fv && ['string', 'int32', 'bool'].some(s => s === fv.type); +} + export function writeJavaClassesToDir(nodes: Record, output: string) { + // remove operators, they are hardcoded as enums + [ + 'UnaryOperator', + 'BinaryOperator', + 'LogicalOperator', + 'AssignmentOperator', + 'UpdateOperator', + ].forEach(o => delete nodes[o]); + const entries = Object.entries(nodes).sort(([a], [b]) => (a < b ? -1 : 1)); const ifaces = entries.filter(([, n]) => isUnionNode(n)).map(([name]) => name); @@ -97,21 +228,19 @@ export function writeJavaClassesToDir(nodes: Record, output: nodes['CallExpression'].fields = [ { name: 'callee', - fieldValue: { type: 'Node' }, // more precise type is Expression | Super, but we can't represent union types in Java easily + fieldValue: { type: 'ExpressionOrSuper' }, // more precise type is Expression | Super, but we can't represent union types in Java easily }, { name: 'arguments', - fieldValue: { type: `List` }, + fieldValue: { type: `List` }, }, ]; const records = []; - const ifaceSrc = []; + const ifaceSrc: string[] = []; const unionIfaces: string[] = []; for (const [name, node] of entries) { - if (ifaces.includes(name)) { - ifaceSrc.push(` public sealed interface ${name} extends Node {\n${ifaceFields(node)}\n }`); - } else { + if (!ifaces.includes(name)) { const fields = [...SHARED_FIELDS]; for (const field of node.fields) { fields.push(`${javaType(field.fieldValue)} ${javaName(field.name)}`); @@ -122,6 +251,12 @@ export function writeJavaClassesToDir(nodes: Record, output: } } + ifaces.forEach(iface => { + const ext = implementsClause(impl, nodes[iface]); + const fields = ifaceFields(nodes[iface]); + ifaceSrc.push(` public sealed interface ${iface} extends ${ext} {\n${fields}\n }`); + }); + function javaType(fieldValue: NodeFieldValue): string { if ('type' in fieldValue) { switch (fieldValue.type) { @@ -139,7 +274,7 @@ export function writeJavaClassesToDir(nodes: Record, output: if (isArray(fieldValue)) { return `List<${javaType(fieldValue.elementValue)}>`; } - if ('unionElements' in fieldValue) { + if ('unionElements' in fieldValue && !fieldValue.unionElements.some(isPrimitive)) { let unionIface = fieldValue.unionElements .map(e => upperCaseFirstLetter(e.name)) .sort() @@ -204,6 +339,8 @@ ${unique(unionIfaces) .join('\n')} ${records.join('\n')} + +${OPERATORS} } `; From 5ea46d84dda6e8f8e460febfec484b70d02f21a8 Mon Sep 17 00:00:00 2001 From: Tibor Blenessy Date: Fri, 7 Jun 2024 13:40:54 +0200 Subject: [PATCH 10/10] fix --- .../sonar/plugins/javascript/api/estree/ESTree.java | 3 ++- .../plugins/javascript/api/estree/ESTreeTest.java | 12 ++++++------ tools/estree/generate-java-ast.ts | 2 ++ 3 files changed, 10 insertions(+), 7 deletions(-) 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 ad043948125..4e64fdd5b6d 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 @@ -20,6 +20,7 @@ package org.sonar.plugins.javascript.api.estree; +import java.math.BigInteger; import java.util.List; /** @@ -90,7 +91,7 @@ public record AssignmentExpression(Location loc, AssignmentOperator operator, Me public record AssignmentPattern(Location loc, Pattern left, Expression right) implements Pattern {} public record AssignmentProperty(Location loc, Pattern value, String kind, boolean method, ExpressionOrPrivateIdentifier key, boolean shorthand, boolean computed) implements Node {} public record AwaitExpression(Location loc, Expression argument) implements Expression {} - public record BigIntLiteral(Location loc, int value, String bigint, String raw) implements Literal {} + public record BigIntLiteral(Location loc, BigInteger value, String bigint, String raw) implements Literal {} public record BinaryExpression(Location loc, BinaryOperator operator, Expression left, Expression right) implements Expression {} public record BlockStatement(Location loc, List body) implements BlockStatementOrExpression, Statement {} public record BreakStatement(Location loc, Identifier label) implements Statement {} diff --git a/sonar-plugin/api/src/test/java/org/sonar/plugins/javascript/api/estree/ESTreeTest.java b/sonar-plugin/api/src/test/java/org/sonar/plugins/javascript/api/estree/ESTreeTest.java index 924066fd9b2..0f9a71302a3 100644 --- a/sonar-plugin/api/src/test/java/org/sonar/plugins/javascript/api/estree/ESTreeTest.java +++ b/sonar-plugin/api/src/test/java/org/sonar/plugins/javascript/api/estree/ESTreeTest.java @@ -11,35 +11,35 @@ class ESTreeTest { void test() { Class[] classes = ESTree.class.getDeclaredClasses(); - assertThat(classes).hasSize(95); + assertThat(classes).hasSize(107); //filter all classes that are interface var ifaceCount = Arrays.stream(classes).filter(Class::isInterface).count(); - assertThat(ifaceCount).isEqualTo(12); + assertThat(ifaceCount).isEqualTo(24); var recordCount = Arrays.stream(classes).filter(Class::isRecord).count(); - assertThat(recordCount).isEqualTo(83); + assertThat(recordCount).isEqualTo(78); } @Test void test_node_subclasses() { Class sealedClass = ESTree.Node.class; Class[] permittedSubclasses = sealedClass.getPermittedSubclasses(); - assertThat(permittedSubclasses).hasSize(34); + assertThat(permittedSubclasses).hasSize(23); } @Test void test_expression_subclasses() { Class sealedClass = ESTree.Expression.class; Class[] permittedSubclasses = sealedClass.getPermittedSubclasses(); - assertThat(permittedSubclasses).hasSize(23); + assertThat(permittedSubclasses).hasSize(25); } @Test void test_statement_subclasses() { Class sealedClass = ESTree.Statement.class; Class[] permittedSubclasses = sealedClass.getPermittedSubclasses(); - assertThat(permittedSubclasses).hasSize(19); + assertThat(permittedSubclasses).hasSize(20); } } diff --git a/tools/estree/generate-java-ast.ts b/tools/estree/generate-java-ast.ts index 5cbe667757a..170297a91df 100644 --- a/tools/estree/generate-java-ast.ts +++ b/tools/estree/generate-java-ast.ts @@ -235,6 +235,7 @@ export function writeJavaClassesToDir(nodes: Record, output: fieldValue: { type: `List` }, }, ]; + nodes['BigIntLiteral'].fields[0].fieldValue = { type: 'BigInteger' }; const records = []; const ifaceSrc: string[] = []; @@ -318,6 +319,7 @@ export function writeJavaClassesToDir(nodes: Record, output: const estree = `${HEADER} package org.sonar.plugins.javascript.api.estree; +import java.math.BigInteger; import java.util.List; /**