From 1079504c8f3dbffde02bcb08a808e3bba09fa7fa Mon Sep 17 00:00:00 2001 From: zhang han Date: Mon, 25 Mar 2024 09:46:37 +0800 Subject: [PATCH] feat: add no-use-before-define rule (#22) --- CHANGELOG.md | 4 + examples/ts/src/index.ts | 102 +++++++++++------------ package.json | 2 +- packages/eslint-config-base/index.js | 1 - packages/eslint-config-base/package.json | 2 +- packages/eslint-config-ts/package.json | 2 +- packages/eslint-config/index.js | 1 - packages/eslint-config/package.json | 2 +- scripts/bump-version.mjs | 2 +- 9 files changed, 60 insertions(+), 58 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f1995c8..467a70a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/examples/ts/src/index.ts b/examples/ts/src/index.ts index 65cd6ff..443916d 100644 --- a/examples/ts/src/index.ts +++ b/examples/ts/src/index.ts @@ -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'; @@ -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' * @@ -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 * @@ -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. @@ -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); }; @@ -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 * diff --git a/package.json b/package.json index 9ec2a06..1130434 100644 --- a/package.json +++ b/package.json @@ -10,5 +10,5 @@ "bumpp": "^9.1.1", "zx": "^7.2.2" }, - "version": "0.2.4" + "version": "0.3.0" } diff --git a/packages/eslint-config-base/index.js b/packages/eslint-config-base/index.js index 6e3f705..6000bf7 100644 --- a/packages/eslint-config-base/index.js +++ b/packages/eslint-config-base/index.js @@ -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', diff --git a/packages/eslint-config-base/package.json b/packages/eslint-config-base/package.json index 5569222..c2cff8e 100644 --- a/packages/eslint-config-base/package.json +++ b/packages/eslint-config-base/package.json @@ -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 ", "homepage": "https://github.com/ArcBlock/eslint-config#readme", diff --git a/packages/eslint-config-ts/package.json b/packages/eslint-config-ts/package.json index 39e14b0..be25f3c 100644 --- a/packages/eslint-config-ts/package.json +++ b/packages/eslint-config-ts/package.json @@ -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 ", "homepage": "https://github.com/ArcBlock/eslint-config#readme", diff --git a/packages/eslint-config/index.js b/packages/eslint-config/index.js index 2442720..7817e66 100644 --- a/packages/eslint-config/index.js +++ b/packages/eslint-config/index.js @@ -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', diff --git a/packages/eslint-config/package.json b/packages/eslint-config/package.json index 9e822df..62a0986 100644 --- a/packages/eslint-config/package.json +++ b/packages/eslint-config/package.json @@ -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 ", "homepage": "https://github.com/ArcBlock/eslint-config#readme", diff --git a/scripts/bump-version.mjs b/scripts/bump-version.mjs index 7c54ddc..8b234aa 100644 --- a/scripts/bump-version.mjs +++ b/scripts/bump-version.mjs @@ -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', });