Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,155 changes: 560 additions & 595 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
"ts-node": "^10.9.2",
"ts-patch": "^3.1.1",
"tsconfig-paths": "4.2.0",
"typescript": "^5.3.3",
"typescript": "^5.4.5",
"webpack": "^5.89.0",
"webpack-cli": "^5.1.4",
"webpack-merge": "^5.10.0",
Expand Down
4 changes: 2 additions & 2 deletions src/transformer/descriptor/enum/enumDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { RandomPropertyAccessor } from '../random/random';
import { IsTsAutoMockRandomEnabled } from '../../../options/random';
import {
createCall,
createNumericLiteral,
createExpressionForNegativeOrPositiveNumber,
createStringLiteral,
} from '../../../typescriptFactory/typescriptFactory';

Expand Down Expand Up @@ -34,7 +34,7 @@ function getEnumMemberValue(
typeChecker.getConstantValue(member) || defaultValue;

if (typeof value === 'number') {
return createNumericLiteral(value);
return createExpressionForNegativeOrPositiveNumber(value);
}

return createStringLiteral(value);
Expand Down
10 changes: 5 additions & 5 deletions src/transformer/descriptor/literal/literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { Scope } from '../../scope/scope';
import { core } from '../../core/core';
import { GetDescriptor } from '../descriptor';
import {
createExpressionForNegativeOrPositiveNumber,
createLiteral,
createNumericLiteral,
createStringLiteral,
} from '../../../typescriptFactory/typescriptFactory';

Expand All @@ -27,15 +27,15 @@ export function GetLiteralDescriptor(

function GetLiteralTokenDescriptor(
node: ts.LiteralTypeNode,
): ts.StringLiteral | ts.NumericLiteral {
): ts.StringLiteral | ts.Expression {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const nodeToken: any = node as any;

if (nodeToken.kind === core.ts.SyntaxKind.NumericLiteral) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
return createNumericLiteral(parseInt(nodeToken.text, 10));
return createExpressionForNegativeOrPositiveNumber(
parseInt(nodeToken.text, 10),
);
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
return createStringLiteral(nodeToken.text);
}
25 changes: 23 additions & 2 deletions src/typescriptFactory/typescriptFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import type {
ParameterDeclaration,
PostfixUnaryExpression,
PostfixUnaryOperator,
PrefixUnaryOperator,
PrefixUnaryExpression,
PrivateIdentifier,
PropertyAccessExpression,
Expand Down Expand Up @@ -92,6 +93,26 @@ export function createNumericLiteral(
return core.ts.factory.createNumericLiteral(value, numericLiteralFlags);
}

export function createExpressionForNegativeOrPositiveNumber(
value: number,
): Expression {
if (value < 0) {
return createPrefixUnaryExpression(
core.ts.SyntaxKind.MinusToken,
createNumericLiteral(Math.abs(value)),
);
}

return createNumericLiteral(value);
}

export function createPrefixUnaryExpression(
unaryOperator: PrefixUnaryOperator,
operand: Expression,
): Expression {
return core.ts.factory.createPrefixUnaryExpression(unaryOperator, operand);
}

export function createArrowFunction(
block: ts.ConciseBody,
parameter: ReadonlyArray<ts.ParameterDeclaration> = [],
Expand Down Expand Up @@ -298,13 +319,13 @@ export function createLogicalNot(operand: Expression): PrefixUnaryExpression {

export function createLiteral(
type: LiteralType,
): StringLiteral | NumericLiteral | BigIntLiteral {
): StringLiteral | Expression | BigIntLiteral {
if (typeof type.value === 'string') {
return createStringLiteral(type.value);
}

if (typeof type.value === 'number') {
return createNumericLiteral(type.value);
return createExpressionForNegativeOrPositiveNumber(type.value);
}

return core.ts.factory.createBigIntLiteral(type.value);
Expand Down
19 changes: 19 additions & 0 deletions test/transformer/descriptor/enum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe('for enum', () => {
Right = 1,
Left = 3,
}

interface Interface {
a: Direction;
b: DirectionAssign;
Expand Down Expand Up @@ -55,3 +56,21 @@ describe('for enum with constant computed values', () => {
expect(properties.a).toEqual(2);
});
});

describe('for enum with negative values', () => {
it('should assign the values', () => {
enum AssignmentWithNegatives {
Negative1 = -4,
Positive = 1,
Negative2 = -7,
}

interface Interface {
enum: AssignmentWithNegatives;
}

const properties: Interface = createMock<Interface>();

expect(properties.enum).toEqual(-4);
});
});
7 changes: 6 additions & 1 deletion test/transformer/descriptor/literal/literal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
TypeUnionTokenAllBoolean,
TypeUnionTokenNumber,
TypeUnionTokenSameBoolean,
TypeUnionWithNegatives,
} from '../utils/types/typeUnion';
import { getObjectKeyValues } from '../../utilities/getObjectKeyValues';

Expand All @@ -26,22 +27,26 @@ describe('for literal', () => {
describe('with a specific number', () => {
interface Interface {
a: 2;
b: -3;
}

it('should set null', () => {
it('should set the number', () => {
const properties: Interface = createMock<Interface>();
expect(properties.a).toBe(2);
expect(properties.b).toBe(-3);
});
});

describe('with import', () => {
interface Interface {
literal: TypeUnion;
literalWithNegatives: TypeUnionWithNegatives;
}

it('should set the first one', () => {
const properties: Interface = createMock<Interface>();
expect(properties.literal).toBe('1');
expect(properties.literalWithNegatives).toBe(-1);
});
});

Expand Down
1 change: 1 addition & 0 deletions test/transformer/descriptor/utils/types/typeUnion.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type TypeUnion = '1' | '2' | 'a' | 'b';
export type TypeUnionWithNegatives = -1 | -2 | 3 | 2;
export type TypeUnionToken = 'a' | 'b';
export type TypeUnionTokenNumber = 1 | 1;
export type TypeUnionTokenSameBoolean = true | true;
Expand Down