Skip to content

Commit

Permalink
Linting & CI check (#11)
Browse files Browse the repository at this point in the history
* configure linting and code formatting

* add CI checks
  • Loading branch information
mieszkosabo committed Apr 15, 2024
1 parent 6988b47 commit b5e4d8d
Show file tree
Hide file tree
Showing 16 changed files with 162 additions and 89 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
node_modueles
27 changes: 27 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"env": {
"browser": true,
"es2021": true,
"node": true
},
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
"overrides": [],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": ["@typescript-eslint"],
"rules": {
"@typescript-eslint/no-explicit-any": "off",
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": [
"error",
{
"argsIgnorePattern": "^_",
"varsIgnorePattern": "^_",
"caughtErrorsIgnorePattern": "^_"
}
]
}
}
29 changes: 29 additions & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Checks

on:
pull_request:
branches:
- main

jobs:
checks:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Bun
uses: oven-sh/setup-bun@v1

- name: Install dependencies
run: bun install

- name: Run check:format
run: bun check:format

- name: Run check:lint
run: bun check:lint

- name: Run check:types
run: bun check:types
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"tabWidth": 2,
"useTabs": false
}
Binary file modified bun.lockb
Binary file not shown.
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
},
"scripts": {
"build": "tsup",
"prettier:write": "prettier --write .",
"check:lint": "eslint .",
"check:format": "prettier --check .",
"check:types": "tsc --noEmit --pretty"
},
"keywords": [],
Expand All @@ -29,11 +32,18 @@
],
"license": "MIT",
"devDependencies": {
"@eslint/js": "^9.0.0",
"@swc-node/register": "^1.6.6",
"@swc/core": "^1.3.76",
"@typescript-eslint/eslint-plugin": "latest",
"@typescript-eslint/parser": "latest",
"bun-types": "^1.0.3",
"eslint": "8.57.0",
"globals": "^15.0.0",
"hotscript": "^1.0.13",
"prettier": "^3.2.5",
"tsup": "^7.2.0",
"typescript": "5.3.3"
"typescript": "5.3.3",
"typescript-eslint": "^7.6.0"
}
}
2 changes: 1 addition & 1 deletion src/arrays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const array = (innerValidator?: Validator<any, any>) => {
chain.parse(el);
} else {
throw new Error(
`No inner validator for array. Make sure to either call .array() after some validator or do c.array(c.someValidator())`
`No inner validator for array. Make sure to either call .array() after some validator or do c.array(c.someValidator())`,
);
}
});
Expand Down
36 changes: 18 additions & 18 deletions src/correttore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Validator } from "./shared.types";
type GetChainableValidators<
OutputType,
Validators extends AnyFunReturning<Validator<any, any>>[],
usedFeatures = never
usedFeatures = never,
> = {
[K in Exclude<
keyof PickByValue<Validators, AnyFunReturning<Validator<OutputType, any>>>,
Expand All @@ -23,7 +23,7 @@ type GetChainableValidators<
usedFeatures | K
> & {
parse: (
arg: unknown
arg: unknown,
) => ReturnType<Validators[K]>["$outputType"] extends Fn
? Apply<ReturnType<Validators[K]>["$outputType"], [OutputType]>
: ReturnType<Validators[K]>["$outputType"];
Expand All @@ -50,7 +50,7 @@ type GetChainableValidators<
// however we need to substitute that arg with `unknown` for the library consumer,
// because in their context they should be able to start the chain with any type.
parse: (
arg: unknown
arg: unknown,
) => ReturnType<Validators[K]>["$outputType"] extends Fn
? Apply<
ReturnType<Validators[K]>["$outputType"],
Expand Down Expand Up @@ -83,7 +83,7 @@ const doesExtend = (A: string, B: string) => {

const callIfFun = (
maybeFn: string | AnyFunReturning<string>,
arg: any
arg: any,
): string => {
if (typeof maybeFn === "function") {
return maybeFn(arg);
Expand All @@ -95,7 +95,7 @@ const callIfFun = (
const createParserProxy = (
validators: AnyFunReturning<Validator<any, any>>[],
validatorsChain: Validator<any, any>[],
outputType: string
outputType: string,
): any => {
return new Proxy(
{},
Expand All @@ -113,10 +113,10 @@ const createParserProxy = (
};
}
const applicableValidators = validators.filter((v) =>
doesExtend(outputType, v().$inputType)
doesExtend(outputType, v().$inputType),
);
const validatorIdx = applicableValidators.findIndex(
(v) => v().name === key
(v) => v().name === key,
);

const isNonCallable = applicableValidators[validatorIdx]!().nonCallable;
Expand All @@ -133,8 +133,8 @@ const createParserProxy = (
[...chain, validator],
callIfFun(
applicableValidators[validatorIdx]().$outputType,
outputType
)
outputType,
),
);
} else {
return (args: any) => {
Expand All @@ -148,32 +148,32 @@ const createParserProxy = (
[...chain, validator],
callIfFun(
applicableValidators[validatorIdx](args).$outputType,
outputType
)
outputType,
),
);
};
}
} else {
throw new Error(`Unknown parser ${key as string}`);
}
},
}
},
);
};

