Skip to content

Commit

Permalink
Implemented TypeDeclaration
Browse files Browse the repository at this point in the history
Implemented visitTypeAliasDeclaration
  • Loading branch information
Andrii Rodionov committed Nov 19, 2024
1 parent 42b9fd6 commit d392661
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 39 deletions.
17 changes: 14 additions & 3 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1464,7 +1464,7 @@ export class JavaScriptParserVisitor {
this.prefix(node),
Markers.EMPTY,
[],
null,
node.name ? this.visit(node.name) : null,
this.mapTypeParametersAsObject(node),
this.mapCommaSeparatedList(this.getParameterListNodes(node)),
this.mapTypeInfo(node),
Expand Down Expand Up @@ -2284,7 +2284,16 @@ export class JavaScriptParserVisitor {
}

visitTypeAliasDeclaration(node: ts.TypeAliasDeclaration) {
return this.visitUnknown(node);
return new JS.TypeDeclaration(
randomId(),
this.prefix(node),
Markers.EMPTY,
[],
this.visit(node.name),
node.typeParameters ? this.mapTypeParametersAsObject(node) : null,
this.leftPadded(this.prefix(this.findChildNode(node, ts.SyntaxKind.EqualsToken)!), this.convert(node.type)),
this.mapType(node)
);
}

visitEnumDeclaration(node: ts.EnumDeclaration) {
Expand Down Expand Up @@ -3038,14 +3047,16 @@ export class JavaScriptParserVisitor {
}

private mapTypeParametersAsObject(node: ts.MethodDeclaration | ts.MethodSignature | ts.FunctionDeclaration
| ts.CallSignatureDeclaration | ts.ConstructSignatureDeclaration | ts.FunctionExpression | ts.ArrowFunction) : J.TypeParameters | null {
| ts.CallSignatureDeclaration | ts.ConstructSignatureDeclaration | ts.FunctionExpression | ts.ArrowFunction | ts.TypeAliasDeclaration) : J.TypeParameters | null {
if (!node.typeParameters) return null;

let ts_prefix: Space;
if (ts.isConstructSignatureDeclaration(node)) {
ts_prefix = this.suffix(this.findChildNode(node, ts.SyntaxKind.NewKeyword)!);
} else if (ts.isFunctionExpression(node)) {
ts_prefix = this.suffix(this.findChildNode(node, ts.SyntaxKind.FunctionKeyword)!);
} else if (ts.isTypeAliasDeclaration(node)) {
ts_prefix = this.suffix(node.name);
} else {
ts_prefix = node.questionToken ? this.suffix(node.questionToken) : node.name ? this.suffix(node.name) : Space.EMPTY;
}
Expand Down
2 changes: 0 additions & 2 deletions openrewrite/src/javascript/remote/receiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
typeDeclaration = typeDeclaration.withId(ctx.receiveValue(typeDeclaration.id, ValueType.UUID)!);
typeDeclaration = typeDeclaration.withPrefix(ctx.receiveNode(typeDeclaration.prefix, receiveSpace)!);
typeDeclaration = typeDeclaration.withMarkers(ctx.receiveNode(typeDeclaration.markers, ctx.receiveMarkers)!);
typeDeclaration = typeDeclaration.withLeadingAnnotations(ctx.receiveNodes(typeDeclaration.leadingAnnotations, ctx.receiveTree)!);
typeDeclaration = typeDeclaration.withModifiers(ctx.receiveNodes(typeDeclaration.modifiers, ctx.receiveTree)!);
typeDeclaration = typeDeclaration.withName(ctx.receiveNode(typeDeclaration.name, ctx.receiveTree)!);
typeDeclaration = typeDeclaration.withTypeParameters(ctx.receiveNode(typeDeclaration.typeParameters, ctx.receiveTree));
Expand Down Expand Up @@ -1295,7 +1294,6 @@ class Factory implements ReceiverFactory {
ctx.receiveValue(null, ValueType.UUID)!,
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode(null, ctx.receiveMarkers)!,
ctx.receiveNodes<Java.Annotation>(null, ctx.receiveTree)!,
ctx.receiveNodes<Java.Modifier>(null, ctx.receiveTree)!,
ctx.receiveNode<Java.Identifier>(null, ctx.receiveTree)!,
ctx.receiveNode<Java.TypeParameters>(null, ctx.receiveTree),
Expand Down
1 change: 0 additions & 1 deletion openrewrite/src/javascript/remote/sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@ class Visitor extends JavaScriptVisitor<SenderContext> {
ctx.sendValue(typeDeclaration, v => v.id, ValueType.UUID);
ctx.sendNode(typeDeclaration, v => v.prefix, Visitor.sendSpace);
ctx.sendNode(typeDeclaration, v => v.markers, ctx.sendMarkers);
ctx.sendNodes(typeDeclaration, v => v.leadingAnnotations, ctx.sendTree, t => t.id);
ctx.sendNodes(typeDeclaration, v => v.modifiers, ctx.sendTree, t => t.id);
ctx.sendNode(typeDeclaration, v => v.name, ctx.sendTree);
ctx.sendNode(typeDeclaration, v => v.typeParameters, ctx.sendTree);
Expand Down
29 changes: 9 additions & 20 deletions openrewrite/src/javascript/tree/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1879,12 +1879,11 @@ export class Tuple extends JSMixin(Object) implements Expression, TypeTree {

@LstType("org.openrewrite.javascript.tree.JS$TypeDeclaration")
export class TypeDeclaration extends JSMixin(Object) implements Statement, TypedTree {
public constructor(id: UUID, prefix: Space, markers: Markers, leadingAnnotations: Java.Annotation[], modifiers: Java.Modifier[], name: Java.Identifier, typeParameters: Java.TypeParameters | null, initializer: JLeftPadded<Expression>, _type: JavaType | null) {
public constructor(id: UUID, prefix: Space, markers: Markers, modifiers: Java.Modifier[], name: Java.Identifier, typeParameters: Java.TypeParameters | null, initializer: JLeftPadded<Expression>, _type: JavaType | null) {
super();
this._id = id;
this._prefix = prefix;
this._markers = markers;
this._leadingAnnotations = leadingAnnotations;
this._modifiers = modifiers;
this._name = name;
this._typeParameters = typeParameters;
Expand All @@ -1899,7 +1898,7 @@ export class TypeDeclaration extends JSMixin(Object) implements Statement, Typed
}

public withId(id: UUID): TypeDeclaration {
return id === this._id ? this : new TypeDeclaration(id, this._prefix, this._markers, this._leadingAnnotations, this._modifiers, this._name, this._typeParameters, this._initializer, this._type);
return id === this._id ? this : new TypeDeclaration(id, this._prefix, this._markers, this._modifiers, this._name, this._typeParameters, this._initializer, this._type);
}

private readonly _prefix: Space;
Expand All @@ -1909,7 +1908,7 @@ export class TypeDeclaration extends JSMixin(Object) implements Statement, Typed
}

public withPrefix(prefix: Space): TypeDeclaration {
return prefix === this._prefix ? this : new TypeDeclaration(this._id, prefix, this._markers, this._leadingAnnotations, this._modifiers, this._name, this._typeParameters, this._initializer, this._type);
return prefix === this._prefix ? this : new TypeDeclaration(this._id, prefix, this._markers, this._modifiers, this._name, this._typeParameters, this._initializer, this._type);
}

private readonly _markers: Markers;
Expand All @@ -1919,17 +1918,7 @@ export class TypeDeclaration extends JSMixin(Object) implements Statement, Typed
}

public withMarkers(markers: Markers): TypeDeclaration {
return markers === this._markers ? this : new TypeDeclaration(this._id, this._prefix, markers, this._leadingAnnotations, this._modifiers, this._name, this._typeParameters, this._initializer, this._type);
}

private readonly _leadingAnnotations: Java.Annotation[];

public get leadingAnnotations(): Java.Annotation[] {
return this._leadingAnnotations;
}

public withLeadingAnnotations(leadingAnnotations: Java.Annotation[]): TypeDeclaration {
return leadingAnnotations === this._leadingAnnotations ? this : new TypeDeclaration(this._id, this._prefix, this._markers, leadingAnnotations, this._modifiers, this._name, this._typeParameters, this._initializer, this._type);
return markers === this._markers ? this : new TypeDeclaration(this._id, this._prefix, markers, this._modifiers, this._name, this._typeParameters, this._initializer, this._type);
}

private readonly _modifiers: Java.Modifier[];
Expand All @@ -1939,7 +1928,7 @@ export class TypeDeclaration extends JSMixin(Object) implements Statement, Typed
}

public withModifiers(modifiers: Java.Modifier[]): TypeDeclaration {
return modifiers === this._modifiers ? this : new TypeDeclaration(this._id, this._prefix, this._markers, this._leadingAnnotations, modifiers, this._name, this._typeParameters, this._initializer, this._type);
return modifiers === this._modifiers ? this : new TypeDeclaration(this._id, this._prefix, this._markers, modifiers, this._name, this._typeParameters, this._initializer, this._type);
}

private readonly _name: Java.Identifier;
Expand All @@ -1949,7 +1938,7 @@ export class TypeDeclaration extends JSMixin(Object) implements Statement, Typed
}

public withName(name: Java.Identifier): TypeDeclaration {
return name === this._name ? this : new TypeDeclaration(this._id, this._prefix, this._markers, this._leadingAnnotations, this._modifiers, name, this._typeParameters, this._initializer, this._type);
return name === this._name ? this : new TypeDeclaration(this._id, this._prefix, this._markers, this._modifiers, name, this._typeParameters, this._initializer, this._type);
}

private readonly _typeParameters: Java.TypeParameters | null;
Expand All @@ -1959,7 +1948,7 @@ export class TypeDeclaration extends JSMixin(Object) implements Statement, Typed
}

public withTypeParameters(typeParameters: Java.TypeParameters | null): TypeDeclaration {
return typeParameters === this._typeParameters ? this : new TypeDeclaration(this._id, this._prefix, this._markers, this._leadingAnnotations, this._modifiers, this._name, typeParameters, this._initializer, this._type);
return typeParameters === this._typeParameters ? this : new TypeDeclaration(this._id, this._prefix, this._markers, this._modifiers, this._name, typeParameters, this._initializer, this._type);
}

private readonly _initializer: JLeftPadded<Expression>;
Expand All @@ -1979,7 +1968,7 @@ export class TypeDeclaration extends JSMixin(Object) implements Statement, Typed
}

public withType(_type: JavaType | null): TypeDeclaration {
return _type === this._type ? this : new TypeDeclaration(this._id, this._prefix, this._markers, this._leadingAnnotations, this._modifiers, this._name, this._typeParameters, this._initializer, _type);
return _type === this._type ? this : new TypeDeclaration(this._id, this._prefix, this._markers, this._modifiers, this._name, this._typeParameters, this._initializer, _type);
}

public acceptJavaScript<P>(v: JavaScriptVisitor<P>, p: P): J | null {
Expand All @@ -1993,7 +1982,7 @@ export class TypeDeclaration extends JSMixin(Object) implements Statement, Typed
return t._initializer;
}
public withInitializer(initializer: JLeftPadded<Expression>): TypeDeclaration {
return t._initializer === initializer ? t : new TypeDeclaration(t._id, t._prefix, t._markers, t._leadingAnnotations, t._modifiers, t._name, t._typeParameters, initializer, t._type);
return t._initializer === initializer ? t : new TypeDeclaration(t._id, t._prefix, t._markers, t._modifiers, t._name, t._typeParameters, initializer, t._type);
}
}
}
Expand Down
1 change: 0 additions & 1 deletion openrewrite/src/javascript/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,6 @@ export class JavaScriptVisitor<P> extends JavaVisitor<P> {
}
typeDeclaration = tempStatement as TypeDeclaration;
typeDeclaration = typeDeclaration.withMarkers(this.visitMarkers(typeDeclaration.markers, p));
typeDeclaration = typeDeclaration.withLeadingAnnotations(ListUtils.map(typeDeclaration.leadingAnnotations, el => this.visitAndCast(el, p)));
typeDeclaration = typeDeclaration.withModifiers(ListUtils.map(typeDeclaration.modifiers, el => this.visitAndCast(el, p)));
typeDeclaration = typeDeclaration.withName(this.visitAndCast(typeDeclaration.name, p)!);
typeDeclaration = typeDeclaration.withTypeParameters(this.visitAndCast(typeDeclaration.typeParameters, p));
Expand Down
28 changes: 27 additions & 1 deletion openrewrite/test/javascript/parser/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ describe('function mapping', () => {
);
});

