Skip to content

Commit

Permalink
A bit more type mapping support
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Sep 29, 2024
1 parent 12ff035 commit 3fc2553
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
6 changes: 5 additions & 1 deletion openrewrite/src/javascript/typeMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,18 @@ export class JavaScriptTypeMapping {
private createType(node: ts.Node, type: ts.Type, signature: string): JavaType {
if (type.isLiteral()) {
if (type.isNumberLiteral()) {
return JavaType.Primitive.of(JavaType.PrimitiveKind.Long);
return JavaType.Primitive.of(JavaType.PrimitiveKind.Double);
} else if (type.isStringLiteral()) {
return JavaType.Primitive.of(JavaType.PrimitiveKind.String);
}
}

if (type.flags === ts.TypeFlags.Null) {
return JavaType.Primitive.of(JavaType.PrimitiveKind.Null);
} else if (type.flags === ts.TypeFlags.Number) {
return JavaType.Primitive.of(JavaType.PrimitiveKind.Double);
} else if (type.flags === ts.TypeFlags.String) {
return JavaType.Primitive.of(JavaType.PrimitiveKind.String);
} else if (type.flags === ts.TypeFlags.BooleanLiteral) {
return JavaType.Primitive.of(JavaType.PrimitiveKind.Boolean);
} else if (type.flags === ts.TypeFlags.Void) {
Expand Down
23 changes: 22 additions & 1 deletion openrewrite/test/javascript/parser/binary.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import * as J from "../../../dist/src/java/tree";
import {JavaType} from "../../../dist/src/java";
import * as JS from "../../../dist/src/javascript";
import {connect, disconnect, rewriteRun, typeScript} from '../testHarness';

describe('arithmetic operator mapping', () => {
Expand All @@ -7,7 +10,25 @@ describe('arithmetic operator mapping', () => {
test('plus', () => {
rewriteRun(
//language=typescript
typeScript('1 + 2')
typeScript(
'1 + 2',
cu => {
const binary = <J.Binary>(<JS.ExpressionStatement>cu.statements[0]).expression;
expect((<JavaType.Primitive>binary.type).kind).toBe(JavaType.PrimitiveKind.Double);
}
)
);
});
test('concat', () => {
rewriteRun(
//language=typescript
typeScript(
'"1" + 2',
cu => {
const binary = <J.Binary>(<JS.ExpressionStatement>cu.statements[0]).expression;
expect((<JavaType.Primitive>binary.type).kind).toBe(JavaType.PrimitiveKind.String);
}
)
);
});

Expand Down
2 changes: 1 addition & 1 deletion openrewrite/test/javascript/parser/literal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('identifier mapping', () => {
rewriteRunWithOptions(
{normalizeIndent: false},
typeScript(' 1', sourceFile => {
assertLiteralLst(sourceFile, '1', JavaType.PrimitiveKind.Long);
assertLiteralLst(sourceFile, '1', JavaType.PrimitiveKind.Double);
}));
});
test('string', () => {
Expand Down

0 comments on commit 3fc2553

Please sign in to comment.