From 9981432fd1bfb88cdbc3660d47e14e3116159fc8 Mon Sep 17 00:00:00 2001 From: Oleh Dokuka Date: Tue, 14 Jan 2025 14:13:30 +0200 Subject: [PATCH] rework jsimport to reflect AST structure and fix remaining gaps --- openrewrite/src/java/remote/receiver.ts | 19 +- openrewrite/src/java/remote/sender.ts | 10 +- openrewrite/src/java/tree/tree.ts | 64 --- openrewrite/src/java/visitor.ts | 20 +- openrewrite/src/javascript/parser.ts | 149 +++++-- openrewrite/src/javascript/remote/receiver.ts | 115 +++-- openrewrite/src/javascript/remote/sender.ts | 57 ++- .../src/javascript/tree/support_types.ts | 10 + openrewrite/src/javascript/tree/tree.ts | 421 +++++++++++++++--- openrewrite/src/javascript/visitor.ts | 48 +- .../test/javascript/parser/import.test.ts | 16 +- .../javascript/remote/JavaScriptReceiver.java | 123 +++-- .../javascript/remote/JavaScriptSender.java | 60 ++- .../remote/JavaScriptValidator.java | 39 +- .../javascript/JavaScriptVisitor.java | 58 ++- .../internal/JavaScriptPrinter.java | 78 +++- .../org/openrewrite/javascript/tree/JS.java | 371 ++++++++++++--- .../javascript/tree/JsContainer.java | 5 +- .../javascript/tree/JsLeftPadded.java | 8 +- .../javascript/tree/JsRightPadded.java | 6 +- .../openrewrite/javascript/tree/JsSpace.java | 16 +- 21 files changed, 1300 insertions(+), 393 deletions(-) diff --git a/openrewrite/src/java/remote/receiver.ts b/openrewrite/src/java/remote/receiver.ts index 5cd54809..132b5019 100644 --- a/openrewrite/src/java/remote/receiver.ts +++ b/openrewrite/src/java/remote/receiver.ts @@ -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 {JavaVisitor} from '..'; -import {J, Comment, Expression, JavaSourceFile, JavaType, JContainer, JLeftPadded, JRightPadded, Loop, MethodCall, NameTree, Space, Statement, TextComment, TypedTree, TypeTree, AnnotatedType, Annotation, ArrayAccess, ArrayType, Assert, Assignment, AssignmentOperation, Binary, Block, Break, Case, ClassDeclaration, CompilationUnit, Continue, DoWhileLoop, Empty, EnumValue, EnumValueSet, FieldAccess, ForEachLoop, ForLoop, ParenthesizedTypeTree, Identifier, If, Import, InstanceOf, IntersectionType, Label, Lambda, Literal, MemberReference, MethodDeclaration, MethodInvocation, Modifier, MultiCatch, NewArray, ArrayDimension, NewClass, NullableType, Package, ParameterizedType, Parentheses, ControlParentheses, Primitive, Return, Switch, SwitchExpression, Synchronized, Ternary, Throw, Try, TypeCast, TypeParameter, TypeParameters, Unary, VariableDeclarations, WhileLoop, Wildcard, Yield, Unknown, Erroneous} from '../tree'; +import {J, Comment, Expression, JavaSourceFile, JavaType, JContainer, JLeftPadded, JRightPadded, Loop, MethodCall, NameTree, Space, Statement, TextComment, TypedTree, TypeTree, AnnotatedType, Annotation, ArrayAccess, ArrayType, Assert, Assignment, AssignmentOperation, Binary, Block, Break, Case, ClassDeclaration, CompilationUnit, Continue, DoWhileLoop, Empty, EnumValue, EnumValueSet, FieldAccess, ForEachLoop, ForLoop, ParenthesizedTypeTree, Identifier, If, Import, InstanceOf, IntersectionType, Label, Lambda, Literal, MemberReference, MethodDeclaration, MethodInvocation, Modifier, MultiCatch, NewArray, ArrayDimension, NewClass, NullableType, Package, ParameterizedType, Parentheses, ControlParentheses, Primitive, Return, Switch, SwitchExpression, Synchronized, Ternary, Throw, Try, TypeCast, TypeParameter, TypeParameters, Unary, VariableDeclarations, WhileLoop, Wildcard, Yield, Unknown} from '../tree'; import * as Java from "../../java/tree"; export class JavaReceiver implements Receiver { @@ -708,14 +708,6 @@ class Visitor extends JavaVisitor { return source; } - public visitErroneous(erroneous: Erroneous, ctx: ReceiverContext): J { - erroneous = erroneous.withId(ctx.receiveValue(erroneous.id, ValueType.UUID)!); - erroneous = erroneous.withPrefix(ctx.receiveNode(erroneous.prefix, receiveSpace)!); - erroneous = erroneous.withMarkers(ctx.receiveNode(erroneous.markers, ctx.receiveMarkers)!); - erroneous = erroneous.withText(ctx.receiveValue(erroneous.text, ValueType.Primitive)!); - return erroneous; - } - } class Factory implements ReceiverFactory { @@ -1471,15 +1463,6 @@ class Factory implements ReceiverFactory { ); } - if (type === "org.openrewrite.java.tree.J$Erroneous") { - return new Erroneous( - ctx.receiveValue(null, ValueType.UUID)!, - ctx.receiveNode(null, receiveSpace)!, - ctx.receiveNode(null, ctx.receiveMarkers)!, - ctx.receiveValue(null, ValueType.Primitive)! - ); - } - throw new Error("No factory method for type: " + type); } } diff --git a/openrewrite/src/java/remote/sender.ts b/openrewrite/src/java/remote/sender.ts index f84cfd2c..a8880dbf 100644 --- a/openrewrite/src/java/remote/sender.ts +++ b/openrewrite/src/java/remote/sender.ts @@ -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 {JavaVisitor} from '..'; -import {J, Comment, Expression, JavaSourceFile, JavaType, JContainer, JLeftPadded, JRightPadded, Loop, MethodCall, NameTree, Space, Statement, TextComment, TypedTree, TypeTree, AnnotatedType, Annotation, ArrayAccess, ArrayType, Assert, Assignment, AssignmentOperation, Binary, Block, Break, Case, ClassDeclaration, CompilationUnit, Continue, DoWhileLoop, Empty, EnumValue, EnumValueSet, FieldAccess, ForEachLoop, ForLoop, ParenthesizedTypeTree, Identifier, If, Import, InstanceOf, IntersectionType, Label, Lambda, Literal, MemberReference, MethodDeclaration, MethodInvocation, Modifier, MultiCatch, NewArray, ArrayDimension, NewClass, NullableType, Package, ParameterizedType, Parentheses, ControlParentheses, Primitive, Return, Switch, SwitchExpression, Synchronized, Ternary, Throw, Try, TypeCast, TypeParameter, TypeParameters, Unary, VariableDeclarations, WhileLoop, Wildcard, Yield, Unknown, Erroneous} from '../tree'; +import {J, Comment, Expression, JavaSourceFile, JavaType, JContainer, JLeftPadded, JRightPadded, Loop, MethodCall, NameTree, Space, Statement, TextComment, TypedTree, TypeTree, AnnotatedType, Annotation, ArrayAccess, ArrayType, Assert, Assignment, AssignmentOperation, Binary, Block, Break, Case, ClassDeclaration, CompilationUnit, Continue, DoWhileLoop, Empty, EnumValue, EnumValueSet, FieldAccess, ForEachLoop, ForLoop, ParenthesizedTypeTree, Identifier, If, Import, InstanceOf, IntersectionType, Label, Lambda, Literal, MemberReference, MethodDeclaration, MethodInvocation, Modifier, MultiCatch, NewArray, ArrayDimension, NewClass, NullableType, Package, ParameterizedType, Parentheses, ControlParentheses, Primitive, Return, Switch, SwitchExpression, Synchronized, Ternary, Throw, Try, TypeCast, TypeParameter, TypeParameters, Unary, VariableDeclarations, WhileLoop, Wildcard, Yield, Unknown} from '../tree'; import * as Java from "../../java/tree"; export class JavaSender implements Sender { @@ -708,14 +708,6 @@ class Visitor extends JavaVisitor { return source; } - public visitErroneous(erroneous: Erroneous, ctx: SenderContext): J { - ctx.sendValue(erroneous, v => v.id, ValueType.UUID); - ctx.sendNode(erroneous, v => v.prefix, Visitor.sendSpace); - ctx.sendNode(erroneous, v => v.markers, ctx.sendMarkers); - ctx.sendValue(erroneous, v => v.text, ValueType.Primitive); - return erroneous; - } - private static sendContainer(type: ValueType): (container: JContainer, ctx: SenderContext) => void { return extensions.sendContainer(type); } diff --git a/openrewrite/src/java/tree/tree.ts b/openrewrite/src/java/tree/tree.ts index 32d998aa..37d910cf 100644 --- a/openrewrite/src/java/tree/tree.ts +++ b/openrewrite/src/java/tree/tree.ts @@ -6320,67 +6320,3 @@ export namespace Unknown { } } - -@LstType("org.openrewrite.java.tree.J$Erroneous") -export class Erroneous extends JMixin(Object) implements Statement, Expression { - public constructor(id: UUID, prefix: Space, markers: Markers, text: string) { - super(); - this._id = id; - this._prefix = prefix; - this._markers = markers; - this._text = text; - } - - private readonly _id: UUID; - - public get id(): UUID { - return this._id; - } - - public withId(id: UUID): Erroneous { - return id === this._id ? this : new Erroneous(id, this._prefix, this._markers, this._text); - } - - private readonly _prefix: Space; - - public get prefix(): Space { - return this._prefix; - } - - public withPrefix(prefix: Space): Erroneous { - return prefix === this._prefix ? this : new Erroneous(this._id, prefix, this._markers, this._text); - } - - private readonly _markers: Markers; - - public get markers(): Markers { - return this._markers; - } - - public withMarkers(markers: Markers): Erroneous { - return markers === this._markers ? this : new Erroneous(this._id, this._prefix, markers, this._text); - } - - private readonly _text: string; - - public get text(): string { - return this._text; - } - - public withText(text: string): Erroneous { - return text === this._text ? this : new Erroneous(this._id, this._prefix, this._markers, text); - } - - public acceptJava

(v: JavaVisitor

, p: P): J | null { - return v.visitErroneous(this, p); - } - - public get type(): JavaType | null { - return extensions.getJavaType(this); - } - - public withType(type: JavaType): Erroneous { - return extensions.withJavaType(this, type); - } - -} diff --git a/openrewrite/src/java/visitor.ts b/openrewrite/src/java/visitor.ts index 6aea08a2..39c9e5a8 100644 --- a/openrewrite/src/java/visitor.ts +++ b/openrewrite/src/java/visitor.ts @@ -1,7 +1,7 @@ import * as extensions from "./extensions"; import {ListUtils, SourceFile, Tree, TreeVisitor} from "../core"; import {J, isJava, Comment, Expression, JavaSourceFile, JavaType, JContainer, JLeftPadded, JRightPadded, Loop, MethodCall, NameTree, Space, Statement, TextComment, TypedTree, TypeTree} from "./tree"; -import {AnnotatedType, Annotation, ArrayAccess, ArrayType, Assert, Assignment, AssignmentOperation, Binary, Block, Break, Case, ClassDeclaration, CompilationUnit, Continue, DoWhileLoop, Empty, EnumValue, EnumValueSet, FieldAccess, ForEachLoop, ForLoop, ParenthesizedTypeTree, Identifier, If, Import, InstanceOf, IntersectionType, Label, Lambda, Literal, MemberReference, MethodDeclaration, MethodInvocation, Modifier, MultiCatch, NewArray, ArrayDimension, NewClass, NullableType, Package, ParameterizedType, Parentheses, ControlParentheses, Primitive, Return, Switch, SwitchExpression, Synchronized, Ternary, Throw, Try, TypeCast, TypeParameter, TypeParameters, Unary, VariableDeclarations, WhileLoop, Wildcard, Yield, Unknown, Erroneous} from "./tree"; +import {AnnotatedType, Annotation, ArrayAccess, ArrayType, Assert, Assignment, AssignmentOperation, Binary, Block, Break, Case, ClassDeclaration, CompilationUnit, Continue, DoWhileLoop, Empty, EnumValue, EnumValueSet, FieldAccess, ForEachLoop, ForLoop, ParenthesizedTypeTree, Identifier, If, Import, InstanceOf, IntersectionType, Label, Lambda, Literal, MemberReference, MethodDeclaration, MethodInvocation, Modifier, MultiCatch, NewArray, ArrayDimension, NewClass, NullableType, Package, ParameterizedType, Parentheses, ControlParentheses, Primitive, Return, Switch, SwitchExpression, Synchronized, Ternary, Throw, Try, TypeCast, TypeParameter, TypeParameters, Unary, VariableDeclarations, WhileLoop, Wildcard, Yield, Unknown} from "./tree"; export class JavaVisitor

