Skip to content

Commit

Permalink
feat: add no-use-before-define rule (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
LancelotLewis authored Mar 25, 2024
1 parent 1fe0ed9 commit 1079504
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 58 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.0 (March 25, 2024)

- feat: add `no-use-before-define` rule

## 0.2.4 (June 06, 2023)

- chore: add lint in ci
Expand Down
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"bumpp": "^9.1.1",
"zx": "^7.2.2"
},
"version": "0.2.4"
"version": "0.3.0"
}
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
2 changes: 1 addition & 1 deletion packages/eslint-config-base/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@arcblock/eslint-config-base",
"version": "0.2.4",
"version": "0.3.0",
"description": "Arcblock's ESLint config base",
"author": "zhanghan <[email protected]>",
"homepage": "https://github.com/ArcBlock/eslint-config#readme",
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-config-ts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@arcblock/eslint-config-ts",
"version": "0.2.4",
"version": "0.3.0",
"description": "Arcblock's base config for ESLint & Typescript",
"author": "wangshijun <[email protected]>",
"homepage": "https://github.com/ArcBlock/eslint-config#readme",
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
2 changes: 1 addition & 1 deletion packages/eslint-config/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@arcblock/eslint-config",
"version": "0.2.4",
"version": "0.3.0",
"description": "Arcblock's ESLint config for react",
"author": "zhanghan <[email protected]>",
"homepage": "https://github.com/ArcBlock/eslint-config#readme",
Expand Down
2 changes: 1 addition & 1 deletion scripts/bump-version.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { execSync } from 'child_process';
import { $, chalk, fs } from 'zx';

// or use pnpm to bump version: `pnpm -r --filter {packages/*, themes/*} -- pnpm version`
execSync('bumpp package.json packages/*/package.json', {
execSync('bumpp package.json packages/*/package.json --no-push --no-tag --no-commit', {
stdio: 'inherit',
});

Expand Down

0 comments on commit 1079504

Please sign in to comment.