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

Implemented MappedType mapping #167

Merged
merged 2 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
70 changes: 69 additions & 1 deletion openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1440,7 +1440,75 @@ export class JavaScriptParserVisitor {
}

visitMappedType(node: ts.MappedTypeNode) {
return this.visitUnknown(node);
function hasPrefixToken(readonlyToken?: ts.ReadonlyKeyword | ts.PlusToken | ts.MinusToken): boolean {
if (readonlyToken && (readonlyToken.kind == ts.SyntaxKind.PlusToken || readonlyToken.kind == ts.SyntaxKind.MinusToken)) {
return true;
} else {
return false;
}
}

function hasSuffixToken(questionToken?: ts.QuestionToken | ts.PlusToken | ts.MinusToken): boolean {
if (questionToken && (questionToken.kind == ts.SyntaxKind.PlusToken || questionToken.kind == ts.SyntaxKind.MinusToken)) {
return true;
} else {
return false;
}
}

return new JS.MappedType(
randomId(),
this.prefix(node),
Markers.EMPTY,
hasPrefixToken(node.readonlyToken) ? this.leftPadded(this.prefix(node.readonlyToken!),
new J.Literal(
randomId(),
this.prefix(node.readonlyToken!),
Markers.EMPTY,
null,
node.readonlyToken!.getText(),
null,
this.mapPrimitiveType(node.readonlyToken!)
)) : null,
node.readonlyToken ? this.leftPadded(this.prefix(this.findChildNode(node, ts.SyntaxKind.ReadonlyKeyword)!), true) : this.leftPadded(Space.EMPTY, false),
new JS.MappedType.KeysRemapping(
randomId(),
this.prefix(this.findChildNode(node, ts.SyntaxKind.OpenBracketToken)!),
Markers.EMPTY,
this.rightPadded(
new JS.MappedType.MappedTypeParameter(
randomId(),
this.prefix(node.typeParameter),
Markers.EMPTY,
this.visit(node.typeParameter.name),
this.leftPadded(this.suffix(node.typeParameter.name), this.visit(node.typeParameter.constraint!))
),
this.suffix(node.typeParameter)),
node.nameType ? this.rightPadded(this.visit(node.nameType), this.suffix(node.nameType)) : null,
),
hasSuffixToken(node.questionToken) ? this.leftPadded(this.prefix(node.questionToken!),
new J.Literal(
randomId(),
this.prefix(node.questionToken!),
Markers.EMPTY,
null,
node.questionToken!.getText(),
null,
this.mapPrimitiveType(node.questionToken!)
)
): null,
node.questionToken ? this.leftPadded(this.prefix(this.findChildNode(node, ts.SyntaxKind.QuestionToken)!), true) : this.leftPadded(Space.EMPTY, false),
new JContainer(
this.prefix(this.findChildNode(node, ts.SyntaxKind.ColonToken)!),
[this.rightPadded(this.visit(node.type!), this.suffix(node.type!)),
this.findChildNode(node, ts.SyntaxKind.SemicolonToken) ?
this.rightPadded(this.newJEmpty(Space.EMPTY, Markers.build([new Semicolon(randomId())])), this.prefix(node.getLastToken()!))
: this.rightPadded(this.newJEmpty(), this.prefix(node.getLastToken()!))
],
Markers.EMPTY
),
this.mapType(node)
);
}

visitLiteralType(node: ts.LiteralTypeNode) {
Expand Down
69 changes: 68 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, ConditionalType, DefaultType, Delete, Export, ExpressionStatement, ExpressionWithTypeArguments, FunctionType, InferType, ImportType, JsImport, JsImportSpecifier, JsBinary, LiteralType, ObjectBindingDeclarations, PropertyAssignment, SatisfiesExpression, ScopedVariableDeclarations, StatementExpression, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, TypePredicate, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement, ExportDeclaration, ExportAssignment, NamedExports, ExportSpecifier, IndexedAccessType, JsAssignmentOperation} from '../tree';
import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, ConditionalType, DefaultType, Delete, Export, ExpressionStatement, ExpressionWithTypeArguments, FunctionType, InferType, ImportType, JsImport, JsImportSpecifier, JsBinary, LiteralType, MappedType, ObjectBindingDeclarations, PropertyAssignment, SatisfiesExpression, ScopedVariableDeclarations, StatementExpression, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, TypePredicate, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement, ExportDeclaration, ExportAssignment, NamedExports, ExportSpecifier, IndexedAccessType, JsAssignmentOperation} 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 @@ -209,6 +209,38 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
return literalType;
}

