Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add type-literal & index signature support #154

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,249 changes: 1,151 additions & 98 deletions openrewrite/package-lock.json

Large diffs are not rendered by default.

46 changes: 39 additions & 7 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,11 @@ export class JavaScriptParserVisitor {

private mapModifiers(node: ts.VariableDeclarationList | ts.VariableStatement | ts.ClassDeclaration | ts.PropertyDeclaration
| ts.FunctionDeclaration | ts.ParameterDeclaration | ts.MethodDeclaration | ts.EnumDeclaration | ts.InterfaceDeclaration
| ts.PropertySignature | ts.ConstructorDeclaration | ts.ModuleDeclaration | ts.GetAccessorDeclaration | ts.SetAccessorDeclaration | ts.ArrowFunction) {
| ts.PropertySignature | ts.ConstructorDeclaration | ts.ModuleDeclaration | ts.GetAccessorDeclaration | ts.SetAccessorDeclaration
| ts.ArrowFunction | ts.IndexSignatureDeclaration) {
if (ts.isVariableStatement(node) || ts.isModuleDeclaration(node) || ts.isClassDeclaration(node) || ts.isEnumDeclaration(node)
|| ts.isInterfaceDeclaration(node) || ts.isPropertyDeclaration(node) || ts.isPropertySignature(node) || ts.isParameter(node)
|| ts.isMethodDeclaration(node) || ts.isConstructorDeclaration(node) || ts.isArrowFunction(node)) {
|| ts.isMethodDeclaration(node) || ts.isConstructorDeclaration(node) || ts.isArrowFunction(node) || ts.isIndexSignatureDeclaration(node)) {
return node.modifiers ? node.modifiers?.filter(ts.isModifier).map(this.mapModifier) : [];
} else if (ts.isFunctionDeclaration(node)) {
return [...node.modifiers ? node.modifiers?.filter(ts.isModifier).map(this.mapModifier) : [],
Expand Down Expand Up @@ -1128,7 +1129,15 @@ export class JavaScriptParserVisitor {
}

visitIndexSignature(node: ts.IndexSignatureDeclaration) {
return this.visitUnknown(node);
return new JS.IndexSignatureDeclaration(
randomId(),
this.prefix(node),
Markers.EMPTY,
this.mapModifiers(node),
this.mapCommaSeparatedList(this.getParameterListNodes(node, ts.SyntaxKind.OpenBracketToken)),
this.leftPadded(this.prefix(node.getChildAt(node.getChildren().indexOf(node.type) - 1)), this.convert(node.type)),
this.mapType(node)
);
}

visitTypePredicate(node: ts.TypePredicateNode) {
Expand Down Expand Up @@ -1172,7 +1181,24 @@ export class JavaScriptParserVisitor {
}

visitTypeLiteral(node: ts.TypeLiteralNode) {
return this.visitUnknown(node);
return new JS.TypeLiteral(
randomId(),
this.prefix(node),
Markers.EMPTY,
new J.Block(
randomId(),
this.prefix(this.findChildNode(node, ts.SyntaxKind.OpenBraceToken)!),
Markers.EMPTY,
this.rightPadded(false, Space.EMPTY),
node.members.map(te => new JRightPadded(
this.convert(te),
(te.getLastToken()?.kind === ts.SyntaxKind.SemicolonToken) || (te.getLastToken()?.kind === ts.SyntaxKind.CommaToken) ? this.prefix(te.getLastToken()!) : Space.EMPTY,
(te.getLastToken()?.kind === ts.SyntaxKind.SemicolonToken) || (te.getLastToken()?.kind === ts.SyntaxKind.CommaToken) ? Markers.build([this.convertToken(te.getLastToken())!]) : Markers.EMPTY
)),
this.prefix(node.getLastToken()!)
),
this.mapType(node)
);
}

visitArrayType(node: ts.ArrayTypeNode) {
Expand Down Expand Up @@ -1202,7 +1228,13 @@ export class JavaScriptParserVisitor {
}

visitIntersectionType(node: ts.IntersectionTypeNode) {
return this.visitUnknown(node);
return new JS.Intersection(
randomId(),
this.prefix(node),
Markers.EMPTY,
this.rightPaddedList([...node.types], (n) => this.keywordPrefix(ts.SyntaxKind.AmpersandToken)(n)),
this.mapType(node),
);
}

visitConditionalType(node: ts.ConditionalTypeNode) {
Expand Down Expand Up @@ -2273,10 +2305,10 @@ export class JavaScriptParserVisitor {
);
}

private getParameterListNodes(node: ts.SignatureDeclarationBase) {
private getParameterListNodes(node: ts.SignatureDeclarationBase, openToken : ts.SyntaxKind = ts.SyntaxKind.OpenParenToken) {
const children = node.getChildren(this.sourceFile);
for (let i = 0; i < children.length; i++) {
if (children[i].kind == ts.SyntaxKind.OpenParenToken) {
if (children[i].kind == openToken) {
return children.slice(i, i + 3);
}
}
Expand Down
63 changes: 62 additions & 1 deletion openrewrite/src/javascript/remote/receiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as extensions from "./remote_extensions";
import {Checksum, Cursor, FileAttributes, ListUtils, Tree} from '../../core';
import {DetailsReceiver, Receiver, ReceiverContext, ReceiverFactory, ValueType} from '@openrewrite/rewrite-remote';
import {JavaScriptVisitor} from '..';
import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, DefaultType, Delete, Export, ExpressionStatement, FunctionType, JsImport, JsImportSpecifier, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeOperator, Unary, Union, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSMethodInvocation, NamespaceDeclaration, FunctionDeclaration} from '../tree';
import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, DefaultType, Delete, Export, ExpressionStatement, FunctionType, JsImport, JsImportSpecifier, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeOperator, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSMethodInvocation, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration} from '../tree';
import {Expression, J, JContainer, JLeftPadded, JRightPadded, NameTree, Space, Statement, TypeTree, TypedTree} from "../../java";
import * as Java from "../../java/tree";

Expand Down Expand Up @@ -286,6 +286,15 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
return union;
}

public visitIntersection(intersection: Intersection, ctx: ReceiverContext): J {
intersection = intersection.withId(ctx.receiveValue(intersection.id, ValueType.UUID)!);
intersection = intersection.withPrefix(ctx.receiveNode(intersection.prefix, receiveSpace)!);
intersection = intersection.withMarkers(ctx.receiveNode(intersection.markers, ctx.receiveMarkers)!);
intersection = intersection.padding.withTypes(ctx.receiveNodes(intersection.padding.types, receiveRightPaddedTree)!);
intersection = intersection.withType(ctx.receiveValue(intersection.type, ValueType.Object));
return intersection;
}

public visitVoid(_void: Void, ctx: ReceiverContext): J {
_void = _void.withId(ctx.receiveValue(_void.id, ValueType.UUID)!);
_void = _void.withPrefix(ctx.receiveNode(_void.prefix, receiveSpace)!);
Expand Down Expand Up @@ -389,6 +398,26 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
return functionDeclaration;
}

public visitTypeLiteral(typeLiteral: TypeLiteral, ctx: ReceiverContext): J {
typeLiteral = typeLiteral.withId(ctx.receiveValue(typeLiteral.id, ValueType.UUID)!);
typeLiteral = typeLiteral.withPrefix(ctx.receiveNode(typeLiteral.prefix, receiveSpace)!);
typeLiteral = typeLiteral.withMarkers(ctx.receiveNode(typeLiteral.markers, ctx.receiveMarkers)!);
typeLiteral = typeLiteral.withMembers(ctx.receiveNode(typeLiteral.members, ctx.receiveTree)!);
typeLiteral = typeLiteral.withType(ctx.receiveValue(typeLiteral.type, ValueType.Object));
return typeLiteral;
}

public visitIndexSignatureDeclaration(indexSignatureDeclaration: IndexSignatureDeclaration, ctx: ReceiverContext): J {
indexSignatureDeclaration = indexSignatureDeclaration.withId(ctx.receiveValue(indexSignatureDeclaration.id, ValueType.UUID)!);
indexSignatureDeclaration = indexSignatureDeclaration.withPrefix(ctx.receiveNode(indexSignatureDeclaration.prefix, receiveSpace)!);
indexSignatureDeclaration = indexSignatureDeclaration.withMarkers(ctx.receiveNode(indexSignatureDeclaration.markers, ctx.receiveMarkers)!);
indexSignatureDeclaration = indexSignatureDeclaration.withModifiers(ctx.receiveNodes(indexSignatureDeclaration.modifiers, ctx.receiveTree)!);
indexSignatureDeclaration = indexSignatureDeclaration.padding.withParameters(ctx.receiveNode(indexSignatureDeclaration.padding.parameters, receiveContainer)!);
indexSignatureDeclaration = indexSignatureDeclaration.padding.withTypeExpression(ctx.receiveNode(indexSignatureDeclaration.padding.typeExpression, receiveLeftPaddedTree)!);
indexSignatureDeclaration = indexSignatureDeclaration.withType(ctx.receiveValue(indexSignatureDeclaration.type, ValueType.Object));
return indexSignatureDeclaration;
}

public visitAnnotatedType(annotatedType: Java.AnnotatedType, ctx: ReceiverContext): J {
annotatedType = annotatedType.withId(ctx.receiveValue(annotatedType.id, ValueType.UUID)!);
annotatedType = annotatedType.withPrefix(ctx.receiveNode(annotatedType.prefix, receiveSpace)!);
Expand Down Expand Up @@ -1343,6 +1372,16 @@ class Factory implements ReceiverFactory {
);
}

if (type === "org.openrewrite.javascript.tree.JS$Intersection") {
return new Intersection(
ctx.receiveValue(null, ValueType.UUID)!,
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode(null, ctx.receiveMarkers)!,
ctx.receiveNodes(null, receiveRightPaddedTree)!,
ctx.receiveValue(null, ValueType.Object)
);
}

if (type === "org.openrewrite.javascript.tree.JS$Void") {
return new Void(
ctx.receiveValue(null, ValueType.UUID)!,
Expand Down Expand Up @@ -1455,6 +1494,28 @@ class Factory implements ReceiverFactory {
);
}

if (type === "org.openrewrite.javascript.tree.JS$TypeLiteral") {
return new TypeLiteral(
ctx.receiveValue(null, ValueType.UUID)!,
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode(null, ctx.receiveMarkers)!,
ctx.receiveNode<Java.Block>(null, ctx.receiveTree)!,
ctx.receiveValue(null, ValueType.Object)
);
}

if (type === "org.openrewrite.javascript.tree.JS$IndexSignatureDeclaration") {
return new IndexSignatureDeclaration(
ctx.receiveValue(null, ValueType.UUID)!,
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode(null, ctx.receiveMarkers)!,
ctx.receiveNodes<Java.Modifier>(null, ctx.receiveTree)!,
ctx.receiveNode<JContainer<J>>(null, receiveContainer)!,
ctx.receiveNode<JLeftPadded<Expression>>(null, receiveLeftPaddedTree)!,
ctx.receiveValue(null, ValueType.Object)
);
}

if (type === "org.openrewrite.java.tree.J$AnnotatedType") {
return new Java.AnnotatedType(
ctx.receiveValue(null, ValueType.UUID)!,
Expand Down
31 changes: 30 additions & 1 deletion openrewrite/src/javascript/remote/sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as extensions from "./remote_extensions";
import {Cursor, ListUtils, Tree} from '../../core';
import {Sender, SenderContext, ValueType} from '@openrewrite/rewrite-remote';
import {JavaScriptVisitor} from '..';
import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, DefaultType, Delete, Export, ExpressionStatement, FunctionType, JsImport, JsImportSpecifier, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeOperator, Unary, Union, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSMethodInvocation, NamespaceDeclaration, FunctionDeclaration} from '../tree';
import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, DefaultType, Delete, Export, ExpressionStatement, FunctionType, JsImport, JsImportSpecifier, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeOperator, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSMethodInvocation, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration} from '../tree';
import {Expression, J, JContainer, JLeftPadded, JRightPadded, Space, Statement} from "../../java";
import * as Java from "../../java/tree";

Expand Down Expand Up @@ -281,6 +281,15 @@ class Visitor extends JavaScriptVisitor<SenderContext> {
return union;
}

public visitIntersection(intersection: Intersection, ctx: SenderContext): J {
ctx.sendValue(intersection, v => v.id, ValueType.UUID);
ctx.sendNode(intersection, v => v.prefix, Visitor.sendSpace);
ctx.sendNode(intersection, v => v.markers, ctx.sendMarkers);
ctx.sendNodes(intersection, v => v.padding.types, Visitor.sendRightPadded(ValueType.Tree), t => t.element.id);
ctx.sendTypedValue(intersection, v => v.type, ValueType.Object);
return intersection;
}

public visitVoid(_void: Void, ctx: SenderContext): J {
ctx.sendValue(_void, v => v.id, ValueType.UUID);
ctx.sendNode(_void, v => v.prefix, Visitor.sendSpace);
Expand Down Expand Up @@ -384,6 +393,26 @@ class Visitor extends JavaScriptVisitor<SenderContext> {
return functionDeclaration;
}

public visitTypeLiteral(typeLiteral: TypeLiteral, ctx: SenderContext): J {
ctx.sendValue(typeLiteral, v => v.id, ValueType.UUID);
ctx.sendNode(typeLiteral, v => v.prefix, Visitor.sendSpace);
ctx.sendNode(typeLiteral, v => v.markers, ctx.sendMarkers);
ctx.sendNode(typeLiteral, v => v.members, ctx.sendTree);
ctx.sendTypedValue(typeLiteral, v => v.type, ValueType.Object);
return typeLiteral;
}

public visitIndexSignatureDeclaration(indexSignatureDeclaration: IndexSignatureDeclaration, ctx: SenderContext): J {
ctx.sendValue(indexSignatureDeclaration, v => v.id, ValueType.UUID);
ctx.sendNode(indexSignatureDeclaration, v => v.prefix, Visitor.sendSpace);
ctx.sendNode(indexSignatureDeclaration, v => v.markers, ctx.sendMarkers);
ctx.sendNodes(indexSignatureDeclaration, v => v.modifiers, ctx.sendTree, t => t.id);
ctx.sendNode(indexSignatureDeclaration, v => v.padding.parameters, Visitor.sendContainer(ValueType.Tree));
ctx.sendNode(indexSignatureDeclaration, v => v.padding.typeExpression, Visitor.sendLeftPadded(ValueType.Tree));
ctx.sendTypedValue(indexSignatureDeclaration, v => v.type, ValueType.Object);
return indexSignatureDeclaration;
}

public visitAnnotatedType(annotatedType: Java.AnnotatedType, ctx: SenderContext): J {
ctx.sendValue(annotatedType, v => v.id, ValueType.UUID);
ctx.sendNode(annotatedType, v => v.prefix, Visitor.sendSpace);
Expand Down
22 changes: 18 additions & 4 deletions openrewrite/src/javascript/tree/support_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,15 @@ export namespace JsSpace {
NAMESPACE_DECLARATION_KEYWORD_PREFIX,
JSMETHOD_DECLARATION_PREFIX,
FUNCTION_DECLARATION_PREFIX,
JSMETHOD_INVOCATION_PREFIX
JSMETHOD_INVOCATION_PREFIX,
INTERSECTION_PREFIX,
TYPE_LITERAL_PREFIX,
TYPE_LITERAL_MEMBERS_PREFIX,
TYPE_LITERAL_MEMBERS_SUFFIX,
INDEX_SIGNATURE_DECLARATION_PREFIX,
INDEX_SIGNATURE_DECLARATION_PARAMETERS_PREFIX,
INDEX_SIGNATURE_DECLARATION_PARAMETERS_SUFFIX,
INDEX_SIGNATURE_DECLARATION_TYPE_EXPRESSION_PREFIX,
}
}
export namespace JsLeftPadded {
Expand All @@ -245,7 +253,8 @@ export namespace JsLeftPadded {
JSMETHOD_DECLARATION_DEFAULT_VALUE,
NAMESPACE_DECLARATION_KEYWORD_TYPE,
SCOPED_VARIABLE_DECLARATIONS_SCOPE,
JS_IMPORT_SPECIFIER_IMPORT_TYPE
JS_IMPORT_SPECIFIER_IMPORT_TYPE,
INDEX_SIGNATURE_DECLARATION_TYPE_EXPRESSION,
}
}
export namespace JsRightPadded {
Expand All @@ -260,7 +269,10 @@ export namespace JsRightPadded {
UNION_TYPES,
JSVARIABLE_DECLARATIONS_VARIABLES,
NAMESPACE_DECLARATION_NAME,
JSMETHOD_INVOCATION_SELECT
INTERSECTION_TYPES,
JSMETHOD_INVOCATION_SELECT,
TYPE_LITERAL_MEMBERS,
INDEX_SIGNATURE_DECLARATION_PARAMETERS,
}
}
export namespace JsContainer {
Expand All @@ -274,6 +286,8 @@ export namespace JsContainer {
JSMETHOD_DECLARATION_THROWZ,
FUNCTION_DECLARATION_PARAMETERS,
JSMETHOD_INVOCATION_TYPE_PARAMETERS,
JSMETHOD_INVOCATION_ARGUMENTS
JSMETHOD_INVOCATION_ARGUMENTS,
TYPE_LITERAL_MEMBERS,
INDEX_SIGNATURE_DECLARATION_PARAMETERS,
}
}
Loading