Skip to content

Commit

Permalink
polish
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegDokuka committed Jan 14, 2025
1 parent 9981432 commit 4cd758f
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 111 deletions.
90 changes: 28 additions & 62 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3214,11 +3214,8 @@ export class JavaScriptParserVisitor {
}

visitImportEqualsDeclaration(node: ts.ImportEqualsDeclaration) {

const kind = this.findChildNode(node, ts.SyntaxKind.ImportKeyword)!;



return new JS.ScopedVariableDeclarations(
randomId(),
this.prefix(node),
Expand All @@ -3230,31 +3227,31 @@ export class JavaScriptParserVisitor {
),
[
this.rightPadded(new J.VariableDeclarations(
randomId(),
Space.EMPTY,
Markers.EMPTY,
[], // FIXME decorators?
node.isTypeOnly ? [new J.Modifier(
randomId(),
this.prefix(this.findChildNode(node, ts.SyntaxKind.TypeKeyword)!),
Markers.EMPTY,
"type",
J.Modifier.Type.LanguageExtension,
[]
)] : [],
null,
null, // FIXME varargs
[],
[this.rightPadded(new J.VariableDeclarations.NamedVariable(
randomId(),
Space.EMPTY,
Markers.EMPTY,
this.visit(node.name),
[],
this.leftPadded(this.suffix(node.name), this.visit(node.moduleReference)),
this.mapVariableType(node)
), Space.EMPTY)]
), Space.EMPTY)
randomId(),
Space.EMPTY,
Markers.EMPTY,
[],
node.isTypeOnly ? [new J.Modifier(
randomId(),
this.prefix(this.findChildNode(node, ts.SyntaxKind.TypeKeyword)!),
Markers.EMPTY,
"type",
J.Modifier.Type.LanguageExtension,
[]
)] : [],
null,
null,
[],
[this.rightPadded(new J.VariableDeclarations.NamedVariable(
randomId(),
Space.EMPTY,
Markers.EMPTY,
this.visit(node.name),
[],
this.leftPadded(this.suffix(node.name), this.visit(node.moduleReference)),
this.mapVariableType(node)
), Space.EMPTY)]
), Space.EMPTY)
]
)
}
Expand All @@ -3265,49 +3262,18 @@ export class JavaScriptParserVisitor {
}

visitImportDeclaration(node: ts.ImportDeclaration) {
// const children = node.getChildren(this.sourceFile);
// const _default = !!node.importClause?.name;
// const onlyDefault = _default && node.importClause.namedBindings == undefined;
// return new JS.JsImport(
// randomId(),
// this.prefix(node),
// Markers.EMPTY,
// _default ? this.rightPadded(this.visit(node.importClause?.name), this.suffix(node.importClause?.name)) : null,
// node.importClause?.isTypeOnly ? this.leftPadded(this.prefix(this.findChildNode(node.importClause, ts.SyntaxKind.TypeKeyword)!), node.importClause.isTypeOnly) : this.leftPadded(Space.EMPTY, false),
// node.importClause && !onlyDefault ? this.visit(node.importClause) : null,
// children[children.indexOf(node.moduleSpecifier) - 1].kind == ts.SyntaxKind.FromKeyword ? this.prefix(children[children.indexOf(node.moduleSpecifier) - 1]) : null,
// this.convert<J.Literal>(node.moduleSpecifier),
// null
// );

return new JS.JsImport(
randomId(),
this.prefix(node),
Markers.EMPTY,
this.mapModifiers(node),
node.importClause ? this.leftPadded(Space.EMPTY, this.visit(node.importClause)) : null,
node.importClause ? this.visit(node.importClause) : null,
this.leftPadded(node.importClause ? this.prefix(this.findChildNode(node, ts.SyntaxKind.FromKeyword)!) : Space.EMPTY, this.visit(node.moduleSpecifier)),
node.attributes ? this.visit(node.attributes) : null
);
}

