diff --git a/openrewrite/src/javascript/parser.ts b/openrewrite/src/javascript/parser.ts index fda9650d..04b0dfb9 100644 --- a/openrewrite/src/javascript/parser.ts +++ b/openrewrite/src/javascript/parser.ts @@ -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), @@ -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) { @@ -3038,7 +3047,7 @@ 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; @@ -3046,6 +3055,8 @@ export class JavaScriptParserVisitor { 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; } diff --git a/openrewrite/src/javascript/remote/receiver.ts b/openrewrite/src/javascript/remote/receiver.ts index f4daa299..5765c8d3 100644 --- a/openrewrite/src/javascript/remote/receiver.ts +++ b/openrewrite/src/javascript/remote/receiver.ts @@ -241,7 +241,6 @@ class Visitor extends JavaScriptVisitor { 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)); @@ -1295,7 +1294,6 @@ class Factory implements ReceiverFactory { ctx.receiveValue(null, ValueType.UUID)!, ctx.receiveNode(null, receiveSpace)!, ctx.receiveNode(null, ctx.receiveMarkers)!, - ctx.receiveNodes(null, ctx.receiveTree)!, ctx.receiveNodes(null, ctx.receiveTree)!, ctx.receiveNode(null, ctx.receiveTree)!, ctx.receiveNode(null, ctx.receiveTree), diff --git a/openrewrite/src/javascript/remote/sender.ts b/openrewrite/src/javascript/remote/sender.ts index 36aa8c5c..a563d9a7 100644 --- a/openrewrite/src/javascript/remote/sender.ts +++ b/openrewrite/src/javascript/remote/sender.ts @@ -236,7 +236,6 @@ class Visitor extends JavaScriptVisitor { 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); diff --git a/openrewrite/src/javascript/tree/tree.ts b/openrewrite/src/javascript/tree/tree.ts index 8f90d679..39783e8c 100644 --- a/openrewrite/src/javascript/tree/tree.ts +++ b/openrewrite/src/javascript/tree/tree.ts @@ -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, _type: JavaType | null) { + public constructor(id: UUID, prefix: Space, markers: Markers, modifiers: Java.Modifier[], name: Java.Identifier, typeParameters: Java.TypeParameters | null, initializer: JLeftPadded, _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; @@ -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; @@ -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; @@ -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[]; @@ -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; @@ -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; @@ -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; @@ -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

(v: JavaScriptVisitor

, p: P): J | null { @@ -1993,7 +1982,7 @@ export class TypeDeclaration extends JSMixin(Object) implements Statement, Typed return t._initializer; } public withInitializer(initializer: JLeftPadded): 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); } } } diff --git a/openrewrite/src/javascript/visitor.ts b/openrewrite/src/javascript/visitor.ts index b43c0e90..7864cf00 100644 --- a/openrewrite/src/javascript/visitor.ts +++ b/openrewrite/src/javascript/visitor.ts @@ -301,7 +301,6 @@ export class JavaScriptVisitor

extends JavaVisitor

{ } 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)); diff --git a/openrewrite/test/javascript/parser/function.test.ts b/openrewrite/test/javascript/parser/function.test.ts index 4651e5b1..0b6f8887 100644 --- a/openrewrite/test/javascript/parser/function.test.ts +++ b/openrewrite/test/javascript/parser/function.test.ts @@ -145,7 +145,7 @@ describe('function mapping', () => { ); }); - test.skip('function type with parameter', () => { + test('function type with parameter', () => { rewriteRun( //language=typescript typeScript('type Transformer = (input: T) => T;') @@ -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; + }; + `) + ); + }); }); diff --git a/openrewrite/test/javascript/parser/typeAlias.test.ts b/openrewrite/test/javascript/parser/typeAlias.test.ts new file mode 100644 index 00000000..0db5ef91 --- /dev/null +++ b/openrewrite/test/javascript/parser/typeAlias.test.ts @@ -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 = (x: T, y: R) => Y;; + `) + ); + }); + + test('generic type alias with comments', () => { + rewriteRun( + //language=typescript + typeScript(` + /*a*/type/*b*/ Response/*c*/ /*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; + `) + ); + }); +}); diff --git a/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptReceiver.java b/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptReceiver.java index a5149cbb..4f8f694a 100644 --- a/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptReceiver.java +++ b/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptReceiver.java @@ -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)); @@ -1406,7 +1405,6 @@ public T create(Class 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), diff --git a/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptSender.java b/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptSender.java index bf8b9ab3..18a723cc 100644 --- a/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptSender.java +++ b/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptSender.java @@ -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); diff --git a/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java b/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java index 0f71a7b8..7ad184f8 100644 --- a/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java +++ b/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java @@ -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)))); diff --git a/rewrite-javascript/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java b/rewrite-javascript/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java index e4c694e9..9cd0c4af 100644 --- a/rewrite-javascript/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java +++ b/rewrite-javascript/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java @@ -397,8 +397,8 @@ public J visitTuple(JS.Tuple tuple, PrintOutputCapture

p) { @Override public J visitTypeDeclaration(JS.TypeDeclaration typeDeclaration, PrintOutputCapture

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) { diff --git a/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JS.java b/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JS.java index c106ca6c..72bed872 100644 --- a/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JS.java +++ b/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JS.java @@ -1777,16 +1777,12 @@ final class TypeDeclaration implements JS, Statement, TypedTree { @With Markers markers; - @With - List leadingAnnotations; - @With List modifiers; @With J.Identifier name; - @With J.@Nullable TypeParameters typeParameters; @@ -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 @@ -1849,7 +1845,7 @@ public JLeftPadded getInitializer() { } public TypeDeclaration withInitializer(JLeftPadded 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); } } }