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

Appearance settings

Commit 2e75e31

Browse filesBrowse files
committed
fix: ignore when constructor is typed array
1 parent 0bff5f3 commit 2e75e31
Copy full SHA for 2e75e31

File tree

Expand file treeCollapse file tree

2 files changed

+40
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+40
-0
lines changed

‎packages/eslint-plugin/src/rules/consistent-generic-constructors.ts

Copy file name to clipboardExpand all lines: packages/eslint-plugin/src/rules/consistent-generic-constructors.ts
+30Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,30 @@ export default createRule<Options, MessageIds>({
3333
},
3434
defaultOptions: ['constructor'],
3535
create(context, [mode]) {
36+
const isTypedArrayReference = (typeNode: TSESTree.TypeNode) => {
37+
const typedArrayNames = [
38+
'Int8Array',
39+
'Uint8Array',
40+
'Uint8ClampedArray',
41+
'Int16Array',
42+
'Uint16Array',
43+
'Int32Array',
44+
'Uint32Array',
45+
'BigInt64Array',
46+
'BigUint64Array',
47+
'Float32Array',
48+
'Float64Array',
49+
];
50+
if (
51+
typeNode.type === AST_NODE_TYPES.TSTypeReference &&
52+
typeNode.typeName.type === AST_NODE_TYPES.Identifier &&
53+
typedArrayNames.includes(typeNode.typeName.name)
54+
) {
55+
return true;
56+
}
57+
return false;
58+
};
59+
3660
return {
3761
'VariableDeclarator,PropertyDefinition,:matches(FunctionDeclaration,FunctionExpression) > AssignmentPattern'(
3862
node:
@@ -60,6 +84,12 @@ export default createRule<Options, MessageIds>({
6084
const [lhsName, rhs] = getLHSRHS();
6185
const lhs = lhsName.typeAnnotation?.typeAnnotation;
6286

87+
// If it's a typed array, we will ignore.
88+
// https://github.com/typescript-eslint/typescript-eslint/issues/10445
89+
if (lhs && isTypedArrayReference(lhs)) {
90+
return;
91+
}
92+
6393
if (
6494
!rhs ||
6595
rhs.type !== AST_NODE_TYPES.NewExpression ||

‎packages/eslint-plugin/tests/rules/consistent-generic-constructors.test.ts

Copy file name to clipboardExpand all lines: packages/eslint-plugin/tests/rules/consistent-generic-constructors.test.ts
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ class A {
4040
`
4141
const a = function (a: Foo = new Foo<string>()) {};
4242
`,
43+
'let a: Uint8Array<ArrayBufferLike> = new Uint8Array();',
44+
'let a: Uint8ClampedArray<ArrayBufferLike> = new Uint8ClampedArray();',
45+
'let a: Int16Array<ArrayBufferLike> = new Int16Array();',
46+
'let a: Uint16Array<ArrayBufferLike> = new Uint16Array();',
47+
'let a: Int32Array<ArrayBufferLike> = new Int32Array();',
48+
'let a: Uint32Array<ArrayBufferLike> = new Uint32Array();',
49+
'let a: BigInt64Array<ArrayBufferLike> = new BigInt64Array();',
50+
'let a: BigUint64Array<ArrayBufferLike> = new BigUint64Array();',
51+
'let a: Float32Array<ArrayBufferLike> = new Float32Array();',
52+
'let a: Float64Array<ArrayBufferLike> = new Float64Array();',
4353
// type-annotation
4454
{
4555
code: 'const a = new Foo();',

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.