Skip to content

Commit

Permalink
style: Prevent use of any and tidy up types to match
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyJones committed Oct 25, 2022
1 parent b438da3 commit 150ac95
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 99 deletions.
79 changes: 43 additions & 36 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,38 +1,45 @@
{
"parser": "@typescript-eslint/parser",
"extends": [
"plugin:@typescript-eslint/recommended"
],
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
},
"rules": {
"curly": 2,
"indent": 0,
"no-console": 0,
"max-len": [
2,
{
"code": 200
}
],
"trailing-comma": 0,
"object-literal-sort-keys": 0,
"no-reference": 0,
"no-trailing-whitespace": 0,
"typedef-whitespace": 0,
"whitespace": 0,
"interface-name": 0,
"ordered-imports": 0,
"variable-name": 0,
"no-var-requires": 0,
"object-literal-shorthand": 0,
"no-empty": 0,
"max-classes-per-file": 0,
"prefer-const": 0,
"no-unused-expression": 0,
"forin": 0,
"object-literal-key-quotes": 0
}
"parser": "@typescript-eslint/parser",
"extends": ["plugin:@typescript-eslint/recommended"],
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
},
"rules": {
"curly": 2,
"indent": 0,
"no-console": 0,
"max-len": [
2,
{
"code": 200
}
],
"@typescript-eslint/no-explicit-any": "error",
"trailing-comma": 0,
"object-literal-sort-keys": 0,
"no-reference": 0,
"no-trailing-whitespace": 0,
"typedef-whitespace": 0,
"whitespace": 0,
"interface-name": 0,
"ordered-imports": 0,
"variable-name": 0,
"no-var-requires": 0,
"object-literal-shorthand": 0,
"no-empty": 0,
"max-classes-per-file": 0,
"prefer-const": 0,
"no-unused-expression": 0,
"forin": 0,
"object-literal-key-quotes": 0
},
"overrides": [
{
"files": ["*.spec.ts"],
"rules": {
"@typescript-eslint/no-explicit-any": "off"
}
}
]
}
8 changes: 2 additions & 6 deletions src/verifier/argumentMapper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,12 @@ export const setupVerification = (
switch (validation.status) {
case FnValidationStatus.FAIL:
logErrorAndThrow(
`the required ffi function '${fn}' failed validation with errors: ${
validation.messages || [].join(',')
}`
`the required ffi function '${fn}' failed validation with errors: ${validation.messages}`
);
break;
case FnValidationStatus.IGNORE:
logger.debug(
`the optional ffi function '${fn}' was not executed as it had non-fatal validation errors: ${
validation.messages || [].join(',')
}`
`the optional ffi function '${fn}' was not executed as it had non-fatal validation errors: ${validation.messages}`
);
break;
case FnValidationStatus.SUCCESS:
Expand Down
28 changes: 14 additions & 14 deletions src/verifier/argumentMapper/types.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import logger from '../../logger';
import { Ffi, FfiHandle } from '../../ffi/types';

export const deprecatedFunction = (_: any, property: string): boolean => {
export const deprecatedFunction = (_: unknown, property: string): boolean => {
logger.warn(`${property} is deprecated and no longer has any effect`);

return true;
};
import { Ffi, FfiHandle } from '../../ffi/types';

export type FnObject = {
[key: string]: (...args: any) => any;
type KeyedObject = {
[key: string]: unknown;
};

type FnArgumentMapping<O> = {
validateAndExecute: (
ffi: Ffi,
handle: FfiHandle,
options: O
) => FnValidationResult;
};

export type FnMapping<T extends FnObject, O> = {
export type FnMapping<T extends KeyedObject, O> = {
[Key in keyof T]: FnArgumentMapping<O>;
};

Expand All @@ -21,15 +29,7 @@ export enum FnValidationStatus {
FAIL = 2,
}

export type FnValidationResult = {
type FnValidationResult = {
status: FnValidationStatus;
messages?: string[];
};

export type FnArgumentMapping<O> = {
validateAndExecute: (
ffi: Ffi,
handle: FfiHandle,
options: O
) => FnValidationResult;
};
92 changes: 49 additions & 43 deletions src/verifier/validateOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {

export const deprecatedFunction =
() =>
(_: any, property: string): boolean => {
(_: unknown, property: string): boolean => {
logger.warn(`${property} is deprecated and no longer has any effect`);

return true;
Expand All @@ -19,7 +19,7 @@ export const deprecatedFunction =
export const deprecatedBy =
(preferredOption: string) =>
() =>
(_: any, property: string): boolean => {
(_: unknown, property: string): boolean => {
logger.warn(`${property} is deprecated, use ${preferredOption} instead`);

return true;
Expand All @@ -28,7 +28,7 @@ export const deprecatedBy =
export const incompatibleWith =
(keys: (keyof InternalPactVerifierOptions)[]) =>
(options: InternalPactVerifierOptions) =>
(_: any, property: string): boolean => {
(_: unknown, property: string): boolean => {
const incompatibilities = pick(options, keys);

if (Object.keys(incompatibilities).length > 0) {
Expand All @@ -46,7 +46,7 @@ export const incompatibleWith =
export const requires =
(keys: (keyof InternalPactVerifierOptions)[]) =>
(options: InternalPactVerifierOptions) =>
(_: any, property: string): boolean => {
(_: unknown, property: string): boolean => {
const required = pick(options, keys);

if (keys.length !== Object.keys(required).length) {
Expand All @@ -62,7 +62,7 @@ export const requires =
export const requiresOneOf =
(keys: (keyof InternalPactVerifierOptions)[]) =>
(options: InternalPactVerifierOptions) =>
(_: any, property: string): boolean => {
(_: unknown, property: string): boolean => {
const required = pick(options, keys);

if (Object.keys(required).length === 0) {
Expand All @@ -77,25 +77,34 @@ export const requiresOneOf =
return true;
};

export type AssertFunction = (a: any, ...args: any) => boolean;
type AssertFunction = (a: unknown, property: string) => boolean;

export const wrapCheckType = (fn: AssertFunction) => (): AssertFunction => fn;
const assertNonEmptyString =
(): AssertFunction => (a: unknown, property: string) =>
checkTypes.assert.nonEmptyString(a as string, property);

const assertBoolean = (): AssertFunction => (a: unknown, property: string) =>
checkTypes.assert.boolean(a as boolean, property);

const assertPositive = (): AssertFunction => (a: unknown, property: string) =>
checkTypes.assert.positive(a as number, property);

const LogLevels: LogLevel[] = ['debug', 'error', 'info', 'trace', 'warn'];

const logLevelValidator =
() =>
(l: LogLevel): boolean => {
if (LogLevels.includes(l.toLowerCase() as LogLevel)) {
l = l.toLowerCase() as LogLevel;
} else {
throw new Error(
`The logLevel '${l}' is not a valid logLevel. The valid options are: ${LogLevels.join(
', '
)}`
);
(l: unknown): boolean => {
if (typeof l === 'string') {
if (LogLevels.includes(l.toLowerCase() as LogLevel)) {
l = l.toLowerCase() as LogLevel;
return true;
}
}
return true;
throw new Error(
`The logLevel '${l}' is not a valid logLevel. The valid options are: ${LogLevels.join(
', '
)}`
);
};

const consumerVersionSelectorValidator =
Expand Down Expand Up @@ -188,19 +197,19 @@ export type ArgumentValidationRules<T> = {

export const validationRules: ArgumentValidationRules<InternalPactVerifierOptions> =
{
providerBaseUrl: [wrapCheckType(checkTypes.assert.nonEmptyString)],
buildUrl: [wrapCheckType(checkTypes.assert.nonEmptyString)],
providerBaseUrl: [assertNonEmptyString],
buildUrl: [assertNonEmptyString],
consumerVersionSelectors: [consumerVersionSelectorValidator],
consumerVersionTags: [consumerVersionTagsValidator],
customProviderHeaders: [customProviderHeadersValidator],
disableSslVerification: [wrapCheckType(checkTypes.assert.boolean)],
enablePending: [wrapCheckType(checkTypes.assert.boolean)],
disableSslVerification: [assertBoolean],
enablePending: [assertBoolean],
format: [deprecatedFunction],
includeWipPactsSince: [wrapCheckType(checkTypes.assert.nonEmptyString)],
provider: [wrapCheckType(checkTypes.assert.nonEmptyString)],
pactUrls: [wrapCheckType(checkTypes.assert.nonEmptyString)],
includeWipPactsSince: [assertNonEmptyString],
provider: [assertNonEmptyString],
pactUrls: [assertNonEmptyString],
pactBrokerUrl: [
wrapCheckType(checkTypes.assert.nonEmptyString),
assertNonEmptyString,
requires(['provider']),
requiresOneOf([
'pactUrls',
Expand All @@ -209,41 +218,38 @@ export const validationRules: ArgumentValidationRules<InternalPactVerifierOption
]),
],
pactBrokerUsername: [
wrapCheckType(checkTypes.assert.nonEmptyString),
assertNonEmptyString,
incompatibleWith(['pactBrokerToken']),
requires(['pactBrokerPassword']),
],
pactBrokerPassword: [
wrapCheckType(checkTypes.assert.nonEmptyString),
assertNonEmptyString,
incompatibleWith(['pactBrokerToken']),
requires(['pactBrokerUsername']),
],
pactBrokerToken: [
wrapCheckType(checkTypes.assert.nonEmptyString),
assertNonEmptyString,
incompatibleWith(['pactBrokerUsername', 'pactBrokerPassword']),
],
providerVersionTags: [wrapCheckType(checkTypes.assert.nonEmptyString)],
providerVersionTags: [assertNonEmptyString],
providerBranch: [
wrapCheckType(checkTypes.assert.nonEmptyString),
assertNonEmptyString,
deprecatedBy('providerVersionBranch'),
],
providerVersionBranch: [wrapCheckType(checkTypes.assert.nonEmptyString)],
providerStatesSetupUrl: [wrapCheckType(checkTypes.assert.nonEmptyString)],
providerStatesSetupTeardown: [wrapCheckType(checkTypes.assert.boolean)],
providerStatesSetupBody: [wrapCheckType(checkTypes.assert.boolean)],
publishVerificationResult: [
wrapCheckType(checkTypes.assert.boolean),
requires(['providerVersion']),
],
providerVersion: [wrapCheckType(checkTypes.assert.nonEmptyString)],
timeout: [wrapCheckType(checkTypes.assert.positive)],
providerVersionBranch: [assertNonEmptyString],
providerStatesSetupUrl: [assertNonEmptyString],
providerStatesSetupTeardown: [assertBoolean],
providerStatesSetupBody: [assertBoolean],
publishVerificationResult: [assertBoolean, requires(['providerVersion'])],
providerVersion: [assertNonEmptyString],
timeout: [assertPositive],
logLevel: [logLevelValidator],
out: [deprecatedFunction],
verbose: [deprecatedFunction],
monkeypatch: [deprecatedFunction],
logDir: [deprecatedFunction],
consumerFilters: [wrapCheckType(checkTypes.assert.nonEmptyString)],
failIfNoPactsFound: [wrapCheckType(checkTypes.assert.boolean)],
consumerFilters: [assertNonEmptyString],
failIfNoPactsFound: [assertBoolean],
};

export const validateOptions = (options: VerifierOptions): VerifierOptions => {
Expand All @@ -256,7 +262,7 @@ export const validateOptions = (options: VerifierOptions): VerifierOptions => {

// get type of parameter (if an array, we apply the rule to each item of the array instead)
if (Array.isArray(options[k])) {
(options[k] as Array<any>).map((item) => {
options[k].map((item: unknown) => {
rules.map((rule) => {
// rule(item) // If the messages aren't clear, we can do this
rule(options)(item, k);
Expand Down

0 comments on commit 150ac95

Please sign in to comment.