visitImportClause(node: ts.ImportClause) {
// if (node.namedBindings && ts.isNamespaceImport(node.namedBindings)) {
// return new JContainer(
// this.prefix(node),
// [this.rightPadded(new JS.Alias(
// randomId(),
// Space.EMPTY,
// Markers.EMPTY,
// // this.rightPadded(node.isTypeOnly, node.isTypeOnly ? this.suffix(this.findChildNode(node, ts.SyntaxKind.TypeKeyword)!) : Space.EMPTY),
// this.rightPadded(this.mapIdentifier(node.namedBindings, "*"), this.prefix(node.namedBindings.getChildAt(1, this.sourceFile))),
// this.convert(node.namedBindings.name)
// ), Space.EMPTY)],
// Markers.EMPTY
// );
// }
// return this.mapCommaSeparatedList(node.namedBindings?.getChildren(this.sourceFile)!);

return new JS.JsImportClause(
randomId(),
this.prefix(node),
Expand Down Expand Up @@ -3588,13 +3554,13 @@ export class JavaScriptParserVisitor {

visitImportAttributes(node: ts.ImportAttributes) {
const openBraceIndex = node.getChildren().findIndex(n => n.kind === ts.SyntaxKind.OpenBraceToken);
const elements = this.mapCommaSeparatedList(node.getChildren(this.sourceFile).slice(openBraceIndex, openBraceIndex + 3));
const elements = this.mapCommaSeparatedList<JS.ImportAttribute>(node.getChildren(this.sourceFile).slice(openBraceIndex, openBraceIndex + 3));
return new JS.ImportAttributes(
randomId(),
this.prefix(node),
Markers.EMPTY,
ts.SyntaxKind.WithKeyword === node.token ? JS.ImportAttributes.Token.With : JS.ImportAttributes.Token.Assert,
elements as any
elements
);
}

Expand Down
4 changes: 2 additions & 2 deletions openrewrite/src/javascript/remote/receiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
jsImport = jsImport.withPrefix(ctx.receiveNode(jsImport.prefix, receiveSpace)!);
jsImport = jsImport.withMarkers(ctx.receiveNode(jsImport.markers, ctx.receiveMarkers)!);
jsImport = jsImport.withModifiers(ctx.receiveNodes(jsImport.modifiers, ctx.receiveTree)!);
jsImport = jsImport.padding.withImportClause(ctx.receiveNode(jsImport.padding.importClause, receiveLeftPaddedTree));
jsImport = jsImport.withImportClause(ctx.receiveNode(jsImport.importClause, ctx.receiveTree));
jsImport = jsImport.padding.withModuleSpecifier(ctx.receiveNode(jsImport.padding.moduleSpecifier, receiveLeftPaddedTree)!);
jsImport = jsImport.withAttributes(ctx.receiveNode(jsImport.attributes, ctx.receiveTree));
return jsImport;
Expand Down Expand Up @@ -1555,7 +1555,7 @@ class Factory implements ReceiverFactory {
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode(null, ctx.receiveMarkers)!,
ctx.receiveNodes<Java.Modifier>(null, ctx.receiveTree)!,
ctx.receiveNode<JLeftPadded<JsImportClause>>(null, receiveLeftPaddedTree),
ctx.receiveNode<JsImportClause>(null, ctx.receiveTree),
ctx.receiveNode<JLeftPadded<Expression>>(null, receiveLeftPaddedTree)!,
ctx.receiveNode<ImportAttributes>(null, ctx.receiveTree)
);
Expand Down
2 changes: 1 addition & 1 deletion openrewrite/src/javascript/remote/sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class Visitor extends JavaScriptVisitor<SenderContext> {
ctx.sendNode(jsImport, v => v.prefix, Visitor.sendSpace);
ctx.sendNode(jsImport, v => v.markers, ctx.sendMarkers);
ctx.sendNodes(jsImport, v => v.modifiers, ctx.sendTree, t => t.id);
ctx.sendNode(jsImport, v => v.padding.importClause, Visitor.sendLeftPadded(ValueType.Tree));
ctx.sendNode(jsImport, v => v.importClause, ctx.sendTree);
ctx.sendNode(jsImport, v => v.padding.moduleSpecifier, Visitor.sendLeftPadded(ValueType.Tree));
ctx.sendNode(jsImport, v => v.attributes, ctx.sendTree);
return jsImport;
Expand Down
14 changes: 4 additions & 10 deletions openrewrite/src/javascript/tree/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1317,7 +1317,7 @@ export class ImportType extends JSMixin(Object) implements Expression, TypeTree

@LstType("org.openrewrite.javascript.tree.JS$JsImport")
export class JsImport extends JSMixin(Object) implements Statement {
public constructor(id: UUID, prefix: Space, markers: Markers, modifiers: Java.Modifier[], importClause: JLeftPadded<JsImportClause> | null, moduleSpecifier: JLeftPadded<Expression>, attributes: ImportAttributes | null) {
public constructor(id: UUID, prefix: Space, markers: Markers, modifiers: Java.Modifier[], importClause: JsImportClause | null, moduleSpecifier: JLeftPadded<Expression>, attributes: ImportAttributes | null) {
super();
this._id = id;
this._prefix = prefix;
Expand Down Expand Up @@ -1368,14 +1368,14 @@ export class JsImport extends JSMixin(Object) implements Statement {
return modifiers === this._modifiers ? this : new JsImport(this._id, this._prefix, this._markers, modifiers, this._importClause, this._moduleSpecifier, this._attributes);
}

private readonly _importClause: JLeftPadded<JsImportClause> | null;
private readonly _importClause: JsImportClause | null;

public get importClause(): JsImportClause | null {
return this._importClause === null ? null : this._importClause.element;
return this._importClause;
}

public withImportClause(importClause: JsImportClause | null): JsImport {
return this.padding.withImportClause(JLeftPadded.withElement(this._importClause, importClause));
return importClause === this._importClause ? this : new JsImport(this._id, this._prefix, this._markers, this._modifiers, importClause, this._moduleSpecifier, this._attributes);
}

private readonly _moduleSpecifier: JLeftPadded<Expression>;
Expand Down Expand Up @@ -1405,12 +1405,6 @@ export class JsImport extends JSMixin(Object) implements Statement {
get padding() {
const t = this;
return new class {
public get importClause(): JLeftPadded<JsImportClause> | null {
return t._importClause;
}
public withImportClause(importClause: JLeftPadded<JsImportClause> | null): JsImport {
return t._importClause === importClause ? t : new JsImport(t._id, t._prefix, t._markers, t._modifiers, importClause, t._moduleSpecifier, t._attributes);
}
public get moduleSpecifier(): JLeftPadded<Expression> {
return t._moduleSpecifier;
}
Expand Down
2 changes: 1 addition & 1 deletion openrewrite/src/javascript/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ export class JavaScriptVisitor<P> extends JavaVisitor<P> {
jsImport = tempStatement as JsImport;
jsImport = jsImport.withMarkers(this.visitMarkers(jsImport.markers, p));
jsImport = jsImport.withModifiers(ListUtils.map(jsImport.modifiers, el => this.visitAndCast(el, p)));
jsImport = jsImport.padding.withImportClause(this.visitJsLeftPadded(jsImport.padding.importClause, JsLeftPadded.Location.JS_IMPORT_IMPORT_CLAUSE, p));
jsImport = jsImport.withImportClause(this.visitAndCast(jsImport.importClause, p));
jsImport = jsImport.padding.withModuleSpecifier(this.visitJsLeftPadded(jsImport.padding.moduleSpecifier, JsLeftPadded.Location.JS_IMPORT_MODULE_SPECIFIER, p)!);
jsImport = jsImport.withAttributes(this.visitAndCast(jsImport.attributes, p));
return jsImport;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public JS.JsImport visitJsImport(JS.JsImport jsImport, ReceiverContext ctx) {
jsImport = jsImport.withPrefix(ctx.receiveNonNullNode(jsImport.getPrefix(), JavaScriptReceiver::receiveSpace));
jsImport = jsImport.withMarkers(ctx.receiveNonNullNode(jsImport.getMarkers(), ctx::receiveMarkers));
jsImport = jsImport.withModifiers(ctx.receiveNonNullNodes(jsImport.getModifiers(), ctx::receiveTree));
jsImport = jsImport.getPadding().withImportClause(ctx.receiveNode(jsImport.getPadding().getImportClause(), JavaScriptReceiver::receiveLeftPaddedTree));
jsImport = jsImport.withImportClause(ctx.receiveNode(jsImport.getImportClause(), ctx::receiveTree));
jsImport = jsImport.getPadding().withModuleSpecifier(ctx.receiveNonNullNode(jsImport.getPadding().getModuleSpecifier(), JavaScriptReceiver::receiveLeftPaddedTree));
jsImport = jsImport.withAttributes(ctx.receiveNode(jsImport.getAttributes(), ctx::receiveTree));
return jsImport;
Expand Down Expand Up @@ -1857,7 +1857,7 @@ private static JS.JsImport createJSJsImport(ReceiverContext ctx) {
ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace),
ctx.receiveNonNullNode(null, ctx::receiveMarkers),
ctx.receiveNonNullNodes(null, ctx::receiveTree),
ctx.receiveNode(null, JavaScriptReceiver::receiveLeftPaddedTree),
ctx.receiveNode(null, ctx::receiveTree),
ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveLeftPaddedTree),
ctx.receiveNode(null, ctx::receiveTree)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public JS.JsImport visitJsImport(JS.JsImport jsImport, SenderContext ctx) {
ctx.sendNode(jsImport, JS.JsImport::getPrefix, JavaScriptSender::sendSpace);
ctx.sendNode(jsImport, JS.JsImport::getMarkers, ctx::sendMarkers);
ctx.sendNodes(jsImport, JS.JsImport::getModifiers, ctx::sendTree, Tree::getId);
ctx.sendNode(jsImport, e -> e.getPadding().getImportClause(), JavaScriptSender::sendLeftPadded);
ctx.sendNode(jsImport, JS.JsImport::getImportClause, ctx::sendTree);
ctx.sendNode(jsImport, e -> e.getPadding().getModuleSpecifier(), JavaScriptSender::sendLeftPadded);
ctx.sendNode(jsImport, JS.JsImport::getAttributes, ctx::sendTree);
return jsImport;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ public J visitJsImport(JS.JsImport jsImport, P p) {
i = (JS.JsImport) temp;
}
i = i.withModifiers(Objects.requireNonNull(ListUtils.map(i.getModifiers(), e -> visitAndCast(e, p))));
i = i.getPadding().withImportClause(visitLeftPadded(i.getPadding().getImportClause(), JsLeftPadded.Location.JS_IMPORT_IMPORT_CLAUSE, p));
i = i.withImportClause(visitAndCast(i.getImportClause(), p));
i = i.getPadding().withModuleSpecifier(Objects.requireNonNull(visitLeftPadded(i.getPadding().getModuleSpecifier(), JsLeftPadded.Location.JS_IMPORT_MODULE_SPECIFIER, p)));
i = i.withAttributes(visitAndCast(i.getAttributes(), p));
return i;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,9 @@ public J visitJsImport(JS.JsImport jsImport, PrintOutputCapture<P> p) {

p.append("import");

JLeftPadded<JS.JsImportClause> importClause = jsImport.getPadding().getImportClause();
visitLeftPadded(importClause, JsLeftPadded.Location.JS_IMPORT_IMPORT_CLAUSE, p);
visit(jsImport.getImportClause(), p);

visitLeftPadded(importClause == null ? "" : "from", jsImport.getPadding().getModuleSpecifier(), JsLeftPadded.Location.JS_IMPORT_MODULE_SPECIFIER, p);
visitLeftPadded(jsImport.getImportClause() == null ? "" : "from", jsImport.getPadding().getModuleSpecifier(), JsLeftPadded.Location.JS_IMPORT_MODULE_SPECIFIER, p);

visit(jsImport.getAttributes(), p);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1307,17 +1307,10 @@ class JsImport implements JS, Statement {
@Getter
List<J.Modifier> modifiers;

@With
@Getter
@Nullable
JLeftPadded<JsImportClause> importClause;

@Nullable
public JsImportClause getImportClause() {
return importClause != null ? importClause.getElement() : null;
}

public JS.JsImport withImportClause(JsImportClause importClause) {
return getPadding().withImportClause(JLeftPadded.withElement(this.importClause, importClause));
}
JsImportClause importClause;

JLeftPadded<Expression> moduleSpecifier;

Expand Down Expand Up @@ -1370,14 +1363,6 @@ public JLeftPadded<Expression> getModuleSpecifier() {
public JsImport withModuleSpecifier(JLeftPadded<Expression> moduleSpecifier) {
return t.moduleSpecifier == moduleSpecifier ? t : new JsImport(t.id, t.prefix, t.markers, t.modifiers, t.importClause, moduleSpecifier, t.attributes);
}

public @Nullable JLeftPadded<JsImportClause> getImportClause() {
return t.importClause;
}

public JsImport withImportClause(@Nullable JLeftPadded<JsImportClause> importClause) {
return t.importClause == importClause ? t : new JsImport(t.id, t.prefix, t.markers, t.modifiers, importClause, t.moduleSpecifier, t.attributes);
}
}
}

Expand Down Expand Up @@ -1455,14 +1440,6 @@ public static class Padding {
public JsImportClause withName(@Nullable JRightPadded<J.Identifier> name) {
return t.name == name ? t : new JsImportClause(t.id, t.prefix, t.markers, t.typeOnly, name, t.namedBindings);
}
//
// public JRightPadded<Boolean> getTypeOnly() {
// return t.typeOnly;
// }
//
// public JsImportClause withTypeOnly(JRightPadded<Boolean> typeOnly) {
// return t.typeOnly == typeOnly ? t : new JsImportClause(t.id, t.prefix, t.markers, typeOnly, t.name, t.namedBindings);
// }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public enum Location {
JS_YIELD_DELEGATED(JsSpace.Location.JS_YIELD_DELEGATED_PREFIX),
FUNCTION_TYPE_CONSTRUCTOR(JsSpace.Location.FUNCTION_TYPE_CONSTRUCTOR_PREFIX),
JSTRY_FINALLY(JsSpace.Location.JSTRY_FINALLY_PREFIX),
JS_IMPORT_IMPORT_CLAUSE(JsSpace.Location.JS_IMPORT_IMPORT_CLAUSE_PREFIX),
JS_IMPORT_MODULE_SPECIFIER(JsSpace.Location.JS_IMPORT_MODULE_SPECIFIER_PREFIX),
JS_IMPORT_ATTRIBUTE_VALUE(JsSpace.Location.JS_IMPORT_ATTRIBUTE_VALUE_PREFIX),
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ public enum Location {
JSCATCH_PREFIX,
WITH_PREFIX,
WITH_BODY_SUFFIX,
JS_IMPORT_IMPORT_CLAUSE_PREFIX,
JS_IMPORT_MODULE_SPECIFIER_PREFIX,
JS_IMPORT_CLAUSE_PREFIX,
JS_IMPORT_CLAUSE_NAME_SUFFIX,
Expand Down

0 comments on commit 4cd758f

Please sign in to comment.