-
EnvironmentNode version: v19.6.1 What parser are you using?TypeScript Parser (@typescript-eslint/parser) What did you do?Configuration
import { deepEquality } from '@santi100/equal-lib';
function indexOf(arr: any[], item: any) {
for (const idx in arr) {
if (deepEquality(arr[idx], item)) return arr[idx];
}
return -1;
}
export class AssertionError<E = unknown, A = unknown>
extends Error
implements AssertOptionalParams<E, A>
{
/**
* Expected value for the assertion.
*/
readonly expected: E;
/**
* Received value for the assertion.
*/
readonly actual: A;
/**
* Optional operator used for the assertion.
*/
readonly operator?: string;
constructor(expected: E, actual: A, operator?: string) {
super(
`Assertion failed! Expected ${expected}. Got ${actual} when using operator ${operator}.`
);
this.expected = expected;
this.actual = actual;
this.operator = operator;
}
}
/**
* Asserts that `condition` is truthy.
* Throws an {@link AssertionError} otherwise.
*
* @param {boolean} condition A boolean condition to assert.
* @param {AssertOptionalParams} errorParams Parameters for the {@link AssertionError} that will be
* thrown in case the condition is falsy.
* @param {unknown} errorParams.expected Expected value for the assertion.
* @param {unknown} errorParams.actual Received value for the assertion.
* @param {string?} errorParams.operator Optional operator used for the assertion.
* @returns {void} Nothing.
*/
export function assert(
condition: boolean,
errorParams: AssertOptionalParams<true, boolean> = {
expected: true,
actual: false,
operator: '==='
}
): void {
const { expected, actual, operator } = errorParams;
if (!condition) throw new AssertionError(expected, actual, operator);
}
/**
* Asserts that the type of `val` is `expectedType`.
* Throws an {@link AssertionError} otherwise.
*
* @param val An expression whose type is to be asserted.
* @param expectedType The expected type. Must be one of {@link Type}.
* @deprecated Use {@link assertTypeOf} instead.
*/
export function assertType<T = unknown>(val: T, expectedType: Type): void {
const wrongType = typeof val !== expectedType;
if (wrongType) throw new AssertionError(expectedType, typeof val, 'typeof');
}
export type Type =
| 'string'
| 'number'
| 'bigint'
| 'boolean'
| 'symbol'
| 'undefined'
| 'object'
| 'function';
/**
* The shape of the assertion options.
*/
export interface AssertOptionalParams<E, A> {
/**
* Expected value for the assertion.
*/
readonly expected: E;
/**
* Received value for the assertion.
*/
readonly actual: A;
/**
* Optional operator used for the assertion.
*/
readonly operator?: string;
}
/**
* Asserts that the type of `arg` is `expectedType`. Throws a `TypeError` otherwise.
*
* @param arg An expression whose type is to be asserted.
* @param expectedType The expected type. Must be one of {@link Type}.
* @param name An optional expression name to be put in the `TypeError`'s message. Defaults to "arg".
*/
export function assertTypeOf(arg: any, expectedType: Type, name = 'arg') {
const TYPES = [
'string',
'number',
'bigint',
'boolean',
'symbol',
'undefined',
'object',
'function'
];
assertOneOf(expectedType, 'expectedType', TYPES);
if (typeof arg !== expectedType)
throw new TypeError(
`"${name}" must be of type "${expectedType}". Got "${arg}" of type "${typeof arg}".`
);
}
/**
* Asserts `arg` is one of `choices`. Throws a `TypeError` otherwise.
*
* @param arg The value that's expected to be included within `choices`.
* @param name An expression name to be put in the `TypeError`'s message.
* @param choices An array containing the posible values `arg` should have in order for an error not
* to be thrown.
* @param shallow Whether or not to use shallow equality (default deep equality is powered by
`@santi100/equal-lib` 😉).
*/
export function assertOneOf<T = unknown>(arg: any, name: string, choices: T[], shallow = false) {
if (shallow ? choices.indexOf(arg) : indexOf(choices, arg) === -1)
throw new TypeError(
`"${name}" must be one of "${choices.join(
', '
)}". Got "${arg}" of type "${typeof arg}".`
);
}
function __isInteger(n: number) {
return n - Math.floor(n) <= 0;
}
/**
* Asserts `arg` is an integer. Throws a `TypeError` otherwise.
*
* @param arg Any number.
* @param name An optional expression name to be put in the `TypeError`'s message. Defaults to "arg".
*/
export function assertInteger(arg: number, name = 'arg') {
if (!__isInteger(arg))
throw new TypeError(
`"${name}" must be an integer. Got "${arg}" of type "${typeof arg}".`
);
}
/**
* Asserts `arg` is bigger than or equal than `min`. Throws a `RangeError` otherwise.
*
* @param arg Any value.
* @param name An expression name to be put in the `TypeError`'s message.
* @param min The minimum value for `arg`.
*/
export function assertMin(arg: any, name: string, min: any) {
if (arg < min)
throw new RangeError(
`"${name}" must be bigger than or equal to ${min}. Got "${arg}" of type "${typeof arg}".`
);
}
/**
* Asserts `arg` is smaller or equal than `max`. Throws a `RangeError` otherwise.
*
* @param arg Any value.
* @param name An expression name to be put in the `TypeError`'s message.
* @param max The maximum value for `arg`.
*/
export function assertMax(arg: any, name: string, max: any) {
if (arg > max)
throw new RangeError(
`"${name}" must be smaller than or equal to ${max}. Got "${arg}" of type "${typeof arg}".`
);
}
/**
* Asserts `arg` is between `min + 1` and `max + 1` (inclusive). Throws a `RangeError` otherwise.
*
* @param arg Any value.
* @param name An expression name to be put in the `TypeError`'s message.
* @param min The minimum value for `arg`.
* @param max The maximum value for `arg`.
*/
export function assertRange(arg: any, name: string, min: any, max: any) {
if (arg > max || arg < min)
throw new RangeError(
`"${name}" must be smaller than ${max} and bigger than ${min}. Got "${arg}" of type "${typeof arg}".`
);
}
/**
* Asserts `arg` is an Array. Throws a `TypeError` otherwise.
*
* @param arg Any value.
* @param name An optional expression name to be put in the `TypeError`'s message. Defaults to "arg".
*/
export function assertArray(arg: any, name = 'arg') {
if (!(arg instanceof Array))
throw new TypeError(
`"${name}" must be an Array. Got "${arg}" of type "${typeof arg}".`
);
}
/**
* Asserts `arg` is an instance of `clas`. Throws a `TypeError` otherwise.
*
* @param arg An object whose class is to be asserted to be `clas`.
* @param clas Any valid constructor.
* @param name An optional expression name to be put in the `TypeError`'s message. Defaults to "arg".
* @since 1.0.8
*/
export function assertInstanceOf<T = any>(
arg: any,
clas: new (...args: any[]) => T,
name = 'arg'
) {
try {
new clas();
} catch (_) {
throw new TypeError(
`"clas" must be a valid constructor. Got ${clas} of type ${typeof clas}.`
);
}
if (!(arg instanceof clas))
throw new TypeError(
`${name} must be an instance of ${
clas.name
}. Got ${arg} of type ${typeof arg}.`
);
}
/**
* Asserts `arg` is smaller than `max`. Throws a `TypeError` otherwise.
*
* @param arg The value whose value is to be asserted to be smaller than `max`.
* @param max The maximum value `arg` is allowed to have.
* @param name An optional expression name to be put in the `TypeError`'s message. Defaults to "arg".
* @since 1.0.8
*/
export function assertExclusiveMax(arg: any, max: any, name = 'arg') {
if (arg >= max)
throw new TypeError(
`"${name}" must be smaller than ${max}. Got ${arg} of type ${typeof arg}.`
);
}
/**
* Asserts `arg` is bigger than `min`. Throws a `TypeError` otherwise.
*
* @param arg The value whose value is to be asserted to be bigger than `max`.
* @param min The minimum value `arg` is allowed to have.
* @param name An optional expression name to be put in the `TypeError`'s message. Defaults to "arg".
* @since 1.0.8
*/
export function assertExclusiveMin(arg: any, min: any, name = 'arg') {
if (arg <= min)
throw new TypeError(
`"${name}" must be bigger than ${min}. Got ${arg} of type ${typeof arg}.`
);
} What did you expect to happen?I expected ESLint to run correctly and lint through my source code. What actually happened?I got this error:
Link to Minimal Reproducible Examplehttps://github.com/santi100a/assertion-lib Participation
Additional commentsNo response |
Beta Was this translation helpful? Give feedback.
Replies: 13 comments 50 replies
-
Hi @santi100a, thanks for the issue. I wasn't able to reproduce the error you are getting. Here are the steps I followed (with output): > yarn install
# script from package.json
> yarn lint
/Users/joelmathew/Desktop/assertion-lib/src/index.ts
3:23 warning Unexpected any. Specify a different type @typescript-eslint/no-explicit-any
3:36 warning Unexpected any. Specify a different type @typescript-eslint/no-explicit-any
104:35 warning Unexpected any. Specify a different type @typescript-eslint/no-explicit-any
131:47 warning Unexpected any. Specify a different type @typescript-eslint/no-explicit-any
161:32 warning Unexpected any. Specify a different type @typescript-eslint/no-explicit-any
161:56 warning Unexpected any. Specify a different type @typescript-eslint/no-explicit-any
176:32 warning Unexpected any. Specify a different type @typescript-eslint/no-explicit-any
176:56 warning Unexpected any. Specify a different type @typescript-eslint/no-explicit-any
190:34 warning Unexpected any. Specify a different type @typescript-eslint/no-explicit-any
190:58 warning Unexpected any. Specify a different type @typescript-eslint/no-explicit-any
190:68 warning Unexpected any. Specify a different type @typescript-eslint/no-explicit-any
202:34 warning Unexpected any. Specify a different type @typescript-eslint/no-explicit-any
216:38 warning Unexpected any. Specify a different type @typescript-eslint/no-explicit-any
217:7 warning Unexpected any. Specify a different type @typescript-eslint/no-explicit-any
218:22 warning Unexpected any. Specify a different type @typescript-eslint/no-explicit-any
243:41 warning Unexpected any. Specify a different type @typescript-eslint/no-explicit-any
243:51 warning Unexpected any. Specify a different type @typescript-eslint/no-explicit-any
257:41 warning Unexpected any. Specify a different type @typescript-eslint/no-explicit-any
257:51 warning Unexpected any. Specify a different type @typescript-eslint/no-explicit-any
✖ 19 problems (0 errors, 19 warnings) ESLint is working as expected. It could be an issue with your Thanks for using ESLint. |
Beta Was this translation helpful? Give feedback.
-
Got the error too, when using latest versions at this day :
|
Beta Was this translation helpful? Give feedback.
-
Having this too. Dang. What works in one environment wont work in another using the same version of node.
Worked which is absolutely not a good solve. Also breaks once you intstall a new pkg without regenerating the lock. Why is this affecting yarn repos? Can we get more information on the actual bug here? I can't get anything to work on v18 unless I do this everywhere. Willing to lend a hand with a PR. Update: long term the best solve really is to just use npm. |
Beta Was this translation helpful? Give feedback.
-
To clarify, ESLint requires strip-ansi v6: Line 99 in d31c180 By the error messages posted in this discussion, it seems that ESLint is getting strip-ansi v7 to work with. I'm not sure what exactly is causing this, but this is not a problem in ESLint. It's the responsibility of package managers to provide packages with dependencies that satisfy the declared ranges. |
Beta Was this translation helpful? Give feedback.
-
just let us know ahead of time so we don't get confused and open an issue!
Also the thing marked as the "answer" just says it cant recreate. Let's mark one of the various hacks as the solve for v1 yarn users and attach an explainer! |
Beta Was this translation helpful? Give feedback.
-
Would someone have an actual reproduction to share? I'm curious about that, but I can't find one in this thread or the Yarn issue, so it's difficult to find out exactly what happens and understand whether this is expected or not. Help appreciated! |
Beta Was this translation helpful? Give feedback.
-
I was able to solve this issue by just deleting the lock file and reinstalling dependencies |
Beta Was this translation helpful? Give feedback.
-
I originally used yarn, but after doing a |
Beta Was this translation helpful? Give feedback.
-
I just recreated |
Beta Was this translation helpful? Give feedback.
-
Upgraded to Yarn 4 as suggested. Problem gone. Nothing else need to be changed. Cheers. |
Beta Was this translation helpful? Give feedback.
-
I think a package should work in any yarn version. yarn v1.x is still the most used version and its the default |
Beta Was this translation helpful? Give feedback.
-
Deleting the lock file and running |
Beta Was this translation helpful? Give feedback.
-
Fixed 1- check you have in eslint config parser: "@babel/eslint-parser" |
Beta Was this translation helpful? Give feedback.
Hi @santi100a, thanks for the issue. I wasn't able to reproduce the error you are getting.
Here are the steps I followed (with output):