|
1 | 1 | 'use strict'; |
2 | | -const {isClosingParenToken} = require('eslint-utils'); |
| 2 | +const {isClosingParenToken, getStaticValue} = require('eslint-utils'); |
3 | 3 | const getDocumentationUrl = require('./utils/get-documentation-url'); |
4 | 4 | const isLiteralValue = require('./utils/is-literal-value'); |
5 | 5 | const avoidCapture = require('./utils/avoid-capture'); |
@@ -60,7 +60,7 @@ const getStrictComparisonOperands = binaryExpression => { |
60 | 60 | } |
61 | 61 | }; |
62 | 62 |
|
63 | | -const getArrayIdentifierNameFromBinaryExpression = (binaryExpression, indexIdentifierName) => { |
| 63 | +const getArrayIdentifierFromBinaryExpression = (binaryExpression, indexIdentifierName) => { |
64 | 64 | const operands = getStrictComparisonOperands(binaryExpression); |
65 | 65 |
|
66 | 66 | if (!operands) { |
@@ -88,17 +88,17 @@ const getArrayIdentifierNameFromBinaryExpression = (binaryExpression, indexIdent |
88 | 88 | return; |
89 | 89 | } |
90 | 90 |
|
91 | | - return greater.object.name; |
| 91 | + return greater.object; |
92 | 92 | }; |
93 | 93 |
|
94 | | -const getArrayIdentifierName = (forStatement, indexIdentifierName) => { |
| 94 | +const getArrayIdentifier = (forStatement, indexIdentifierName) => { |
95 | 95 | const {test} = forStatement; |
96 | 96 |
|
97 | 97 | if (!test || test.type !== 'BinaryExpression') { |
98 | 98 | return; |
99 | 99 | } |
100 | 100 |
|
101 | | - return getArrayIdentifierNameFromBinaryExpression(test, indexIdentifierName); |
| 101 | + return getArrayIdentifierFromBinaryExpression(test, indexIdentifierName); |
102 | 102 | }; |
103 | 103 |
|
104 | 104 | const isLiteralOnePlusIdentifierWithName = (node, identifierName) => { |
@@ -281,9 +281,17 @@ const create = context => { |
281 | 281 | return; |
282 | 282 | } |
283 | 283 |
|
284 | | - const arrayIdentifierName = getArrayIdentifierName(node, indexIdentifierName); |
| 284 | + const arrayIdentifier = getArrayIdentifier(node, indexIdentifierName); |
| 285 | + if (!arrayIdentifier) { |
| 286 | + return; |
| 287 | + } |
| 288 | + |
| 289 | + const arrayIdentifierName = arrayIdentifier.name; |
285 | 290 |
|
286 | | - if (!arrayIdentifierName) { |
| 291 | + const scope = context.getScope(); |
| 292 | + const staticResult = getStaticValue(arrayIdentifier, scope); |
| 293 | + if (staticResult && !Array.isArray(staticResult.value)) { |
| 294 | + // Bail out if we can tell that the array variable has a non-array value (i.e. we're looping through the characters of a string constant). |
287 | 295 | return; |
288 | 296 | } |
289 | 297 |
|
|
0 commit comments