Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin): add rule no-unsafe-member-access
- Loading branch information
1 parent
3b40231
commit 745b7b3
Showing
9 changed files
with
265 additions
and
3 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
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
43 changes: 43 additions & 0 deletions
43
packages/eslint-plugin/docs/rules/no-unsafe-member-access.md
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,43 @@ | ||
# Disallows member access on any typed variables (`no-unsafe-member-access`) | ||
|
||
Despite your best intentions, the `any` type can sometimes leak into your codebase. | ||
Member access on `any` typed variables is not checked at all by TypeScript, so it creates a potential safety hole, and source of bugs in your codebase. | ||
|
||
## Rule Details | ||
|
||
This rule disallows member access on any variable that is typed as `any`. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```ts | ||
declare const anyVar: any; | ||
declare const nestedAny: { prop: any }; | ||
|
||
anyVar.a; | ||
anyVar.a.b; | ||
anyVar['a']; | ||
anyVar['a']['b']; | ||
|
||
nestedAny.prop.a; | ||
nestedAny.prop['a']; | ||
|
||
const key = 'a'; | ||
nestedAny.prop[key]; | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```ts | ||
declare const properlyTyped: { prop: { a: string } }; | ||
|
||
nestedAny.prop.a; | ||
nestedAny.prop['a']; | ||
|
||
const key = 'a'; | ||
nestedAny.prop[key]; | ||
``` | ||
|
||
## Related to | ||
|
||
- [`no-explicit-any`](./no-explicit-any.md) | ||
- TSLint: [`no-unsafe-any`](https://palantir.github.io/tslint/rules/no-unsafe-any/) |
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
73 changes: 73 additions & 0 deletions
73
packages/eslint-plugin/src/rules/no-unsafe-member-access.ts
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,73 @@ | ||
import { TSESTree } from '@typescript-eslint/experimental-utils'; | ||
import * as util from '../util'; | ||
|
||
const enum State { | ||
Unsafe = 1, | ||
Safe = 2, | ||
} | ||
|
||
export default util.createRule({ | ||
name: 'no-unsafe-member-access', | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Disallows member access on any typed variables', | ||
category: 'Possible Errors', | ||
recommended: false, | ||
requiresTypeChecking: true, | ||
}, | ||
messages: { | ||
unsafeMemberExpression: | ||
'Unsafe member access {{property}} on an any value', | ||
}, | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
const { program, esTreeNodeToTSNodeMap } = util.getParserServices(context); | ||
const checker = program.getTypeChecker(); | ||
const sourceCode = context.getSourceCode(); | ||
|
||
const stateCache = new Map<TSESTree.Node, State>(); | ||
|
||
function checkMemberExpression( | ||
node: TSESTree.MemberExpression | TSESTree.OptionalMemberExpression, | ||
): State { | ||
const cachedState = stateCache.get(node); | ||
if (cachedState) { | ||
return cachedState; | ||
} | ||
|
||
if (util.isMemberOrOptionalMemberExpression(node.object)) { | ||
const objectState = checkMemberExpression(node.object); | ||
if (objectState === State.Unsafe) { | ||
// if the object is unsafe, we know this will be unsafe as well | ||
// we don't need to report, as we have already reported on the inner member expr | ||
stateCache.set(node, objectState); | ||
return objectState; | ||
} | ||
} | ||
|
||
const tsNode = esTreeNodeToTSNodeMap.get(node.object); | ||
const state = util.isAnyType(tsNode, checker) ? State.Unsafe : State.Safe; | ||
stateCache.set(node, state); | ||
|
||
if (state === State.Unsafe) { | ||
const propertyName = sourceCode.getText(node.property); | ||
context.report({ | ||
node, | ||
messageId: 'unsafeMemberExpression', | ||
data: { | ||
property: node.computed ? `[${propertyName}]` : `.${propertyName}`, | ||
}, | ||
}); | ||
} | ||
|
||
return state; | ||
} | ||
|
||
return { | ||
'MemberExpression, OptionalMemberExpression': checkMemberExpression, | ||
}; | ||
}, | ||
}); |
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
85 changes: 85 additions & 0 deletions
85
packages/eslint-plugin/tests/rules/no-unsafe-member-access.test.ts
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,85 @@ | ||
import rule from '../../src/rules/no-unsafe-member-access'; | ||
import { | ||
RuleTester, | ||
batchedSingleLineTests, | ||
getFixturesRootDir, | ||
} from '../RuleTester'; | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: '@typescript-eslint/parser', | ||
parserOptions: { | ||
project: './tsconfig.json', | ||
tsconfigRootDir: getFixturesRootDir(), | ||
}, | ||
}); | ||
|
||
ruleTester.run('no-unsafe-member-access', rule, { | ||
valid: [ | ||
'function foo(x: { a: number }) { x.a }', | ||
'function foo(x?: { a: number }) { x?.a }', | ||
], | ||
invalid: [ | ||
...batchedSingleLineTests({ | ||
code: ` | ||
function foo(x: any) { x.a } | ||
function foo(x: any) { x.a.b.c.d.e.f.g } | ||
function foo(x: { a: any }) { x.a.b.c.d.e.f.g } | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'unsafeMemberExpression', | ||
data: { | ||
property: '.a', | ||
}, | ||
line: 2, | ||
column: 24, | ||
endColumn: 27, | ||
}, | ||
{ | ||
messageId: 'unsafeMemberExpression', | ||
data: { | ||
property: '.a', | ||
}, | ||
line: 3, | ||
column: 24, | ||
endColumn: 27, | ||
}, | ||
{ | ||
messageId: 'unsafeMemberExpression', | ||
data: { | ||
property: '.b', | ||
}, | ||
line: 4, | ||
column: 31, | ||
endColumn: 36, | ||
}, | ||
], | ||
}), | ||
...batchedSingleLineTests({ | ||
code: ` | ||
function foo(x: any) { x['a'] } | ||
function foo(x: any) { x['a']['b']['c'] } | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'unsafeMemberExpression', | ||
data: { | ||
property: "['a']", | ||
}, | ||
line: 2, | ||
column: 24, | ||
endColumn: 30, | ||
}, | ||
{ | ||
messageId: 'unsafeMemberExpression', | ||
data: { | ||
property: "['a']", | ||
}, | ||
line: 3, | ||
column: 24, | ||
endColumn: 30, | ||
}, | ||
], | ||
}), | ||
], | ||
}); |