Skip to content

fix(eslint-plugin): [consistent-generic-constructors] ignore when constructor is typed array #10477

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

Conversation

mdm317
Copy link
Contributor

@mdm317 mdm317 commented Dec 9, 2024

PR Checklist

Overview

Hard-code the typed arrays and added code to skip check if the type is a typed array.

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @mdm317!

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.

@mdm317 mdm317 marked this pull request as draft December 9, 2024 09:33
Copy link

nx-cloud bot commented Dec 9, 2024

View your CI Pipeline Execution ↗ for commit 91b65ae.

Command Status Duration Result
nx test eslint-plugin ✅ Succeeded 7m 47s View ↗
nx run eslint-plugin:test -- --coverage ✅ Succeeded 6m 14s View ↗
nx test eslint-plugin --coverage=false ✅ Succeeded 6m 24s View ↗
nx run rule-tester:test -- --coverage ✅ Succeeded <1s View ↗
nx run types:build ✅ Succeeded <1s View ↗
nx run-many --target=build --exclude website --... ✅ Succeeded 14s View ↗
nx test utils ✅ Succeeded <1s View ↗
nx run-many --target=clean ✅ Succeeded 12s View ↗
Additional runs (25) ✅ Succeeded ... View ↗

☁️ Nx Cloud last updated this comment at 2025-03-14 17:13:59 UTC

Copy link

netlify bot commented Dec 9, 2024

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit 91b65ae
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/67d4606c0cd7bc0009565010
😎 Deploy Preview https://deploy-preview-10477--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 6 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

codecov bot commented Dec 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.58%. Comparing base (7469e72) to head (91b65ae).
Report is 20 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #10477   +/-   ##
=======================================
  Coverage   87.58%   87.58%           
=======================================
  Files         470      470           
  Lines       16095    16096    +1     
  Branches     4668     4669    +1     
=======================================
+ Hits        14097    14098    +1     
  Misses       1642     1642           
  Partials      356      356           
Flag Coverage Δ
unittest 87.58% <100.00%> (+<0.01%) ⬆️

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

Files with missing lines Coverage Δ
...lugin/src/rules/consistent-generic-constructors.ts 90.69% <100.00%> (+0.22%) ⬆️
🚀 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.

@mdm317 mdm317 marked this pull request as ready for review December 9, 2024 15:56
return true;
}
return false;
};
Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg Dec 14, 2024

Choose a reason for hiding this comment

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

[Bug] It's not enough to just look at the name. If someone happens to redeclare their own class, such as class Uint8Array<T>, the rule should know to check that:

https://deploy-preview-10477--typescript-eslint.netlify.app/play/#ts=5.7.2&fileType=.ts&code=MYGwhgzhAECqCWA7ALgDgIICdNgJ4B4AVAPmgG8AoaaAehugDomKBfCikAU2WjAC44SNFhwEReAEIBXAGYzOmADLwA1p1IBeaIk4B3QSgzY8ACgCUAbnacAHgAcA9ph5kWQA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6MgeyeUuX0Ra0A5gMRUytTt3xwy%2BDtFQZIY6AsjgwAXxBagA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkqyARhWANqTokBMADJwMyQAugBpwNSPFgATAK4Zm7Lr06RRNKCTyZk0PADk5qAMIALdNADWZSlpno14vAENMAc3R4FHHv0hiAviD%2BQA&tokens=false

class Uint8Array<T> {
  // ...
}

let a: Uint8Array<ArrayBufferLike> = new Uint8Array();

export {}

You'll want to use the scope manager to see whether the identifier is referencing something declared or imported in the file vs. a global. Searching on context.sourceCode.getScope should get you some good starting points.

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.

A good start! 🚀

@JoshuaKGoldberg JoshuaKGoldberg added the awaiting response Issues waiting for a reply from the OP or another party label Dec 14, 2024
@Josh-Cena
Copy link
Member

FWIW, my personal preference is still "we should not special case built-ins". These types of declarations can legitimately happen with user-defined types, so we should just create a generic allowlist of types.

@github-actions github-actions bot removed the awaiting response Issues waiting for a reply from the OP or another party label Dec 17, 2024
@mdm317
Copy link
Contributor Author

mdm317 commented Dec 17, 2024

Should we avoid dealing specifically with the built-in array-likes?

@JoshuaKGoldberg JoshuaKGoldberg changed the title fix: ignore when constructor is typed array fix(eslint-plugin): [consistent-generic-constructors] ignore when constructor is typed array Jan 13, 2025
@JoshuaKGoldberg
Copy link
Member

@Josh-Cena just confirming, what do you suggest we do here?

@Josh-Cena
Copy link
Member

This:

we should just create a generic allowlist of types.

Like a new option called ignore or allow where the types would just be ignored for consistency checks.

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

mdm317 commented Mar 14, 2025

Like a new option called ignore or allow where the types would just be ignored for consistency checks.

I added an allow option to ignore consistency checks.

Should I add TypedArray to the default option?

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.

Sorry for the back-and-forth on this rule's option(s)! It looks like we have a bit of design/discussion work to do before we're settled?

@@ -77,7 +96,8 @@ export default createRule<Options, MessageIds>({
lhs &&
(lhs.type !== AST_NODE_TYPES.TSTypeReference ||
lhs.typeName.type !== AST_NODE_TYPES.Identifier ||
lhs.typeName.name !== rhs.callee.name)
lhs.typeName.name !== rhs.callee.name ||
options?.ignore?.includes(lhs.typeName.name))
Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg Mar 31, 2025

Choose a reason for hiding this comment

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

Threading #10477 (comment):

Should I add TypedArray to the default option?

It looks like we don't have consensus on this yet?

One concern with having default values in the default list is that it gets inconvenient to add to the list rather than replace it. If the default type includes, say, Proxy and all the Uint*Arrays, then that's a lot of manual re-typing for folks to write if they want to keep ignoring them.

We're previously had options like ban-types > extendDefaults to get around this, but they're kind of clunky.

@Josh-Cena I think what's confusing me here is:

  • What benefit is there to reporting on Uint8Array and similar? I.e.: in what situations would one want the rule to report on them?
  • If there are none, why not hardcode the rule to always ignore them?

cc @kirkwaiblinger

ignore: {
type: 'array',
description:
'A list of constructor names to ignore when enforcing the rule.',
Copy link
Member

Choose a reason for hiding this comment

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

IMO if we do go with a list then we'd want to use TypeOrValueSpecifier. Otherwise it'll have the same issue as #10477 (comment) with coincidentally matching names.

But, I'm not sure this is the right step. I started a thread later in this file about it.

@JoshuaKGoldberg JoshuaKGoldberg added the triage Waiting for team members to take a look label Mar 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting response Issues waiting for a reply from the OP or another party triage Waiting for team members to take a look
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: [consistent-generic-constructors] improper fix into new Uint8Array<ArrayBufferLike>()
3 participants