Skip to content
This repository was archived by the owner on May 12, 2025. It is now read-only.
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
52 changes: 49 additions & 3 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ export class JavaScriptParserVisitor {
);
}

private mapModifiers(node: ts.VariableDeclarationList | ts.VariableStatement | ts.ClassDeclaration | ts.PropertyDeclaration | ts.FunctionDeclaration | ts.ParameterDeclaration | ts.MethodDeclaration | ts.EnumDeclaration | ts.InterfaceDeclaration | ts.PropertySignature | ts.ConstructorDeclaration) {
private mapModifiers(node: ts.VariableDeclarationList | ts.VariableStatement | ts.ClassDeclaration | ts.PropertyDeclaration | ts.FunctionDeclaration | ts.ParameterDeclaration | ts.MethodDeclaration | ts.EnumDeclaration | ts.InterfaceDeclaration | ts.PropertySignature | ts.ConstructorDeclaration | ts.ModuleDeclaration) {
if (ts.isVariableStatement(node)) {
return [new J.Modifier(
randomId(),
Expand All @@ -257,6 +257,8 @@ export class JavaScriptParserVisitor {
J.Modifier.Type.LanguageExtension,
[]
)];
} else if (ts.isModuleDeclaration(node)) {
return node.modifiers ? node.modifiers?.filter(ts.isModifier).map(this.mapModifier) : [];
} else if (ts.isClassDeclaration(node)) {
return node.modifiers ? node.modifiers?.filter(ts.isModifier).map(this.mapModifier) : [];
} else if (ts.isEnumDeclaration(node) || ts.isInterfaceDeclaration(node)) {
Expand Down Expand Up @@ -1824,11 +1826,55 @@ export class JavaScriptParserVisitor {
}

visitModuleDeclaration(node: ts.ModuleDeclaration) {
return this.visitUnknown(node);
const body = this.visit(node.body as ts.Node);

const namespaceKeyword = this.findChildNode(node, ts.SyntaxKind.NamespaceKeyword);
if (body instanceof JS.NamespaceDeclaration) {
return new JS.NamespaceDeclaration(
randomId(),
Space.EMPTY,
Markers.EMPTY,
this.mapModifiers(node),
namespaceKeyword ? this.prefix(namespaceKeyword) : Space.EMPTY,
this.rightPadded(
new J.FieldAccess(
randomId(),
this.prefix(node),
Markers.EMPTY,
this.visit(node.name),
new J.JLeftPadded(
this.suffix(node.name),
body.name as J.Identifier,
Markers.EMPTY
),
null
),
Space.EMPTY
),
body.body
);
} else {
return new JS.NamespaceDeclaration(
randomId(),
node.parent.kind === ts.SyntaxKind.ModuleBlock ? this.prefix(node) : Space.EMPTY,
Markers.EMPTY,
this.mapModifiers(node),
namespaceKeyword ? this.prefix(namespaceKeyword) : Space.EMPTY,
this.rightPadded(this.convert(node.name), this.prefix(node)), // J.FieldAccess
body // J.Block
);
}
}

visitModuleBlock(node: ts.ModuleBlock) {
return this.visitUnknown(node);
return new J.Block(
randomId(),
this.prefix(node),
Markers.EMPTY,
this.rightPadded(false, Space.EMPTY),
this.semicolonPaddedStatementList(node.statements),
this.prefix(node.getLastToken()!)
);
}

visitCaseBlock(node: ts.CaseBlock) {
Expand Down
25 changes: 24 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, DefaultType, Delete, Export, ExpressionStatement, FunctionType, JsImport, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeOperator, Unary, Union, Void, Yield, TypeInfo} from '../tree';
import {JS, CompilationUnit, Alias, ArrowFunction, Await, DefaultType, Delete, Export, ExpressionStatement, FunctionType, JsImport, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeOperator, Unary, Union, Void, Yield, TypeInfo, NamespaceDeclaration} 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 @@ -300,6 +300,17 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
return typeInfo;
}

public visitNamespaceDeclaration(namespaceDeclaration: NamespaceDeclaration, ctx: ReceiverContext): J {
namespaceDeclaration = namespaceDeclaration.withId(ctx.receiveValue(namespaceDeclaration.id, ValueType.UUID)!);
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.padding.withName(ctx.receiveNode(namespaceDeclaration.padding.name, receiveRightPaddedTree)!);
namespaceDeclaration = namespaceDeclaration.withBody(ctx.receiveNode(namespaceDeclaration.body, ctx.receiveTree)!);
return namespaceDeclaration;
}

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 @@ -1270,6 +1281,18 @@ class Factory implements ReceiverFactory {
);
}

if (type === "org.openrewrite.javascript.tree.JS$NamespaceDeclaration") {
return new NamespaceDeclaration(
ctx.receiveValue(null, ValueType.UUID)!,
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode(null, ctx.receiveMarkers)!,
ctx.receiveNodes<Java.Modifier>(null, ctx.receiveTree)!,
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode<JRightPadded<Expression>>(null, receiveRightPaddedTree)!,
ctx.receiveNode<Java.Block>(null, ctx.receiveTree)!
);
}

if (type === "org.openrewrite.java.tree.J$AnnotatedType") {
return new Java.AnnotatedType(
ctx.receiveValue(null, ValueType.UUID)!,
Expand Down
13 changes: 12 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, DefaultType, Delete, Export, ExpressionStatement, FunctionType, JsImport, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeOperator, Unary, Union, Void, Yield, TypeInfo} from '../tree';
import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, DefaultType, Delete, Export, ExpressionStatement, FunctionType, JsImport, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeOperator, Unary, Union, Void, Yield, TypeInfo, NamespaceDeclaration} from '../tree';
import {Expression, J, JContainer, JLeftPadded, JRightPadded, Space, Statement} from "../../java";
import * as Java from "../../java/tree";

Expand Down Expand Up @@ -295,6 +295,17 @@ class Visitor extends JavaScriptVisitor<SenderContext> {
return typeInfo;
}

public visitNamespaceDeclaration(namespaceDeclaration: NamespaceDeclaration, ctx: SenderContext): J {
ctx.sendValue(namespaceDeclaration, v => v.id, ValueType.UUID);
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.padding.name, Visitor.sendRightPadded(ValueType.Tree));
ctx.sendNode(namespaceDeclaration, v => v.body, ctx.sendTree);
return namespaceDeclaration;
}

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
3 changes: 3 additions & 0 deletions openrewrite/src/javascript/tree/support_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ export namespace JsSpace {
VOID_PREFIX,
YIELD_PREFIX,
TYPE_INFO_PREFIX,
NAMESPACE_DECLARATION_PREFIX,
NAMESPACE_KEYWORD_DECLARATION_PREFIX
}
}
export namespace JsLeftPadded {
Expand All @@ -239,6 +241,7 @@ export namespace JsRightPadded {
SCOPED_VARIABLE_DECLARATIONS_VARIABLES,
TEMPLATE_EXPRESSION_TAG,
UNION_TYPES,
NAMESPACE_DECLARATION_NAME,
}
}
export namespace JsContainer {
Expand Down
101 changes: 101 additions & 0 deletions openrewrite/src/javascript/tree/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2409,3 +2409,104 @@ export class TypeInfo extends JSMixin(Object) implements Expression, TypeTree {
}

}

