Skip to content

Commit

Permalink
Merge pull request #566 from maximebiloe/fix-negative-number
Browse files Browse the repository at this point in the history
fix: negative number
  • Loading branch information
horiuchi committed Jun 11, 2024
2 parents 6fa1e9a + 8cbcca6 commit e0a64b5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/core/astBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ export function buildStringLiteralTypeNode(s: string): ts.LiteralTypeNode {
return ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(s));
}
export function buildNumericLiteralTypeNode(n: string): ts.LiteralTypeNode {
if (!isNaN(parseFloat(n)) && parseFloat(n) < 0) {
return ts.factory.createLiteralTypeNode(
ts.factory.createPrefixUnaryExpression(
ts.SyntaxKind.MinusToken,
ts.factory.createNumericLiteral(Math.abs(parseFloat(n))),
),
);
}

return ts.factory.createLiteralTypeNode(ts.factory.createNumericLiteral(n));
}
export function buildBooleanLiteralTypeNode(b: boolean): ts.LiteralTypeNode {
Expand Down
25 changes: 25 additions & 0 deletions test/simple_schema_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,31 @@ describe('simple schema test', () => {
numbers?: "001" | "002" | "003" | "004" | "005" | "006" | "007" | "008" | "009" | "010";
}
}
`;
assert.strictEqual(result, expected, result);
});
it('positive and negative number type enum schema', async () => {
const schema: JsonSchemaDraft04.Schema = {
id: '/test/positive_negative_number_type',
type: 'object',
properties: {
numbers: {
enum: [1, -1],
example: '1',
},
},
};
const result = await dtsgenerator({ contents: [parseSchema(schema)] });

const expected = `declare namespace Test {
export interface PositiveNegativeNumberType {
/**
* example:
* 1
*/
numbers?: 1 | -1;
}
}
`;
assert.strictEqual(result, expected, result);
});
Expand Down

0 comments on commit e0a64b5

Please sign in to comment.