test.skip('function type with parameter', () => {
test('function type with parameter', () => {
rewriteRun(
//language=typescript
typeScript('type Transformer<T> = (input: T) => T;')
Expand Down Expand Up @@ -173,4 +173,30 @@ describe('function mapping', () => {
`)
);
});

test('function expression with name assigment', () => {
rewriteRun(
//language=typescript
typeScript(`
var helloString = 'Hello world!';
var hello = function hello() {
return helloString;
};
`)
);
});

test('function expression with name assigment with comments', () => {
rewriteRun(
//language=typescript
typeScript(`
var helloString = 'Hello world!';
var hello = /*a*/function/*b*/ hello /*c*/(/*d*/) {
return helloString;
};
`)
);
});
});
69 changes: 69 additions & 0 deletions openrewrite/test/javascript/parser/typeAlias.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import {connect, disconnect, rewriteRun, typeScript} from '../testHarness';

describe('type alias mapping', () => {
beforeAll(() => connect());
afterAll(() => disconnect());

test('simple alias', () => {
rewriteRun(
//language=typescript
typeScript(`
type StringAlias = string;
`)
);
});

test('simple alias with comments', () => {
rewriteRun(
//language=typescript
typeScript(`
/*a*/type /*b*/ StringAlias /*c*/= /*d*/string /*e*/;/*f*/
`)
);
});

test('function type alias', () => {
rewriteRun(
//language=typescript
typeScript(`
type MyFunctionType = (x: number, y: number) => string;
`)
);
});

test('generic type alias', () => {
rewriteRun(
//language=typescript
typeScript(`
type Response<T, R, Y> = (x: T, y: R) => Y;;
`)
);
});

test('generic type alias with comments', () => {
rewriteRun(
//language=typescript
typeScript(`
/*a*/type/*b*/ Response/*c*/</*d*/T/*e*/> /*f*/ = /*g*/(x: T, y: number) => string;;
`)
);
});

test('union type', () => {
rewriteRun(
//language=typescript
typeScript(`
type ID = /*a*/ number /*b*/ | /*c*/ string /*d*/;
`)
);
});

test('union type with comments', () => {
rewriteRun(
//language=typescript
typeScript(`
type ID = number | string;
`)
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,6 @@ public JS.TypeDeclaration visitTypeDeclaration(JS.TypeDeclaration typeDeclaratio
typeDeclaration = typeDeclaration.withId(ctx.receiveNonNullValue(typeDeclaration.getId(), UUID.class));
typeDeclaration = typeDeclaration.withPrefix(ctx.receiveNonNullNode(typeDeclaration.getPrefix(), JavaScriptReceiver::receiveSpace));
typeDeclaration = typeDeclaration.withMarkers(ctx.receiveNonNullNode(typeDeclaration.getMarkers(), ctx::receiveMarkers));
typeDeclaration = typeDeclaration.withLeadingAnnotations(ctx.receiveNonNullNodes(typeDeclaration.getLeadingAnnotations(), ctx::receiveTree));
typeDeclaration = typeDeclaration.withModifiers(ctx.receiveNonNullNodes(typeDeclaration.getModifiers(), ctx::receiveTree));
typeDeclaration = typeDeclaration.withName(ctx.receiveNonNullNode(typeDeclaration.getName(), ctx::receiveTree));
typeDeclaration = typeDeclaration.withTypeParameters(ctx.receiveNode(typeDeclaration.getTypeParameters(), ctx::receiveTree));
Expand Down Expand Up @@ -1406,7 +1405,6 @@ public <T> T create(Class<T> type, ReceiverContext ctx) {
ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace),
ctx.receiveNonNullNode(null, ctx::receiveMarkers),
ctx.receiveNonNullNodes(null, ctx::receiveTree),
ctx.receiveNonNullNodes(null, ctx::receiveTree),
ctx.receiveNonNullNode(null, ctx::receiveTree),
ctx.receiveNode(null, ctx::receiveTree),
ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveLeftPaddedTree),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,6 @@ public JS.TypeDeclaration visitTypeDeclaration(JS.TypeDeclaration typeDeclaratio
ctx.sendValue(typeDeclaration, JS.TypeDeclaration::getId);
ctx.sendNode(typeDeclaration, JS.TypeDeclaration::getPrefix, JavaScriptSender::sendSpace);
ctx.sendNode(typeDeclaration, JS.TypeDeclaration::getMarkers, ctx::sendMarkers);
ctx.sendNodes(typeDeclaration, JS.TypeDeclaration::getLeadingAnnotations, ctx::sendTree, Tree::getId);
ctx.sendNodes(typeDeclaration, JS.TypeDeclaration::getModifiers, ctx::sendTree, Tree::getId);
ctx.sendNode(typeDeclaration, JS.TypeDeclaration::getName, ctx::sendTree);
ctx.sendNode(typeDeclaration, JS.TypeDeclaration::getTypeParameters, ctx::sendTree);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,6 @@ public J visitTypeDeclaration(JS.TypeDeclaration typeDeclaration, P p) {
} else {
t = (JS.TypeDeclaration) temp;
}
t = t.withLeadingAnnotations(ListUtils.map(t.getLeadingAnnotations(), a -> visitAndCast(a, p)));
t = t.withModifiers(ListUtils.map(t.getModifiers(),
mod -> mod.withPrefix(visitSpace(mod.getPrefix(), Space.Location.MODIFIER_PREFIX, p))));
t = t.withModifiers(Objects.requireNonNull(ListUtils.map(t.getModifiers(), e -> visitAndCast(e, p))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,8 @@ public J visitTuple(JS.Tuple tuple, PrintOutputCapture<P> p) {
@Override
public J visitTypeDeclaration(JS.TypeDeclaration typeDeclaration, PrintOutputCapture<P> p) {
beforeSyntax(typeDeclaration, JsSpace.Location.TYPE_DECLARATION_PREFIX, p);
visit(typeDeclaration.getLeadingAnnotations(), p);
typeDeclaration.getModifiers().forEach(m -> delegate.visitModifier(m, p));
p.append("type");
visit(typeDeclaration.getName(), p);
J.TypeParameters typeParameters = typeDeclaration.getTypeParameters();
if (typeParameters != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1777,16 +1777,12 @@ final class TypeDeclaration implements JS, Statement, TypedTree {
@With
Markers markers;

@With
List<Annotation> leadingAnnotations;

@With
List<Modifier> modifiers;

@With
J.Identifier name;


@With
J.@Nullable TypeParameters typeParameters;

Expand All @@ -1811,7 +1807,7 @@ public TypeDeclaration withInitializer(Expression initializer) {
@SuppressWarnings("unchecked")
@Override
public TypeDeclaration withType(@Nullable JavaType javaType) {
return this.type == javaType ? this : new TypeDeclaration(id, prefix, markers, leadingAnnotations, modifiers, name, typeParameters, initializer, javaType);
return this.type == javaType ? this : new TypeDeclaration(id, prefix, markers, modifiers, name, typeParameters, initializer, javaType);
}

@Override
Expand Down Expand Up @@ -1849,7 +1845,7 @@ public JLeftPadded<Expression> getInitializer() {
}

public TypeDeclaration withInitializer(JLeftPadded<Expression> initializer) {
return t.initializer == initializer ? t : new TypeDeclaration(t.id, t.prefix, t.markers, t.leadingAnnotations, t.modifiers, t.name, t.typeParameters, initializer, t.type);
return t.initializer == initializer ? t : new TypeDeclaration(t.id, t.prefix, t.markers, t.modifiers, t.name, t.typeParameters, initializer, t.type);
}
}
}
Expand Down

0 comments on commit d392661

Please sign in to comment.