Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
fix(eslint-plugin): [no-for-in-array] report on any type which may be an array or array-like#10535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
75d1ecb
4ded1c5
7efca69
e126dab
b612eef
6466fb5
2ce3cea
006eb4c
762934d
599ac75
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import * as tsutils from 'ts-api-utils'; | ||
import * as ts from 'typescript'; | ||
import { | ||
createRule, | ||
getConstrainedTypeAtLocation, | ||
getParserServices, | ||
} from '../util'; | ||
import { getForStatementHeadLoc } from '../util/getForStatementHeadLoc'; | ||
@@ -32,10 +32,7 @@ export default createRule({ | ||
const type = getConstrainedTypeAtLocation(services, node.right); | ||
kirkwaiblinger marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
if (isArrayLike(checker, type)) { | ||
context.report({ | ||
loc: getForStatementHeadLoc(context.sourceCode, node), | ||
messageId: 'forInViolation', | ||
@@ -45,3 +42,34 @@ export default createRule({ | ||
}; | ||
}, | ||
}); | ||
function isArrayLike(checker: ts.TypeChecker, type: ts.Type): boolean { | ||
return isTypeRecurser( | ||
type, | ||
t => t.getNumberIndexType() != null && hasArrayishLength(checker, t), | ||
); | ||
} | ||
function hasArrayishLength(checker: ts.TypeChecker, type: ts.Type): boolean { | ||
const lengthProperty = type.getProperty('length'); | ||
if (lengthProperty == null) { | ||
return false; | ||
} | ||
return tsutils.isTypeFlagSet( | ||
checker.getTypeOfSymbol(lengthProperty), | ||
ts.TypeFlags.NumberLike, | ||
); | ||
} | ||
function isTypeRecurser( | ||
type: ts.Type, | ||
predicate: (t: ts.Type) => boolean, | ||
): boolean { | ||
if (type.isUnionOrIntersection()) { | ||
return type.types.some(t => isTypeRecurser(t, predicate)); | ||
} | ||
return predicate(type); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"lib": ["es2015", "es2017", "esnext", "dom"] | ||
} | ||
} | ||
Comment on lines +1 to +6 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Is there a better way to do this? Specifically There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Yep, this is good! |
Uh oh!
There was an error while loading.Please reload this page.