eslint-plugin-no-array-concat

0.1.2 • Public • Published

eslint-plugin-no-array-concat

npm version

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

Package Sidebar

Install

npm i eslint-plugin-no-array-concat

Weekly Downloads

15

Version

0.1.2

License

MIT

Unpacked Size

10.2 kB

Total Files

9

Last publish

Collaborators

  • mkpoli