Skip to content

fix(eslint-plugin): [no-deprecated] support computed member access #10867

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions packages/eslint-plugin/src/rules/no-deprecated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,13 +395,58 @@ export default createRule<Options, MessageIds>({
});
}

function checkMemberExpression(node: TSESTree.MemberExpression): void {
if (!node.computed) {
return;
}

const propertyType = services.getTypeAtLocation(node.property);

if (propertyType.isStringLiteral() || propertyType.isLiteral()) {
const objectType = services.getTypeAtLocation(node.object);

let propertyName: string;

if (propertyType.isStringLiteral()) {
propertyName = propertyType.value;
} else {
propertyName = String(propertyType.value as number);
}

const property = objectType.getProperty(propertyName);

const reason = getJsDocDeprecation(property);
if (reason == null) {
return;
}

if (typeMatchesSomeSpecifier(objectType, allow, services.program)) {
return;
}

context.report({
...(reason
? {
messageId: 'deprecatedWithReason',
data: { name: propertyName, reason },
}
: {
messageId: 'deprecated',
data: { name: propertyName },
}),
node: node.property,
});
}
}

return {
Identifier: checkIdentifier,
JSXIdentifier(node): void {
if (node.parent.type !== AST_NODE_TYPES.JSXClosingElement) {
checkIdentifier(node);
}
},
MemberExpression: checkMemberExpression,
PrivateIdentifier: checkIdentifier,
Super: checkIdentifier,
};
Expand Down
269 changes: 265 additions & 4 deletions packages/eslint-plugin/tests/rules/no-deprecated.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,13 +364,126 @@ exists('/foo');
const bar = { test };
`,
`
class A {
#b = () => {};
const a = {
/** @deprecated */
b: 'string',
};

c() {
this.#b();
const complex = Symbol() as any;
const c = a[complex];
`,
`
const a = {
b: 'string',
};

const c = a['b'];
`,
{
code: `
interface AllowedType {
/** @deprecated */
prop: string;
}

const obj: AllowedType = {
prop: 'test',
};

const value = obj['prop'];
`,
options: [
{
allow: [
{
from: 'file',
name: 'AllowedType',
},
],
},
],
},
`
const a = {
/** @deprecated */
b: 'string',
};

const key = {};
const c = a[key as any];
`,
`
const a = {
/** @deprecated */
b: 'string',
};

const key = Symbol();
const c = a[key as any];
`,
`
const a = {
/** @deprecated */
b: 'string',
};

const key = undefined;
const c = a[key as any];
`,
`
const a = {
/** @deprecated */
b: 'string',
};

const c = a['nonExistentProperty'];
`,
`
const a = {
/** @deprecated */
b: 'string',
};

function getKey() {
return 'c';
}

const c = a[getKey()];
`,
`
const a = {
/** @deprecated */
b: 'string',
};

const key = {};
const c = a[key];
`,
`
const stringObj = new String('b');
const a = {
/** @deprecated */
b: 'string',
};
const c = a[stringObj];
`,
`
const a = {
/** @deprecated */
b: 'string',
};

const key = Symbol('key');
const c = a[key];
`,
`
const a = {
/** @deprecated */
b: 'string',
};

const key = null;
const c = a[key as any];
`,
],
invalid: [
Expand Down Expand Up @@ -2912,5 +3025,153 @@ class B extends A {
},
],
},
{
code: `
const a = {
/** @deprecated */
b: 'string',
};

const c = a['b'];
`,
errors: [
{
column: 21,
data: { name: 'b' },
endColumn: 24,
endLine: 7,
line: 7,
messageId: 'deprecated',
},
],
},
{
code: `
const a = {
/** @deprecated */
b: 'string',
};
const x = 'b';
const c = a[x];
`,
errors: [
{
column: 21,
data: { name: 'b' },
endColumn: 22,
endLine: 7,
line: 7,
messageId: 'deprecated',
},
],
},
{
code: `
const a = {
/** @deprecated */
[2]: 'string',
};
const x = 'b';
const c = a[2];
`,
errors: [
{
column: 21,
data: { name: '2' },
endColumn: 22,
endLine: 7,
line: 7,
messageId: 'deprecated',
},
],
},
{
code: `
const a = {
/** @deprecated reason for deprecation */
b: 'string',
};

const key = 'b';
const stringKey = key as const;
const c = a[stringKey];
`,
errors: [
{
column: 21,
data: { name: 'b', reason: 'reason for deprecation' },
endColumn: 30,
endLine: 9,
line: 9,
messageId: 'deprecatedWithReason',
},
],
},
{
code: `
enum Keys {
B = 'b',
}

const a = {
/** @deprecated reason for deprecation */
b: 'string',
};

const key = Keys.B;
const c = a[key];
`,
errors: [
{
column: 21,
data: { name: 'b', reason: 'reason for deprecation' },
endColumn: 24,
endLine: 12,
line: 12,
messageId: 'deprecatedWithReason',
},
],
},
{
code: `
const a = {
/** @deprecated */
b: 'string',
};

const key = \`b\`;
const c = a[key];
`,
errors: [
{
column: 21,
data: { name: 'b' },
endColumn: 24,
endLine: 8,
line: 8,
messageId: 'deprecated',
},
],
},
{
code: `
const stringObj = 'b';
const a = {
/** @deprecated */
b: 'string',
};
const c = a[stringObj];
`,
errors: [
{
column: 21,
data: { name: 'b' },
endColumn: 30,
endLine: 7,
line: 7,
messageId: 'deprecated',
},
],
},
],
});