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

Commit 2d8ec7c

Browse files
arodionovAndrii Rodionov
andauthored
Variable declaration with using keyword (#188)
Co-authored-by: Andrii Rodionov <[email protected]>
1 parent 5739368 commit 2d8ec7c

File tree

13 files changed

+80
-13
lines changed

13 files changed

+80
-13
lines changed

openrewrite/src/javascript/parser.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,7 +1331,9 @@ export class JavaScriptParserVisitor {
13311331
this.mapTypeParametersAsObject(node),
13321332
new JContainer(
13331333
this.prefix(node.getChildAt(node.getChildren().findIndex(n => n.pos === node.parameters.pos) - 1)),
1334-
node.parameters.map(p => this.rightPadded(this.visit(p), this.suffix(p)))
1334+
node.parameters.length == 0 ?
1335+
[this.rightPadded(this.newJEmpty(), this.prefix(this.findChildNode(node, ts.SyntaxKind.CloseParenToken)!))]
1336+
: node.parameters.map(p => this.rightPadded(this.visit(p), this.suffix(p)))
13351337
.concat(node.parameters.hasTrailingComma ? this.rightPadded(this.newJEmpty(), this.prefix(this.findChildNode(node, ts.SyntaxKind.CloseParenToken)!)) : []),
13361338
Markers.EMPTY),
13371339
this.prefix(this.findChildNode(node, ts.SyntaxKind.EqualsGreaterThanToken)!),
@@ -2123,7 +2125,7 @@ export class JavaScriptParserVisitor {
21232125
randomId(),
21242126
this.prefix(node),
21252127
Markers.EMPTY,
2126-
this.leftPadded(this.prefix(node.getFirstToken()!), unaryOperator),
2128+
this.leftPadded(this.suffix(node.operand), unaryOperator),
21272129
this.convert(node.operand),
21282130
this.mapType(node)
21292131
);
@@ -2608,7 +2610,7 @@ export class JavaScriptParserVisitor {
26082610
this.rightPadded(
26092611
this.convert(node.statement),
26102612
this.semicolonPrefix(node.statement),
2611-
node.statement.getLastToken()?.kind == ts.SyntaxKind.SemicolonToken ? Markers.build([new Semicolon(randomId())]) : Markers.EMPTY
2613+
node.statement.getChildAt(node.statement.getChildCount() - 1)?.kind == ts.SyntaxKind.SemicolonToken ? Markers.build([new Semicolon(randomId())]) : Markers.EMPTY
26122614
)
26132615
);
26142616
}
@@ -2823,6 +2825,8 @@ export class JavaScriptParserVisitor {
28232825
? JS.ScopedVariableDeclarations.Scope.Let
28242826
: kind?.kind === ts.SyntaxKind.ConstKeyword
28252827
? JS.ScopedVariableDeclarations.Scope.Const
2828+
: kind?.kind === ts.SyntaxKind.UsingKeyword
2829+
? JS.ScopedVariableDeclarations.Scope.Using
28262830
: JS.ScopedVariableDeclarations.Scope.Var
28272831
),
28282832
node.declarations.map((declaration) => {
@@ -3785,7 +3789,7 @@ export class JavaScriptParserVisitor {
37853789
private mapTypeParametersAsJContainer(node: ts.ClassDeclaration | ts.InterfaceDeclaration | ts.ClassExpression): JContainer<J.TypeParameter> | null {
37863790
return node.typeParameters
37873791
? JContainer.build(
3788-
this.suffix(this.findChildNode(node, ts.SyntaxKind.Identifier)!),
3792+
this.prefix(this.findChildNode(node, ts.SyntaxKind.LessThanToken)!),
37893793
this.mapTypeParametersList(node.typeParameters)
37903794
.concat(node.typeParameters.hasTrailingComma ? this.rightPadded(
37913795
new J.TypeParameter(randomId(), Space.EMPTY, Markers.EMPTY, [], [], this.newJEmpty(), null),
@@ -3802,10 +3806,12 @@ export class JavaScriptParserVisitor {
38023806

38033807
return new J.TypeParameters(
38043808
randomId(),
3805-
this.prefix(node.getChildAt(node.getChildren().findIndex(n => n.pos === typeParameters[0].pos) - 1)),
3809+
this.prefix(this.findChildNode(node, ts.SyntaxKind.LessThanToken)!),
38063810
Markers.EMPTY,
38073811
[],
3808-
typeParameters.map(tp => this.rightPadded(this.visit(tp), this.suffix(tp)))
3812+
typeParameters.length == 0 ?
3813+
[this.rightPadded(new J.TypeParameter(randomId(), Space.EMPTY, Markers.EMPTY, [], [], this.newJEmpty(), null), this.prefix(this.findChildNode(node, ts.SyntaxKind.GreaterThanToken)!))]
3814+
: typeParameters.map(tp => this.rightPadded(this.visit(tp), this.suffix(tp)))
38093815
.concat(typeParameters.hasTrailingComma ? this.rightPadded(
38103816
new J.TypeParameter(randomId(), Space.EMPTY, Markers.EMPTY, [], [], this.newJEmpty(), null),
38113817
this.prefix(this.findChildNode(node, ts.SyntaxKind.GreaterThanToken)!)) : []),

openrewrite/src/javascript/tree/tree.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2477,6 +2477,7 @@ export namespace ScopedVariableDeclarations {
24772477
Const = 0,
24782478
Let = 1,
24792479
Var = 2,
2480+
Using = 3,
24802481

24812482
}
24822483

openrewrite/test/javascript/parser/class.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,4 +553,15 @@ describe('class mapping', () => {
553553
);
554554
});
555555

556+
test('new anonymous class with type params', () => {
557+
rewriteRun(
558+
//language=typescript
559+
typeScript(`
560+
new (class/*a*/<T extends object> extends WeakRef<T> {
561+
foo = "bar";
562+
})({hello: "world"});
563+
`)
564+
);
565+
});
566+
556567
});

openrewrite/test/javascript/parser/interface.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,4 +389,15 @@ describe('interface mapping', () => {
389389
);
390390
});
391391

392+
test('function type with empty args', () => {
393+
rewriteRun(
394+
//language=typescript
395+
typeScript(`
396+
export interface ProxyCursorHooks {
397+
getValue?: (/*a*/)=> any;
398+
}
399+
`)
400+
);
401+
});
402+
392403
});

openrewrite/test/javascript/parser/typeAlias.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,4 +261,13 @@ describe('type alias mapping', () => {
261261
);
262262
});
263263

264+
test('type with empty type argument', () => {
265+
rewriteRun(
266+
//language=typescript
267+
typeScript(`
268+
type A/*a*/</*b*/>/*c*/ = {/*d*/}
269+
`)
270+
);
271+
});
272+
264273
});

openrewrite/test/javascript/parser/unary.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,11 @@ describe('postfix operator mapping', () => {
6464
typeScript('a--;')
6565
);
6666
});
67+
68+
test('unary with comments', () => {
69+
rewriteRun(
70+
//language=typescript
71+
typeScript('/*a*/a/*b*/++/*c*/;')
72+
);
73+
});
6774
});

