Skip to content

Commit

Permalink
prefer-spread: Ignore Buffer.concat() (#1069)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jan 26, 2021
1 parent 673c440 commit da685f7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
10 changes: 7 additions & 3 deletions rules/prefer-spread.js
Expand Up @@ -38,9 +38,13 @@ const arrayConcatCallSelector = [
}),
`:not(${
[
'Literal',
'TemplateLiteral'
].map(type => `[callee.object.type="${type}"]`).join(', ')
...[
'Literal',
'TemplateLiteral'
].map(type => `[callee.object.type="${type}"]`),
// Most likely it's a static method of a class
'[callee.object.name=/^[A-Z]/]'
].join(', ')
})`
].join('');

Expand Down
12 changes: 10 additions & 2 deletions test/prefer-spread.js
Expand Up @@ -146,7 +146,13 @@ test.snapshot({
outdent`
const string = 'foo';
foo = string.concat("bar");
`
`,
// #1068
'const bufA = Buffer.concat([buf1, buf2, buf3], totalLength);',
'Foo.concat(1)',
'FooBar.concat(1)',
'FOO.concat(1)',
'A.concat(1)'
],
invalid: [
'[1].concat(2)',
Expand Down Expand Up @@ -229,6 +235,8 @@ test.snapshot({
'[].concat((a.b.c), 2)',
'[].concat(a.b(), 2)',
'foo.concat(bar, 2, [3, 4], baz, 5, [6, 7])',
'foo.concat(bar, 2, 3, ...baz)'
'foo.concat(bar, 2, 3, ...baz)',
'notClass.concat(1)',
'_A.concat(1)'
]
});
32 changes: 32 additions & 0 deletions test/snapshots/prefer-spread.js.md
Expand Up @@ -1374,3 +1374,35 @@ Generated by [AVA](https://avajs.dev).
Suggestion 3/3: Test first argument with `Array.isArray(…)`.␊
1 | [...foo, ...(Array.isArray(bar) ? bar : [bar]), 2, 3].concat(...baz)␊
`

## Invalid #52
1 | notClass.concat(1)

> Output
`␊
1 | [...notClass, 1]␊
`

> Error 1/1
`␊
> 1 | notClass.concat(1)␊
| ^^^^^^ Prefer the spread operator over `Array#concat(…)`.␊
`

## Invalid #53
1 | _A.concat(1)

> Output
`␊
1 | [..._A, 1]␊
`

> Error 1/1
`␊
> 1 | _A.concat(1)␊
| ^^^^^^ Prefer the spread operator over `Array#concat(…)`.␊
`
Binary file modified test/snapshots/prefer-spread.js.snap
Binary file not shown.

0 comments on commit da685f7

Please sign in to comment.