eslint-plugin-no-array-concat
v0.1.2
Published
ESLint rule for not using Array.prototype.concat()
Downloads
32
Maintainers
Readme
eslint-plugin-no-array-concat
Installation
npm
npm install --save-dev eslint-plugin-no-array-concat
yarn
yarn add -D eslint-plugin-no-array-concat
pnpm
pnpm i -D eslint-plugin-no-array-concat
Requirements
Rules
- no-array-concat: Prevent using Array.prototype.concat() for Array concatenation.
Usage
In .eslintrc.js
:
module.exports = {
plugins: ['no-array-concat'],
rules: {
'no-array-concat/no-array-concat': 'error',
},
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 2021,
sourceType: 'module',
project: ['./tsconfig.json'],
tsconfigRootDir: __dirname
}
}
Reasoning
It's difficult to see Array.prototype.concat() if it's in a long chain of function-style map(), filter(), forEach() etc (See the following example).
someVeryLongVariable
.filter((a) => someCheckFunction(a))
.concat(someVeryLongVariableB)
.forEach((a, i) => {
doSomething(a, i)
})
In addition, Even if it's not in the chains, since array concatenation is adding up two arrays, it should be a symmetrical operation, such as 1 + 2 or plus(1, 2), rather than 1.plus(2).
The alternative way is to concat using Array destructuring:
[
...someVeryLongVariable.filter((a) => someCheckFunction(a)),
...someVeryLongVariableB
].forEach((a, i) => {
doSomething(a, i)
})
or
const concatenatedVariable = [
...someVeryLongVariable.filter((a) => someCheckFunction(a)),
...someVeryLongVariableB
]
for (const [i, a] of concatenatedVariable.entries()) {
doSometing(i, a)
}
License
MIT © 2022 mkpoli