Skip to content

Commit

Permalink
add more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegDokuka committed Nov 12, 2024
1 parent e1dc0a2 commit de95bdf
Show file tree
Hide file tree
Showing 14 changed files with 139 additions and 242 deletions.
38 changes: 11 additions & 27 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 @@ -1995,16 +1978,16 @@ export class JavaScriptParserVisitor {
const body = this.visit(node.body as ts.Node);

let namespaceKeyword = this.findChildNode(node, ts.SyntaxKind.NamespaceKeyword);
const kindType = namespaceKeyword ? JS.NamespaceDeclaration.Kind.Type.Namespace : JS.NamespaceDeclaration.Kind.Type.Module
const keywordType = namespaceKeyword ? JS.NamespaceDeclaration.KeywordType.Namespace : JS.NamespaceDeclaration.KeywordType.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),
kind,
namespaceKeyword ? this.prefix(namespaceKeyword) : Space.EMPTY,
keywordType,
this.rightPadded(
new J.FieldAccess(
randomId(),
Expand All @@ -2028,7 +2011,8 @@ export class JavaScriptParserVisitor {
node.parent.kind === ts.SyntaxKind.ModuleBlock ? this.prefix(node) : Space.EMPTY,
Markers.EMPTY,
this.mapModifiers(node),
kind,
namespaceKeyword ? this.prefix(namespaceKeyword) : Space.EMPTY,
keywordType,
this.rightPadded(this.convert(node.name), this.prefix(node)), // J.FieldAccess
body // J.Block
);
Expand Down
27 changes: 8 additions & 19 deletions 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,20 +347,13 @@ 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.withKind(ctx.receiveNode(namespaceDeclaration.kind, ctx.receiveTree)!);
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;
}

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 @@ -1200,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 @@ -1378,21 +1375,13 @@ class Factory implements ReceiverFactory {
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode(null, ctx.receiveMarkers)!,
ctx.receiveNodes<Java.Modifier>(null, ctx.receiveTree)!,
ctx.receiveNode<NamespaceDeclaration.Kind>(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)!
);
}

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
13 changes: 4 additions & 9 deletions 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,20 +342,13 @@ 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.kind, ctx.sendTree);
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;
}

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
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 de95bdf

Please sign in to comment.