Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
mdm317 wants to merge11 commits intotypescript-eslint:main
base:main
Choose a base branch
Loading
frommdm317:fix/issue-10445-consistent-generic-constructors
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
11 commits
Select commitHold shift + click to select a range
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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -79,6 +79,37 @@ const set: Set<string> = new Set<string>();
</TabItem>
</Tabs>

### `ignore`

{/* insert option description */}

Some constructors have different type signatures between their type and value, for example like Uint8Array.

<Tabs>
<TabItem value="❌ Incorrect">

```ts option='"constructor"'
// Incorrect because Uint8Array has deffenrent type signature and not in ignorelist
let a: Uint8Array<ArrayBufferLike> = new Uint8Array();

// Incorrect because type arguments appear in type-annotation and not in ignorelist
let a: UserConstructor<Type> = new UserConstructor();
```

</TabItem>
<TabItem value="✅ Correct">

```ts option='"constructor", { "ignore": ["Uint8Array", "UserConstructor"] }' showPlaygroundButton
// Correct because Uint8Array has deffenrent type signature but are included in the ignorelist.
let a: Uint8Array<ArrayBufferLike> = new Uint8Array();

// Correct because type arguments appear in type-annotations but are included in the ignorelist.
let a: UserConstructor<Type> = new UserConstructor();
```

</TabItem>
</Tabs>

## When Not To Use It

You can turn this rule off if you don't want to enforce one kind of generic constructor style over the other.
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,8 +4,13 @@ import { AST_NODE_TYPES } from '@typescript-eslint/utils';

import { createRule, nullThrows, NullThrowsReasons } from '../util';

export type MessageIds = 'preferConstructor' | 'preferTypeAnnotation';
export type Options = ['constructor' | 'type-annotation'];
type MessageIds = 'preferConstructor' | 'preferTypeAnnotation';
type Options = [
'constructor' | 'type-annotation',
{
ignore?: string[];
}?,
];

export default createRule<Options, MessageIds>({
name: 'consistent-generic-constructors',
Expand All@@ -29,10 +34,24 @@ export default createRule<Options, MessageIds>({
description: 'Which constructor call syntax to prefer.',
enum: ['type-annotation', 'constructor'],
},
{
type: 'object',
additionalProperties: false,
properties: {
ignore: {
type: 'array',
description:
'A list of constructor names to ignore when enforcing the rule.',

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 useTypeOrValueSpecifier. 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.

mdm317 reacted with thumbs up emoji
items: {
type: 'string',
},
},
},
},
],
},
defaultOptions: ['constructor'],
create(context, [mode]) {
defaultOptions: ['constructor', {}],
create(context, [mode, options]) {
return {
'VariableDeclarator,PropertyDefinition,AccessorProperty,:matches(FunctionDeclaration,FunctionExpression) > AssignmentPattern'(
node:
Expand DownExpand Up@@ -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

@JoshuaKGoldbergJoshuaKGoldbergMar 31, 2025
edited
Loading

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 theUint*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 likeban-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 onUint8Array 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

Copy link
Member

Choose a reason for hiding this comment

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

  • What benefit is there to reporting onUint8Array and similar? I.e.: in what situations would one want the rule to report on them?

There's none; I would expect them to be ignored by default.

If there are none, why not hardcode the rule to always ignore them?

Therule should be generic. Any library-specific logic (even those related to native objects) should be injected via options. Built-in objects should not be distinguishable from user-defined objects.

Choose a reason for hiding this comment

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

Ok great I think we're aligned? Just confirming, I'm of the opinion that:

  • The rule should not report those things by default
  • The list should be empty ([]) by default

...so the rule should not provide a way to now report on those built-ins?

) {
return;
}
Expand Down

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

View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -45,6 +45,10 @@ class A {
`
const a = function (a: Foo = new Foo<string>()) {};
`,
{
code: 'let a: Uint8Array<ArrayBufferLike> = new Uint8Array();',
options: ['constructor', { ignore: ['Uint8Array'] }],
},
// type-annotation
{
code: 'const a = new Foo();',
Expand Down
View file
Open in desktop

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


[8]ページ先頭

©2009-2025 Movatter.jp