Skip to content

Commit

Permalink
feat: add no-use-before-define rule
Browse files Browse the repository at this point in the history
  • Loading branch information
LancelotLewis committed Mar 25, 2024
1 parent 1fe0ed9 commit f4f169c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 53 deletions.
102 changes: 51 additions & 51 deletions examples/ts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/no-use-before-define */
/* eslint-disable no-param-reassign */
/* eslint-disable no-bitwise */
import isBoolean from 'lodash/isBoolean';
Expand Down Expand Up @@ -36,44 +35,6 @@ export type KeyPairType = { publicKey: BytesType; secretKey: BytesType };

export { BN, leftPad, rightPad };

/**
* Returns a BN object, converts a number value to a BN
* @param {String|Number|Object} `arg` input a string number, hex string number, number, BigNumber or BN object
* @return {Object} `output` BN object of the number
* @throws if the argument is not an array, object that isn't a bignumber, not a string number or number
*/
export const numberToBN = (arg: string | number | BN) => {
if (typeof arg === 'string' || typeof arg === 'number') {
let multiplier = new BN(1); // eslint-disable-line
const formattedString = String(arg).toLowerCase().trim();
const isHexPrefixed = formattedString.substr(0, 2) === '0x' || formattedString.substr(0, 3) === '-0x';
let stringArg = stripHexPrefix(formattedString); // eslint-disable-line
if (stringArg.substr(0, 1) === '-') {
stringArg = stripHexPrefix(stringArg.slice(1));
multiplier = new BN(-1, 10);
}
stringArg = stringArg === '' ? '0' : stringArg;
if (
(!stringArg.match(/^-?[0-9]+$/) && stringArg.match(/^[0-9A-Fa-f]+$/)) ||
stringArg.match(/^[a-fA-F]+$/) ||
(isHexPrefixed === true && stringArg.match(/^[0-9A-Fa-f]+$/))
) {
return new BN(stringArg, 16).mul(multiplier);
}
if ((stringArg.match(/^-?[0-9]+$/) || stringArg === '') && isHexPrefixed === false) {
return new BN(stringArg, 10).mul(multiplier);
}
}
if (isBN(arg)) {
return new BN(arg.toString(10), 10);
}
throw new Error(
`[number-to-bn] while converting number ${JSON.stringify(
arg,
)} to BN.js instance, error: invalid number value. Value must be an integer, hex string, BN or BigNumber instance. Note, decimals are not supported.`,
);
};

/**
* Returns a `boolean` on whether or not the `string` starts with '0x'
*
Expand Down Expand Up @@ -151,6 +112,44 @@ export const isHexStrict = (hex: string) => (isString(hex) || isNumber(hex)) &&
*/
export const isHex = (hex: string) => (isString(hex) || isNumber(hex)) && /^(-0x|0x|0X|-0X)?[0-9a-f]*$/i.test(hex);

/**
* Returns a BN object, converts a number value to a BN
* @param {String|Number|Object} `arg` input a string number, hex string number, number, BigNumber or BN object
* @return {Object} `output` BN object of the number
* @throws if the argument is not an array, object that isn't a bignumber, not a string number or number
*/
export const numberToBN = (arg: string | number | BN) => {
if (typeof arg === 'string' || typeof arg === 'number') {
let multiplier = new BN(1); // eslint-disable-line
const formattedString = String(arg).toLowerCase().trim();
const isHexPrefixedValue = formattedString.substr(0, 2) === '0x' || formattedString.substr(0, 3) === '-0x';
let stringArg = stripHexPrefix(formattedString); // eslint-disable-line
if (stringArg.substr(0, 1) === '-') {
stringArg = stripHexPrefix(stringArg.slice(1));
multiplier = new BN(-1, 10);
}
stringArg = stringArg === '' ? '0' : stringArg;
if (
(!stringArg.match(/^-?[0-9]+$/) && stringArg.match(/^[0-9A-Fa-f]+$/)) ||
stringArg.match(/^[a-fA-F]+$/) ||
(isHexPrefixedValue === true && stringArg.match(/^[0-9A-Fa-f]+$/))
) {
return new BN(stringArg, 16).mul(multiplier);
}
if ((stringArg.match(/^-?[0-9]+$/) || stringArg === '') && isHexPrefixedValue === false) {
return new BN(stringArg, 10).mul(multiplier);
}
}
if (isBN(arg)) {
return new BN(arg.toString(10), 10);
}
throw new Error(
`[number-to-bn] while converting number ${JSON.stringify(
arg,
)} to BN.js instance, error: invalid number value. Value must be an integer, hex string, BN or BigNumber instance. Note, decimals are not supported.`,
);
};

/**
* Takes an input and transforms it into an BN
*
Expand Down Expand Up @@ -309,6 +308,18 @@ export const hexToBytes = (hex: $TSFixMe) => {
return bytes;
};

/**
* Validates if a value is an Uint8Array.
*
* @public
* @static
* @param {*} value - value to validate
* @returns {Boolean} boolean indicating if a value is an Uint8Array
*/
export function isUint8Array(value: $TSFixMe) {
return Object.prototype.toString.call(value) === '[object Uint8Array]';
}

/**
* Auto converts any given value into it's hex representation.
* And even stringify objects before.
Expand Down Expand Up @@ -348,6 +359,7 @@ export const toHex = (
// TODO: some edge case may be not properly handled here
return returnType ? 'string' : utf8ToHex(value);
}
// @ts-ignore
// eslint-disable-next-line no-nested-ternary
return returnType ? (value < 0 ? 'int256' : 'uint256') : numberToHex(value);
};
Expand Down Expand Up @@ -463,18 +475,6 @@ export const fromTokenToUnit = (input: string | number, decimal = 18) => {
return new BN(unit.toString(10), 10);
};

/**
* Validates if a value is an Uint8Array.
*
* @public
* @static
* @param {*} value - value to validate
* @returns {Boolean} boolean indicating if a value is an Uint8Array
*/
export function isUint8Array(value: $TSFixMe) {
return Object.prototype.toString.call(value) === '[object Uint8Array]';
}

/**
* Generate a random UUID
*
Expand Down
1 change: 0 additions & 1 deletion packages/eslint-config-base/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ module.exports = {
'no-plusplus': 'off',
'no-restricted-syntax': ['error', 'ForInStatement', 'LabeledStatement', 'WithStatement'],
'no-underscore-dangle': 'off',
'no-use-before-define': 'off',
'prefer-object-spread': 'off',
'unicorn/filename-case': [
'error',
Expand Down
1 change: 0 additions & 1 deletion packages/eslint-config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ module.exports = {
'no-plusplus': 'off',
'no-restricted-syntax': ['error', 'ForInStatement', 'LabeledStatement', 'WithStatement'],
'no-underscore-dangle': 'off',
'no-use-before-define': 'off',
'prefer-object-spread': 'off',
'react/destructuring-assignment': 'off',
'react/forbid-prop-types': 'off',
Expand Down

0 comments on commit f4f169c

Please sign in to comment.