@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) {
super();
this._id = id;
this._prefix = prefix;
this._markers = markers;
this._modifiers = modifiers;
this._namespace = namespace;
this._name = name;
this._body = body;
}

private readonly _id: UUID;

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

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

private readonly _prefix: Space;

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

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

private readonly _markers: Markers;

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

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

private readonly _modifiers: Java.Modifier[];

public get modifiers(): Java.Modifier[] {
return this._modifiers;
}

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

private readonly _namespace: Space;

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

private readonly _name: JRightPadded<Expression>;

public get name(): Expression {
return this._name.element;
}

public withName(name: Expression): NamespaceDeclaration {
return this.padding.withName(this._name.withElement(name));
}

private readonly _body: Java.Block;

public get body(): Java.Block {
return this._body;
}

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

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

get padding() {
const t = this;
return new class {
public get name(): JRightPadded<Expression> {
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);
}
}
}

}
18 changes: 17 additions & 1 deletion openrewrite/src/javascript/visitor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as extensions from "./extensions";
import {ListUtils, SourceFile, Tree, TreeVisitor} from "../core";
import {JS, isJavaScript, JsLeftPadded, JsRightPadded, JsContainer, JsSpace} from "./tree";
import {CompilationUnit, Alias, ArrowFunction, Await, DefaultType, Delete, Export, ExpressionStatement, FunctionType, JsImport, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeOperator, Unary, Union, Void, Yield, TypeInfo} from "./tree";
import {CompilationUnit, Alias, ArrowFunction, Await, DefaultType, Delete, Export, ExpressionStatement, FunctionType, JsImport, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeOperator, Unary, Union, Void, Yield, TypeInfo, NamespaceDeclaration} from "./tree";
import {Expression, J, JContainer, JLeftPadded, JRightPadded, Space, Statement} from "../java/tree";
import {JavaVisitor} from "../java";
import * as Java from "../java/tree";
Expand Down Expand Up @@ -395,6 +395,22 @@ export class JavaScriptVisitor<P> extends JavaVisitor<P> {
return typeInfo;
}

public visitNamespaceDeclaration(namespaceDeclaration: NamespaceDeclaration, p: P): J | null {
namespaceDeclaration = namespaceDeclaration.withPrefix(this.visitJsSpace(namespaceDeclaration.prefix, JsSpace.Location.NAMESPACE_DECLARATION_PREFIX, p)!);
let tempStatement = this.visitStatement(namespaceDeclaration, p) as Statement;
if (!(tempStatement instanceof NamespaceDeclaration))
{
return tempStatement;
}
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_KEYWORD_DECLARATION_PREFIX, 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 visitJsLeftPadded<T>(left: JLeftPadded<T> | null, loc: JsLeftPadded.Location, p: P): JLeftPadded<T> {
return extensions.visitJsLeftPadded(this, left, loc, p);
}
Expand Down
15 changes: 14 additions & 1 deletion openrewrite/test/javascript/parser/enum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,26 @@ describe('empty mapping', () => {
);
});

test('enum with declare const modifier and comments', () => {
rewriteRun(
//language=typescript
typeScript(`
/*a*/ declare /*b*/ const /*c*/ enum Test {
A,
B,
C,
};
`)
);
});

test('enum members with comments', () => {
rewriteRun(
//language=typescript
typeScript(`
enum Test /*xx*/ {
A /*aa*/,
/*bb*/ B /*cc*/,
/*bb*/ B /*cc*/,
C, /*dd*/
};
`)
Expand Down
2 changes: 1 addition & 1 deletion openrewrite/test/javascript/parser/interface.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('as mapping', () => {
interface HasLegs {
count: string;
}

interface Animal {
name: string;
}
Expand Down
Loading