Skip to content

Commit

Permalink
fix indexed access type to return TypeTree (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegDokuka authored Dec 2, 2024
1 parent 74e44ce commit e29599b
Show file tree
Hide file tree
Showing 16 changed files with 773 additions and 14 deletions.
16 changes: 10 additions & 6 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1420,16 +1420,20 @@ export class JavaScriptParserVisitor {
}

visitIndexedAccessType(node: ts.IndexedAccessTypeNode) {
return new J.ArrayAccess(
return new JS.IndexedAccessType(
randomId(),
this.prefix(node),
Markers.EMPTY,
this.convert(node.objectType),
new J.ArrayDimension(
randomId(),
this.prefix(this.findChildNode(node, ts.SyntaxKind.OpenBracketToken)!),
Markers.EMPTY,
this.rightPadded(this.convert(node.indexType), this.suffix(node.indexType))
this.rightPadded(
new JS.IndexedAccessType.IndexType(
randomId(),
this.prefix(this.findChildNode(node, ts.SyntaxKind.OpenBracketToken)!),
Markers.EMPTY,
this.rightPadded(this.convert(node.indexType), this.suffix(node.indexType)),
this.mapType(node.indexType)
),
this.suffix(this.findChildNode(node, ts.SyntaxKind.CloseBracketToken)!)
),
this.mapType(node)
);
Expand Down
42 changes: 41 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} 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, 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} 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 @@ -572,6 +572,25 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
return exportSpecifier;
}

public visitIndexedAccessType(indexedAccessType: IndexedAccessType, ctx: ReceiverContext): J {
indexedAccessType = indexedAccessType.withId(ctx.receiveValue(indexedAccessType.id, ValueType.UUID)!);
indexedAccessType = indexedAccessType.withPrefix(ctx.receiveNode(indexedAccessType.prefix, receiveSpace)!);
indexedAccessType = indexedAccessType.withMarkers(ctx.receiveNode(indexedAccessType.markers, ctx.receiveMarkers)!);
indexedAccessType = indexedAccessType.withObjectType(ctx.receiveNode(indexedAccessType.objectType, ctx.receiveTree)!);
indexedAccessType = indexedAccessType.padding.withIndexType(ctx.receiveNode(indexedAccessType.padding.indexType, receiveRightPaddedTree)!);
indexedAccessType = indexedAccessType.withType(ctx.receiveValue(indexedAccessType.type, ValueType.Object));
return indexedAccessType;
}

public visitIndexedAccessTypeIndexType(indexType: IndexedAccessType.IndexType, ctx: ReceiverContext): J {
indexType = indexType.withId(ctx.receiveValue(indexType.id, ValueType.UUID)!);
indexType = indexType.withPrefix(ctx.receiveNode(indexType.prefix, receiveSpace)!);
indexType = indexType.withMarkers(ctx.receiveNode(indexType.markers, ctx.receiveMarkers)!);
indexType = indexType.padding.withElement(ctx.receiveNode(indexType.padding.element, receiveRightPaddedTree)!);
indexType = indexType.withType(ctx.receiveValue(indexType.type, ValueType.Object));
return indexType;
}

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 @@ -1840,6 +1859,27 @@ class Factory implements ReceiverFactory {
);
}

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

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

if (type === "org.openrewrite.java.tree.J$AnnotatedType") {
return new Java.AnnotatedType(
ctx.receiveValue(null, ValueType.UUID)!,
Expand Down
21 changes: 20 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} 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, 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} from '../tree';
import {Expression, J, JContainer, JLeftPadded, JRightPadded, Space, Statement} from "../../java";
import * as Java from "../../java/tree";

Expand Down Expand Up @@ -567,6 +567,25 @@ class Visitor extends JavaScriptVisitor<SenderContext> {
return exportSpecifier;
}

