11'use strict' ;
22const { isParenthesized, getStaticValue, isCommaToken, hasSideEffect} = require ( 'eslint-utils' ) ;
3- const { methodCallSelector, not } = require ( './selectors/index.js' ) ;
3+ const { methodCallSelector} = require ( './selectors/index.js' ) ;
44const needsSemicolon = require ( './utils/needs-semicolon.js' ) ;
55const { getParenthesizedRange, getParenthesizedText} = require ( './utils/parentheses.js' ) ;
66const shouldAddParenthesesToSpreadElementArgument = require ( './utils/should-add-parentheses-to-spread-element-argument.js' ) ;
@@ -11,6 +11,7 @@ const {
1111 removeMethodCall,
1212} = require ( './fix/index.js' ) ;
1313const { isLiteral} = require ( './ast/index.js' ) ;
14+ const isMethodNamed = require ( './utils/is-method-named.js' ) ;
1415
1516const ERROR_ARRAY_FROM = 'array-from' ;
1617const ERROR_ARRAY_CONCAT = 'array-concat' ;
@@ -44,15 +45,7 @@ const arrayFromCallSelector = [
4445 '[arguments.0.type!="ObjectExpression"]' ,
4546] . join ( '' ) ;
4647
47- const arrayConcatCallSelector = [
48- methodCallSelector ( 'concat' ) ,
49- not (
50- [
51- 'Literal' ,
52- 'TemplateLiteral' ,
53- ] . map ( type => `[callee.object.type="${ type } "]` ) ,
54- ) ,
55- ] . join ( '' ) ;
48+ const arrayConcatCallSelector = methodCallSelector ( 'concat' ) ;
5649
5750const arraySliceCallSelector = [
5851 methodCallSelector ( {
@@ -320,6 +313,26 @@ function isClassName(node) {
320313 return / ^ [ A - Z ] ./ . test ( name ) && name . toUpperCase ( ) !== name ;
321314}
322315
316+ function isNotArray ( node , scope ) {
317+ if (
318+ node . type === 'TemplateLiteral'
319+ || node . type === 'Literal'
320+ || node . type === 'BinaryExpression'
321+ || isClassName ( node )
322+ // `foo.join()`
323+ || ( isMethodNamed ( node , 'join' ) && node . arguments . length <= 1 )
324+ ) {
325+ return true ;
326+ }
327+
328+ const staticValue = getStaticValue ( node , scope ) ;
329+ if ( staticValue && ! Array . isArray ( staticValue . value ) ) {
330+ return true ;
331+ }
332+
333+ return false ;
334+ }
335+
323336/** @param {import('eslint').Rule.RuleContext } context */
324337const create = context => {
325338 const sourceCode = context . getSourceCode ( ) ;
@@ -335,7 +348,7 @@ const create = context => {
335348 [ arrayConcatCallSelector ] ( node ) {
336349 const { object} = node . callee ;
337350
338- if ( isClassName ( object ) ) {
351+ if ( isNotArray ( object , context . getScope ( ) ) ) {
339352 return ;
340353 }
341354
0 commit comments