Skip to content
This repository was archived by the owner on May 12, 2025. It is now read-only.

Commit 2454ee1

Browse files
arodionovAndrii Rodionov
andauthored
Change model and printer to fix parse errors with ImportAttribute and MappedType (#205)
Co-authored-by: Andrii Rodionov <[email protected]>
1 parent bfc57d6 commit 2454ee1

File tree

9 files changed

+76
-20
lines changed

9 files changed

+76
-20
lines changed

openrewrite/src/javascript/parser.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,15 +1686,20 @@ export class JavaScriptParserVisitor {
16861686
)
16871687
): null,
16881688
node.questionToken ? this.leftPadded(this.prefix(this.findChildNode(node, ts.SyntaxKind.QuestionToken)!), true) : this.leftPadded(Space.EMPTY, false),
1689-
new JContainer(
1689+
node.type ? new JContainer(
16901690
this.prefix(this.findChildNode(node, ts.SyntaxKind.ColonToken)!),
1691-
[this.rightPadded(this.visit(node.type!), this.suffix(node.type!)),
1691+
[this.rightPadded(this.visit(node.type), this.suffix(node.type)),
16921692
this.findChildNode(node, ts.SyntaxKind.SemicolonToken) ?
16931693
this.rightPadded(this.newJEmpty(Space.EMPTY, Markers.build([new Semicolon(randomId())])), this.prefix(node.getLastToken()!))
16941694
: this.rightPadded(this.newJEmpty(), this.prefix(node.getLastToken()!))
16951695
],
16961696
Markers.EMPTY
1697-
),
1697+
) : new JContainer(
1698+
Space.EMPTY,
1699+
[this.findChildNode(node, ts.SyntaxKind.SemicolonToken) ?
1700+
this.rightPadded(this.newJEmpty(this.prefix(this.findChildNode(node, ts.SyntaxKind.SemicolonToken)!), Markers.build([new Semicolon(randomId())])), this.prefix(node.getLastToken()!))
1701+
: this.rightPadded(this.newJEmpty(), this.prefix(node.getLastToken()!))
1702+
], Markers.EMPTY),
16981703
this.mapType(node)
16991704
);
17001705
}
@@ -3606,7 +3611,7 @@ export class JavaScriptParserVisitor {
36063611

36073612
visitImportAttributes(node: ts.ImportAttributes) {
36083613
const openBraceIndex = node.getChildren().findIndex(n => n.kind === ts.SyntaxKind.OpenBraceToken);
3609-
const elements = this.mapCommaSeparatedList<JS.ImportAttribute>(node.getChildren(this.sourceFile).slice(openBraceIndex, openBraceIndex + 3));
3614+
const elements = this.mapCommaSeparatedList(node.getChildren(this.sourceFile).slice(openBraceIndex, openBraceIndex + 3));
36103615
return new JS.ImportAttributes(
36113616
randomId(),
36123617
this.prefix(node),

openrewrite/src/javascript/remote/receiver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1630,7 +1630,7 @@ class Factory implements ReceiverFactory {
16301630
ctx.receiveNode(null, receiveSpace)!,
16311631
ctx.receiveNode(null, ctx.receiveMarkers)!,
16321632
ctx.receiveValue(null, ValueType.Enum)!,
1633-
ctx.receiveNode<JContainer<ImportAttribute>>(null, receiveContainer)!
1633+
ctx.receiveNode<JContainer<Statement>>(null, receiveContainer)!
16341634
);
16351635
}
16361636

openrewrite/src/javascript/tree/tree.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1683,7 +1683,7 @@ export class JsImportSpecifier extends JSMixin(Object) implements Expression, Ty
16831683

16841684
@LstType("org.openrewrite.javascript.tree.JS$ImportAttributes")
16851685
export class ImportAttributes extends JSMixin(Object) {
1686-
public constructor(id: UUID, prefix: Space, markers: Markers, token: ImportAttributes.Token, elements: JContainer<ImportAttribute>) {
1686+
public constructor(id: UUID, prefix: Space, markers: Markers, token: ImportAttributes.Token, elements: JContainer<Statement>) {
16871687
super();
16881688
this._id = id;
16891689
this._prefix = prefix;
@@ -1732,13 +1732,13 @@ export class ImportAttributes extends JSMixin(Object) {
17321732
return token === this._token ? this : new ImportAttributes(this._id, this._prefix, this._markers, token, this._elements);
17331733
}
17341734

1735-
private readonly _elements: JContainer<ImportAttribute>;
1735+
private readonly _elements: JContainer<Statement>;
17361736

1737-
public get elements(): ImportAttribute[] {
1737+
public get elements(): Statement[] {
17381738
return this._elements.elements;
17391739
}
17401740

1741-
public withElements(elements: ImportAttribute[]): ImportAttributes {
1741+
public withElements(elements: Statement[]): ImportAttributes {
17421742
return this.padding.withElements(JContainer.withElements(this._elements, elements));
17431743
}
17441744

@@ -1749,10 +1749,10 @@ export class ImportAttributes extends JSMixin(Object) {
17491749
get padding() {
17501750
const t = this;
17511751
return new class {
1752-
public get elements(): JContainer<ImportAttribute> {
1752+
public get elements(): JContainer<Statement> {
17531753
return t._elements;
17541754
}
1755-
public withElements(elements: JContainer<ImportAttribute>): ImportAttributes {
1755+
public withElements(elements: JContainer<Statement>): ImportAttributes {
17561756
return t._elements === elements ? t : new ImportAttributes(t._id, t._prefix, t._markers, t._token, elements);
17571757
}
17581758
}
@@ -1866,7 +1866,7 @@ export class ImportTypeAttributes extends JSMixin(Object) {
18661866
}
18671867

18681868
@LstType("org.openrewrite.javascript.tree.JS$ImportAttribute")
1869-
export class ImportAttribute extends JSMixin(Object) {
1869+
export class ImportAttribute extends JSMixin(Object) implements Statement {
18701870
public constructor(id: UUID, prefix: Space, markers: Markers, name: Expression, value: JLeftPadded<Expression>) {
18711871
super();
18721872
this._id = id;

openrewrite/src/javascript/visitor.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,12 @@ export class JavaScriptVisitor<P> extends JavaVisitor<P> {
289289

290290
public visitImportAttribute(importAttribute: ImportAttribute, p: P): J | null {
291291
importAttribute = importAttribute.withPrefix(this.visitJsSpace(importAttribute.prefix, JsSpace.Location.IMPORT_ATTRIBUTE_PREFIX, p)!);
292+
let tempStatement = this.visitStatement(importAttribute, p) as Statement;
293+
if (!(tempStatement instanceof ImportAttribute))
294+
{
295+
return tempStatement;
296+
}
297+
importAttribute = tempStatement as ImportAttribute;
292298
importAttribute = importAttribute.withMarkers(this.visitMarkers(importAttribute.markers, p));
293299
importAttribute = importAttribute.withName(this.visitAndCast(importAttribute.name, p)!);
294300
importAttribute = importAttribute.padding.withValue(this.visitJsLeftPadded(importAttribute.padding.value, JsLeftPadded.Location.IMPORT_ATTRIBUTE_VALUE, p)!);

openrewrite/test/javascript/parser/export.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,21 @@ describe('export keyword tests', () => {
178178
`)
179179
);
180180
});
181+
182+
test('export/import with empty attributes', () => {
183+
rewriteRun(
184+
//language=typescript
185+
typeScript(`
186+
export * as foo from "foo.json"
187+
export * as bar from "bar.json" assert { }
188+
export * as baz from "baz.json" assert { /* comment */ }
189+
190+
import * as foo from "foo.json"
191+
import * as bar from "bar.json" assert { }
192+
import * as baz from "baz.json" assert { /* comment */ }
193+
`)
194+
);
195+
});
181196
});
182197

183198

openrewrite/test/javascript/parser/mappedType.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,28 @@ describe('mapped type mapping', () => {
176176
);
177177
});
178178

179+
test('no node type ', () => {
180+
rewriteRun(
181+
//language=typescript
182+
typeScript(`
183+
type Type = {
184+
// comment
185+
readonly [T in number] /*a*/
186+
};
187+
`)
188+
);
189+
});
190+
191+
test('no node type with ;', () => {
192+
rewriteRun(
193+
//language=typescript
194+
typeScript(`
195+
type Type = {
196+
// comment
197+
readonly [T in number] /*a*/;/*b*/
198+
};
199+
`)
200+
);
201+
});
202+
179203
});

rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public JS.JsImportSpecifier visitJsImportSpecifier(JS.JsImportSpecifier jsImport
189189

190190
@Override
191191
public JS.ImportAttributes visitImportAttributes(JS.ImportAttributes importAttributes, P p) {
192-
visitAndValidate(importAttributes.getElements(), JS.ImportAttribute.class, p);
192+
visitAndValidate(importAttributes.getElements(), Statement.class, p);
193193
return importAttributes;
194194
}
195195

rewrite-javascript/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,8 @@ public J visitMappedType(JS.MappedType mappedType, PrintOutputCapture<P> p) {
531531
visitLeftPaddedBoolean("?", mappedType.getPadding().getHasQuestionToken(), JsLeftPadded.Location.MAPPED_TYPE_QUESTION_TOKEN, p);
532532
}
533533

534-
visitContainer(":", mappedType.getPadding().getValueType(), JsContainer.Location.MAPPED_TYPE_VALUE_TYPE, "", "", p);
534+
String colon = mappedType.getValueType().get(0) instanceof J.Empty ? "" : ":";
535+
visitContainer(colon, mappedType.getPadding().getValueType(), JsContainer.Location.MAPPED_TYPE_VALUE_TYPE, "", "", p);
535536

536537
p.append("}");
537538
afterSyntax(mappedType, p);

rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JS.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,13 +1636,13 @@ final class ImportAttributes implements JS {
16361636
@Getter
16371637
Token token;
16381638

1639-
JContainer<ImportAttribute> elements;
1639+
JContainer<Statement> elements;
16401640

1641-
public List<ImportAttribute> getElements() {
1641+
public List<Statement> getElements() {
16421642
return elements.getElements();
16431643
}
16441644

1645-
public ImportAttributes withElements(List<ImportAttribute> elements) {
1645+
public ImportAttributes withElements(List<Statement> elements) {
16461646
return getPadding().withElements(JContainer.withElements(this.elements, elements));
16471647
}
16481648

@@ -1675,11 +1675,11 @@ public Padding getPadding() {
16751675
public static class Padding {
16761676
private final ImportAttributes t;
16771677

1678-
public JContainer<ImportAttribute> getElements() {
1678+
public JContainer<Statement> getElements() {
16791679
return t.elements;
16801680
}
16811681

1682-
public ImportAttributes withElements(JContainer<ImportAttribute> elements) {
1682+
public ImportAttributes withElements(JContainer<Statement> elements) {
16831683
return t.elements == elements ? t : new ImportAttributes(t.id, t.prefix, t.markers, t.token, elements);
16841684
}
16851685
}
@@ -1777,7 +1777,7 @@ public ImportTypeAttributes withElements(JContainer<ImportAttribute> elements) {
17771777
@EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true)
17781778
@RequiredArgsConstructor
17791779
@AllArgsConstructor(access = AccessLevel.PRIVATE)
1780-
final class ImportAttribute implements JS {
1780+
final class ImportAttribute implements JS, Statement {
17811781
@Nullable
17821782
@NonFinal
17831783
transient WeakReference<Padding> padding;
@@ -1829,6 +1829,11 @@ public Padding getPadding() {
18291829
return p;
18301830
}
18311831

1832+
@Override
1833+
public CoordinateBuilder.Statement getCoordinates() {
1834+
return new CoordinateBuilder.Statement(this);
1835+
}
1836+
18321837
@RequiredArgsConstructor
18331838
public static class Padding {
18341839
private final ImportAttribute t;

0 commit comments

Comments
 (0)