public visitIndexedAccessType(indexedAccessType: IndexedAccessType, ctx: SenderContext): J {
ctx.sendValue(indexedAccessType, v => v.id, ValueType.UUID);
ctx.sendNode(indexedAccessType, v => v.prefix, Visitor.sendSpace);
ctx.sendNode(indexedAccessType, v => v.markers, ctx.sendMarkers);
ctx.sendNode(indexedAccessType, v => v.objectType, ctx.sendTree);
ctx.sendNode(indexedAccessType, v => v.padding.indexType, Visitor.sendRightPadded(ValueType.Tree));
ctx.sendTypedValue(indexedAccessType, v => v.type, ValueType.Object);
return indexedAccessType;
}

public visitIndexedAccessTypeIndexType(indexType: IndexedAccessType.IndexType, ctx: SenderContext): J {
ctx.sendValue(indexType, v => v.id, ValueType.UUID);
ctx.sendNode(indexType, v => v.prefix, Visitor.sendSpace);
ctx.sendNode(indexType, v => v.markers, ctx.sendMarkers);
ctx.sendNode(indexType, v => v.padding.element, Visitor.sendRightPadded(ValueType.Tree));
ctx.sendTypedValue(indexType, v => v.type, ValueType.Object);
return indexType;
}

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
6 changes: 6 additions & 0 deletions openrewrite/src/javascript/tree/support_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ export namespace JsSpace {
EXPORT_SPECIFIER_PREFIX,
NAMED_EXPORTS_PREFIX,
EXPORT_ASSIGNMENT_PREFIX,
INDEXED_ACCESS_TYPE_PREFIX,
INDEXED_ACCESS_TYPE_INDEX_TYPE_PREFIX,
INDEXED_ACCESS_TYPE_INDEX_TYPE_SUFFIX,
INDEXED_ACCESS_TYPE_INDEX_TYPE_ELEMENT_SUFFIX,
}
}
export namespace JsLeftPadded {
Expand Down Expand Up @@ -311,6 +315,8 @@ export namespace JsRightPadded {
TEMPLATE_EXPRESSION_TEMPLATE_SPANS,
TAGGED_TEMPLATE_EXPRESSION_TAG,
IMPORT_TYPE_HAS_TYPEOF,
INDEXED_ACCESS_TYPE_INDEX_TYPE,
INDEXED_ACCESS_TYPE_INDEX_TYPE_ELEMENT,
}
}
export namespace JsContainer {
Expand Down
174 changes: 173 additions & 1 deletion openrewrite/src/javascript/tree/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2632,7 +2632,7 @@ export class TypeQuery extends JSMixin(Object) implements Expression, TypeTree {
}

@LstType("org.openrewrite.javascript.tree.JS$TypeOperator")
export class TypeOperator extends JSMixin(Object) implements Expression, TypedTree, NameTree {
export class TypeOperator extends JSMixin(Object) implements Expression, TypeTree {
public constructor(id: UUID, prefix: Space, markers: Markers, operator: TypeOperator.Type, expression: JLeftPadded<Expression>) {
super();
this._id = id;
Expand Down Expand Up @@ -4991,3 +4991,175 @@ export class ExportSpecifier extends JSMixin(Object) implements Expression, Type
}

}

@LstType("org.openrewrite.javascript.tree.JS$IndexedAccessType")
export class IndexedAccessType extends JSMixin(Object) implements Expression, TypeTree {
public constructor(id: UUID, prefix: Space, markers: Markers, objectType: TypeTree, indexType: JRightPadded<TypeTree>, _type: JavaType | null) {
super();
this._id = id;
this._prefix = prefix;
this._markers = markers;
this._objectType = objectType;
this._indexType = indexType;
this._type = _type;
}

private readonly _id: UUID;

public get id(): UUID {
return this._id;
}

public withId(id: UUID): IndexedAccessType {
return id === this._id ? this : new IndexedAccessType(id, this._prefix, this._markers, this._objectType, this._indexType, this._type);
}

private readonly _prefix: Space;

public get prefix(): Space {
return this._prefix;
}

public withPrefix(prefix: Space): IndexedAccessType {
return prefix === this._prefix ? this : new IndexedAccessType(this._id, prefix, this._markers, this._objectType, this._indexType, this._type);
}

private readonly _markers: Markers;

public get markers(): Markers {
return this._markers;
}

public withMarkers(markers: Markers): IndexedAccessType {
return markers === this._markers ? this : new IndexedAccessType(this._id, this._prefix, markers, this._objectType, this._indexType, this._type);
}

private readonly _objectType: TypeTree;

public get objectType(): TypeTree {
return this._objectType;
}

public withObjectType(objectType: TypeTree): IndexedAccessType {
return objectType === this._objectType ? this : new IndexedAccessType(this._id, this._prefix, this._markers, objectType, this._indexType, this._type);
}

private readonly _indexType: JRightPadded<TypeTree>;

public get indexType(): TypeTree {
return this._indexType.element;
}

public withIndexType(indexType: TypeTree): IndexedAccessType {
return this.padding.withIndexType(this._indexType.withElement(indexType));
}

private readonly _type: JavaType | null;

public get type(): JavaType | null {
return this._type;
}

public withType(_type: JavaType | null): IndexedAccessType {
return _type === this._type ? this : new IndexedAccessType(this._id, this._prefix, this._markers, this._objectType, this._indexType, _type);
}

public acceptJavaScript<P>(v: JavaScriptVisitor<P>, p: P): J | null {
return v.visitIndexedAccessType(this, p);
}

get padding() {
const t = this;
return new class {
public get indexType(): JRightPadded<TypeTree> {
return t._indexType;
}
public withIndexType(indexType: JRightPadded<TypeTree>): IndexedAccessType {
return t._indexType === indexType ? t : new IndexedAccessType(t._id, t._prefix, t._markers, t._objectType, indexType, t._type);
}
}
}

}

export namespace IndexedAccessType {
@LstType("org.openrewrite.javascript.tree.JS$IndexedAccessType$IndexType")
export class IndexType extends JSMixin(Object) implements Expression, TypeTree {
public constructor(id: UUID, prefix: Space, markers: Markers, element: JRightPadded<TypeTree>, _type: JavaType | null) {
super();
this._id = id;
this._prefix = prefix;
this._markers = markers;
this._element = element;
this._type = _type;
}

private readonly _id: UUID;

public get id(): UUID {
return this._id;
}

public withId(id: UUID): IndexedAccessType.IndexType {
return id === this._id ? this : new IndexedAccessType.IndexType(id, this._prefix, this._markers, this._element, this._type);
}

private readonly _prefix: Space;

public get prefix(): Space {
return this._prefix;
}

public withPrefix(prefix: Space): IndexedAccessType.IndexType {
return prefix === this._prefix ? this : new IndexedAccessType.IndexType(this._id, prefix, this._markers, this._element, this._type);
}

private readonly _markers: Markers;

public get markers(): Markers {
return this._markers;
}

public withMarkers(markers: Markers): IndexedAccessType.IndexType {
return markers === this._markers ? this : new IndexedAccessType.IndexType(this._id, this._prefix, markers, this._element, this._type);
}

private readonly _element: JRightPadded<TypeTree>;

public get element(): TypeTree {
return this._element.element;
}

public withElement(element: TypeTree): IndexedAccessType.IndexType {
return this.padding.withElement(this._element.withElement(element));
}

private readonly _type: JavaType | null;

public get type(): JavaType | null {
return this._type;
}

public withType(_type: JavaType | null): IndexedAccessType.IndexType {
return _type === this._type ? this : new IndexedAccessType.IndexType(this._id, this._prefix, this._markers, this._element, _type);
}

public acceptJavaScript<P>(v: JavaScriptVisitor<P>, p: P): J | null {
return v.visitIndexedAccessTypeIndexType(this, p);
}

get padding() {
const t = this;
return new class {
public get element(): JRightPadded<TypeTree> {
return t._element;
}
public withElement(element: JRightPadded<TypeTree>): IndexedAccessType.IndexType {
return t._element === element ? t : new IndexedAccessType.IndexType(t._id, t._prefix, t._markers, element, t._type);
}
}
}

}

}
Loading

0 comments on commit e29599b

Please sign in to comment.