Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
feat(eslint-plugin): add rule no-unsafe-member-access#1643
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
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletionspackages/eslint-plugin/README.md
This file contains hidden or 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
7 changes: 4 additions & 3 deletionspackages/eslint-plugin/ROADMAP.md
This file contains hidden or 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 deletionspackages/eslint-plugin/docs/rules/no-unsafe-member-access.md
This file contains hidden or 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/) |
1 change: 1 addition & 0 deletionspackages/eslint-plugin/src/configs/all.json
This file contains hidden or 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
2 changes: 2 additions & 0 deletionspackages/eslint-plugin/src/rules/index.ts
This file contains hidden or 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
74 changes: 74 additions & 0 deletionspackages/eslint-plugin/src/rules/no-unsafe-member-access.ts
This file contains hidden or 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,74 @@ | ||
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 type = checker.getTypeAtLocation(tsNode); | ||
const state = util.isTypeAnyType(type) ? 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, | ||
}; | ||
}, | ||
}); |
10 changes: 10 additions & 0 deletionspackages/eslint-plugin/src/util/astUtils.ts
This file contains hidden or 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
7 changes: 7 additions & 0 deletionspackages/eslint-plugin/src/util/types.ts
This file contains hidden or 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 deletionspackages/eslint-plugin/tests/rules/no-unsafe-member-access.test.ts
This file contains hidden or 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, | ||
}, | ||
], | ||
}), | ||
], | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.