Skip to content

Commit

Permalink
Map undefined type to Primitive.None
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Sep 29, 2024
1 parent 3fc2553 commit 9d4197d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
6 changes: 5 additions & 1 deletion openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ export class JavaScriptParserVisitor {
return this.mapLiteral(node, null);
}

private mapLiteral(node: ts.LiteralExpression | ts.TrueLiteral | ts.FalseLiteral | ts.NullLiteral, value: any): J.Literal {
private mapLiteral(node: ts.LiteralExpression | ts.TrueLiteral | ts.FalseLiteral | ts.NullLiteral | ts.Identifier, value: any): J.Literal {
return new J.Literal(
randomId(),
this.prefix(node),
Expand Down Expand Up @@ -469,6 +469,10 @@ export class JavaScriptParserVisitor {
}

visitIdentifier(node: ts.Identifier) {
if (node.text === 'undefined') {
// unsure why this appears as a ts.Identifier in the AST
return this.mapLiteral(node, undefined);
}
return this.mapIdentifier(node, node.text);
}

Expand Down
2 changes: 2 additions & 0 deletions openrewrite/src/javascript/typeMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export class JavaScriptTypeMapping {

if (type.flags === ts.TypeFlags.Null) {
return JavaType.Primitive.of(JavaType.PrimitiveKind.Null);
} else if (type.flags === ts.TypeFlags.Undefined) {
return JavaType.Primitive.of(JavaType.PrimitiveKind.None);
} else if (type.flags === ts.TypeFlags.Number) {
return JavaType.Primitive.of(JavaType.PrimitiveKind.Double);
} else if (type.flags === ts.TypeFlags.String) {
Expand Down
7 changes: 7 additions & 0 deletions openrewrite/test/javascript/parser/literal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ describe('identifier mapping', () => {
assertLiteralLst(sourceFile, 'null', JavaType.PrimitiveKind.Null);
}));
});
test('undefined', () => {
rewriteRunWithOptions(
{normalizeIndent: false},
typeScript('undefined', sourceFile => {
assertLiteralLst(sourceFile, 'undefined', JavaType.PrimitiveKind.None);
}));
});
test('regex', () => {
rewriteRunWithOptions(
{normalizeIndent: false},
Expand Down

0 comments on commit 9d4197d

Please sign in to comment.