Skip to content

Commit

Permalink
fix(eslint-plugin): [no-unsafe-member-access] differentiate a types-e…
Browse files Browse the repository at this point in the history
…rror any from a true any (#9291)

* fix: support error type

* fix: add "typed"

* fix: Fixed the case of unsafeComputedMemberAccess as well.

* test: update snapshot
  • Loading branch information
y-hsgw authored Jun 17, 2024
1 parent 4598b0d commit bf48dc5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
12 changes: 10 additions & 2 deletions packages/eslint-plugin/src/rules/no-unsafe-member-access.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import * as tsutils from 'ts-api-utils';
import type * as ts from 'typescript';

import {
createRule,
Expand All @@ -15,6 +16,11 @@ const enum State {
Safe = 2,
}

function createDataType(type: ts.Type): '`error` typed' | '`any`' {
const isErrorType = tsutils.isIntrinsicErrorType(type);
return isErrorType ? '`error` typed' : '`any`';
}

export default createRule({
name: 'no-unsafe-member-access',
meta: {
Expand All @@ -26,13 +32,13 @@ export default createRule({
},
messages: {
unsafeMemberExpression:
'Unsafe member access {{property}} on an `any` value.',
'Unsafe member access {{property}} on an {{type}} value.',
unsafeThisMemberExpression: [
'Unsafe member access {{property}} on an `any` value. `this` is typed as `any`.',
'You can try to fix this by turning on the `noImplicitThis` compiler option, or adding a `this` parameter to the function.',
].join('\n'),
unsafeComputedMemberAccess:
'Computed name {{property}} resolves to an any value.',
'Computed name {{property}} resolves to an {{type}} value.',
},
schema: [],
},
Expand Down Expand Up @@ -92,6 +98,7 @@ export default createRule({
messageId,
data: {
property: node.computed ? `[${propertyName}]` : `.${propertyName}`,
type: createDataType(type),
},
});
}
Expand Down Expand Up @@ -127,6 +134,7 @@ export default createRule({
messageId: 'unsafeComputedMemberAccess',
data: {
property: `[${propertyName}]`,
type: createDataType(type),
},
});
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions packages/eslint-plugin/tests/rules/no-unsafe-member-access.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ function foo(x: any) {
column: 5,
endColumn: 6,
data: {
type: '`any`',
property: '.a',
},
},
Expand All @@ -93,6 +94,7 @@ function foo(x: any) {
column: 5,
endColumn: 6,
data: {
type: '`any`',
property: '.a',
},
},
Expand All @@ -111,6 +113,7 @@ function foo(x: { a: any }) {
column: 7,
endColumn: 8,
data: {
type: '`any`',
property: '.b',
},
},
Expand All @@ -129,6 +132,7 @@ function foo(x: any) {
column: 5,
endColumn: 8,
data: {
type: '`any`',
property: "['a']",
},
},
Expand All @@ -147,13 +151,33 @@ function foo(x: any) {
column: 5,
endColumn: 8,
data: {
type: '`any`',
property: "['a']",
},
},
],
},
{
code: `
let value: NotKnown;
value.property;
`,
errors: [
{
messageId: 'unsafeMemberExpression',
line: 4,
column: 7,
endColumn: 15,
data: {
type: '`error` typed',
property: '.property',
},
},
],
},
{
code: `
function foo(x: { a: number }, y: any) {
x[y];
}
Expand All @@ -166,6 +190,7 @@ function foo(x: { a: number }, y: any) {
endColumn: 6,
data: {
property: '[y]',
type: '`any`',
},
},
],
Expand All @@ -184,6 +209,7 @@ function foo(x?: { a: number }, y: any) {
endColumn: 8,
data: {
property: '[y]',
type: '`any`',
},
},
],
Expand All @@ -202,6 +228,7 @@ function foo(x: { a: number }, y: any) {
endColumn: 12,
data: {
property: '[y += 1]',
type: '`any`',
},
},
],
Expand All @@ -220,6 +247,7 @@ function foo(x: { a: number }, y: any) {
endColumn: 13,
data: {
property: '[1 as any]',
type: '`any`',
},
},
],
Expand All @@ -238,6 +266,7 @@ function foo(x: { a: number }, y: any) {
endColumn: 8,
data: {
property: '[y()]',
type: '`any`',
},
},
],
Expand All @@ -256,6 +285,26 @@ function foo(x: string[], y: any) {
endColumn: 6,
data: {
property: '[y]',
type: '`any`',
},
},
],
},
{
code: `
function foo(x: { a: number }, y: NotKnown) {
x[y];
}
`,
errors: [
{
messageId: 'unsafeComputedMemberAccess',
line: 3,
column: 5,
endColumn: 6,
data: {
property: '[y]',
type: '`error` typed',
},
},
],
Expand Down

0 comments on commit bf48dc5

Please sign in to comment.