extends TreeVisitor { isAcceptable(sourceFile: SourceFile, p: P): boolean { @@ -965,24 +965,6 @@ export class JavaVisitor

extends TreeVisitor { return source; } - public visitErroneous(erroneous: Erroneous, p: P): J | null { - erroneous = erroneous.withPrefix(this.visitSpace(erroneous.prefix, Space.Location.ERRONEOUS_PREFIX, p)!); - let tempStatement = this.visitStatement(erroneous, p) as Statement; - if (!(tempStatement instanceof Erroneous)) - { - return tempStatement; - } - erroneous = tempStatement as Erroneous; - let tempExpression = this.visitExpression(erroneous, p) as Expression; - if (!(tempExpression instanceof Erroneous)) - { - return tempExpression; - } - erroneous = tempExpression as Erroneous; - erroneous = erroneous.withMarkers(this.visitMarkers(erroneous.markers, p)); - return erroneous; - } - public visitContainer(container: JContainer | null, loc: JContainer.Location, p: P): JContainer | null { return extensions.visitContainer(this, container, loc, p); } diff --git a/openrewrite/src/javascript/parser.ts b/openrewrite/src/javascript/parser.ts index 20f7e9a4..b36cee57 100644 --- a/openrewrite/src/javascript/parser.ts +++ b/openrewrite/src/javascript/parser.ts @@ -293,12 +293,12 @@ export class JavaScriptParserVisitor { | ts.FunctionDeclaration | ts.ParameterDeclaration | ts.MethodDeclaration | ts.EnumDeclaration | ts.InterfaceDeclaration | ts.PropertySignature | ts.ConstructorDeclaration | ts.ModuleDeclaration | ts.GetAccessorDeclaration | ts.SetAccessorDeclaration | ts.ArrowFunction | ts.IndexSignatureDeclaration | ts.TypeAliasDeclaration | ts.ExportDeclaration | ts.ExportAssignment | ts.FunctionExpression - | ts.ConstructorTypeNode | ts.TypeParameterDeclaration) { + | ts.ConstructorTypeNode | ts.TypeParameterDeclaration | ts.ImportDeclaration | ts.ImportEqualsDeclaration) { if (ts.isVariableStatement(node) || ts.isModuleDeclaration(node) || ts.isClassDeclaration(node) || ts.isEnumDeclaration(node) || ts.isInterfaceDeclaration(node) || ts.isPropertyDeclaration(node) || ts.isPropertySignature(node) || ts.isParameter(node) || ts.isMethodDeclaration(node) || ts.isConstructorDeclaration(node) || ts.isArrowFunction(node) || ts.isIndexSignatureDeclaration(node) || ts.isTypeAliasDeclaration(node) || ts.isExportDeclaration(node) - || ts.isFunctionDeclaration(node) || ts.isFunctionExpression(node) || ts.isConstructorTypeNode(node) || ts.isTypeParameterDeclaration(node)) { + || ts.isFunctionDeclaration(node) || ts.isFunctionExpression(node) || ts.isConstructorTypeNode(node) || ts.isTypeParameterDeclaration(node) || ts.isImportDeclaration(node) || ts.isImportEqualsDeclaration(node)) { return node.modifiers ? node.modifiers?.filter(ts.isModifier).map(this.mapModifier) : []; } else if (ts.isExportAssignment(node)) { @@ -3214,7 +3214,49 @@ export class JavaScriptParserVisitor { } visitImportEqualsDeclaration(node: ts.ImportEqualsDeclaration) { - return this.visitUnknown(node); + + const kind = this.findChildNode(node, ts.SyntaxKind.ImportKeyword)!; + + + + return new JS.ScopedVariableDeclarations( + randomId(), + this.prefix(node), + Markers.EMPTY, + this.mapModifiers(node), + this.leftPadded( + this.prefix(kind), + JS.ScopedVariableDeclarations.Scope.Import + ), + [ + 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) + ] + ) } visitImportKeyword(node: ts.ImportExpression) { @@ -3223,46 +3265,77 @@ 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; + // 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(node.moduleSpecifier), + // null + // ); + 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(node.moduleSpecifier), - null + this.mapModifiers(node), + node.importClause ? this.leftPadded(Space.EMPTY, 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)!); + // 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), + Markers.EMPTY, + node.isTypeOnly, + node.name ? this.rightPadded(this.visit(node.name), this.suffix(node.name)) : null, + node.namedBindings ? this.visit(node.namedBindings) : null + ); } visitNamespaceImport(node: ts.NamespaceImport) { - return this.visitUnknown(node); + return new JS.Alias( + randomId(), + this.prefix(node), + Markers.EMPTY, + this.rightPadded(this.mapIdentifier(node, "*"), this.prefix(this.findChildNode(node, ts.SyntaxKind.AsKeyword)!)), + this.visit(node.name) + ); } visitNamedImports(node: ts.NamedImports) { - return this.visitUnknown(node); + return new JS.NamedImports( + randomId(), + this.prefix(node), + Markers.EMPTY, + this.mapCommaSeparatedList(node.getChildren(this.sourceFile)), + null + ); } visitImportSpecifier(node: ts.ImportSpecifier) { @@ -3514,11 +3587,25 @@ export class JavaScriptParserVisitor { } visitImportAttributes(node: ts.ImportAttributes) { - return this.visitUnknown(node); + const openBraceIndex = node.getChildren().findIndex(n => n.kind === ts.SyntaxKind.OpenBraceToken); + const elements = this.mapCommaSeparatedList(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 + ); } visitImportAttribute(node: ts.ImportAttribute) { - return this.visitUnknown(node); + return new JS.ImportAttribute( + randomId(), + this.prefix(node), + Markers.EMPTY, + this.visit(node.name), + this.leftPadded(this.suffix(node.name), this.visit(node.value)) + ); } visitPropertyAssignment(node: ts.PropertyAssignment) { diff --git a/openrewrite/src/javascript/remote/receiver.ts b/openrewrite/src/javascript/remote/receiver.ts index 8713d1c2..210531bd 100644 --- a/openrewrite/src/javascript/remote/receiver.ts +++ b/openrewrite/src/javascript/remote/receiver.ts @@ -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, ConditionalType, DefaultType, Delete, Export, ExpressionStatement, TrailingTokenStatement, ExpressionWithTypeArguments, FunctionType, InferType, ImportType, JsImport, JsImportSpecifier, JsBinary, LiteralType, MappedType, ObjectBindingDeclarations, PropertyAssignment, SatisfiesExpression, ScopedVariableDeclarations, StatementExpression, WithStatement, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, TypePredicate, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, JSTry, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement, ExportDeclaration, ExportAssignment, NamedExports, ExportSpecifier, IndexedAccessType, JsAssignmentOperation, TypeTreeExpression} from '../tree'; +import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, ConditionalType, DefaultType, Delete, Export, ExpressionStatement, TrailingTokenStatement, ExpressionWithTypeArguments, FunctionType, InferType, ImportType, JsImport, JsImportClause, NamedImports, JsImportSpecifier, ImportAttributes, ImportAttribute, JsBinary, LiteralType, MappedType, ObjectBindingDeclarations, PropertyAssignment, SatisfiesExpression, ScopedVariableDeclarations, StatementExpression, WithStatement, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, TypePredicate, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, JSTry, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement, ExportDeclaration, ExportAssignment, NamedExports, ExportSpecifier, IndexedAccessType, JsAssignmentOperation, TypeTreeExpression} from '../tree'; import {Expression, J, JContainer, JLeftPadded, JRightPadded, NameTree, Space, Statement, TypeTree, TypedTree} from "../../java"; import * as Java from "../../java/tree"; @@ -179,15 +179,32 @@ class Visitor extends JavaScriptVisitor { jsImport = jsImport.withId(ctx.receiveValue(jsImport.id, ValueType.UUID)!); jsImport = jsImport.withPrefix(ctx.receiveNode(jsImport.prefix, receiveSpace)!); jsImport = jsImport.withMarkers(ctx.receiveNode(jsImport.markers, ctx.receiveMarkers)!); - jsImport = jsImport.padding.withName(ctx.receiveNode(jsImport.padding.name, receiveRightPaddedTree)); - jsImport = jsImport.padding.withImportType(ctx.receiveNode(jsImport.padding.importType, leftPaddedValueReceiver(ValueType.Primitive))!); - jsImport = jsImport.padding.withImports(ctx.receiveNode(jsImport.padding.imports, receiveContainer)); - jsImport = jsImport.withFrom(ctx.receiveNode(jsImport.from, receiveSpace)); - jsImport = jsImport.withTarget(ctx.receiveNode(jsImport.target, ctx.receiveTree)); - jsImport = jsImport.padding.withInitializer(ctx.receiveNode(jsImport.padding.initializer, receiveLeftPaddedTree)); + jsImport = jsImport.withModifiers(ctx.receiveNodes(jsImport.modifiers, ctx.receiveTree)!); + jsImport = jsImport.padding.withImportClause(ctx.receiveNode(jsImport.padding.importClause, receiveLeftPaddedTree)); + jsImport = jsImport.padding.withModuleSpecifier(ctx.receiveNode(jsImport.padding.moduleSpecifier, receiveLeftPaddedTree)!); + jsImport = jsImport.withAttributes(ctx.receiveNode(jsImport.attributes, ctx.receiveTree)); return jsImport; } + public visitJsImportClause(jsImportClause: JsImportClause, ctx: ReceiverContext): J { + jsImportClause = jsImportClause.withId(ctx.receiveValue(jsImportClause.id, ValueType.UUID)!); + jsImportClause = jsImportClause.withPrefix(ctx.receiveNode(jsImportClause.prefix, receiveSpace)!); + jsImportClause = jsImportClause.withMarkers(ctx.receiveNode(jsImportClause.markers, ctx.receiveMarkers)!); + jsImportClause = jsImportClause.withTypeOnly(ctx.receiveValue(jsImportClause.typeOnly, ValueType.Primitive)!); + jsImportClause = jsImportClause.padding.withName(ctx.receiveNode(jsImportClause.padding.name, receiveRightPaddedTree)); + jsImportClause = jsImportClause.withNamedBindings(ctx.receiveNode(jsImportClause.namedBindings, ctx.receiveTree)); + return jsImportClause; + } + + public visitNamedImports(namedImports: NamedImports, ctx: ReceiverContext): J { + namedImports = namedImports.withId(ctx.receiveValue(namedImports.id, ValueType.UUID)!); + namedImports = namedImports.withPrefix(ctx.receiveNode(namedImports.prefix, receiveSpace)!); + namedImports = namedImports.withMarkers(ctx.receiveNode(namedImports.markers, ctx.receiveMarkers)!); + namedImports = namedImports.padding.withElements(ctx.receiveNode(namedImports.padding.elements, receiveContainer)!); + namedImports = namedImports.withType(ctx.receiveValue(namedImports.type, ValueType.Object)); + return namedImports; + } + public visitJsImportSpecifier(jsImportSpecifier: JsImportSpecifier, ctx: ReceiverContext): J { jsImportSpecifier = jsImportSpecifier.withId(ctx.receiveValue(jsImportSpecifier.id, ValueType.UUID)!); jsImportSpecifier = jsImportSpecifier.withPrefix(ctx.receiveNode(jsImportSpecifier.prefix, receiveSpace)!); @@ -198,6 +215,24 @@ class Visitor extends JavaScriptVisitor { return jsImportSpecifier; } + public visitImportAttributes(importAttributes: ImportAttributes, ctx: ReceiverContext): J { + importAttributes = importAttributes.withId(ctx.receiveValue(importAttributes.id, ValueType.UUID)!); + importAttributes = importAttributes.withPrefix(ctx.receiveNode(importAttributes.prefix, receiveSpace)!); + importAttributes = importAttributes.withMarkers(ctx.receiveNode(importAttributes.markers, ctx.receiveMarkers)!); + importAttributes = importAttributes.withToken(ctx.receiveValue(importAttributes.token, ValueType.Enum)!); + importAttributes = importAttributes.padding.withElements(ctx.receiveNode(importAttributes.padding.elements, receiveContainer)!); + return importAttributes; + } + + public visitImportAttribute(importAttribute: ImportAttribute, ctx: ReceiverContext): J { + importAttribute = importAttribute.withId(ctx.receiveValue(importAttribute.id, ValueType.UUID)!); + importAttribute = importAttribute.withPrefix(ctx.receiveNode(importAttribute.prefix, receiveSpace)!); + importAttribute = importAttribute.withMarkers(ctx.receiveNode(importAttribute.markers, ctx.receiveMarkers)!); + importAttribute = importAttribute.withName(ctx.receiveNode(importAttribute.name, ctx.receiveTree)!); + importAttribute = importAttribute.padding.withValue(ctx.receiveNode(importAttribute.padding.value, receiveLeftPaddedTree)!); + return importAttribute; + } + public visitJsBinary(jsBinary: JsBinary, ctx: ReceiverContext): J { jsBinary = jsBinary.withId(ctx.receiveValue(jsBinary.id, ValueType.UUID)!); jsBinary = jsBinary.withPrefix(ctx.receiveNode(jsBinary.prefix, receiveSpace)!); @@ -1348,14 +1383,6 @@ class Visitor extends JavaScriptVisitor { return source; } - public visitErroneous(erroneous: Java.Erroneous, ctx: ReceiverContext): J { - erroneous = erroneous.withId(ctx.receiveValue(erroneous.id, ValueType.UUID)!); - erroneous = erroneous.withPrefix(ctx.receiveNode(erroneous.prefix, receiveSpace)!); - erroneous = erroneous.withMarkers(ctx.receiveNode(erroneous.markers, ctx.receiveMarkers)!); - erroneous = erroneous.withText(ctx.receiveValue(erroneous.text, ValueType.Primitive)!); - return erroneous; - } - } class Factory implements ReceiverFactory { @@ -1527,12 +1554,31 @@ class Factory implements ReceiverFactory { ctx.receiveValue(null, ValueType.UUID)!, ctx.receiveNode(null, receiveSpace)!, ctx.receiveNode(null, ctx.receiveMarkers)!, + ctx.receiveNodes(null, ctx.receiveTree)!, + ctx.receiveNode>(null, receiveLeftPaddedTree), + ctx.receiveNode>(null, receiveLeftPaddedTree)!, + ctx.receiveNode(null, ctx.receiveTree) + ); + } + + if (type === "org.openrewrite.javascript.tree.JS$JsImportClause") { + return new JsImportClause( + ctx.receiveValue(null, ValueType.UUID)!, + ctx.receiveNode(null, receiveSpace)!, + ctx.receiveNode(null, ctx.receiveMarkers)!, + ctx.receiveValue(null, ValueType.Primitive)!, ctx.receiveNode>(null, receiveRightPaddedTree), - ctx.receiveNode>(null, leftPaddedValueReceiver(ValueType.Primitive))!, - ctx.receiveNode>(null, receiveContainer), - ctx.receiveNode(null, receiveSpace), - ctx.receiveNode(null, ctx.receiveTree), - ctx.receiveNode>(null, receiveLeftPaddedTree) + ctx.receiveNode(null, ctx.receiveTree) + ); + } + + if (type === "org.openrewrite.javascript.tree.JS$NamedImports") { + return new NamedImports( + ctx.receiveValue(null, ValueType.UUID)!, + ctx.receiveNode(null, receiveSpace)!, + ctx.receiveNode(null, ctx.receiveMarkers)!, + ctx.receiveNode>(null, receiveContainer)!, + ctx.receiveValue(null, ValueType.Object) ); } @@ -1547,6 +1593,26 @@ class Factory implements ReceiverFactory { ); } + if (type === "org.openrewrite.javascript.tree.JS$ImportAttributes") { + return new ImportAttributes( + ctx.receiveValue(null, ValueType.UUID)!, + ctx.receiveNode(null, receiveSpace)!, + ctx.receiveNode(null, ctx.receiveMarkers)!, + ctx.receiveValue(null, ValueType.Enum)!, + ctx.receiveNode>(null, receiveContainer)! + ); + } + + if (type === "org.openrewrite.javascript.tree.JS$ImportAttribute") { + return new ImportAttribute( + ctx.receiveValue(null, ValueType.UUID)!, + ctx.receiveNode(null, receiveSpace)!, + ctx.receiveNode(null, ctx.receiveMarkers)!, + ctx.receiveNode(null, ctx.receiveTree)!, + ctx.receiveNode>(null, receiveLeftPaddedTree)! + ); + } + if (type === "org.openrewrite.javascript.tree.JS$JsBinary") { return new JsBinary( ctx.receiveValue(null, ValueType.UUID)!, @@ -2813,15 +2879,6 @@ class Factory implements ReceiverFactory { ); } - if (type === "org.openrewrite.java.tree.J$Erroneous") { - return new Java.Erroneous( - ctx.receiveValue(null, ValueType.UUID)!, - ctx.receiveNode(null, receiveSpace)!, - ctx.receiveNode(null, ctx.receiveMarkers)!, - ctx.receiveValue(null, ValueType.Primitive)! - ); - } - throw new Error("No factory method for type: " + type); } } diff --git a/openrewrite/src/javascript/remote/sender.ts b/openrewrite/src/javascript/remote/sender.ts index f75afeae..964bb88b 100644 --- a/openrewrite/src/javascript/remote/sender.ts +++ b/openrewrite/src/javascript/remote/sender.ts @@ -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, ConditionalType, DefaultType, Delete, Export, ExpressionStatement, TrailingTokenStatement, ExpressionWithTypeArguments, FunctionType, InferType, ImportType, JsImport, JsImportSpecifier, JsBinary, LiteralType, MappedType, ObjectBindingDeclarations, PropertyAssignment, SatisfiesExpression, ScopedVariableDeclarations, StatementExpression, WithStatement, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, TypePredicate, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, JSTry, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement, ExportDeclaration, ExportAssignment, NamedExports, ExportSpecifier, IndexedAccessType, JsAssignmentOperation, TypeTreeExpression} from '../tree'; +import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, ConditionalType, DefaultType, Delete, Export, ExpressionStatement, TrailingTokenStatement, ExpressionWithTypeArguments, FunctionType, InferType, ImportType, JsImport, JsImportClause, NamedImports, JsImportSpecifier, ImportAttributes, ImportAttribute, JsBinary, LiteralType, MappedType, ObjectBindingDeclarations, PropertyAssignment, SatisfiesExpression, ScopedVariableDeclarations, StatementExpression, WithStatement, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, TypePredicate, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, JSTry, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement, ExportDeclaration, ExportAssignment, NamedExports, ExportSpecifier, IndexedAccessType, JsAssignmentOperation, TypeTreeExpression} from '../tree'; import {Expression, J, JContainer, JLeftPadded, JRightPadded, Space, Statement} from "../../java"; import * as Java from "../../java/tree"; @@ -174,15 +174,32 @@ class Visitor extends JavaScriptVisitor { ctx.sendValue(jsImport, v => v.id, ValueType.UUID); ctx.sendNode(jsImport, v => v.prefix, Visitor.sendSpace); ctx.sendNode(jsImport, v => v.markers, ctx.sendMarkers); - ctx.sendNode(jsImport, v => v.padding.name, Visitor.sendRightPadded(ValueType.Tree)); - ctx.sendNode(jsImport, v => v.padding.importType, Visitor.sendLeftPadded(ValueType.Primitive)); - ctx.sendNode(jsImport, v => v.padding.imports, Visitor.sendContainer(ValueType.Tree)); - ctx.sendNode(jsImport, v => v.from, Visitor.sendSpace); - ctx.sendNode(jsImport, v => v.target, ctx.sendTree); - ctx.sendNode(jsImport, v => v.padding.initializer, Visitor.sendLeftPadded(ValueType.Tree)); + 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.padding.moduleSpecifier, Visitor.sendLeftPadded(ValueType.Tree)); + ctx.sendNode(jsImport, v => v.attributes, ctx.sendTree); return jsImport; } + public visitJsImportClause(jsImportClause: JsImportClause, ctx: SenderContext): J { + ctx.sendValue(jsImportClause, v => v.id, ValueType.UUID); + ctx.sendNode(jsImportClause, v => v.prefix, Visitor.sendSpace); + ctx.sendNode(jsImportClause, v => v.markers, ctx.sendMarkers); + ctx.sendValue(jsImportClause, v => v.typeOnly, ValueType.Primitive); + ctx.sendNode(jsImportClause, v => v.padding.name, Visitor.sendRightPadded(ValueType.Tree)); + ctx.sendNode(jsImportClause, v => v.namedBindings, ctx.sendTree); + return jsImportClause; + } + + public visitNamedImports(namedImports: NamedImports, ctx: SenderContext): J { + ctx.sendValue(namedImports, v => v.id, ValueType.UUID); + ctx.sendNode(namedImports, v => v.prefix, Visitor.sendSpace); + ctx.sendNode(namedImports, v => v.markers, ctx.sendMarkers); + ctx.sendNode(namedImports, v => v.padding.elements, Visitor.sendContainer(ValueType.Tree)); + ctx.sendTypedValue(namedImports, v => v.type, ValueType.Object); + return namedImports; + } + public visitJsImportSpecifier(jsImportSpecifier: JsImportSpecifier, ctx: SenderContext): J { ctx.sendValue(jsImportSpecifier, v => v.id, ValueType.UUID); ctx.sendNode(jsImportSpecifier, v => v.prefix, Visitor.sendSpace); @@ -193,6 +210,24 @@ class Visitor extends JavaScriptVisitor { return jsImportSpecifier; } + public visitImportAttributes(importAttributes: ImportAttributes, ctx: SenderContext): J { + ctx.sendValue(importAttributes, v => v.id, ValueType.UUID); + ctx.sendNode(importAttributes, v => v.prefix, Visitor.sendSpace); + ctx.sendNode(importAttributes, v => v.markers, ctx.sendMarkers); + ctx.sendValue(importAttributes, v => v.token, ValueType.Enum); + ctx.sendNode(importAttributes, v => v.padding.elements, Visitor.sendContainer(ValueType.Tree)); + return importAttributes; + } + + public visitImportAttribute(importAttribute: ImportAttribute, ctx: SenderContext): J { + ctx.sendValue(importAttribute, v => v.id, ValueType.UUID); + ctx.sendNode(importAttribute, v => v.prefix, Visitor.sendSpace); + ctx.sendNode(importAttribute, v => v.markers, ctx.sendMarkers); + ctx.sendNode(importAttribute, v => v.name, ctx.sendTree); + ctx.sendNode(importAttribute, v => v.padding.value, Visitor.sendLeftPadded(ValueType.Tree)); + return importAttribute; + } + public visitJsBinary(jsBinary: JsBinary, ctx: SenderContext): J { ctx.sendValue(jsBinary, v => v.id, ValueType.UUID); ctx.sendNode(jsBinary, v => v.prefix, Visitor.sendSpace); @@ -1348,14 +1383,6 @@ class Visitor extends JavaScriptVisitor { return source; } - public visitErroneous(erroneous: Java.Erroneous, ctx: SenderContext): J { - ctx.sendValue(erroneous, v => v.id, ValueType.UUID); - ctx.sendNode(erroneous, v => v.prefix, Visitor.sendSpace); - ctx.sendNode(erroneous, v => v.markers, ctx.sendMarkers); - ctx.sendValue(erroneous, v => v.text, ValueType.Primitive); - return erroneous; - } - private static sendContainer(type: ValueType): (container: JContainer, ctx: SenderContext) => void { return extensions.sendContainer(type); } diff --git a/openrewrite/src/javascript/tree/support_types.ts b/openrewrite/src/javascript/tree/support_types.ts index 79e24117..a81af084 100644 --- a/openrewrite/src/javascript/tree/support_types.ts +++ b/openrewrite/src/javascript/tree/support_types.ts @@ -264,6 +264,10 @@ export namespace JsSpace { JSTRY_PREFIX, JSTRY_JSCATCH_PREFIX, WITH_STATEMENT_PREFIX, + IMPORT_ATTRIBUTE_PREFIX, + IMPORT_ATTRIBUTES_PREFIX, + NAMED_IMPORTS_PREFIX, + JS_IMPORT_CLAUSE_PREFIX, } } export namespace JsLeftPadded { @@ -310,6 +314,9 @@ export namespace JsLeftPadded { YIELD_DELEGATED, FUNCTION_TYPE_CONSTRUCTOR_TYPE, JSTRY_FINALLIE, + IMPORT_ATTRIBUTE_VALUE, + JS_IMPORT_MODULE_SPECIFIER, + JS_IMPORT_IMPORT_CLAUSE, } } export namespace JsRightPadded { @@ -339,6 +346,7 @@ export namespace JsRightPadded { MAPPED_TYPE_KEYS_REMAPPING_NAME_TYPE, TRAILING_TOKEN_STATEMENT_EXPRESSION, WITH_STATEMENT_BODY, + JS_IMPORT_CLAUSE_NAME, } } export namespace JsContainer { @@ -361,5 +369,7 @@ export namespace JsContainer { NAMED_EXPORTS_ELEMENTS, MAPPED_TYPE_VALUE_TYPE, TYPE_QUERY_TYPE_ARGUMENTS, + IMPORT_ATTRIBUTES_ELEMENTS, + NAMED_IMPORTS_ELEMENTS, } } diff --git a/openrewrite/src/javascript/tree/tree.ts b/openrewrite/src/javascript/tree/tree.ts index e631730a..e48236fa 100644 --- a/openrewrite/src/javascript/tree/tree.ts +++ b/openrewrite/src/javascript/tree/tree.ts @@ -1317,17 +1317,15 @@ 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, name: JRightPadded | null, importType: JLeftPadded, imports: JContainer | null, _from: Space | null, target: Java.Literal | null, initializer: JLeftPadded | null) { + public constructor(id: UUID, prefix: Space, markers: Markers, modifiers: Java.Modifier[], importClause: JLeftPadded | null, moduleSpecifier: JLeftPadded, attributes: ImportAttributes | null) { super(); this._id = id; this._prefix = prefix; this._markers = markers; - this._name = name; - this._importType = importType; - this._imports = imports; - this._from = _from; - this._target = target; - this._initializer = initializer; + this._modifiers = modifiers; + this._importClause = importClause; + this._moduleSpecifier = moduleSpecifier; + this._attributes = attributes; } private readonly _id: UUID; @@ -1337,7 +1335,7 @@ export class JsImport extends JSMixin(Object) implements Statement { } public withId(id: UUID): JsImport { - return id === this._id ? this : new JsImport(id, this._prefix, this._markers, this._name, this._importType, this._imports, this._from, this._target, this._initializer); + return id === this._id ? this : new JsImport(id, this._prefix, this._markers, this._modifiers, this._importClause, this._moduleSpecifier, this._attributes); } private readonly _prefix: Space; @@ -1347,7 +1345,7 @@ export class JsImport extends JSMixin(Object) implements Statement { } public withPrefix(prefix: Space): JsImport { - return prefix === this._prefix ? this : new JsImport(this._id, prefix, this._markers, this._name, this._importType, this._imports, this._from, this._target, this._initializer); + return prefix === this._prefix ? this : new JsImport(this._id, prefix, this._markers, this._modifiers, this._importClause, this._moduleSpecifier, this._attributes); } private readonly _markers: Markers; @@ -1357,71 +1355,147 @@ export class JsImport extends JSMixin(Object) implements Statement { } public withMarkers(markers: Markers): JsImport { - return markers === this._markers ? this : new JsImport(this._id, this._prefix, markers, this._name, this._importType, this._imports, this._from, this._target, this._initializer); + return markers === this._markers ? this : new JsImport(this._id, this._prefix, markers, this._modifiers, this._importClause, this._moduleSpecifier, this._attributes); } - private readonly _name: JRightPadded | null; + private readonly _modifiers: Java.Modifier[]; - public get name(): Java.Identifier | null { - return this._name === null ? null : this._name.element; + public get modifiers(): Java.Modifier[] { + return this._modifiers; } - public withName(name: Java.Identifier | null): JsImport { - return this.padding.withName(JRightPadded.withElement(this._name, name)); + public withModifiers(modifiers: Java.Modifier[]): JsImport { + return modifiers === this._modifiers ? this : new JsImport(this._id, this._prefix, this._markers, modifiers, this._importClause, this._moduleSpecifier, this._attributes); } - private readonly _importType: JLeftPadded; + private readonly _importClause: JLeftPadded | null; - public get importType(): boolean { - return this._importType.element; + public get importClause(): JsImportClause | null { + return this._importClause === null ? null : this._importClause.element; } - public withImportType(importType: boolean): JsImport { - return this.padding.withImportType(this._importType.withElement(importType)); + public withImportClause(importClause: JsImportClause | null): JsImport { + return this.padding.withImportClause(JLeftPadded.withElement(this._importClause, importClause)); } - private readonly _imports: JContainer | null; + private readonly _moduleSpecifier: JLeftPadded; - public get imports(): Expression[] | null { - return this._imports === null ? null : this._imports.elements; + public get moduleSpecifier(): Expression { + return this._moduleSpecifier.element; } - public withImports(imports: Expression[] | null): JsImport { - return this.padding.withImports(JContainer.withElementsNullable(this._imports, imports)); + public withModuleSpecifier(moduleSpecifier: Expression): JsImport { + return this.padding.withModuleSpecifier(this._moduleSpecifier.withElement(moduleSpecifier)); } - private readonly _from: Space | null; + private readonly _attributes: ImportAttributes | null; - public get from(): Space | null { - return this._from; + public get attributes(): ImportAttributes | null { + return this._attributes; } - public withFrom(_from: Space | null): JsImport { - return _from === this._from ? this : new JsImport(this._id, this._prefix, this._markers, this._name, this._importType, this._imports, _from, this._target, this._initializer); + public withAttributes(attributes: ImportAttributes | null): JsImport { + return attributes === this._attributes ? this : new JsImport(this._id, this._prefix, this._markers, this._modifiers, this._importClause, this._moduleSpecifier, attributes); } - private readonly _target: Java.Literal | null; + public acceptJavaScript

(v: JavaScriptVisitor

, p: P): J | null { + return v.visitJsImport(this, p); + } - public get target(): Java.Literal | null { - return this._target; + get padding() { + const t = this; + return new class { + public get importClause(): JLeftPadded | null { + return t._importClause; + } + public withImportClause(importClause: JLeftPadded | 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 { + return t._moduleSpecifier; + } + public withModuleSpecifier(moduleSpecifier: JLeftPadded): JsImport { + return t._moduleSpecifier === moduleSpecifier ? t : new JsImport(t._id, t._prefix, t._markers, t._modifiers, t._importClause, moduleSpecifier, t._attributes); + } } + } + +} + +@LstType("org.openrewrite.javascript.tree.JS$JsImportClause") +export class JsImportClause extends JSMixin(Object) { + public constructor(id: UUID, prefix: Space, markers: Markers, typeOnly: boolean, name: JRightPadded | null, namedBindings: Expression | null) { + super(); + this._id = id; + this._prefix = prefix; + this._markers = markers; + this._typeOnly = typeOnly; + this._name = name; + this._namedBindings = namedBindings; + } + + private readonly _id: UUID; - public withTarget(target: Java.Literal | null): JsImport { - return target === this._target ? this : new JsImport(this._id, this._prefix, this._markers, this._name, this._importType, this._imports, this._from, target, this._initializer); + public get id(): UUID { + return this._id; } - private readonly _initializer: JLeftPadded | null; + public withId(id: UUID): JsImportClause { + return id === this._id ? this : new JsImportClause(id, this._prefix, this._markers, this._typeOnly, this._name, this._namedBindings); + } - public get initializer(): Expression | null { - return this._initializer === null ? null : this._initializer.element; + private readonly _prefix: Space; + + public get prefix(): Space { + return this._prefix; } - public withInitializer(initializer: Expression | null): JsImport { - return this.padding.withInitializer(JLeftPadded.withElement(this._initializer, initializer)); + public withPrefix(prefix: Space): JsImportClause { + return prefix === this._prefix ? this : new JsImportClause(this._id, prefix, this._markers, this._typeOnly, this._name, this._namedBindings); + } + + private readonly _markers: Markers; + + public get markers(): Markers { + return this._markers; + } + + public withMarkers(markers: Markers): JsImportClause { + return markers === this._markers ? this : new JsImportClause(this._id, this._prefix, markers, this._typeOnly, this._name, this._namedBindings); + } + + private readonly _typeOnly: boolean; + + public get typeOnly(): boolean { + return this._typeOnly; + } + + public withTypeOnly(typeOnly: boolean): JsImportClause { + return typeOnly === this._typeOnly ? this : new JsImportClause(this._id, this._prefix, this._markers, typeOnly, this._name, this._namedBindings); + } + + private readonly _name: JRightPadded | null; + + public get name(): Java.Identifier | null { + return this._name === null ? null : this._name.element; + } + + public withName(name: Java.Identifier | null): JsImportClause { + return this.padding.withName(JRightPadded.withElement(this._name, name)); + } + + private readonly _namedBindings: Expression | null; + + public get namedBindings(): Expression | null { + return this._namedBindings; + } + + public withNamedBindings(namedBindings: Expression | null): JsImportClause { + return namedBindings === this._namedBindings ? this : new JsImportClause(this._id, this._prefix, this._markers, this._typeOnly, this._name, namedBindings); } public acceptJavaScript

(v: JavaScriptVisitor

, p: P): J | null { - return v.visitJsImport(this, p); + return v.visitJsImportClause(this, p); } get padding() { @@ -1430,26 +1504,87 @@ export class JsImport extends JSMixin(Object) implements Statement { public get name(): JRightPadded | null { return t._name; } - public withName(name: JRightPadded | null): JsImport { - return t._name === name ? t : new JsImport(t._id, t._prefix, t._markers, name, t._importType, t._imports, t._from, t._target, t._initializer); - } - public get importType(): JLeftPadded { - return t._importType; - } - public withImportType(importType: JLeftPadded): JsImport { - return t._importType === importType ? t : new JsImport(t._id, t._prefix, t._markers, t._name, importType, t._imports, t._from, t._target, t._initializer); + public withName(name: JRightPadded | null): JsImportClause { + return t._name === name ? t : new JsImportClause(t._id, t._prefix, t._markers, t._typeOnly, name, t._namedBindings); } - public get imports(): JContainer | null { - return t._imports; - } - public withImports(imports: JContainer | null): JsImport { - return t._imports === imports ? t : new JsImport(t._id, t._prefix, t._markers, t._name, t._importType, imports, t._from, t._target, t._initializer); - } - public get initializer(): JLeftPadded | null { - return t._initializer; + } + } + +} + +@LstType("org.openrewrite.javascript.tree.JS$NamedImports") +export class NamedImports extends JSMixin(Object) implements Expression { + public constructor(id: UUID, prefix: Space, markers: Markers, elements: JContainer, _type: JavaType | null) { + super(); + this._id = id; + this._prefix = prefix; + this._markers = markers; + this._elements = elements; + this._type = _type; + } + + private readonly _id: UUID; + + public get id(): UUID { + return this._id; + } + + public withId(id: UUID): NamedImports { + return id === this._id ? this : new NamedImports(id, this._prefix, this._markers, this._elements, this._type); + } + + private readonly _prefix: Space; + + public get prefix(): Space { + return this._prefix; + } + + public withPrefix(prefix: Space): NamedImports { + return prefix === this._prefix ? this : new NamedImports(this._id, prefix, this._markers, this._elements, this._type); + } + + private readonly _markers: Markers; + + public get markers(): Markers { + return this._markers; + } + + public withMarkers(markers: Markers): NamedImports { + return markers === this._markers ? this : new NamedImports(this._id, this._prefix, markers, this._elements, this._type); + } + + private readonly _elements: JContainer; + + public get elements(): Expression[] { + return this._elements.elements; + } + + public withElements(elements: Expression[]): NamedImports { + return this.padding.withElements(JContainer.withElements(this._elements, elements)); + } + + private readonly _type: JavaType | null; + + public get type(): JavaType | null { + return this._type; + } + + public withType(_type: JavaType | null): NamedImports { + return _type === this._type ? this : new NamedImports(this._id, this._prefix, this._markers, this._elements, _type); + } + + public acceptJavaScript

(v: JavaScriptVisitor

, p: P): J | null { + return v.visitNamedImports(this, p); + } + + get padding() { + const t = this; + return new class { + public get elements(): JContainer { + return t._elements; } - public withInitializer(initializer: JLeftPadded | null): JsImport { - return t._initializer === initializer ? t : new JsImport(t._id, t._prefix, t._markers, t._name, t._importType, t._imports, t._from, t._target, initializer); + public withElements(elements: JContainer): NamedImports { + return t._elements === elements ? t : new NamedImports(t._id, t._prefix, t._markers, elements, t._type); } } } @@ -1546,6 +1681,173 @@ export class JsImportSpecifier extends JSMixin(Object) implements Expression, Ty } +@LstType("org.openrewrite.javascript.tree.JS$ImportAttributes") +export class ImportAttributes extends JSMixin(Object) { + public constructor(id: UUID, prefix: Space, markers: Markers, token: ImportAttributes.Token, elements: JContainer) { + super(); + this._id = id; + this._prefix = prefix; + this._markers = markers; + this._token = token; + this._elements = elements; + } + + private readonly _id: UUID; + + public get id(): UUID { + return this._id; + } + + public withId(id: UUID): ImportAttributes { + return id === this._id ? this : new ImportAttributes(id, this._prefix, this._markers, this._token, this._elements); + } + + private readonly _prefix: Space; + + public get prefix(): Space { + return this._prefix; + } + + public withPrefix(prefix: Space): ImportAttributes { + return prefix === this._prefix ? this : new ImportAttributes(this._id, prefix, this._markers, this._token, this._elements); + } + + private readonly _markers: Markers; + + public get markers(): Markers { + return this._markers; + } + + public withMarkers(markers: Markers): ImportAttributes { + return markers === this._markers ? this : new ImportAttributes(this._id, this._prefix, markers, this._token, this._elements); + } + + private readonly _token: ImportAttributes.Token; + + public get token(): ImportAttributes.Token { + return this._token; + } + + public withToken(token: ImportAttributes.Token): ImportAttributes { + return token === this._token ? this : new ImportAttributes(this._id, this._prefix, this._markers, token, this._elements); + } + + private readonly _elements: JContainer; + + public get elements(): ImportAttribute[] { + return this._elements.elements; + } + + public withElements(elements: ImportAttribute[]): ImportAttributes { + return this.padding.withElements(JContainer.withElements(this._elements, elements)); + } + + public acceptJavaScript

(v: JavaScriptVisitor

, p: P): J | null { + return v.visitImportAttributes(this, p); + } + + get padding() { + const t = this; + return new class { + public get elements(): JContainer { + return t._elements; + } + public withElements(elements: JContainer): ImportAttributes { + return t._elements === elements ? t : new ImportAttributes(t._id, t._prefix, t._markers, t._token, elements); + } + } + } + +} + +export namespace ImportAttributes { + export enum Token { + With = 0, + Assert = 1, + + } + +} + +@LstType("org.openrewrite.javascript.tree.JS$ImportAttribute") +export class ImportAttribute extends JSMixin(Object) { + public constructor(id: UUID, prefix: Space, markers: Markers, name: Expression, value: JLeftPadded) { + super(); + this._id = id; + this._prefix = prefix; + this._markers = markers; + this._name = name; + this._value = value; + } + + private readonly _id: UUID; + + public get id(): UUID { + return this._id; + } + + public withId(id: UUID): ImportAttribute { + return id === this._id ? this : new ImportAttribute(id, this._prefix, this._markers, this._name, this._value); + } + + private readonly _prefix: Space; + + public get prefix(): Space { + return this._prefix; + } + + public withPrefix(prefix: Space): ImportAttribute { + return prefix === this._prefix ? this : new ImportAttribute(this._id, prefix, this._markers, this._name, this._value); + } + + private readonly _markers: Markers; + + public get markers(): Markers { + return this._markers; + } + + public withMarkers(markers: Markers): ImportAttribute { + return markers === this._markers ? this : new ImportAttribute(this._id, this._prefix, markers, this._name, this._value); + } + + private readonly _name: Expression; + + public get name(): Expression { + return this._name; + } + + public withName(name: Expression): ImportAttribute { + return name === this._name ? this : new ImportAttribute(this._id, this._prefix, this._markers, name, this._value); + } + + private readonly _value: JLeftPadded; + + public get value(): Expression { + return this._value.element; + } + + public withValue(value: Expression): ImportAttribute { + return this.padding.withValue(this._value.withElement(value)); + } + + public acceptJavaScript

(v: JavaScriptVisitor

, p: P): J | null { + return v.visitImportAttribute(this, p); + } + + get padding() { + const t = this; + return new class { + public get value(): JLeftPadded { + return t._value; + } + public withValue(value: JLeftPadded): ImportAttribute { + return t._value === value ? t : new ImportAttribute(t._id, t._prefix, t._markers, t._name, value); + } + } + } + +} + @LstType("org.openrewrite.javascript.tree.JS$JsBinary") export class JsBinary extends JSMixin(Object) implements Expression, TypedTree { public constructor(id: UUID, prefix: Space, markers: Markers, left: Expression, operator: JLeftPadded, right: Expression, _type: JavaType | null) { @@ -2478,6 +2780,7 @@ export namespace ScopedVariableDeclarations { Let = 1, Var = 2, Using = 3, + Import = 4, } diff --git a/openrewrite/src/javascript/visitor.ts b/openrewrite/src/javascript/visitor.ts index 65260b39..793a7b9e 100644 --- a/openrewrite/src/javascript/visitor.ts +++ b/openrewrite/src/javascript/visitor.ts @@ -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, ConditionalType, DefaultType, Delete, Export, ExpressionStatement, TrailingTokenStatement, ExpressionWithTypeArguments, FunctionType, InferType, ImportType, JsImport, JsImportSpecifier, JsBinary, LiteralType, MappedType, ObjectBindingDeclarations, PropertyAssignment, SatisfiesExpression, ScopedVariableDeclarations, StatementExpression, WithStatement, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, TypePredicate, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, JSTry, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement, ExportDeclaration, ExportAssignment, NamedExports, ExportSpecifier, IndexedAccessType, JsAssignmentOperation, TypeTreeExpression} from "./tree"; +import {CompilationUnit, Alias, ArrowFunction, Await, ConditionalType, DefaultType, Delete, Export, ExpressionStatement, TrailingTokenStatement, ExpressionWithTypeArguments, FunctionType, InferType, ImportType, JsImport, JsImportClause, NamedImports, JsImportSpecifier, ImportAttributes, ImportAttribute, JsBinary, LiteralType, MappedType, ObjectBindingDeclarations, PropertyAssignment, SatisfiesExpression, ScopedVariableDeclarations, StatementExpression, WithStatement, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, TypePredicate, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, JSTry, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement, ExportDeclaration, ExportAssignment, NamedExports, ExportSpecifier, IndexedAccessType, JsAssignmentOperation, TypeTreeExpression} from "./tree"; import {Expression, J, JContainer, JLeftPadded, JRightPadded, Space, Statement} from "../java/tree"; import {JavaVisitor} from "../java"; import * as Java from "../java/tree"; @@ -229,15 +229,34 @@ export class JavaScriptVisitor

extends JavaVisitor

{ } jsImport = tempStatement as JsImport; jsImport = jsImport.withMarkers(this.visitMarkers(jsImport.markers, p)); - jsImport = jsImport.padding.withName(this.visitJsRightPadded(jsImport.padding.name, JsRightPadded.Location.JS_IMPORT_NAME, p)); - jsImport = jsImport.padding.withImportType(this.visitJsLeftPadded(jsImport.padding.importType, JsLeftPadded.Location.JS_IMPORT_IMPORT_TYPE, p)!); - jsImport = jsImport.padding.withImports(this.visitJsContainer(jsImport.padding.imports, JsContainer.Location.JS_IMPORT_IMPORTS, p)); - jsImport = jsImport.withFrom(this.visitJsSpace(jsImport.from, JsSpace.Location.JS_IMPORT_FROM, p)); - jsImport = jsImport.withTarget(this.visitAndCast(jsImport.target, p)); - jsImport = jsImport.padding.withInitializer(this.visitJsLeftPadded(jsImport.padding.initializer, JsLeftPadded.Location.JS_IMPORT_INITIALIZER, 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.padding.withModuleSpecifier(this.visitJsLeftPadded(jsImport.padding.moduleSpecifier, JsLeftPadded.Location.JS_IMPORT_MODULE_SPECIFIER, p)!); + jsImport = jsImport.withAttributes(this.visitAndCast(jsImport.attributes, p)); return jsImport; } + public visitJsImportClause(jsImportClause: JsImportClause, p: P): J | null { + jsImportClause = jsImportClause.withPrefix(this.visitJsSpace(jsImportClause.prefix, JsSpace.Location.JS_IMPORT_CLAUSE_PREFIX, p)!); + jsImportClause = jsImportClause.withMarkers(this.visitMarkers(jsImportClause.markers, p)); + jsImportClause = jsImportClause.padding.withName(this.visitJsRightPadded(jsImportClause.padding.name, JsRightPadded.Location.JS_IMPORT_CLAUSE_NAME, p)); + jsImportClause = jsImportClause.withNamedBindings(this.visitAndCast(jsImportClause.namedBindings, p)); + return jsImportClause; + } + + public visitNamedImports(namedImports: NamedImports, p: P): J | null { + namedImports = namedImports.withPrefix(this.visitJsSpace(namedImports.prefix, JsSpace.Location.NAMED_IMPORTS_PREFIX, p)!); + let tempExpression = this.visitExpression(namedImports, p) as Expression; + if (!(tempExpression instanceof NamedImports)) + { + return tempExpression; + } + namedImports = tempExpression as NamedImports; + namedImports = namedImports.withMarkers(this.visitMarkers(namedImports.markers, p)); + namedImports = namedImports.padding.withElements(this.visitJsContainer(namedImports.padding.elements, JsContainer.Location.NAMED_IMPORTS_ELEMENTS, p)!); + return namedImports; + } + public visitJsImportSpecifier(jsImportSpecifier: JsImportSpecifier, p: P): J | null { jsImportSpecifier = jsImportSpecifier.withPrefix(this.visitJsSpace(jsImportSpecifier.prefix, JsSpace.Location.JS_IMPORT_SPECIFIER_PREFIX, p)!); let tempExpression = this.visitExpression(jsImportSpecifier, p) as Expression; @@ -252,6 +271,21 @@ export class JavaScriptVisitor

extends JavaVisitor

{ return jsImportSpecifier; } + public visitImportAttributes(importAttributes: ImportAttributes, p: P): J | null { + importAttributes = importAttributes.withPrefix(this.visitJsSpace(importAttributes.prefix, JsSpace.Location.IMPORT_ATTRIBUTES_PREFIX, p)!); + importAttributes = importAttributes.withMarkers(this.visitMarkers(importAttributes.markers, p)); + importAttributes = importAttributes.padding.withElements(this.visitJsContainer(importAttributes.padding.elements, JsContainer.Location.IMPORT_ATTRIBUTES_ELEMENTS, p)!); + return importAttributes; + } + + public visitImportAttribute(importAttribute: ImportAttribute, p: P): J | null { + importAttribute = importAttribute.withPrefix(this.visitJsSpace(importAttribute.prefix, JsSpace.Location.IMPORT_ATTRIBUTE_PREFIX, p)!); + importAttribute = importAttribute.withMarkers(this.visitMarkers(importAttribute.markers, p)); + importAttribute = importAttribute.withName(this.visitAndCast(importAttribute.name, p)!); + importAttribute = importAttribute.padding.withValue(this.visitJsLeftPadded(importAttribute.padding.value, JsLeftPadded.Location.IMPORT_ATTRIBUTE_VALUE, p)!); + return importAttribute; + } + public visitJsBinary(jsBinary: JsBinary, p: P): J | null { jsBinary = jsBinary.withPrefix(this.visitJsSpace(jsBinary.prefix, JsSpace.Location.JS_BINARY_PREFIX, p)!); let tempExpression = this.visitExpression(jsBinary, p) as Expression; diff --git a/openrewrite/test/javascript/parser/import.test.ts b/openrewrite/test/javascript/parser/import.test.ts index 017abe2f..d40d10b0 100644 --- a/openrewrite/test/javascript/parser/import.test.ts +++ b/openrewrite/test/javascript/parser/import.test.ts @@ -94,12 +94,24 @@ describe('import mapping', () => { ); }); - test.skip('experimental: import with import attributes', () => { + test('experimental: import with import attributes', () => { rewriteRun( //language=typescript typeScript(` - import foo from 'module-name' with { type: "json" }; import Package from 'module-name' assert { type: "json" } + import foo from 'module-name' with { type: "json" }; + /*{1}*/import/*{2}*/ foo /*{3}*/from /*{4}*/'module-name'/*{5}*/ with/*{6}*/ {/*{7}*/ type/*{8}*/: /*{9}*/"json", /*{10}*/ } /*{11}*/; + `) + ); + }); + + test('experimental: import with import attributes', () => { + rewriteRun( + //language=typescript + typeScript(` + import SpyInstance = jest.SpyInstance; + import type SpyInstance = jest.SpyInstance; + /*{1}*/import /*{2}*/type /*{3}*/SpyInstance /*{4}*/= /*{5}*/jest.SpyInstance/*{6}*/; `) ); }); diff --git a/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptReceiver.java b/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptReceiver.java index b18f2836..50425924 100644 --- a/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptReceiver.java +++ b/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptReceiver.java @@ -241,15 +241,34 @@ public JS.JsImport visitJsImport(JS.JsImport jsImport, ReceiverContext ctx) { jsImport = jsImport.withId(ctx.receiveNonNullValue(jsImport.getId(), UUID.class)); jsImport = jsImport.withPrefix(ctx.receiveNonNullNode(jsImport.getPrefix(), JavaScriptReceiver::receiveSpace)); jsImport = jsImport.withMarkers(ctx.receiveNonNullNode(jsImport.getMarkers(), ctx::receiveMarkers)); - jsImport = jsImport.getPadding().withName(ctx.receiveNode(jsImport.getPadding().getName(), JavaScriptReceiver::receiveRightPaddedTree)); - jsImport = jsImport.getPadding().withImportType(ctx.receiveNonNullNode(jsImport.getPadding().getImportType(), leftPaddedValueReceiver(java.lang.Boolean.class))); - jsImport = jsImport.getPadding().withImports(ctx.receiveNode(jsImport.getPadding().getImports(), JavaScriptReceiver::receiveContainer)); - jsImport = jsImport.withFrom(ctx.receiveNode(jsImport.getFrom(), JavaScriptReceiver::receiveSpace)); - jsImport = jsImport.withTarget(ctx.receiveNode(jsImport.getTarget(), ctx::receiveTree)); - jsImport = jsImport.getPadding().withInitializer(ctx.receiveNode(jsImport.getPadding().getInitializer(), JavaScriptReceiver::receiveLeftPaddedTree)); + jsImport = jsImport.withModifiers(ctx.receiveNonNullNodes(jsImport.getModifiers(), ctx::receiveTree)); + jsImport = jsImport.getPadding().withImportClause(ctx.receiveNode(jsImport.getPadding().getImportClause(), JavaScriptReceiver::receiveLeftPaddedTree)); + jsImport = jsImport.getPadding().withModuleSpecifier(ctx.receiveNonNullNode(jsImport.getPadding().getModuleSpecifier(), JavaScriptReceiver::receiveLeftPaddedTree)); + jsImport = jsImport.withAttributes(ctx.receiveNode(jsImport.getAttributes(), ctx::receiveTree)); return jsImport; } + @Override + public JS.JsImportClause visitJsImportClause(JS.JsImportClause jsImportClause, ReceiverContext ctx) { + jsImportClause = jsImportClause.withId(ctx.receiveNonNullValue(jsImportClause.getId(), UUID.class)); + jsImportClause = jsImportClause.withPrefix(ctx.receiveNonNullNode(jsImportClause.getPrefix(), JavaScriptReceiver::receiveSpace)); + jsImportClause = jsImportClause.withMarkers(ctx.receiveNonNullNode(jsImportClause.getMarkers(), ctx::receiveMarkers)); + jsImportClause = jsImportClause.withTypeOnly(ctx.receiveNonNullValue(jsImportClause.isTypeOnly(), boolean.class)); + jsImportClause = jsImportClause.getPadding().withName(ctx.receiveNode(jsImportClause.getPadding().getName(), JavaScriptReceiver::receiveRightPaddedTree)); + jsImportClause = jsImportClause.withNamedBindings(ctx.receiveNode(jsImportClause.getNamedBindings(), ctx::receiveTree)); + return jsImportClause; + } + + @Override + public JS.NamedImports visitNamedImports(JS.NamedImports namedImports, ReceiverContext ctx) { + namedImports = namedImports.withId(ctx.receiveNonNullValue(namedImports.getId(), UUID.class)); + namedImports = namedImports.withPrefix(ctx.receiveNonNullNode(namedImports.getPrefix(), JavaScriptReceiver::receiveSpace)); + namedImports = namedImports.withMarkers(ctx.receiveNonNullNode(namedImports.getMarkers(), ctx::receiveMarkers)); + namedImports = namedImports.getPadding().withElements(ctx.receiveNonNullNode(namedImports.getPadding().getElements(), JavaScriptReceiver::receiveContainer)); + namedImports = namedImports.withType(ctx.receiveValue(namedImports.getType(), JavaType.class)); + return namedImports; + } + @Override public JS.JsImportSpecifier visitJsImportSpecifier(JS.JsImportSpecifier jsImportSpecifier, ReceiverContext ctx) { jsImportSpecifier = jsImportSpecifier.withId(ctx.receiveNonNullValue(jsImportSpecifier.getId(), UUID.class)); @@ -261,6 +280,26 @@ public JS.JsImportSpecifier visitJsImportSpecifier(JS.JsImportSpecifier jsImport return jsImportSpecifier; } + @Override + public JS.ImportAttributes visitImportAttributes(JS.ImportAttributes importAttributes, ReceiverContext ctx) { + importAttributes = importAttributes.withId(ctx.receiveNonNullValue(importAttributes.getId(), UUID.class)); + importAttributes = importAttributes.withPrefix(ctx.receiveNonNullNode(importAttributes.getPrefix(), JavaScriptReceiver::receiveSpace)); + importAttributes = importAttributes.withMarkers(ctx.receiveNonNullNode(importAttributes.getMarkers(), ctx::receiveMarkers)); + importAttributes = importAttributes.withToken(ctx.receiveNonNullValue(importAttributes.getToken(), JS.ImportAttributes.Token.class)); + importAttributes = importAttributes.getPadding().withElements(ctx.receiveNonNullNode(importAttributes.getPadding().getElements(), JavaScriptReceiver::receiveContainer)); + return importAttributes; + } + + @Override + public JS.ImportAttribute visitImportAttribute(JS.ImportAttribute importAttribute, ReceiverContext ctx) { + importAttribute = importAttribute.withId(ctx.receiveNonNullValue(importAttribute.getId(), UUID.class)); + importAttribute = importAttribute.withPrefix(ctx.receiveNonNullNode(importAttribute.getPrefix(), JavaScriptReceiver::receiveSpace)); + importAttribute = importAttribute.withMarkers(ctx.receiveNonNullNode(importAttribute.getMarkers(), ctx::receiveMarkers)); + importAttribute = importAttribute.withName(ctx.receiveNonNullNode(importAttribute.getName(), ctx::receiveTree)); + importAttribute = importAttribute.getPadding().withValue(ctx.receiveNonNullNode(importAttribute.getPadding().getValue(), JavaScriptReceiver::receiveLeftPaddedTree)); + return importAttribute; + } + @Override public JS.JsBinary visitJsBinary(JS.JsBinary jsBinary, ReceiverContext ctx) { jsBinary = jsBinary.withId(ctx.receiveNonNullValue(jsBinary.getId(), UUID.class)); @@ -1497,15 +1536,6 @@ public J.Unknown.Source visitUnknownSource(J.Unknown.Source source, ReceiverCont return source; } - @Override - public J.Erroneous visitErroneous(J.Erroneous erroneous, ReceiverContext ctx) { - erroneous = erroneous.withId(ctx.receiveNonNullValue(erroneous.getId(), UUID.class)); - erroneous = erroneous.withPrefix(ctx.receiveNonNullNode(erroneous.getPrefix(), JavaScriptReceiver::receiveSpace)); - erroneous = erroneous.withMarkers(ctx.receiveNonNullNode(erroneous.getMarkers(), ctx::receiveMarkers)); - erroneous = erroneous.withText(ctx.receiveNonNullValue(erroneous.getText(), String.class)); - return erroneous; - } - } private static class Factory implements ReceiverFactory { @@ -1528,7 +1558,11 @@ protected Function computeValue(Class type) { if (type == JS.InferType.class) return Factory::createJSInferType; if (type == JS.ImportType.class) return Factory::createJSImportType; if (type == JS.JsImport.class) return Factory::createJSJsImport; + if (type == JS.JsImportClause.class) return Factory::createJSJsImportClause; + if (type == JS.NamedImports.class) return Factory::createJSNamedImports; if (type == JS.JsImportSpecifier.class) return Factory::createJSJsImportSpecifier; + if (type == JS.ImportAttributes.class) return Factory::createJSImportAttributes; + if (type == JS.ImportAttribute.class) return Factory::createJSImportAttribute; if (type == JS.JsBinary.class) return Factory::createJSJsBinary; if (type == JS.LiteralType.class) return Factory::createJSLiteralType; if (type == JS.MappedType.class) return Factory::createJSMappedType; @@ -1645,7 +1679,6 @@ protected Function computeValue(Class type) { if (type == J.Yield.class) return Factory::createJYield; if (type == J.Unknown.class) return Factory::createJUnknown; if (type == J.Unknown.Source.class) return Factory::createJUnknownSource; - if (type == J.Erroneous.class) return Factory::createJErroneous; throw new IllegalArgumentException("Unknown type: " + type); } }; @@ -1823,12 +1856,31 @@ private static JS.JsImport createJSJsImport(ReceiverContext ctx) { ctx.receiveNonNullValue(null, UUID.class), ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace), ctx.receiveNonNullNode(null, ctx::receiveMarkers), + ctx.receiveNonNullNodes(null, ctx::receiveTree), + ctx.receiveNode(null, JavaScriptReceiver::receiveLeftPaddedTree), + ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveLeftPaddedTree), + ctx.receiveNode(null, ctx::receiveTree) + ); + } + + private static JS.JsImportClause createJSJsImportClause(ReceiverContext ctx) { + return new JS.JsImportClause( + ctx.receiveNonNullValue(null, UUID.class), + ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace), + ctx.receiveNonNullNode(null, ctx::receiveMarkers), + ctx.receiveNonNullValue(null, boolean.class), ctx.receiveNode(null, JavaScriptReceiver::receiveRightPaddedTree), - ctx.receiveNonNullNode(null, leftPaddedValueReceiver(java.lang.Boolean.class)), - ctx.receiveNode(null, JavaScriptReceiver::receiveContainer), - ctx.receiveNode(null, JavaScriptReceiver::receiveSpace), - ctx.receiveNode(null, ctx::receiveTree), - ctx.receiveNode(null, JavaScriptReceiver::receiveLeftPaddedTree) + ctx.receiveNode(null, ctx::receiveTree) + ); + } + + private static JS.NamedImports createJSNamedImports(ReceiverContext ctx) { + return new JS.NamedImports( + ctx.receiveNonNullValue(null, UUID.class), + ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace), + ctx.receiveNonNullNode(null, ctx::receiveMarkers), + ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveContainer), + ctx.receiveValue(null, JavaType.class) ); } @@ -1843,6 +1895,26 @@ private static JS.JsImportSpecifier createJSJsImportSpecifier(ReceiverContext ct ); } + private static JS.ImportAttributes createJSImportAttributes(ReceiverContext ctx) { + return new JS.ImportAttributes( + ctx.receiveNonNullValue(null, UUID.class), + ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace), + ctx.receiveNonNullNode(null, ctx::receiveMarkers), + ctx.receiveNonNullValue(null, JS.ImportAttributes.Token.class), + ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveContainer) + ); + } + + private static JS.ImportAttribute createJSImportAttribute(ReceiverContext ctx) { + return new JS.ImportAttribute( + ctx.receiveNonNullValue(null, UUID.class), + ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace), + ctx.receiveNonNullNode(null, ctx::receiveMarkers), + ctx.receiveNonNullNode(null, ctx::receiveTree), + ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveLeftPaddedTree) + ); + } + private static JS.JsBinary createJSJsBinary(ReceiverContext ctx) { return new JS.JsBinary( ctx.receiveNonNullValue(null, UUID.class), @@ -3109,15 +3181,6 @@ private static J.Unknown.Source createJUnknownSource(ReceiverContext ctx) { ); } - private static J.Erroneous createJErroneous(ReceiverContext ctx) { - return new J.Erroneous( - ctx.receiveNonNullValue(null, UUID.class), - ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace), - ctx.receiveNonNullNode(null, ctx::receiveMarkers), - ctx.receiveNonNullValue(null, String.class) - ); - } - } private static J.ClassDeclaration.Kind receiveClassDeclarationKind(J.ClassDeclaration.@Nullable Kind kind, @Nullable Class type, ReceiverContext ctx) { diff --git a/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptSender.java b/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptSender.java index efb16da4..0a4dd428 100644 --- a/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptSender.java +++ b/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptSender.java @@ -223,15 +223,34 @@ public JS.JsImport visitJsImport(JS.JsImport jsImport, SenderContext ctx) { ctx.sendValue(jsImport, JS.JsImport::getId); ctx.sendNode(jsImport, JS.JsImport::getPrefix, JavaScriptSender::sendSpace); ctx.sendNode(jsImport, JS.JsImport::getMarkers, ctx::sendMarkers); - ctx.sendNode(jsImport, e -> e.getPadding().getName(), JavaScriptSender::sendRightPadded); - ctx.sendNode(jsImport, e -> e.getPadding().getImportType(), JavaScriptSender::sendLeftPadded); - ctx.sendNode(jsImport, e -> e.getPadding().getImports(), JavaScriptSender::sendContainer); - ctx.sendNode(jsImport, JS.JsImport::getFrom, JavaScriptSender::sendSpace); - ctx.sendNode(jsImport, JS.JsImport::getTarget, ctx::sendTree); - ctx.sendNode(jsImport, e -> e.getPadding().getInitializer(), JavaScriptSender::sendLeftPadded); + ctx.sendNodes(jsImport, JS.JsImport::getModifiers, ctx::sendTree, Tree::getId); + ctx.sendNode(jsImport, e -> e.getPadding().getImportClause(), JavaScriptSender::sendLeftPadded); + ctx.sendNode(jsImport, e -> e.getPadding().getModuleSpecifier(), JavaScriptSender::sendLeftPadded); + ctx.sendNode(jsImport, JS.JsImport::getAttributes, ctx::sendTree); return jsImport; } + @Override + public JS.JsImportClause visitJsImportClause(JS.JsImportClause jsImportClause, SenderContext ctx) { + ctx.sendValue(jsImportClause, JS.JsImportClause::getId); + ctx.sendNode(jsImportClause, JS.JsImportClause::getPrefix, JavaScriptSender::sendSpace); + ctx.sendNode(jsImportClause, JS.JsImportClause::getMarkers, ctx::sendMarkers); + ctx.sendValue(jsImportClause, JS.JsImportClause::isTypeOnly); + ctx.sendNode(jsImportClause, e -> e.getPadding().getName(), JavaScriptSender::sendRightPadded); + ctx.sendNode(jsImportClause, JS.JsImportClause::getNamedBindings, ctx::sendTree); + return jsImportClause; + } + + @Override + public JS.NamedImports visitNamedImports(JS.NamedImports namedImports, SenderContext ctx) { + ctx.sendValue(namedImports, JS.NamedImports::getId); + ctx.sendNode(namedImports, JS.NamedImports::getPrefix, JavaScriptSender::sendSpace); + ctx.sendNode(namedImports, JS.NamedImports::getMarkers, ctx::sendMarkers); + ctx.sendNode(namedImports, e -> e.getPadding().getElements(), JavaScriptSender::sendContainer); + ctx.sendTypedValue(namedImports, JS.NamedImports::getType); + return namedImports; + } + @Override public JS.JsImportSpecifier visitJsImportSpecifier(JS.JsImportSpecifier jsImportSpecifier, SenderContext ctx) { ctx.sendValue(jsImportSpecifier, JS.JsImportSpecifier::getId); @@ -243,6 +262,26 @@ public JS.JsImportSpecifier visitJsImportSpecifier(JS.JsImportSpecifier jsImport return jsImportSpecifier; } + @Override + public JS.ImportAttributes visitImportAttributes(JS.ImportAttributes importAttributes, SenderContext ctx) { + ctx.sendValue(importAttributes, JS.ImportAttributes::getId); + ctx.sendNode(importAttributes, JS.ImportAttributes::getPrefix, JavaScriptSender::sendSpace); + ctx.sendNode(importAttributes, JS.ImportAttributes::getMarkers, ctx::sendMarkers); + ctx.sendValue(importAttributes, JS.ImportAttributes::getToken); + ctx.sendNode(importAttributes, e -> e.getPadding().getElements(), JavaScriptSender::sendContainer); + return importAttributes; + } + + @Override + public JS.ImportAttribute visitImportAttribute(JS.ImportAttribute importAttribute, SenderContext ctx) { + ctx.sendValue(importAttribute, JS.ImportAttribute::getId); + ctx.sendNode(importAttribute, JS.ImportAttribute::getPrefix, JavaScriptSender::sendSpace); + ctx.sendNode(importAttribute, JS.ImportAttribute::getMarkers, ctx::sendMarkers); + ctx.sendNode(importAttribute, JS.ImportAttribute::getName, ctx::sendTree); + ctx.sendNode(importAttribute, e -> e.getPadding().getValue(), JavaScriptSender::sendLeftPadded); + return importAttribute; + } + @Override public JS.JsBinary visitJsBinary(JS.JsBinary jsBinary, SenderContext ctx) { ctx.sendValue(jsBinary, JS.JsBinary::getId); @@ -1508,15 +1547,6 @@ public J.Unknown.Source visitUnknownSource(J.Unknown.Source source, SenderContex return source; } - @Override - public J.Erroneous visitErroneous(J.Erroneous erroneous, SenderContext ctx) { - ctx.sendValue(erroneous, J.Erroneous::getId); - ctx.sendNode(erroneous, J.Erroneous::getPrefix, JavaScriptSender::sendSpace); - ctx.sendNode(erroneous, J.Erroneous::getMarkers, ctx::sendMarkers); - ctx.sendValue(erroneous, J.Erroneous::getText); - return erroneous; - } - } private static void sendContainer(JContainer container, SenderContext ctx) { diff --git a/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptValidator.java b/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptValidator.java index 6aa308e4..19daf396 100644 --- a/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptValidator.java +++ b/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptValidator.java @@ -161,19 +161,45 @@ public JS.ImportType visitImportType(JS.ImportType importType, P p) { @Override public JS.JsImport visitJsImport(JS.JsImport jsImport, P p) { - visitAndValidate(jsImport.getName(), J.Identifier.class, p); - visitAndValidate(jsImport.getImports(), Expression.class, p); - visitAndValidate(jsImport.getTarget(), J.Literal.class, p); - visitAndValidate(jsImport.getInitializer(), Expression.class, p); + ListUtils.map(jsImport.getModifiers(), el -> visitAndValidateNonNull(el, J.Modifier.class, p)); + visitAndValidate(jsImport.getImportClause(), JS.JsImportClause.class, p); + visitAndValidateNonNull(jsImport.getModuleSpecifier(), Expression.class, p); + visitAndValidate(jsImport.getAttributes(), JS.ImportAttributes.class, p); return jsImport; } + @Override + public JS.JsImportClause visitJsImportClause(JS.JsImportClause jsImportClause, P p) { + visitAndValidate(jsImportClause.getName(), J.Identifier.class, p); + visitAndValidate(jsImportClause.getNamedBindings(), Expression.class, p); + return jsImportClause; + } + + @Override + public JS.NamedImports visitNamedImports(JS.NamedImports namedImports, P p) { + visitAndValidate(namedImports.getElements(), Expression.class, p); + return namedImports; + } + @Override public JS.JsImportSpecifier visitJsImportSpecifier(JS.JsImportSpecifier jsImportSpecifier, P p) { visitAndValidateNonNull(jsImportSpecifier.getSpecifier(), Expression.class, p); return jsImportSpecifier; } + @Override + public JS.ImportAttributes visitImportAttributes(JS.ImportAttributes importAttributes, P p) { + visitAndValidate(importAttributes.getElements(), JS.ImportAttribute.class, p); + return importAttributes; + } + + @Override + public JS.ImportAttribute visitImportAttribute(JS.ImportAttribute importAttribute, P p) { + visitAndValidateNonNull(importAttribute.getName(), Expression.class, p); + visitAndValidateNonNull(importAttribute.getValue(), Expression.class, p); + return importAttribute; + } + @Override public JS.JsBinary visitJsBinary(JS.JsBinary jsBinary, P p) { visitAndValidateNonNull(jsBinary.getLeft(), Expression.class, p); @@ -979,9 +1005,4 @@ public J.Unknown.Source visitUnknownSource(J.Unknown.Source source, P p) { return source; } - @Override - public J.Erroneous visitErroneous(J.Erroneous erroneous, P p) { - return erroneous; - } - } diff --git a/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java b/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java index 6c420679..3603732d 100644 --- a/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java +++ b/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java @@ -318,22 +318,37 @@ public J visitJsImport(JS.JsImport jsImport, P p) { } else { i = (JS.JsImport) temp; } - i = i.getPadding().withImportType(Objects.requireNonNull(visitLeftPadded(i.getPadding().getImportType(), JsLeftPadded.Location.JS_IMPORT_IMPORT_TYPE, p))); - i = i.withName(visitAndCast(i.getName(), p)); - if (i.getPadding().getImports() != null) { - i = i.getPadding().withImports(visitContainer(i.getPadding().getImports(), JsContainer.Location.IMPORT_ELEMENT, p)); - } - if (i.getFrom() != null) { - i = i.withFrom(visitSpace(i.getFrom(), JsSpace.Location.IMPORT_FROM_PREFIX, p)); - } - i = i.withTarget(visitAndCast(i.getTarget(), p)); - if (i.getPadding().getInitializer() != null) { - i = i.getPadding().withInitializer(visitLeftPadded(i.getPadding().getInitializer(), - JsLeftPadded.Location.IMPORT_INITIALIZER, p)); - } + 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.getPadding().withModuleSpecifier(Objects.requireNonNull(visitLeftPadded(i.getPadding().getModuleSpecifier(), JsLeftPadded.Location.JS_IMPORT_MODULE_SPECIFIER, p))); + i = i.withAttributes(visitAndCast(i.getAttributes(), p)); + return i; + } + + public J visitJsImportClause(JS.JsImportClause jsImportClause, P p) { + JS.JsImportClause i = jsImportClause; + i = i.withPrefix(visitSpace(i.getPrefix(), JsSpace.Location.JS_IMPORT_CLAUSE_PREFIX, p)); + i = i.withMarkers(visitMarkers(i.getMarkers(), p)); + i = i.getPadding().withName(visitRightPadded(i.getPadding().getName(), JsRightPadded.Location.JS_IMPORT_CLAUSE_NAME, p)); + i = i.withNamedBindings(visitAndCast(i.getNamedBindings(), p)); return i; } + public J visitNamedImports(JS.NamedImports namedImports, P p) { + JS.NamedImports ne = namedImports; + ne = ne.withPrefix(visitSpace(ne.getPrefix(), JsSpace.Location.NAMED_IMPORTS_PREFIX, p)); + ne = ne.withMarkers(visitMarkers(ne.getMarkers(), p)); + Expression temp = (Expression) visitExpression(ne, p); + if (!(temp instanceof JS.NamedImports)) { + return temp; + } else { + ne = (JS.NamedImports) temp; + } + ne = ne.getPadding().withElements(Objects.requireNonNull(visitContainer(ne.getPadding().getElements(), JsContainer.Location.NAMED_IMPORTS_ELEMENTS, p))); + ne = ne.withType(visitType(ne.getType(), p)); + return ne; + } + public J visitJsImportSpecifier(JS.JsImportSpecifier jis, P p) { JS.JsImportSpecifier i = jis; i = i.withPrefix(visitSpace(i.getPrefix(), JsSpace.Location.JS_IMPORT_SPECIFIER_PREFIX, p)); @@ -350,6 +365,23 @@ public J visitJsImportSpecifier(JS.JsImportSpecifier jis, P p) { return i; } + public J visitImportAttributes(JS.ImportAttributes importAttributes, P p) { + JS.ImportAttributes i = importAttributes; + i = i.withPrefix(visitSpace(i.getPrefix(), JsSpace.Location.JS_IMPORT_ATTRIBUTES_PREFIX, p)); + i = i.withMarkers(visitMarkers(i.getMarkers(), p)); + i = i.getPadding().withElements(Objects.requireNonNull(visitContainer(i.getPadding().getElements(), JsContainer.Location.JS_IMPORT_ATTRIBUTES_ELEMENTS, p))); + return i; + } + + public J visitImportAttribute(JS.ImportAttribute importAttribute, P p) { + JS.ImportAttribute i = importAttribute; + i = i.withPrefix(visitSpace(i.getPrefix(), JsSpace.Location.JS_IMPORT_ATTRIBUTE_PREFIX, p)); + i = i.withMarkers(visitMarkers(i.getMarkers(), p)); + i = i.withName(Objects.requireNonNull(visitAndCast(i.getName(), p))); + i = i.getPadding().withValue(Objects.requireNonNull(visitLeftPadded(i.getPadding().getValue(), JsLeftPadded.Location.JS_IMPORT_ATTRIBUTE_VALUE, p))); + return i; + } + public J visitLiteralType(JS.LiteralType literalType, P p) { JS.LiteralType type = literalType; type = type.withPrefix(visitSpace(type.getPrefix(), JsSpace.Location.LITERAL_TYPE_PREFIX, p)); diff --git a/rewrite-javascript/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java b/rewrite-javascript/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java index 75073ea6..38badcd8 100644 --- a/rewrite-javascript/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java +++ b/rewrite-javascript/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java @@ -255,33 +255,50 @@ public J visitImportType(JS.ImportType importType, PrintOutputCapture

p) { @Override public J visitJsImport(JS.JsImport jsImport, PrintOutputCapture

p) { beforeSyntax(jsImport, JsSpace.Location.IMPORT_PREFIX, p); + + jsImport.getModifiers().forEach(m -> delegate.visitModifier(m, p)); + p.append("import"); - if (jsImport.getImportType()) { - visitLeftPaddedBoolean("type", jsImport.getPadding().getImportType(), JsLeftPadded.Location.JS_IMPORT_IMPORT_TYPE, p); + JLeftPadded importClause = jsImport.getPadding().getImportClause(); + visitLeftPadded(importClause, JsLeftPadded.Location.JS_IMPORT_IMPORT_CLAUSE, p); + + visitLeftPadded(importClause == null ? "" : "from", jsImport.getPadding().getModuleSpecifier(), JsLeftPadded.Location.JS_IMPORT_MODULE_SPECIFIER, p); + + visit(jsImport.getAttributes(), p); + + afterSyntax(jsImport, p); + return jsImport; + } + + @Override + public J visitJsImportClause(JS.JsImportClause jsImportClause, PrintOutputCapture

p) { + beforeSyntax(jsImportClause, JsSpace.Location.JS_IMPORT_CLAUSE_PREFIX, p); + + if (jsImportClause.isTypeOnly()) { + p.append("type"); } - // for default export or `* as ` - JS.JsImport.Padding padding = jsImport.getPadding(); - visitRightPadded(padding.getName(), JsRightPadded.Location.IMPORT_NAME_SUFFIX, p); + JRightPadded name = jsImportClause.getPadding().getName(); + visitRightPadded(name, JsRightPadded.Location.JS_IMPORT_CLAUSE_NAME, p); - if (jsImport.getName() != null && padding.getImports() != null) { + if (name!= null && jsImportClause.getNamedBindings() != null) { p.append(","); } - boolean braces = padding.getImports() != null && - padding.getImports().getPadding().getElements().stream().noneMatch(e -> e.getElement() instanceof JS.Alias); - visitContainer(braces ? "{" : "", padding.getImports(), JsContainer.Location.IMPORT_ELEMENT, ",", braces ? "}" : "", p); + visit(jsImportClause.getNamedBindings(), p); - if (jsImport.getFrom() != null) { - visitSpace(jsImport.getFrom(), Space.Location.LANGUAGE_EXTENSION, p); - p.append("from"); - } - visit(jsImport.getTarget(), p); + afterSyntax(jsImportClause, p); - visitLeftPadded("=", padding.getInitializer(), JsLeftPadded.Location.IMPORT_INITIALIZER, p); - afterSyntax(jsImport, p); - return jsImport; + return jsImportClause; + } + + @Override + public J visitNamedImports(JS.NamedImports namedImports, PrintOutputCapture

p) { + beforeSyntax(namedImports, JsSpace.Location.NAMED_IMPORTS_PREFIX, p); + visitContainer("{", namedImports.getPadding().getElements(), JsContainer.Location.NAMED_IMPORTS_ELEMENTS, ",", "}", p); + afterSyntax(namedImports, p); + return namedImports; } @Override @@ -297,6 +314,30 @@ public J visitJsImportSpecifier(JS.JsImportSpecifier jis, PrintOutputCapture

return jis; } + @Override + public J visitImportAttributes(JS.ImportAttributes importAttributes, PrintOutputCapture

p) { + beforeSyntax(importAttributes, JsSpace.Location.JS_IMPORT_ATTRIBUTES_PREFIX, p); + + p.append(importAttributes.getToken().name().toLowerCase()); + + visitContainer("{", importAttributes.getPadding().getElements(), JsContainer.Location.JS_IMPORT_ATTRIBUTES_ELEMENTS, ",", "}", p); + + afterSyntax(importAttributes, p); + return importAttributes; + } + + @Override + public J visitImportAttribute(JS.ImportAttribute importAttribute, PrintOutputCapture

p) { + beforeSyntax(importAttribute, JsSpace.Location.JS_IMPORT_ATTRIBUTE_PREFIX, p); + + visit(importAttribute.getName(), p); + + visitLeftPadded(":", importAttribute.getPadding().getValue(), JsLeftPadded.Location.JS_IMPORT_ATTRIBUTE_VALUE, p); + + afterSyntax(importAttribute, p); + return importAttribute; + } + @Override public J visitJsBinary(JS.JsBinary binary, PrintOutputCapture

p) { beforeSyntax(binary, JsSpace.Location.BINARY_PREFIX, p); @@ -434,6 +475,9 @@ public J visitScopedVariableDeclarations(JS.ScopedVariableDeclarations variableD case Using: p.append("using"); break; + case Import: + p.append("import"); + break; } } diff --git a/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JS.java b/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JS.java index 2d613055..3be59e6d 100644 --- a/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JS.java +++ b/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JS.java @@ -1303,77 +1303,141 @@ class JsImport implements JS, Statement { @With Markers markers; + @With + @Getter + List modifiers; + @Nullable - JRightPadded name; + JLeftPadded importClause; - public J.@Nullable Identifier getName() { - return name == null ? null : name.getElement(); + @Nullable + public JsImportClause getImportClause() { + return importClause != null ? importClause.getElement() : null; } - public JsImport withName(J.@Nullable Identifier name) { - return getPadding().withName(JRightPadded.withElement(this.name, name)); + public JS.JsImport withImportClause(JsImportClause importClause) { + return getPadding().withImportClause(JLeftPadded.withElement(this.importClause, importClause)); } - JLeftPadded importType; + JLeftPadded moduleSpecifier; - public boolean getImportType() { - return importType.getElement(); + public Expression getModuleSpecifier() { + return moduleSpecifier.getElement(); } - public JsImport withImportType(boolean importType) { - return getPadding().withImportType(JLeftPadded.withElement(this.importType, importType)); + public JS.JsImport withModuleSpecifier(Expression moduleSpecifier) { + return getPadding().withModuleSpecifier(JLeftPadded.withElement(this.moduleSpecifier, moduleSpecifier)); } + @With + @Getter @Nullable - JContainer imports; + ImportAttributes attributes; - public @Nullable List getImports() { - return imports == null ? null : imports.getElements(); + @Override + public

J acceptJavaScript(JavaScriptVisitor

v, P p) { + return v.visitJsImport(this, p); } - public JsImport withImports(List imports) { - return getPadding().withImports(JContainer.withElementsNullable(this.imports, imports)); + @Override + public CoordinateBuilder.Statement getCoordinates() { + return new CoordinateBuilder.Statement(this); } + public JsImport.Padding getPadding() { + JsImport.Padding p; + if (this.padding == null) { + p = new JsImport.Padding(this); + this.padding = new WeakReference<>(p); + } else { + p = this.padding.get(); + if (p == null || p.t != this) { + p = new JsImport.Padding(this); + this.padding = new WeakReference<>(p); + } + } + return p; + } + + @RequiredArgsConstructor + public static class Padding { + private final JsImport t; + + public JLeftPadded getModuleSpecifier() { + return t.moduleSpecifier; + } + + public JsImport withModuleSpecifier(JLeftPadded moduleSpecifier) { + return t.moduleSpecifier == moduleSpecifier ? t : new JsImport(t.id, t.prefix, t.markers, t.modifiers, t.importClause, moduleSpecifier, t.attributes); + } + + public @Nullable JLeftPadded getImportClause() { + return t.importClause; + } + + public JsImport withImportClause(@Nullable JLeftPadded importClause) { + return t.importClause == importClause ? t : new JsImport(t.id, t.prefix, t.markers, t.modifiers, importClause, t.moduleSpecifier, t.attributes); + } + } + } + + @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) + @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) + @RequiredArgsConstructor + @AllArgsConstructor(access = AccessLevel.PRIVATE) + class JsImportClause implements JS { + @Nullable + @NonFinal + transient WeakReference padding; @Getter @With - Space from; + @EqualsAndHashCode.Include + UUID id; @Getter @With - J.@Nullable Literal target; + Space prefix; + + @Getter + @With + Markers markers; + + @Getter + @With + boolean typeOnly; @Nullable - JLeftPadded initializer; + JRightPadded name; - public @Nullable Expression getInitializer() { - return initializer == null ? null : initializer.getElement(); + public J.@Nullable Identifier getName() { + return name == null ? null : name.getElement(); } - public JS.JsImport withInitializer(@Nullable Expression initializer) { - return getPadding().withInitializer(JLeftPadded.withElement(this.initializer, initializer)); + public JsImportClause withName(J.@Nullable Identifier name) { + return getPadding().withName(JRightPadded.withElement(this.name, name)); } + @With + @Getter + @Nullable + Expression namedBindings; + @Override public

J acceptJavaScript(JavaScriptVisitor

v, P p) { - return v.visitJsImport(this, p); + return v.visitJsImportClause(this, p); } - @Override - public CoordinateBuilder.Statement getCoordinates() { - return new CoordinateBuilder.Statement(this); - } - public JsImport.Padding getPadding() { - JsImport.Padding p; + public Padding getPadding() { + Padding p; if (this.padding == null) { - p = new JsImport.Padding(this); + p = new Padding(this); this.padding = new WeakReference<>(p); } else { p = this.padding.get(); if (p == null || p.t != this) { - p = new JsImport.Padding(this); + p = new Padding(this); this.padding = new WeakReference<>(p); } } @@ -1382,38 +1446,98 @@ public JsImport.Padding getPadding() { @RequiredArgsConstructor public static class Padding { - private final JsImport t; + private final JsImportClause t; public @Nullable JRightPadded getName() { return t.name; } - public JsImport withName(@Nullable JRightPadded name) { - return t.name == name ? t : new JsImport(t.id, t.prefix, t.markers, name, t.importType, t.imports, t.from, t.target, t.initializer); + public JsImportClause withName(@Nullable JRightPadded name) { + return t.name == name ? t : new JsImportClause(t.id, t.prefix, t.markers, t.typeOnly, name, t.namedBindings); } +// +// public JRightPadded getTypeOnly() { +// return t.typeOnly; +// } +// +// public JsImportClause withTypeOnly(JRightPadded typeOnly) { +// return t.typeOnly == typeOnly ? t : new JsImportClause(t.id, t.prefix, t.markers, typeOnly, t.name, t.namedBindings); +// } + } + } - public JLeftPadded getImportType() { - return t.importType; - } + @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) + @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) + @RequiredArgsConstructor + @AllArgsConstructor(access = AccessLevel.PRIVATE) + final class NamedImports implements JS, Expression { + @Nullable + @NonFinal + transient WeakReference padding; - public JsImport withImportType(JLeftPadded importType) { - return t.importType == importType ? t : new JsImport(t.id, t.prefix, t.markers, t.name, importType, t.imports, t.from, t.target, t.initializer); - } + @With + @EqualsAndHashCode.Include + @Getter + UUID id; - public @Nullable JContainer getImports() { - return t.imports; - } + @With + @Getter + Space prefix; + + @With + @Getter + Markers markers; + + JContainer elements; + + public List getElements() { + return elements.getElements(); + } + + public NamedImports withElements(List elements) { + return getPadding().withElements(JContainer.withElements(this.elements, elements)); + } + + @Nullable + @With + @Getter + JavaType type; + + @Override + public

J acceptJavaScript(JavaScriptVisitor

v, P p) { + return v.visitNamedImports(this, p); + } - public JsImport withImports(@Nullable JContainer imports) { - return t.imports == imports ? t : new JsImport(t.id, t.prefix, t.markers, t.name, t.importType, imports, t.from, t.target, t.initializer); + @Override + public CoordinateBuilder.Expression getCoordinates() { + return new CoordinateBuilder.Expression(this); + } + + public NamedImports.Padding getPadding() { + NamedImports.Padding p; + if (this.padding == null) { + p = new NamedImports.Padding(this); + this.padding = new WeakReference<>(p); + } else { + p = this.padding.get(); + if (p == null || p.t != this) { + p = new NamedImports.Padding(this); + this.padding = new WeakReference<>(p); + } } + return p; + } - public @Nullable JLeftPadded getInitializer() { - return t.initializer; + @RequiredArgsConstructor + public static class Padding { + private final NamedImports t; + + public JContainer getElements() { + return t.elements; } - public JsImport withInitializer(@Nullable JLeftPadded initializer) { - return t.initializer == initializer ? t : new JsImport(t.id, t.prefix, t.markers, t.name, t.importType, t.imports, t.from, t.target, initializer); + public NamedImports withElements(JContainer elements) { + return t.elements == elements ? t : new NamedImports(t.id, t.prefix, t.markers, elements, t.type); } } } @@ -1495,6 +1619,152 @@ public JsImportSpecifier withImportType(JLeftPadded importType) { } + + @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) + @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) + @RequiredArgsConstructor + @AllArgsConstructor(access = AccessLevel.PRIVATE) + final class ImportAttributes implements JS { + @Nullable + @NonFinal + transient WeakReference padding; + + @With + @Getter + @EqualsAndHashCode.Include + UUID id; + + @With + @Getter + Space prefix; + + @With + @Getter + Markers markers; + + @With + @Getter + Token token; + + JContainer elements; + + public List getElements() { + return elements.getElements(); + } + + public ImportAttributes withElements(List elements) { + return getPadding().withElements(JContainer.withElements(this.elements, elements)); + } + + @Override + public

J acceptJavaScript(JavaScriptVisitor

v, P p) { + return v.visitImportAttributes(this, p); + } + + public enum Token { + With, + Assert + } + + public Padding getPadding() { + Padding p; + if (this.padding == null) { + p = new Padding(this); + this.padding = new WeakReference<>(p); + } else { + p = this.padding.get(); + if (p == null || p.t != this) { + p = new Padding(this); + this.padding = new WeakReference<>(p); + } + } + return p; + } + + @RequiredArgsConstructor + public static class Padding { + private final ImportAttributes t; + + public JContainer getElements() { + return t.elements; + } + + public ImportAttributes withElements(JContainer elements) { + return t.elements == elements ? t : new ImportAttributes(t.id, t.prefix, t.markers, t.token, elements); + } + } + } + + @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) + @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) + @RequiredArgsConstructor + @AllArgsConstructor(access = AccessLevel.PRIVATE) + final class ImportAttribute implements JS { + @Nullable + @NonFinal + transient WeakReference padding; + + @With + @Getter + @EqualsAndHashCode.Include + UUID id; + + @With + @Getter + Space prefix; + + @With + @Getter + Markers markers; + + @With + @Getter + Expression name; + + JLeftPadded value; + + public Expression getValue() { + return value.getElement(); + } + + public ImportAttribute withValue(Expression value) { + return getPadding().withValue(JLeftPadded.withElement(this.value, value)); + } + + @Override + public

J acceptJavaScript(JavaScriptVisitor

v, P p) { + return v.visitImportAttribute(this, p); + } + + public Padding getPadding() { + Padding p; + if (this.padding == null) { + p = new Padding(this); + this.padding = new WeakReference<>(p); + } else { + p = this.padding.get(); + if (p == null || p.t != this) { + p = new Padding(this); + this.padding = new WeakReference<>(p); + } + } + return p; + } + + @RequiredArgsConstructor + public static class Padding { + private final ImportAttribute t; + + public JLeftPadded getValue() { + return t.value; + } + + public ImportAttribute withValue(JLeftPadded value) { + return t.value == value ? t : new ImportAttribute(t.id, t.prefix, t.markers, t.name, value); + } + } + } + @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) @RequiredArgsConstructor @@ -2333,7 +2603,8 @@ public enum Scope { Const, Let, Var, - Using + Using, + Import } @RequiredArgsConstructor diff --git a/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsContainer.java b/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsContainer.java index 3e56f30b..364a3faa 100644 --- a/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsContainer.java +++ b/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsContainer.java @@ -36,7 +36,10 @@ public enum Location { IMPORT_TYPE_TYPE_ARGUMENTS(JsSpace.Location.IMPORT_TYPE_TYPE_ARGUMENTS, JsRightPadded.Location.IMPORT_TYPE_TYPE_ARGUMENTS), NAMED_EXPORTS_ELEMENTS(JsSpace.Location.NAMED_EXPORTS_ELEMENTS_PREFIX, JsRightPadded.Location.NAMED_EXPORTS_ELEMENTS), MAPPED_TYPE_VALUE_TYPE(JsSpace.Location.MAPPED_TYPE_VALUE_TYPE, JsRightPadded.Location.MAPPED_TYPE_VALUE_TYPE), - TYPE_QUERY_TYPE_ARGUMENTS(JsSpace.Location.TYPE_QUERY_TYPE_ARGUMENTS, JsRightPadded.Location.TYPE_QUERY_TYPE_ARGUMENTS); + TYPE_QUERY_TYPE_ARGUMENTS(JsSpace.Location.TYPE_QUERY_TYPE_ARGUMENTS, JsRightPadded.Location.TYPE_QUERY_TYPE_ARGUMENTS), + NAMED_IMPORTS_ELEMENTS(JsSpace.Location.NAMED_IMPORTS_ELEMENTS_PREFIX, JsRightPadded.Location.NAMED_IMPORTS_ELEMENTS), + JS_IMPORT_ATTRIBUTES_ELEMENTS(JsSpace.Location.JS_IMPORT_ATTRIBUTES_ELEMENTS_PREFIX, JsRightPadded.Location.JS_IMPORT_ATTRIBUTES_ELEMENTS), + ; private final JsSpace.Location beforeLocation; private final JsRightPadded.Location elementLocation; diff --git a/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsLeftPadded.java b/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsLeftPadded.java index 9a61b97d..891ceea4 100644 --- a/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsLeftPadded.java +++ b/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsLeftPadded.java @@ -30,7 +30,7 @@ public enum Location { JSVARIABLE_INITIALIZER(JsSpace.Location.JSVARIABLE_INITIALIZER), SCOPED_VARIABLE_DECLARATIONS_SCOPE(JsSpace.Location.SCOPED_VARIABLE_DECLARATIONS_SCOPE_PREFIX), NAMESPACE_DECLARATION_KEYWORD_TYPE(JsSpace.Location.NAMESPACE_DECLARATION_KEYWORD_PREFIX), - JS_IMPORT_IMPORT_TYPE(JsSpace.Location.JS_IMPORT_IMPORT_TYPE_PREFIX), + JS_IMPORT_CLAUSE_TYPE_ONLY(JsSpace.Location.JS_IMPORT_CLAUSE_TYPE_ONLY_PREFIX), JS_IMPORT_SPECIFIER_IMPORT_TYPE(JsSpace.Location.JS_IMPORT_SPECIFIER_IMPORT_TYPE_PREFIX), INDEXED_SIGNATURE_DECLARATION_TYPE_EXPRESSION(JsSpace.Location.INDEXED_SIGNATURE_DECLARATION_TYPE_EXPRESSION_PREFIX), FOR_OF_AWAIT(JsSpace.Location.FOR_OF_AWAIT_PREFIX), @@ -54,7 +54,11 @@ public enum Location { LAMBDA_ARROW(JsSpace.Location.LAMBDA_ARROW_PREFIX), 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); + 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), + ; private final JsSpace.Location beforeLocation; diff --git a/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsRightPadded.java b/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsRightPadded.java index a48590be..6e497e36 100644 --- a/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsRightPadded.java +++ b/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsRightPadded.java @@ -58,7 +58,11 @@ public enum Location { MAPPED_TYPE_KEYS_REMAPPING_NAME_TYPE(JsSpace.Location.MAPPED_TYPE_KEYS_REMAPPING_NAME_TYPE_SUFFIX), TYPE_QUERY_TYPE_ARGUMENTS(JsSpace.Location.TYPE_QUERY_TYPE_ARGUMENTS_SUFFIX), TRAILING_TOKEN_EXPRESSION(JsSpace.Location.TRAILING_TOKEN_EXPRESSION_SUFFIX), - WITH_BODY(JsSpace.Location.WITH_BODY_SUFFIX); + WITH_BODY(JsSpace.Location.WITH_BODY_SUFFIX), + JS_IMPORT_CLAUSE_NAME(JsSpace.Location.JS_IMPORT_CLAUSE_NAME_SUFFIX), + NAMED_IMPORTS_ELEMENTS(JsSpace.Location.NAMED_IMPORTS_ELEMENTS_SUFFIX), + JS_IMPORT_ATTRIBUTES_ELEMENTS(JsSpace.Location.JS_IMPORT_ATTRIBUTES_ELEMENTS_SUFFIX), + ; private final JsSpace.Location afterLocation; diff --git a/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsSpace.java b/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsSpace.java index fbb2a9ff..9792168d 100644 --- a/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsSpace.java +++ b/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsSpace.java @@ -47,7 +47,7 @@ public enum Location { IMPORT_INITIALIZER_PREFIX, IMPORT_NAME_SUFFIX, IMPORT_PREFIX, - JS_IMPORT_IMPORT_TYPE_PREFIX, + JS_IMPORT_CLAUSE_TYPE_ONLY_PREFIX, OBJECT_BINDING_DECLARATIONS_PREFIX, OBJECT_BINDING_PREFIX, PROPERTY_ASSIGNMENT_NAME_SUFFIX, @@ -174,6 +174,18 @@ public enum Location { JSTRY_FINALLY_PREFIX, JSCATCH_PREFIX, WITH_PREFIX, - WITH_BODY_SUFFIX; + WITH_BODY_SUFFIX, + JS_IMPORT_IMPORT_CLAUSE_PREFIX, + JS_IMPORT_MODULE_SPECIFIER_PREFIX, + JS_IMPORT_CLAUSE_PREFIX, + JS_IMPORT_CLAUSE_NAME_SUFFIX, + NAMED_IMPORTS_PREFIX, + NAMED_IMPORTS_ELEMENTS_PREFIX, + NAMED_IMPORTS_ELEMENTS_SUFFIX, + JS_IMPORT_ATTRIBUTES_PREFIX, + JS_IMPORT_ATTRIBUTES_ELEMENTS_PREFIX, + JS_IMPORT_ATTRIBUTES_ELEMENTS_SUFFIX, + JS_IMPORT_ATTRIBUTE_PREFIX, + JS_IMPORT_ATTRIBUTE_VALUE_PREFIX, } }