export const initCorrettore = <
const Validators extends AnyFunReturning<Validator<any, any>>[]
const Validators extends AnyFunReturning<Validator<any, any>>[],
>(
validators: Validators
validators: Validators,
): GetChainableValidators<unknown, Validators> => {
return new Proxy({} as GetChainableValidators<unknown, Validators>, {
get(_target, key) {
// the base validators (ones that can be used from `c` variable) take "unknown" as their input
const applicableValidators = validators.filter((v) =>
doesExtend("root", v().$inputType)
doesExtend("root", v().$inputType),
);
const validatorIdx = applicableValidators.findIndex(
(v) => v().name === key
(v) => v().name === key,
);

const isNonCallable = applicableValidators[validatorIdx]!().nonCallable;
Expand All @@ -183,14 +183,14 @@ export const initCorrettore = <
return createParserProxy(
validators,
[applicableValidators[validatorIdx]!()],
applicableValidators[validatorIdx]!().$outputType
applicableValidators[validatorIdx]!().$outputType,
);
} else {
return (args: any) => {
return createParserProxy(
validators,
[applicableValidators[validatorIdx]!(args)],
applicableValidators[validatorIdx]!().$outputType
applicableValidators[validatorIdx]!().$outputType,
);
};
}
Expand Down
20 changes: 12 additions & 8 deletions src/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ interface ObjectSchema extends Fn {
}

export const object = <
const Schema extends Record<string, Validator<any, any>>
const Schema extends Record<string, Validator<any, any>>,
>(
schema: Schema
schema: Schema,
) => ({
name: "object" as const,
$inputType: "root" as unknown,
Expand All @@ -20,7 +20,7 @@ export const object = <
throw new Error(`${arg} is not an object.`);
}

const result: Record<string, any> = {}
const result: Record<string, any> = {};
for (const k of Object.keys(schema)) {
if (k in arg) {
result[k] = schema[k].parse((arg as any)[k]);
Expand All @@ -43,8 +43,8 @@ type FieldsWithUndefined<T> = Exclude<
type Expand<T> = T extends (...args: infer A) => infer R
? (...args: Expand<A>) => Expand<R>
: T extends infer O
? { [K in keyof O]: O[K] }
: never;
? { [K in keyof O]: O[K] }
: never;

type MakeFieldsWithUndefinedOptional<T> = Expand<
{
Expand Down Expand Up @@ -80,7 +80,7 @@ export const passthrough = () => {
}
ctx.chain.parse(arg);

const result = {}
const result = {};
Object.assign(result, arg);
return result;
},
Expand Down Expand Up @@ -112,9 +112,13 @@ export const strict = () => {

const parsed = ctx.chain.parse(arg);
const recognizedKeys = Object.keys(parsed);
const unrecognizedKeys = Object.keys(arg).filter((k) => !recognizedKeys.includes(k));
const unrecognizedKeys = Object.keys(arg).filter(
(k) => !recognizedKeys.includes(k),
);
if (unrecognizedKeys.length > 0) {
throw new Error(`Unrecognized keys: ${unrecognizedKeys.map(k => `'${k}'`).join(", ")}`);
throw new Error(
`Unrecognized keys: ${unrecognizedKeys.map((k) => `'${k}'`).join(", ")}`,
);
}
return parsed;
},
Expand Down
4 changes: 2 additions & 2 deletions src/other.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const nullable = (innerValidator?: Validator<any, any>) => {
chain.parse(arg);
} else {
throw new Error(
`No inner validator for array. Make sure to either call .array() after some validator or do c.array(c.someValidator())`
`No inner validator for array. Make sure to either call .array() after some validator or do c.array(c.someValidator())`,
);
}

Expand Down Expand Up @@ -70,7 +70,7 @@ export const optional = (innerValidator?: Validator<any, any>) => {
chain.parse(arg);
} else {
throw new Error(
`No inner validator for array. Make sure to either call .array() after some validator or do c.array(c.someValidator())`
`No inner validator for array. Make sure to either call .array() after some validator or do c.array(c.someValidator())`,
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/primitives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export const neverType = () => ({
$outputType: "never" as unknown as never,
parse: (arg: unknown) => {
throw new Error(
`Value of type ${typeof arg} cannot be assigned to type 'never'.`
`Value of type ${typeof arg} cannot be assigned to type 'never'.`,
);
},
});
Expand Down
4 changes: 2 additions & 2 deletions src/sets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface SetType extends Fn {
}

export const set = <const Schema extends Validator<any, any>>(
chain: Schema
chain: Schema,
) => ({
name: "set" as const,
$inputType: "root" as unknown as any,
Expand All @@ -19,7 +19,7 @@ export const set = <const Schema extends Validator<any, any>>(
chain.parse(el);
} else {
throw new Error(
`No inner validator for set. Make sure to do c.set(c.someValidator())`
`No inner validator for set. Make sure to do c.set(c.someValidator())`,
);
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const length = (exactLength: number) => ({
parse: (arg: string) => {
if (arg.length !== exactLength)
throw new Error(
`${arg} does not have the expected length of ${exactLength}.`
`${arg} does not have the expected length of ${exactLength}.`,
);
return arg;
},
Expand Down
9 changes: 4 additions & 5 deletions tests/helpers.types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export type Expect<T extends true> = T;
export type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <
T
>() => T extends Y ? 1 : 2
? true
: false;
export type Equal<X, Y> =
(<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2
? true
: false;
Loading

0 comments on commit b5e4d8d

Please sign in to comment.