public visitMappedType(mappedType: MappedType, ctx: ReceiverContext): J {
mappedType = mappedType.withId(ctx.receiveValue(mappedType.id, ValueType.UUID)!);
mappedType = mappedType.withPrefix(ctx.receiveNode(mappedType.prefix, receiveSpace)!);
mappedType = mappedType.withMarkers(ctx.receiveNode(mappedType.markers, ctx.receiveMarkers)!);
mappedType = mappedType.padding.withPrefixToken(ctx.receiveNode(mappedType.padding.prefixToken, receiveLeftPaddedTree));
mappedType = mappedType.padding.withHasReadonly(ctx.receiveNode(mappedType.padding.hasReadonly, leftPaddedValueReceiver(ValueType.Primitive))!);
mappedType = mappedType.withKeysRemapping(ctx.receiveNode(mappedType.keysRemapping, ctx.receiveTree)!);
mappedType = mappedType.padding.withSuffixToken(ctx.receiveNode(mappedType.padding.suffixToken, receiveLeftPaddedTree));
mappedType = mappedType.padding.withHasQuestionToken(ctx.receiveNode(mappedType.padding.hasQuestionToken, leftPaddedValueReceiver(ValueType.Primitive))!);
mappedType = mappedType.padding.withValueType(ctx.receiveNode(mappedType.padding.valueType, receiveContainer)!);
mappedType = mappedType.withType(ctx.receiveValue(mappedType.type, ValueType.Object));
return mappedType;
}

public visitMappedTypeKeysRemapping(keysRemapping: MappedType.KeysRemapping, ctx: ReceiverContext): J {
keysRemapping = keysRemapping.withId(ctx.receiveValue(keysRemapping.id, ValueType.UUID)!);
keysRemapping = keysRemapping.withPrefix(ctx.receiveNode(keysRemapping.prefix, receiveSpace)!);
keysRemapping = keysRemapping.withMarkers(ctx.receiveNode(keysRemapping.markers, ctx.receiveMarkers)!);
keysRemapping = keysRemapping.padding.withTypeParameter(ctx.receiveNode(keysRemapping.padding.typeParameter, receiveRightPaddedTree)!);
keysRemapping = keysRemapping.padding.withNameType(ctx.receiveNode(keysRemapping.padding.nameType, receiveRightPaddedTree));
return keysRemapping;
}

public visitMappedTypeMappedTypeParameter(mappedTypeParameter: MappedType.MappedTypeParameter, ctx: ReceiverContext): J {
mappedTypeParameter = mappedTypeParameter.withId(ctx.receiveValue(mappedTypeParameter.id, ValueType.UUID)!);
mappedTypeParameter = mappedTypeParameter.withPrefix(ctx.receiveNode(mappedTypeParameter.prefix, receiveSpace)!);
mappedTypeParameter = mappedTypeParameter.withMarkers(ctx.receiveNode(mappedTypeParameter.markers, ctx.receiveMarkers)!);
mappedTypeParameter = mappedTypeParameter.withName(ctx.receiveNode(mappedTypeParameter.name, ctx.receiveTree)!);
mappedTypeParameter = mappedTypeParameter.padding.withIterateType(ctx.receiveNode(mappedTypeParameter.padding.iterateType, receiveLeftPaddedTree)!);
return mappedTypeParameter;
}

public visitObjectBindingDeclarations(objectBindingDeclarations: ObjectBindingDeclarations, ctx: ReceiverContext): J {
objectBindingDeclarations = objectBindingDeclarations.withId(ctx.receiveValue(objectBindingDeclarations.id, ValueType.UUID)!);
objectBindingDeclarations = objectBindingDeclarations.withPrefix(ctx.receiveNode(objectBindingDeclarations.prefix, receiveSpace)!);
Expand Down Expand Up @@ -1472,6 +1504,41 @@ class Factory implements ReceiverFactory {
);
}

