diff --git a/openrewrite/src/javascript/parser.ts b/openrewrite/src/javascript/parser.ts index 7c73822f..bc4239a8 100644 --- a/openrewrite/src/javascript/parser.ts +++ b/openrewrite/src/javascript/parser.ts @@ -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), @@ -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); } diff --git a/openrewrite/src/javascript/typeMapping.ts b/openrewrite/src/javascript/typeMapping.ts index 8852d47f..a0184d98 100644 --- a/openrewrite/src/javascript/typeMapping.ts +++ b/openrewrite/src/javascript/typeMapping.ts @@ -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) { diff --git a/openrewrite/test/javascript/parser/literal.test.ts b/openrewrite/test/javascript/parser/literal.test.ts index 198ba734..712f823e 100644 --- a/openrewrite/test/javascript/parser/literal.test.ts +++ b/openrewrite/test/javascript/parser/literal.test.ts @@ -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},