Skip to content

Commit

Permalink
Add support for proper module keyword printing in case of JS.Namesp…
Browse files Browse the repository at this point in the history
…aceDeclaration & add export keyword support for variables statement (#143)
  • Loading branch information
OlegDokuka authored Nov 13, 2024
1 parent 7f1f011 commit 79ea7e0
Show file tree
Hide file tree
Showing 15 changed files with 196 additions and 61 deletions.
35 changes: 11 additions & 24 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,26 +250,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 | ts.ModuleDeclaration | ts.GetAccessorDeclaration | ts.SetAccessorDeclaration) {
if (ts.isVariableStatement(node)) {
return [new J.Modifier(
randomId(),
Space.EMPTY,
Markers.EMPTY,
node.declarationList.getFirstToken()?.getText()!,
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)) {
return node.modifiers ? node.modifiers?.filter(ts.isModifier).map(this.mapModifier) : [];
} else if (ts.isPropertyDeclaration(node)) {
return node.modifiers ? node.modifiers?.filter(ts.isModifier).map(this.mapModifier) : [];
} else if (ts.isPropertySignature(node)) {
return node.modifiers ? node.modifiers?.filter(ts.isModifier).map(this.mapModifier) : [];
} else if (ts.isFunctionDeclaration(node) || ts.isParameter(node) || ts.isMethodDeclaration(node) || ts.isConstructorDeclaration(node)) {
if (ts.isVariableStatement(node) || ts.isModuleDeclaration(node) || ts.isClassDeclaration(node) || ts.isEnumDeclaration(node) || ts.isInterfaceDeclaration(node) || ts.isPropertyDeclaration(node) || ts.isPropertySignature(node) || ts.isFunctionDeclaration(node) || ts.isParameter(node) || ts.isMethodDeclaration(node) || ts.isConstructorDeclaration(node)) {
return node.modifiers ? node.modifiers?.filter(ts.isModifier).map(this.mapModifier) : [];
} else if (ts.isVariableDeclarationList(node)) {
let modifier: string | undefined;
Expand Down Expand Up @@ -310,7 +291,7 @@ export class JavaScriptParserVisitor {
throw new Error(`Cannot get modifiers from ${node}`);
}

private mapModifier = (node: ts.Modifier) => {
private mapModifier = (node: ts.Modifier | ts.ModifierLike) => {
let kind: J.Modifier.Type;
switch (node.kind) {
case ts.SyntaxKind.PublicKeyword:
Expand Down Expand Up @@ -1648,7 +1629,7 @@ export class JavaScriptParserVisitor {
}

visitVariableStatement(node: ts.VariableStatement) {
return this.visitVariableDeclarationList(node.declarationList);
return this.visitVariableDeclarationList(node.declarationList).withModifiers(this.mapModifiers(node)).withPrefix(this.prefix(node));
}

visitExpressionStatement(node: ts.ExpressionStatement): J.Statement {
Expand Down Expand Up @@ -1833,8 +1814,10 @@ export class JavaScriptParserVisitor {
const kind = node.getFirstToken(this.sourceFile);
return new JS.ScopedVariableDeclarations(
randomId(),
this.prefix(node),
Space.EMPTY,
Markers.EMPTY,
[],
this.prefix(node),
kind?.kind === ts.SyntaxKind.LetKeyword ? JS.ScopedVariableDeclarations.Scope.Let :
kind?.kind === ts.SyntaxKind.ConstKeyword ? JS.ScopedVariableDeclarations.Scope.Const : JS.ScopedVariableDeclarations.Scope.Var,
node.declarations.map(declaration => {
Expand Down Expand Up @@ -1994,14 +1977,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 keywordType = namespaceKeyword ? JS.NamespaceDeclaration.KeywordType.Namespace : JS.NamespaceDeclaration.KeywordType.Module
namespaceKeyword ??= this.findChildNode(node, ts.SyntaxKind.ModuleKeyword);
if (body instanceof JS.NamespaceDeclaration) {
return new JS.NamespaceDeclaration(
randomId(),
Space.EMPTY,
Markers.EMPTY,
this.mapModifiers(node),
namespaceKeyword ? this.prefix(namespaceKeyword) : Space.EMPTY,
keywordType,
this.rightPadded(
new J.FieldAccess(
randomId(),
Expand All @@ -2026,6 +2012,7 @@ export class JavaScriptParserVisitor {
Markers.EMPTY,
this.mapModifiers(node),
namespaceKeyword ? this.prefix(namespaceKeyword) : Space.EMPTY,
keywordType,
this.rightPadded(this.convert(node.name), this.prefix(node)), // J.FieldAccess
body // J.Block
);
Expand Down
8 changes: 7 additions & 1 deletion openrewrite/src/javascript/remote/receiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
scopedVariableDeclarations = scopedVariableDeclarations.withId(ctx.receiveValue(scopedVariableDeclarations.id, ValueType.UUID)!);
scopedVariableDeclarations = scopedVariableDeclarations.withPrefix(ctx.receiveNode(scopedVariableDeclarations.prefix, receiveSpace)!);
scopedVariableDeclarations = scopedVariableDeclarations.withMarkers(ctx.receiveNode(scopedVariableDeclarations.markers, ctx.receiveMarkers)!);
scopedVariableDeclarations = scopedVariableDeclarations.withModifiers(ctx.receiveNodes(scopedVariableDeclarations.modifiers, ctx.receiveTree)!);
scopedVariableDeclarations = scopedVariableDeclarations.withScopePrefix(ctx.receiveNode(scopedVariableDeclarations.scopePrefix, receiveSpace)!);
scopedVariableDeclarations = scopedVariableDeclarations.withScope(ctx.receiveValue(scopedVariableDeclarations.scope, ValueType.Enum));
scopedVariableDeclarations = scopedVariableDeclarations.padding.withVariables(ctx.receiveNodes(scopedVariableDeclarations.padding.variables, receiveRightPaddedTree)!);
return scopedVariableDeclarations;
Expand Down Expand Up @@ -345,7 +347,8 @@ 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.withKeywordPrefix(ctx.receiveNode(namespaceDeclaration.keywordPrefix, receiveSpace)!);
namespaceDeclaration = namespaceDeclaration.withKeywordType(ctx.receiveValue(namespaceDeclaration.keywordType, ValueType.Enum)!);
namespaceDeclaration = namespaceDeclaration.padding.withName(ctx.receiveNode(namespaceDeclaration.padding.name, receiveRightPaddedTree)!);
namespaceDeclaration = namespaceDeclaration.withBody(ctx.receiveNode(namespaceDeclaration.body, ctx.receiveTree)!);
return namespaceDeclaration;
Expand Down Expand Up @@ -1192,6 +1195,8 @@ class Factory implements ReceiverFactory {
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.receiveValue(null, ValueType.Enum),
ctx.receiveNodes(null, receiveRightPaddedTree)!
);
Expand Down Expand Up @@ -1371,6 +1376,7 @@ class Factory implements ReceiverFactory {
ctx.receiveNode(null, ctx.receiveMarkers)!,
ctx.receiveNodes<Java.Modifier>(null, ctx.receiveTree)!,
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveValue(null, ValueType.Enum)!,
ctx.receiveNode<JRightPadded<Expression>>(null, receiveRightPaddedTree)!,
ctx.receiveNode<Java.Block>(null, ctx.receiveTree)!
);
Expand Down
5 changes: 4 additions & 1 deletion openrewrite/src/javascript/remote/sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ class Visitor extends JavaScriptVisitor<SenderContext> {
ctx.sendValue(scopedVariableDeclarations, v => v.id, ValueType.UUID);
ctx.sendNode(scopedVariableDeclarations, v => v.prefix, Visitor.sendSpace);
ctx.sendNode(scopedVariableDeclarations, v => v.markers, ctx.sendMarkers);
ctx.sendNodes(scopedVariableDeclarations, v => v.modifiers, ctx.sendTree, t => t.id);
ctx.sendNode(scopedVariableDeclarations, v => v.scopePrefix, Visitor.sendSpace);
ctx.sendValue(scopedVariableDeclarations, v => v.scope, ValueType.Enum);
ctx.sendNodes(scopedVariableDeclarations, v => v.padding.variables, Visitor.sendRightPadded(ValueType.Tree), t => t.element.id);
return scopedVariableDeclarations;
Expand Down Expand Up @@ -340,7 +342,8 @@ 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.keywordPrefix, Visitor.sendSpace);
ctx.sendValue(namespaceDeclaration, v => v.keywordType, ValueType.Enum);
ctx.sendNode(namespaceDeclaration, v => v.padding.name, Visitor.sendRightPadded(ValueType.Tree));
ctx.sendNode(namespaceDeclaration, v => v.body, ctx.sendTree);
return namespaceDeclaration;
Expand Down
3 changes: 2 additions & 1 deletion openrewrite/src/javascript/tree/support_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ export namespace JsSpace {
OBJECT_BINDING_DECLARATIONS_PREFIX,
PROPERTY_ASSIGNMENT_PREFIX,
SCOPED_VARIABLE_DECLARATIONS_PREFIX,
SCOPED_VARIABLE_DECLARATIONS_SCOPE_PREFIX,
TEMPLATE_EXPRESSION_PREFIX,
TEMPLATE_EXPRESSION_VALUE_AFTER,
TEMPLATE_EXPRESSION_VALUE_PREFIX,
Expand All @@ -217,7 +218,7 @@ export namespace JsSpace {
JSVARIABLE_DECLARATIONS_VARARGS,
JSVARIABLE_DECLARATIONS_JSNAMED_VARIABLE_PREFIX,
NAMESPACE_DECLARATION_PREFIX,
NAMESPACE_DECLARATION_NAMESPACE,
NAMESPACE_DECLARATION_KEYWORD_PREFIX,
JSMETHOD_DECLARATION_PREFIX
}
}
Expand Down
Loading

0 comments on commit 79ea7e0

Please sign in to comment.