if (type === "org.openrewrite.javascript.tree.JS$MappedType") {
return new MappedType(
ctx.receiveValue(null, ValueType.UUID)!,
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode(null, ctx.receiveMarkers)!,
ctx.receiveNode<JLeftPadded<Java.Literal>>(null, receiveLeftPaddedTree),
ctx.receiveNode<JLeftPadded<boolean>>(null, leftPaddedValueReceiver(ValueType.Primitive))!,
ctx.receiveNode<MappedType.KeysRemapping>(null, ctx.receiveTree)!,
ctx.receiveNode<JLeftPadded<Java.Literal>>(null, receiveLeftPaddedTree),
ctx.receiveNode<JLeftPadded<boolean>>(null, leftPaddedValueReceiver(ValueType.Primitive))!,
ctx.receiveNode<JContainer<TypeTree>>(null, receiveContainer)!,
ctx.receiveValue(null, ValueType.Object)
);
}

if (type === "org.openrewrite.javascript.tree.JS$MappedType$KeysRemapping") {
return new MappedType.KeysRemapping(
ctx.receiveValue(null, ValueType.UUID)!,
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode(null, ctx.receiveMarkers)!,
ctx.receiveNode<JRightPadded<MappedType.MappedTypeParameter>>(null, receiveRightPaddedTree)!,
ctx.receiveNode<JRightPadded<Expression>>(null, receiveRightPaddedTree)
);
}

if (type === "org.openrewrite.javascript.tree.JS$MappedType$MappedTypeParameter") {
return new MappedType.MappedTypeParameter(
ctx.receiveValue(null, ValueType.UUID)!,
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode(null, ctx.receiveMarkers)!,
ctx.receiveNode<Expression>(null, ctx.receiveTree)!,
ctx.receiveNode<JLeftPadded<TypeTree>>(null, receiveLeftPaddedTree)!
);
}

if (type === "org.openrewrite.javascript.tree.JS$ObjectBindingDeclarations") {
return new ObjectBindingDeclarations(
ctx.receiveValue(null, ValueType.UUID)!,
Expand Down
34 changes: 33 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, ConditionalType, DefaultType, Delete, Export, ExpressionStatement, ExpressionWithTypeArguments, FunctionType, InferType, ImportType, JsImport, JsImportSpecifier, JsBinary, LiteralType, ObjectBindingDeclarations, PropertyAssignment, SatisfiesExpression, ScopedVariableDeclarations, StatementExpression, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, TypePredicate, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement, ExportDeclaration, ExportAssignment, NamedExports, ExportSpecifier, IndexedAccessType, JsAssignmentOperation} from '../tree';
import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, ConditionalType, DefaultType, Delete, Export, ExpressionStatement, ExpressionWithTypeArguments, FunctionType, InferType, ImportType, JsImport, JsImportSpecifier, JsBinary, LiteralType, MappedType, ObjectBindingDeclarations, PropertyAssignment, SatisfiesExpression, ScopedVariableDeclarations, StatementExpression, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, TypePredicate, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement, ExportDeclaration, ExportAssignment, NamedExports, ExportSpecifier, IndexedAccessType, JsAssignmentOperation} from '../tree';
import {Expression, J, JContainer, JLeftPadded, JRightPadded, Space, Statement} from "../../java";
import * as Java from "../../java/tree";

Expand Down Expand Up @@ -204,6 +204,38 @@ class Visitor extends JavaScriptVisitor<SenderContext> {
return literalType;
}

public visitMappedType(mappedType: MappedType, ctx: SenderContext): J {
ctx.sendValue(mappedType, v => v.id, ValueType.UUID);
ctx.sendNode(mappedType, v => v.prefix, Visitor.sendSpace);
ctx.sendNode(mappedType, v => v.markers, ctx.sendMarkers);
ctx.sendNode(mappedType, v => v.padding.prefixToken, Visitor.sendLeftPadded(ValueType.Tree));
ctx.sendNode(mappedType, v => v.padding.hasReadonly, Visitor.sendLeftPadded(ValueType.Primitive));
ctx.sendNode(mappedType, v => v.keysRemapping, ctx.sendTree);
ctx.sendNode(mappedType, v => v.padding.suffixToken, Visitor.sendLeftPadded(ValueType.Tree));
ctx.sendNode(mappedType, v => v.padding.hasQuestionToken, Visitor.sendLeftPadded(ValueType.Primitive));
ctx.sendNode(mappedType, v => v.padding.valueType, Visitor.sendContainer(ValueType.Tree));
ctx.sendTypedValue(mappedType, v => v.type, ValueType.Object);
return mappedType;
}

