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 TypeDeclaration #151

Merged
merged 2 commits into from
Nov 20, 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
53 changes: 50 additions & 3 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,24 @@ export class JavaScriptParserVisitor {
);
}

if (ts.isComputedPropertyName(node.name)) {
return new JS.JSMethodDeclaration(
randomId(),
this.prefix(node),
Markers.EMPTY,
[], // no decorators allowed
[], // no modifiers allowed
this.mapTypeParametersAsObject(node),
this.mapTypeInfo(node),
this.convert(node.name),
this.mapCommaSeparatedList(this.getParameterListNodes(node)),
null,
null,
null,
this.mapMethodType(node)
);
}

return new J.MethodDeclaration(
randomId(),
this.prefix(node),
Expand Down Expand Up @@ -931,6 +949,24 @@ export class JavaScriptParserVisitor {
);
}

if (ts.isComputedPropertyName(node.name)) {
return new JS.JSMethodDeclaration(
randomId(),
this.prefix(node),
Markers.EMPTY,
this.mapDecorators(node),
this.mapModifiers(node),
this.mapTypeParametersAsObject(node),
this.mapTypeInfo(node),
this.convert(node.name),
this.mapCommaSeparatedList(this.getParameterListNodes(node)),
null,
node.body ? this.convert<J.Block>(node.body) : null,
null,
this.mapMethodType(node)
);
}

return new J.MethodDeclaration(
randomId(),
this.prefix(node),
Expand Down Expand Up @@ -1464,7 +1500,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 +2320,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 +3083,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;
};
`)
);
});
});
37 changes: 37 additions & 0 deletions openrewrite/test/javascript/parser/method.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,41 @@ describe('method mapping', () => {
`)
);
});

test('method with ComputedPropertyName', () => {
rewriteRun(
//language=typescript
typeScript(`
const asyncIterable = {
[ Symbol . asyncIterator ] () {
return {
async next() {
return {value: undefined, done: true};
},
};
},
};
`)
);
});

test('method signature with ComputedPropertyName', () => {
rewriteRun(
//language=typescript
typeScript(`
const greetSymbol = Symbol("greet");

interface Greeter {
/*a*/[/*b*/greetSymbol/*c*/]/*d*/(message: string): void; // Computed method name
}

const greeter: Greeter = {
[greetSymbol](message: string): void {
console.log(message);
},
};
`)
);
});

});
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
Loading