Skip to content

Commit

Permalink
Implemented type arguments support for qualified names.
Browse files Browse the repository at this point in the history
  • Loading branch information
arodionov committed Oct 18, 2024
1 parent 3ab6fe1 commit 1896694
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
42 changes: 39 additions & 3 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class JavaScriptParser extends Parser {
const result: SourceFile[] = [];
for (const filePath of program.getRootFileNames()) {
const sourceFile = program.getSourceFile(filePath)!;
const input = new ParserInput(filePath, null, false, () => Buffer.from(ts.sys.readFile(filePath)!));
const input = new ParserInput(filePath, null, false, () => Buffer.from(ts.sys.readFile(filePath)!));
try {
const parsed = new JavaScriptParserVisitor(this, sourceFile, typeChecker).visit(sourceFile) as SourceFile;
result.push(parsed);
Expand Down Expand Up @@ -532,14 +532,28 @@ export class JavaScriptParserVisitor {
}

visitQualifiedName(node: ts.QualifiedName) {
return new J.FieldAccess(
const fieldAccess = new J.FieldAccess(
randomId(),
this.prefix(node),
Markers.EMPTY,
this.visit(node.left),
new JLeftPadded<J.Identifier>(this.suffix(node.left), this.convert<J.Identifier>(node.right), Markers.EMPTY),
this.mapType(node)
);

const parent = node.parent as ts.TypeReferenceNode;
if (parent.typeArguments) {
return new J.ParameterizedType(
randomId(),
this.prefix(parent),
Markers.EMPTY,
fieldAccess,
this.mapTypeArguments(parent.typeArguments),
this.mapType(parent)
)
} else {
return fieldAccess;
}
}

visitComputedPropertyName(node: ts.ComputedPropertyName) {
Expand Down Expand Up @@ -688,6 +702,10 @@ export class JavaScriptParserVisitor {

visitTypeReference(node: ts.TypeReferenceNode) {
if (node.typeArguments) {
// Temporary check for supported constructions with type arguments
if (ts.isQualifiedName(node.typeName)) {
return this.visit(node.typeName);
}
return this.visitUnknown(node);
}
return this.visit(node.typeName);
Expand Down Expand Up @@ -831,7 +849,7 @@ export class JavaScriptParserVisitor {
const statements: JRightPadded<J.Statement>[] = this.rightPaddedSeparatedList(
[...statementList.getChildren(this.sourceFile)],
ts.SyntaxKind.CommaToken,
(nodes, i) => i == nodes.length -2 && nodes[i + 1].kind == ts.SyntaxKind.CommaToken ? Markers.build([new TrailingComma(randomId(), this.prefix(nodes[i + 1]))]) : Markers.EMPTY
(nodes, i) => i == nodes.length - 2 && nodes[i + 1].kind == ts.SyntaxKind.CommaToken ? Markers.build([new TrailingComma(randomId(), this.prefix(nodes[i + 1]))]) : Markers.EMPTY
);

return new J.Block(
Expand Down Expand Up @@ -1929,6 +1947,24 @@ export class JavaScriptParserVisitor {
return this.mapToContainer(nodes, this.trailingComma(nodes));
}

private mapTypeArguments(nodes: readonly ts.Node[]): JContainer<J.Expression> {
if (nodes.length === 0) {
return JContainer.empty();
}

const args = nodes.map(node =>
this.rightPadded(
this.visit(node),
this.suffix(node),
Markers.EMPTY
))
return new JContainer(
this.prefix(nodes[0]),
args,
Markers.EMPTY
);
}

private trailingComma = (nodes: readonly ts.Node[]) => (ns: readonly ts.Node[], i: number) => {
const last = i === ns.length - 2;
return last ? Markers.build([new TrailingComma(randomId(), this.prefix(nodes[2], false))]) : Markers.EMPTY;
Expand Down
14 changes: 14 additions & 0 deletions openrewrite/test/javascript/parser/qualifiedName.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ describe('empty mapping', () => {
);
});

test('globalThis qualified name with generic', () => {
rewriteRun(
//language=typescript
typeScript('const value: globalThis.Promise< string > = null')
);
});

test('globalThis qualified name with comments', () => {
rewriteRun(
//language=typescript
typeScript('const value /*a123*/ : globalThis. globalThis . /*asda*/ globalThis.Promise<string> = null;')
);
});

test.skip('nested class qualified name', () => {
rewriteRun(
//language=typescript
Expand Down

0 comments on commit 1896694

Please sign in to comment.