-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e95c87f
commit 4e9da68
Showing
6 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--- | ||
description: 'Disallow using in operator for arrays.' | ||
--- | ||
|
||
> 🛑 This file is source code, not the primary documentation location! 🛑 | ||
> | ||
> See **https://typescript-eslint.io/rules/no-in-array** for documentation. | ||
This rule bans using `in` operator for checking array members existence. | ||
|
||
## Rule Details | ||
|
||
Examples of code for this rule: | ||
|
||
### ❌ Incorrect | ||
|
||
```ts | ||
const arr = ['a', 'b', 'c']; | ||
|
||
if ('c' in arr) { | ||
// ... | ||
} | ||
``` | ||
|
||
### ✅ Correct | ||
|
||
```ts | ||
const arr = ['a', 'b', 'c']; | ||
|
||
if (arr.includes('a')) { | ||
// ... | ||
} | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
When you want exactly iterate over array indexes. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import type { TSESTree } from '@typescript-eslint/utils'; | ||
import * as ts from 'typescript'; | ||
|
||
import * as util from '../util'; | ||
|
||
export default util.createRule<[], 'inArrayViolation'>({ | ||
name: 'no-in-array', | ||
meta: { | ||
docs: { | ||
description: 'Disallow using in operator for arrays', | ||
recommended: 'error', | ||
requiresTypeChecking: true, | ||
}, | ||
messages: { | ||
inArrayViolation: | ||
"'in' operator for arrays is forbidden. Use array.indexOf or array.includes instead.", | ||
}, | ||
schema: [], | ||
type: 'problem', | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
return { | ||
"BinaryExpression[operator='in']"(node: TSESTree.BinaryExpression): void { | ||
const parserServices = util.getParserServices(context); | ||
const checker = parserServices.program.getTypeChecker(); | ||
const originalNode = parserServices.esTreeNodeToTSNodeMap.get(node); | ||
|
||
const type = util.getConstrainedTypeAtLocation( | ||
checker, | ||
originalNode.right, | ||
); | ||
|
||
if ( | ||
util.isTypeArrayTypeOrUnionOfArrayTypes(type, checker) || | ||
(type.flags & ts.TypeFlags.StringLike) !== 0 | ||
) { | ||
context.report({ | ||
node, | ||
messageId: 'inArrayViolation', | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import rule from '../../src/rules/no-in-array'; | ||
import { getFixturesRootDir, RuleTester } from '../RuleTester'; | ||
|
||
const rootDir = getFixturesRootDir(); | ||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 2015, | ||
tsconfigRootDir: rootDir, | ||
project: './tsconfig.json', | ||
}, | ||
parser: '@typescript-eslint/parser', | ||
}); | ||
|
||
ruleTester.run('no-in-array', rule, { | ||
valid: [ | ||
{ | ||
code: ` | ||
if (x in {}) { | ||
} | ||
`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
if (x in ['z', 'y']) { | ||
} | ||
`, | ||
errors: [{ messageId: 'inArrayViolation' }], | ||
}, | ||
{ | ||
code: ` | ||
const arr = [5, 6, 7, 8]; | ||
const has_6 = 6 in arr; | ||
`, | ||
errors: [{ messageId: 'inArrayViolation' }], | ||
}, | ||
], | ||
}); |