Skip to content

chore: add codely eslint linter #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 4, 2025
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
19 changes: 0 additions & 19 deletions .eslintrc.js

This file was deleted.

3 changes: 3 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import eslintConfigCodely from "eslint-config-codely";

export default [...eslintConfigCodely.ts];
8 changes: 4 additions & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
testMatch: ["**/tests/**/*.test.ts"],
transform: {
"\\.ts$": "ts-jest",
},
testMatch: ["**/tests/**/*.test.ts"],
transform: {
"\\.ts$": "ts-jest",
},
};
7,812 changes: 4,084 additions & 3,728 deletions package-lock.json

Large diffs are not rendered by default.

15 changes: 6 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
],
"scripts": {
"build": "tsc",
"lint": "eslint --ignore-path .gitignore . --ext .ts",
"lint": "eslint src tests eslint.config.mjs jest.config.js",
"lint:fix": "npm run lint -- --fix",
"test": "jest"
},
Expand All @@ -33,14 +33,11 @@
"homepage": "https://github.com/CodelyTV/typescript-primitives-type#readme",
"devDependencies": {
"@types/jest": "^27.5.2",
"@typescript-eslint/eslint-plugin": "^5.21.0",
"@typescript-eslint/parser": "^5.21.0",
"eslint": "^8.14.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jest": "^26.1.5",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-simple-import-sort": "^7.0.0",
"@typescript-eslint/eslint-plugin": "^8.15.0",
"@typescript-eslint/parser": "^8.15.0",
"eslint": "^8.56.0",
"eslint-config-codely": "^4.2.0",
"eslint-import-resolver-typescript": "^3.7.0",
"expect-type": "^0.13.0",
"jest": "^28.0.1",
"prettier": "^2.6.2",
Expand Down
30 changes: 15 additions & 15 deletions src/Primitives.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/ban-types */
/* eslint-disable @typescript-eslint/no-unsafe-function-type */
type Methods<T> = {
[P in keyof T]: T[P] extends Function ? P : never;
[P in keyof T]: T[P] extends Function ? P : never;
}[keyof T];

type MethodsAndProperties<T> = { [key in keyof T]: T[key] };
Expand All @@ -10,19 +10,19 @@ type Properties<T> = Omit<MethodsAndProperties<T>, Methods<T>>;
type PrimitiveTypes = string | number | boolean | Date | undefined | null;

type ValueObjectValue<T> = T extends PrimitiveTypes
? T
: T extends { value: infer U }
? U
: T extends Array<{ value: infer U }>
? U[]
: T extends Array<infer U>
? Array<ValueObjectValue<U>>
: T extends { [K in keyof Properties<T>]: unknown }
? { [K in keyof Properties<T>]: ValueObjectValue<Properties<T>[K]> }
: T extends unknown
? T
: never;
? T
: T extends { value: infer U }
? U
: T extends Array<{ value: infer U }>
? U[]
: T extends Array<infer U>
? Array<ValueObjectValue<U>>
: T extends { [K in keyof Properties<T>]: unknown }
? { [K in keyof Properties<T>]: ValueObjectValue<Properties<T>[K]> }
: T extends unknown
? T
: never;

export type Primitives<T> = {
[key in keyof Properties<T>]: ValueObjectValue<T[key]>;
[key in keyof Properties<T>]: ValueObjectValue<T[key]>;
};
6 changes: 0 additions & 6 deletions tests/.eslintrc

This file was deleted.

24 changes: 12 additions & 12 deletions tests/Address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import { City } from "./City";
import { Street } from "./Street";

export class Address {
constructor(
readonly street: Street,
readonly city: City,
readonly number: number
) {}
constructor(
readonly street: Street,
readonly city: City,
readonly number: number,
) {}

toPrimitives(): Primitives<Address> {
return {
street: this.street.value,
city: this.city.value,
number: this.number,
};
}
toPrimitives(): Primitives<Address> {
return {
street: this.street.value,
city: this.city.value,
number: this.number,
};
}
}
20 changes: 10 additions & 10 deletions tests/Course.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { Primitives } from "../src/Primitives";
import { Primitives } from "../src";
import { CourseId } from "./CourseId";

export class Course {
constructor(readonly courseId: CourseId) {}
constructor(readonly courseId: CourseId) {}

toPrimitives(): Primitives<Course> {
return {
courseId: this.courseId.value,
};
}
toPrimitives(): Primitives<Course> {
return {
courseId: this.courseId.value,
};
}

thisFunctionShouldNotBeIncludedInTheToPrimitives(): boolean {
return true;
}
thisFunctionShouldNotBeIncludedInTheToPrimitives(): boolean {
return true;
}
}
14 changes: 7 additions & 7 deletions tests/DeliveryInfo.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Primitives } from "../src/Primitives";
import { Primitives } from "../src";
import { Address } from "./Address";

export class DeliveryInfo {
constructor(readonly addresses: Address[]) {}
constructor(readonly addresses: Address[]) {}

toPrimitives(): Primitives<DeliveryInfo> {
return {
addresses: this.addresses.map((address) => address.toPrimitives()),
};
}
toPrimitives(): Primitives<DeliveryInfo> {
return {
addresses: this.addresses.map((address) => address.toPrimitives()),
};
}
}
14 changes: 7 additions & 7 deletions tests/Learner.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Primitives } from "../src/Primitives";
import { Primitives } from "../src";
import { CourseId } from "./CourseId";

