Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegDokuka committed Nov 12, 2024
1 parent 8cf3073 commit e1dc0a2
Show file tree
Hide file tree
Showing 11 changed files with 270 additions and 32 deletions.
9 changes: 6 additions & 3 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1994,14 +1994,17 @@ export class JavaScriptParserVisitor {
visitModuleDeclaration(node: ts.ModuleDeclaration) {
const body = this.visit(node.body as ts.Node);

const namespaceKeyword = this.findChildNode(node, ts.SyntaxKind.NamespaceKeyword);
let namespaceKeyword = this.findChildNode(node, ts.SyntaxKind.NamespaceKeyword);
const kindType = namespaceKeyword ? JS.NamespaceDeclaration.Kind.Type.Namespace : JS.NamespaceDeclaration.Kind.Type.Module
namespaceKeyword ??= this.findChildNode(node, ts.SyntaxKind.ModuleKeyword);
const kind = new JS.NamespaceDeclaration.Kind(randomId(), this.prefix(namespaceKeyword!), Markers.EMPTY, kindType)
if (body instanceof JS.NamespaceDeclaration) {
return new JS.NamespaceDeclaration(
randomId(),
Space.EMPTY,
Markers.EMPTY,
this.mapModifiers(node),
namespaceKeyword ? this.prefix(namespaceKeyword) : Space.EMPTY,
kind,
this.rightPadded(
new J.FieldAccess(
randomId(),
Expand All @@ -2025,7 +2028,7 @@ export class JavaScriptParserVisitor {
node.parent.kind === ts.SyntaxKind.ModuleBlock ? this.prefix(node) : Space.EMPTY,
Markers.EMPTY,
this.mapModifiers(node),
namespaceKeyword ? this.prefix(namespaceKeyword) : Space.EMPTY,
kind,
this.rightPadded(this.convert(node.name), this.prefix(node)), // J.FieldAccess
body // J.Block
);
Expand Down
21 changes: 19 additions & 2 deletions openrewrite/src/javascript/remote/receiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,12 +345,20 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
namespaceDeclaration = namespaceDeclaration.withPrefix(ctx.receiveNode(namespaceDeclaration.prefix, receiveSpace)!);
namespaceDeclaration = namespaceDeclaration.withMarkers(ctx.receiveNode(namespaceDeclaration.markers, ctx.receiveMarkers)!);
namespaceDeclaration = namespaceDeclaration.withModifiers(ctx.receiveNodes(namespaceDeclaration.modifiers, ctx.receiveTree)!);
namespaceDeclaration = namespaceDeclaration.withNamespace(ctx.receiveNode(namespaceDeclaration.namespace, receiveSpace)!);
namespaceDeclaration = namespaceDeclaration.withKind(ctx.receiveNode(namespaceDeclaration.kind, ctx.receiveTree)!);
namespaceDeclaration = namespaceDeclaration.padding.withName(ctx.receiveNode(namespaceDeclaration.padding.name, receiveRightPaddedTree)!);
namespaceDeclaration = namespaceDeclaration.withBody(ctx.receiveNode(namespaceDeclaration.body, ctx.receiveTree)!);
return namespaceDeclaration;
}

public visitNamespaceDeclarationKind(kind: NamespaceDeclaration.Kind, ctx: ReceiverContext): J {
kind = kind.withId(ctx.receiveValue(kind.id, ValueType.UUID)!);
kind = kind.withPrefix(ctx.receiveNode(kind.prefix, receiveSpace)!);
kind = kind.withMarkers(ctx.receiveNode(kind.markers, ctx.receiveMarkers)!);
kind = kind.withType(ctx.receiveValue(kind.type, ValueType.Enum)!);
return kind;
}

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 @@ -1370,12 +1378,21 @@ class Factory implements ReceiverFactory {
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode(null, ctx.receiveMarkers)!,
ctx.receiveNodes<Java.Modifier>(null, ctx.receiveTree)!,
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode<NamespaceDeclaration.Kind>(null, ctx.receiveTree)!,
ctx.receiveNode<JRightPadded<Expression>>(null, receiveRightPaddedTree)!,
ctx.receiveNode<Java.Block>(null, ctx.receiveTree)!
);
}

if (type === "org.openrewrite.javascript.tree.JS$NamespaceDeclaration$Kind") {
return new NamespaceDeclaration.Kind(
ctx.receiveValue(null, ValueType.UUID)!,
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode(null, ctx.receiveMarkers)!,
ctx.receiveValue(null, ValueType.Enum)!
);
}

if (type === "org.openrewrite.java.tree.J$AnnotatedType") {
return new Java.AnnotatedType(
ctx.receiveValue(null, ValueType.UUID)!,
Expand Down
10 changes: 9 additions & 1 deletion openrewrite/src/javascript/remote/sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,12 +340,20 @@ class Visitor extends JavaScriptVisitor<SenderContext> {
ctx.sendNode(namespaceDeclaration, v => v.prefix, Visitor.sendSpace);
ctx.sendNode(namespaceDeclaration, v => v.markers, ctx.sendMarkers);
ctx.sendNodes(namespaceDeclaration, v => v.modifiers, ctx.sendTree, t => t.id);
ctx.sendNode(namespaceDeclaration, v => v.namespace, Visitor.sendSpace);
ctx.sendNode(namespaceDeclaration, v => v.kind, ctx.sendTree);
ctx.sendNode(namespaceDeclaration, v => v.padding.name, Visitor.sendRightPadded(ValueType.Tree));
ctx.sendNode(namespaceDeclaration, v => v.body, ctx.sendTree);
return namespaceDeclaration;
}

public visitNamespaceDeclarationKind(kind: NamespaceDeclaration.Kind, ctx: SenderContext): J {
ctx.sendValue(kind, v => v.id, ValueType.UUID);
ctx.sendNode(kind, v => v.prefix, Visitor.sendSpace);
ctx.sendNode(kind, v => v.markers, ctx.sendMarkers);
ctx.sendValue(kind, v => v.type, ValueType.Enum);
return kind;
}

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
94 changes: 79 additions & 15 deletions openrewrite/src/javascript/tree/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2838,13 +2838,13 @@ export class JSMethodDeclaration extends JSMixin(Object) implements Statement, T

@LstType("org.openrewrite.javascript.tree.JS$NamespaceDeclaration")
export class NamespaceDeclaration extends JSMixin(Object) implements Statement {
public constructor(id: UUID, prefix: Space, markers: Markers, modifiers: Java.Modifier[], namespace: Space, name: JRightPadded<Expression>, body: Java.Block) {
public constructor(id: UUID, prefix: Space, markers: Markers, modifiers: Java.Modifier[], kind: NamespaceDeclaration.Kind, name: JRightPadded<Expression>, body: Java.Block) {
super();
this._id = id;
this._prefix = prefix;
this._markers = markers;
this._modifiers = modifiers;
this._namespace = namespace;
this._kind = kind;
this._name = name;
this._body = body;
}
Expand All @@ -2856,7 +2856,7 @@ export class NamespaceDeclaration extends JSMixin(Object) implements Statement {
}

public withId(id: UUID): NamespaceDeclaration {
return id === this._id ? this : new NamespaceDeclaration(id, this._prefix, this._markers, this._modifiers, this._namespace, this._name, this._body);
return id === this._id ? this : new NamespaceDeclaration(id, this._prefix, this._markers, this._modifiers, this._kind, this._name, this._body);
}

private readonly _prefix: Space;
Expand All @@ -2866,7 +2866,7 @@ export class NamespaceDeclaration extends JSMixin(Object) implements Statement {
}

public withPrefix(prefix: Space): NamespaceDeclaration {
return prefix === this._prefix ? this : new NamespaceDeclaration(this._id, prefix, this._markers, this._modifiers, this._namespace, this._name, this._body);
return prefix === this._prefix ? this : new NamespaceDeclaration(this._id, prefix, this._markers, this._modifiers, this._kind, this._name, this._body);
}

private readonly _markers: Markers;
Expand All @@ -2876,7 +2876,7 @@ export class NamespaceDeclaration extends JSMixin(Object) implements Statement {
}

public withMarkers(markers: Markers): NamespaceDeclaration {
return markers === this._markers ? this : new NamespaceDeclaration(this._id, this._prefix, markers, this._modifiers, this._namespace, this._name, this._body);
return markers === this._markers ? this : new NamespaceDeclaration(this._id, this._prefix, markers, this._modifiers, this._kind, this._name, this._body);
}

private readonly _modifiers: Java.Modifier[];
Expand All @@ -2886,17 +2886,13 @@ export class NamespaceDeclaration extends JSMixin(Object) implements Statement {
}

public withModifiers(modifiers: Java.Modifier[]): NamespaceDeclaration {
return modifiers === this._modifiers ? this : new NamespaceDeclaration(this._id, this._prefix, this._markers, modifiers, this._namespace, this._name, this._body);
return modifiers === this._modifiers ? this : new NamespaceDeclaration(this._id, this._prefix, this._markers, modifiers, this._kind, this._name, this._body);
}

private readonly _namespace: Space;
private readonly _kind: NamespaceDeclaration.Kind;

public get namespace(): Space {
return this._namespace;
}

public withNamespace(namespace: Space): NamespaceDeclaration {
return namespace === this._namespace ? this : new NamespaceDeclaration(this._id, this._prefix, this._markers, this._modifiers, namespace, this._name, this._body);
public withKind(kind: NamespaceDeclaration.Kind): NamespaceDeclaration {
return kind === this._kind ? this : new NamespaceDeclaration(this._id, this._prefix, this._markers, this._modifiers, kind, this._name, this._body);
}

private readonly _name: JRightPadded<Expression>;
Expand All @@ -2916,7 +2912,7 @@ export class NamespaceDeclaration extends JSMixin(Object) implements Statement {
}

public withBody(body: Java.Block): NamespaceDeclaration {
return body === this._body ? this : new NamespaceDeclaration(this._id, this._prefix, this._markers, this._modifiers, this._namespace, this._name, body);
return body === this._body ? this : new NamespaceDeclaration(this._id, this._prefix, this._markers, this._modifiers, this._kind, this._name, body);
}

public acceptJavaScript<P>(v: JavaScriptVisitor<P>, p: P): J | null {
Expand All @@ -2930,9 +2926,77 @@ export class NamespaceDeclaration extends JSMixin(Object) implements Statement {
return t._name;
}
public withName(name: JRightPadded<Expression>): NamespaceDeclaration {
return t._name === name ? t : new NamespaceDeclaration(t._id, t._prefix, t._markers, t._modifiers, t._namespace, name, t._body);
return t._name === name ? t : new NamespaceDeclaration(t._id, t._prefix, t._markers, t._modifiers, t._kind, name, t._body);
}
}
}

}

export namespace NamespaceDeclaration {
@LstType("org.openrewrite.javascript.tree.JS$NamespaceDeclaration$Kind")
export class Kind extends JSMixin(Object) {
public constructor(id: UUID, prefix: Space, markers: Markers, _type: NamespaceDeclaration.Kind.Type) {
super();
this._id = id;
this._prefix = prefix;
this._markers = markers;
this._type = _type;
}

private readonly _id: UUID;

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

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

private readonly _prefix: Space;

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

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

private readonly _markers: Markers;

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

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

private readonly _type: NamespaceDeclaration.Kind.Type;

public get type(): NamespaceDeclaration.Kind.Type {
return this._type;
}

public withType(_type: NamespaceDeclaration.Kind.Type): NamespaceDeclaration.Kind {
return _type === this._type ? this : new NamespaceDeclaration.Kind(this._id, this._prefix, this._markers, _type);
}

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

}

export namespace Kind {
export enum Type {
Namespace = 0,
Module = 1,

}

}

}
8 changes: 7 additions & 1 deletion openrewrite/src/javascript/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,12 +452,18 @@ export class JavaScriptVisitor<P> extends JavaVisitor<P> {
namespaceDeclaration = tempStatement as NamespaceDeclaration;
namespaceDeclaration = namespaceDeclaration.withMarkers(this.visitMarkers(namespaceDeclaration.markers, p));
namespaceDeclaration = namespaceDeclaration.withModifiers(ListUtils.map(namespaceDeclaration.modifiers, el => this.visitAndCast(el, p)));
namespaceDeclaration = namespaceDeclaration.withNamespace(this.visitJsSpace(namespaceDeclaration.namespace, JsSpace.Location.NAMESPACE_DECLARATION_NAMESPACE, p)!);
namespaceDeclaration = namespaceDeclaration.withKind(this.visitAndCast(namespaceDeclaration.kind, p)!);
namespaceDeclaration = namespaceDeclaration.padding.withName(this.visitJsRightPadded(namespaceDeclaration.padding.name, JsRightPadded.Location.NAMESPACE_DECLARATION_NAME, p)!);
namespaceDeclaration = namespaceDeclaration.withBody(this.visitAndCast(namespaceDeclaration.body, p)!);
return namespaceDeclaration;
}

public visitNamespaceDeclarationKind(kind: NamespaceDeclaration.Kind, p: P): J | null {
kind = kind.withPrefix(this.visitJsSpace(kind.prefix, JsSpace.Location.NAMESPACE_DECLARATION_KIND_PREFIX, p)!);
kind = kind.withMarkers(this.visitMarkers(kind.markers, p));
return kind;
}

public visitJsLeftPadded<T>(left: JLeftPadded<T> | null, loc: JsLeftPadded.Location, p: P): JLeftPadded<T> {
return extensions.visitJsLeftPadded(this, left, loc, p);
}
Expand Down
49 changes: 49 additions & 0 deletions openrewrite/test/javascript/parser/namespace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,53 @@ describe('namespace mapping', () => {
`)
);
});


test('complex test with namaspaces and modules', () => {
rewriteRun(
//language=typescript
typeScript(`
export var asdf = 123;
module porting {
var foo = {};
foo.bar = 123; // error : property 'bar' does not exist on \`{}\`
foo.bas = 'hello'; // error : property 'bas' does not exist on \`{}\`
}
module assert {
interface Foo {
bar: number;
bas: string;
}
var foo = {} as Foo;
foo.bar = 123;
foo.bas = 'hello';
}
module sdfsdfsdf {
var foo: any;
var bar = <string>foo; // bar is now of type "string"
}
namespace doubleAssertion {
function handler1(event: Event) {
let mouseEvent = event as MouseEvent;
}
function handler2(event: Event) {
let element = event as HTMLElement; // Error : Neither 'Event' not type 'HTMLElement' is assignable to the other
}
function handler(event: Event) {
let element = event as any as HTMLElement; // Okay!
}
}
`)
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -422,12 +422,21 @@ public JS.NamespaceDeclaration visitNamespaceDeclaration(JS.NamespaceDeclaration
namespaceDeclaration = namespaceDeclaration.withPrefix(ctx.receiveNonNullNode(namespaceDeclaration.getPrefix(), JavaScriptReceiver::receiveSpace));
namespaceDeclaration = namespaceDeclaration.withMarkers(ctx.receiveNonNullNode(namespaceDeclaration.getMarkers(), ctx::receiveMarkers));
namespaceDeclaration = namespaceDeclaration.withModifiers(ctx.receiveNonNullNodes(namespaceDeclaration.getModifiers(), ctx::receiveTree));
namespaceDeclaration = namespaceDeclaration.withNamespace(ctx.receiveNonNullNode(namespaceDeclaration.getNamespace(), JavaScriptReceiver::receiveSpace));
namespaceDeclaration = namespaceDeclaration.withKind(ctx.receiveNonNullNode(namespaceDeclaration.getKind(), ctx::receiveTree));
namespaceDeclaration = namespaceDeclaration.getPadding().withName(ctx.receiveNonNullNode(namespaceDeclaration.getPadding().getName(), JavaScriptReceiver::receiveRightPaddedTree));
namespaceDeclaration = namespaceDeclaration.withBody(ctx.receiveNonNullNode(namespaceDeclaration.getBody(), ctx::receiveTree));
return namespaceDeclaration;
}

@Override
public JS.NamespaceDeclaration.Kind visitNamespaceDeclarationKind(JS.NamespaceDeclaration.Kind kind, ReceiverContext ctx) {
kind = kind.withId(ctx.receiveNonNullValue(kind.getId(), UUID.class));
kind = kind.withPrefix(ctx.receiveNonNullNode(kind.getPrefix(), JavaScriptReceiver::receiveSpace));
kind = kind.withMarkers(ctx.receiveNonNullNode(kind.getMarkers(), ctx::receiveMarkers));
kind = kind.withType(ctx.receiveNonNullValue(kind.getType(), JS.NamespaceDeclaration.Kind.Type.class));
return kind;
}

@Override
public J.AnnotatedType visitAnnotatedType(J.AnnotatedType annotatedType, ReceiverContext ctx) {
annotatedType = annotatedType.withId(ctx.receiveNonNullValue(annotatedType.getId(), UUID.class));
Expand Down Expand Up @@ -1477,12 +1486,21 @@ 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.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace),
ctx.receiveNonNullNode(null, ctx::receiveTree),
ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveRightPaddedTree),
ctx.receiveNonNullNode(null, ctx::receiveTree)
);
}

if (type == JS.NamespaceDeclaration.Kind.class) {
return (T) new JS.NamespaceDeclaration.Kind(
ctx.receiveNonNullValue(null, UUID.class),
ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace),
ctx.receiveNonNullNode(null, ctx::receiveMarkers),
ctx.receiveNonNullValue(null, JS.NamespaceDeclaration.Kind.Type.class)
);
}

if (type == J.AnnotatedType.class) {
return (T) new J.AnnotatedType(
ctx.receiveNonNullValue(null, UUID.class),
Expand Down
Loading

0 comments on commit e1dc0a2

Please sign in to comment.