openrewrite/test/javascript/parser/variableDeclarations.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,13 @@ describe('variable declaration mapping', () => {
178178
`)
179179
);
180180
});
181+
182+
test('variable with using keyword', () => {
183+
rewriteRun(
184+
//language=typescript
185+
typeScript(`
186+
using unrefTimer = stub(Deno, 'unrefTimer');
187+
`)
188+
);
189+
});
181190
});

openrewrite/test/javascript/parser/while.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,13 @@ describe('while mapping', () => {
5252
`)
5353
);
5454
});
55+
56+
57+
test('while-if with semicolon', () => {
58+
rewriteRun(
59+
//language=typescript
60+
typeScript('while (i--) if (nodeList[i] == elem) return true;')
61+
);
62+
});
63+
5564
});

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
22
* Copyright 2024 the original author or authors.
33
* <p>
4-
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* Licensed under the Moderne Source Available License (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
* <p>
8-
* https://www.apache.org/licenses/LICENSE-2.0
8+
* https://docs.moderne.io/licensing/moderne-source-available-license
99
* <p>
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
22
* Copyright 2024 the original author or authors.
33
* <p>
4-
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* Licensed under the Moderne Source Available License (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
* <p>
8-
* https://www.apache.org/licenses/LICENSE-2.0
8+
* https://docs.moderne.io/licensing/moderne-source-available-license
99
* <p>
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,

0 commit comments

Comments
 (0)