public visitMappedTypeKeysRemapping(keysRemapping: MappedType.KeysRemapping, ctx: SenderContext): J {
ctx.sendValue(keysRemapping, v => v.id, ValueType.UUID);
ctx.sendNode(keysRemapping, v => v.prefix, Visitor.sendSpace);
ctx.sendNode(keysRemapping, v => v.markers, ctx.sendMarkers);
ctx.sendNode(keysRemapping, v => v.padding.typeParameter, Visitor.sendRightPadded(ValueType.Tree));
ctx.sendNode(keysRemapping, v => v.padding.nameType, Visitor.sendRightPadded(ValueType.Tree));
return keysRemapping;
}

public visitMappedTypeMappedTypeParameter(mappedTypeParameter: MappedType.MappedTypeParameter, ctx: SenderContext): J {
ctx.sendValue(mappedTypeParameter, v => v.id, ValueType.UUID);
ctx.sendNode(mappedTypeParameter, v => v.prefix, Visitor.sendSpace);
ctx.sendNode(mappedTypeParameter, v => v.markers, ctx.sendMarkers);
ctx.sendNode(mappedTypeParameter, v => v.name, ctx.sendTree);
ctx.sendNode(mappedTypeParameter, v => v.padding.iterateType, Visitor.sendLeftPadded(ValueType.Tree));
return mappedTypeParameter;
}

public visitObjectBindingDeclarations(objectBindingDeclarations: ObjectBindingDeclarations, ctx: SenderContext): J {
ctx.sendValue(objectBindingDeclarations, v => v.id, ValueType.UUID);
ctx.sendNode(objectBindingDeclarations, v => v.prefix, Visitor.sendSpace);
Expand Down
11 changes: 11 additions & 0 deletions openrewrite/src/javascript/tree/support_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ export namespace JsSpace {
INDEXED_ACCESS_TYPE_INDEX_TYPE_SUFFIX,
INDEXED_ACCESS_TYPE_INDEX_TYPE_ELEMENT_SUFFIX,
JS_ASSIGNMENT_OPERATION_PREFIX,
MAPPED_TYPE_PREFIX,
MAPPED_TYPE_KEYS_REMAPPING_PREFIX,
MAPPED_TYPE_MAPPED_TYPE_PARAMETER_PREFIX,
}
}
export namespace JsLeftPadded {
Expand Down Expand Up @@ -293,6 +296,11 @@ export namespace JsLeftPadded {
FUNCTION_DECLARATION_ASTERISK_TOKEN,
FUNCTION_DECLARATION_NAME,
JS_ASSIGNMENT_OPERATION_OPERATOR,
MAPPED_TYPE_PREFIX_TOKEN,
MAPPED_TYPE_SUFFIX_TOKEN,
MAPPED_TYPE_MAPPED_TYPE_PARAMETER_ITERATE_TYPE,
MAPPED_TYPE_HAS_READONLY,
MAPPED_TYPE_HAS_QUESTION_TOKEN,
}
}
export namespace JsRightPadded {
Expand All @@ -319,6 +327,8 @@ export namespace JsRightPadded {
IMPORT_TYPE_HAS_TYPEOF,
INDEXED_ACCESS_TYPE_INDEX_TYPE,
INDEXED_ACCESS_TYPE_INDEX_TYPE_ELEMENT,
MAPPED_TYPE_KEYS_REMAPPING_TYPE_PARAMETER,
MAPPED_TYPE_KEYS_REMAPPING_NAME_TYPE,
}
}
export namespace JsContainer {
Expand All @@ -339,5 +349,6 @@ export namespace JsContainer {
CONDITIONAL_TYPE_CONDITION,
IMPORT_TYPE_TYPE_ARGUMENTS,
NAMED_EXPORTS_ELEMENTS,
MAPPED_TYPE_VALUE_TYPE,
}
}
Loading
Loading