Skip to content

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

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 9 commits into
base: main
Choose a base branch
from

Conversation

undsoft
Copy link
Contributor

@undsoft undsoft commented Mar 28, 2025

PR Checklist

Overview

Adds support for literal member access like a['b']

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @undsoft!

typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community.

The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately.

Thanks again!


🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint.

Copy link

netlify bot commented Mar 28, 2025

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit 7223496
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/68050fe2e838f60008cb1d87
😎 Deploy Preview https://deploy-preview-11006--typescript-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 99 (🟢 up 3 from production)
Accessibility: 100 (no change from production)
Best Practices: 100 (no change from production)
SEO: 98 (no change from production)
PWA: 80 (no change from production)
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

nx-cloud bot commented Mar 28, 2025

View your CI Pipeline Execution ↗ for commit 7223496.

Command Status Duration Result
nx typecheck ast-spec ✅ Succeeded <1s View ↗
nx run-many --target=build --exclude website --... ✅ Succeeded 2s View ↗
nx run-many --target=clean ✅ Succeeded 11s View ↗

☁️ Nx Cloud last updated this comment at 2025-04-28 05:42:16 UTC

Copy link

codecov bot commented Mar 28, 2025

Codecov Report

Attention: Patch coverage is 98.41270% with 1 line in your changes missing coverage. Please review.

Project coverage is 90.83%. Comparing base (f30a20e) to head (7223496).

Files with missing lines Patch % Lines
packages/eslint-plugin/src/rules/no-deprecated.ts 98.41% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main   #11006   +/-   ##
=======================================
  Coverage   90.82%   90.83%           
=======================================
  Files         497      497           
  Lines       50204    50253   +49     
  Branches     8274     8289   +15     
=======================================
+ Hits        45600    45648   +48     
- Misses       4589     4590    +1     
  Partials       15       15           
Flag Coverage Δ
unittest 90.83% <98.41%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
packages/eslint-plugin/src/rules/no-deprecated.ts 96.81% <98.41%> (+0.18%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙌 Great start, thanks for kicking it off! I think there's just one case around [key] that I think is still missing?

@JoshuaKGoldberg JoshuaKGoldberg added the awaiting response Issues waiting for a reply from the OP or another party label Mar 31, 2025
@undsoft
Copy link
Contributor Author

undsoft commented Apr 4, 2025

@JoshuaKGoldberg
Okay, I've added support for simple identifiers and template strings.

const myObject = {
  /** @deprecated */
  recommended: null,
};
const key = 'recommended';
const k2 = 'recom';
const k3 = 'mended';

myObject[key]; // Works
myObject[`${k2}${k3}`]; // Works

However, more complex identifiers like:

const myObject = {
  /** @deprecated */
  recommended: null,
};
const key = {
  nested: 'recommended'
} as const;

myObject[key.nested]; // Doesn't work

are not supported, because getPropertyName that I use doesn't seem to understand this construction.

I feel like fixing this may be outside of the scope for this PR? Or otherwise I could use some guidance as to how to do this.

@undsoft undsoft requested a review from JoshuaKGoldberg April 4, 2025 14:18
@github-actions github-actions bot removed the awaiting response Issues waiting for a reply from the OP or another party label Apr 4, 2025
Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really lovely progress! 💪

Comment on lines 291 to 293
if (node.type === AST_NODE_TYPES.MemberExpression && node.computed) {
return getComputedPropertyDeprecation(node);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Refactor] getComputedPropertyDeprecation is already being called in getDeprecationReason, just after the getCallLikeDeprecation. If we swap the order to try the computed property check first then we'll be able to remove this call here:

Suggested change
if (node.type === AST_NODE_TYPES.MemberExpression && node.computed) {
return getComputedPropertyDeprecation(node);
}
    function getDeprecationReason(node: IdentifierLike): string | undefined {
      if (isInComputedProperty(node)) {
        return getComputedPropertyDeprecation(
          node.parent as TSESTree.MemberExpression,
        );
      }

      const callLikeNode = getCallLikeNode(node);
      if (callLikeNode) {
        return getCallLikeDeprecation(callLikeNode);
      }

errors: [
{
column: 11,
data: { name: 'key' },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -232,7 +266,7 @@ export default createRule<Options, MessageIds>({
}
}

function getCallLikeNode(node: TSESTree.Node): CallLikeNode | undefined {
function getCallLikeNode(node: TSESTree.Node): CalleeNode | undefined {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Naming] If the type's name is switched from CalleeNode to CallLikeNode, the corresponding get* function's name should too:

Suggested change
function getCallLikeNode(node: TSESTree.Node): CalleeNode | undefined {
function getCalleNode(node: TSESTree.Node): CalleeNode | undefined {

@JoshuaKGoldberg JoshuaKGoldberg added the awaiting response Issues waiting for a reply from the OP or another party label Apr 7, 2025
@JoshuaKGoldberg
Copy link
Member

fixing this may be outside of the scope for this PR?

Agreed, yeah - I think it'd be good as a followup issue.

@undsoft undsoft requested a review from JoshuaKGoldberg April 20, 2025 15:35
@undsoft
Copy link
Contributor Author

undsoft commented Apr 20, 2025

Okay, I believe I've addressed the comments.

@github-actions github-actions bot removed the awaiting response Issues waiting for a reply from the OP or another party label Apr 20, 2025
@kirkwaiblinger kirkwaiblinger changed the title fix(eslint-plugin): [no-deprecated] support for literal member access (#10958) fix(eslint-plugin): [no-deprecated] support for computed literal member access Apr 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: [no-deprecated] does not report on computed member expressions with a known string literal property
2 participants