export class Learner {
constructor(readonly enrolledCourses: CourseId[]) {}
constructor(readonly enrolledCourses: CourseId[]) {}

toPrimitives(): Primitives<Learner> {
return {
enrolledCourses: this.enrolledCourses.map((course) => course.value),
};
}
toPrimitives(): Primitives<Learner> {
return {
enrolledCourses: this.enrolledCourses.map((course) => course.value),
};
}
}
12 changes: 6 additions & 6 deletions tests/Metadata.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Primitives } from "../src";

export class Metadata {
constructor(public readonly data: Record<string, unknown>) {}
constructor(public readonly data: Record<string, unknown>) {}

toPrimitives(): Primitives<Metadata> {
return {
data: this.data,
};
}
toPrimitives(): Primitives<Metadata> {
return {
data: this.data,
};
}
}
120 changes: 60 additions & 60 deletions tests/Primitives.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,84 +10,84 @@ import { User } from "./User";
import { Video } from "./Video";

describe("Primitives", () => {
it("should ensure to only return primitive properties excluding methods", () => {
type actualPrimitives = Primitives<Course>;
it("should ensure to only return primitive properties excluding methods", () => {
type actualPrimitives = Primitives<Course>;

type expectedPrimitives = {
readonly courseId: string;
};
type expectedPrimitives = {
readonly courseId: string;
};

expectTypeOf<actualPrimitives>().toEqualTypeOf<expectedPrimitives>();
});
expectTypeOf<actualPrimitives>().toEqualTypeOf<expectedPrimitives>();
});

it("should get primitive array type from value object array", () => {
type actualPrimitives = Primitives<Learner>;
it("should get primitive array type from value object array", () => {
type actualPrimitives = Primitives<Learner>;

type expectedPrimitives = {
readonly enrolledCourses: string[];
};
type expectedPrimitives = {
readonly enrolledCourses: string[];
};

expectTypeOf<actualPrimitives>().toEqualTypeOf<expectedPrimitives>();
});
expectTypeOf<actualPrimitives>().toEqualTypeOf<expectedPrimitives>();
});

it("should generate nested primitive object", () => {
type actualPrimitives = Primitives<User>;
it("should generate nested primitive object", () => {
type actualPrimitives = Primitives<User>;

type expectedPrimitives = {
readonly address: {
readonly city: string;
readonly street: string;
readonly number: number;
};
};
type expectedPrimitives = {
readonly address: {
readonly city: string;
readonly street: string;
readonly number: number;
};
};

expectTypeOf<actualPrimitives>().toEqualTypeOf<expectedPrimitives>();
});
expectTypeOf<actualPrimitives>().toEqualTypeOf<expectedPrimitives>();
});

it("should generate nested primitive type from array of value objects prop", () => {
type actualPrimitives = Primitives<DeliveryInfo>;
it("should generate nested primitive type from array of value objects prop", () => {
type actualPrimitives = Primitives<DeliveryInfo>;

type expectedPrimitives = {
readonly addresses: {
readonly city: string;
readonly street: string;
readonly number: number;
}[];
};
type expectedPrimitives = {
readonly addresses: {
readonly city: string;
readonly street: string;
readonly number: number;
}[];
};

expectTypeOf<actualPrimitives>().toEqualTypeOf<expectedPrimitives>();
});
expectTypeOf<actualPrimitives>().toEqualTypeOf<expectedPrimitives>();
});

it("should get primitive type in case it is not a value object", () => {
type actualPrimitives = Primitives<Product>;
it("should get primitive type in case it is not a value object", () => {
type actualPrimitives = Primitives<Product>;

type expectedPrimitives = {
readonly active: boolean;
readonly createdAt: Date;
};
type expectedPrimitives = {
readonly active: boolean;
readonly createdAt: Date;
};

expectTypeOf<actualPrimitives>().toEqualTypeOf<expectedPrimitives>();
});
expectTypeOf<actualPrimitives>().toEqualTypeOf<expectedPrimitives>();
});

it("should infer the optional properties", () => {
type actualPrimitives = Primitives<Video>;
it("should infer the optional properties", () => {
type actualPrimitives = Primitives<Video>;

type expectedPrimitives = {
readonly id: string;
readonly name: string | undefined;
readonly duration: number | undefined;
};
type expectedPrimitives = {
readonly id: string;
readonly name: string | undefined;
readonly duration: number | undefined;
};

expectTypeOf<actualPrimitives>().toEqualTypeOf<expectedPrimitives>();
});
expectTypeOf<actualPrimitives>().toEqualTypeOf<expectedPrimitives>();
});

it("should infer properties with unknown type", () => {
type actualPrimitives = Primitives<Metadata>;
it("should infer properties with unknown type", () => {
type actualPrimitives = Primitives<Metadata>;

type expectedPrimitives = {
readonly data: Record<string, unknown>;
};
type expectedPrimitives = {
readonly data: Record<string, unknown>;
};

expectTypeOf<actualPrimitives>().toEqualTypeOf<expectedPrimitives>();
});
expectTypeOf<actualPrimitives>().toEqualTypeOf<expectedPrimitives>();
});
});
20 changes: 10 additions & 10 deletions tests/Product.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Primitives } from "../src";

export class Product {
constructor(
public readonly active: boolean,
public readonly createdAt: Date
) {}
constructor(
public readonly active: boolean,
public readonly createdAt: Date,
) {}

toPrimitives(): Primitives<Product> {
return {
active: this.active,
createdAt: this.createdAt,
};
}
toPrimitives(): Primitives<Product> {
return {
active: this.active,
createdAt: this.createdAt,
};
}
}
Loading