diff --git a/crates/oxc_linter/src/rules/typescript/array_type.rs b/crates/oxc_linter/src/rules/typescript/array_type.rs index 3c11c7ce2df24..15772a17e4f40 100644 --- a/crates/oxc_linter/src/rules/typescript/array_type.rs +++ b/crates/oxc_linter/src/rules/typescript/array_type.rs @@ -215,6 +215,11 @@ impl Rule for ArrayType { ctx, ); } + AstKind::TSTypeParameterInstantiation(ts_type_param_instantiation) => { + for param in &ts_type_param_instantiation.params { + check(param, default_config, readonly_config, ctx); + } + } _ => {} } } @@ -1256,6 +1261,36 @@ fn test() { ("type x = Array[]", None), ("const arr: Array>[] = [];", None), ("export function fn4(arr: Array[]) { return arr; }", None), + ( + "function testFn(param: T) { return param; } +export const test2 = testFn<{name: string}[]>([]);", + Some(serde_json::json!([{"default":"array-simple"}])), + ), + ( + "function testFn(param: T) { return param; } +export const test2 = testFn>([]);", + Some(serde_json::json!([{"default":"array"}])), + ), + ( + "function testFn(param: T) { return param; } +export const test2 = testFn([]);", + Some(serde_json::json!([{"default":"generic"}])), + ), + ( + "function testFn(param: T) { return param; } +export const test2 = testFn<(string | number)[]>([]);", + Some(serde_json::json!([{"default":"generic"}])), + ), + ( + "function testFn(param: T) { return param; } +export const test2 = testFn([]);", + Some(serde_json::json!([{"default":"generic"}])), + ), + ( + "function testFn(param: T) { return param; } +export const test2 = testFn>([]);", + Some(serde_json::json!([{"default":"array"}])), + ), ]; let fix: Vec<(&str, &str, Option)> = vec![ @@ -1788,6 +1823,48 @@ fn test() { "let a: Promise> = Promise.resolve([]);", Some(serde_json::json!([{"default": "generic"}])), ), + ( + "function testFn(param: T) { return param; } +export const test2 = testFn<{name: string}[]>([]);", + "function testFn(param: T) { return param; } +export const test2 = testFn>([]);", + Some(serde_json::json!([{"default":"array-simple"}])), + ), + ( + "function testFn(param: T) { return param; } +export const test2 = testFn>([]);", + "function testFn(param: T) { return param; } +export const test2 = testFn<{name: string}[]>([]);", + Some(serde_json::json!([{"default":"array"}])), + ), + ( + "function testFn(param: T) { return param; } +export const test2 = testFn([]);", + "function testFn(param: T) { return param; } +export const test2 = testFn>([]);", + Some(serde_json::json!([{"default":"generic"}])), + ), + ( + "function testFn(param: T) { return param; } +export const test2 = testFn<(string | number)[]>([]);", + "function testFn(param: T) { return param; } +export const test2 = testFn>([]);", + Some(serde_json::json!([{"default":"generic"}])), + ), + ( + "function testFn(param: T) { return param; } +export const test2 = testFn([]);", + "function testFn(param: T) { return param; } +export const test2 = testFn>([]);", + Some(serde_json::json!([{"default":"generic"}])), + ), + ( + "function testFn(param: T) { return param; } +export const test2 = testFn>([]);", + "function testFn(param: T) { return param; } +export const test2 = testFn([]);", + Some(serde_json::json!([{"default":"array"}])), + ), ]; Tester::new(ArrayType::NAME, ArrayType::PLUGIN, pass, fail) diff --git a/crates/oxc_linter/src/snapshots/typescript_array_type.snap b/crates/oxc_linter/src/snapshots/typescript_array_type.snap index 3bfd64aae821e..79f347487120a 100644 --- a/crates/oxc_linter/src/snapshots/typescript_array_type.snap +++ b/crates/oxc_linter/src/snapshots/typescript_array_type.snap @@ -652,6 +652,13 @@ source: crates/oxc_linter/src/tester.rs ╰──── help: Replace `string[]` with `Array`. + ⚠ typescript-eslint(array-type): Array type using 'string[]' is forbidden. Use 'Array' instead. + ╭─[array_type.ts:1:16] + 1 │ let a: Promise = Promise.resolve([]); + · ──────── + ╰──── + help: Replace `string[]` with `Array`. + ⚠ typescript-eslint(array-type): Array type using 'Array' is forbidden. Use 'number[]' instead. ╭─[array_type.ts:1:10] 1 │ type x = Array[] @@ -666,9 +673,64 @@ source: crates/oxc_linter/src/tester.rs ╰──── help: Replace `Array>` with `Array[]`. + ⚠ typescript-eslint(array-type): Array type using 'Array' is forbidden. Use 'number[]' instead. + ╭─[array_type.ts:1:18] + 1 │ const arr: Array>[] = []; + · ───────────── + ╰──── + help: Replace `Array` with `number[]`. + ⚠ typescript-eslint(array-type): Array type using 'Array' is forbidden. Use 'number[]' instead. ╭─[array_type.ts:1:26] 1 │ export function fn4(arr: Array[]) { return arr; } · ───────────── ╰──── help: Replace `Array` with `number[]`. + + ⚠ typescript-eslint(array-type): Array type using 'T[]' is forbidden for non-simple types. Use 'Array' instead. + ╭─[array_type.ts:2:29] + 1 │ function testFn(param: T) { return param; } + 2 │ export const test2 = testFn<{name: string}[]>([]); + · ──────────────── + ╰──── + help: Replace `{name: string}[]` with `Array<{name: string}>`. + + ⚠ typescript-eslint(array-type): Array type using 'Array' is forbidden. Use 'T[]' instead. + ╭─[array_type.ts:2:29] + 1 │ function testFn(param: T) { return param; } + 2 │ export const test2 = testFn>([]); + · ───────────────────── + ╰──── + help: Replace `Array<{name: string}>` with `{name: string}[]`. + + ⚠ typescript-eslint(array-type): Array type using 'string[]' is forbidden. Use 'Array' instead. + ╭─[array_type.ts:2:29] + 1 │ function testFn(param: T) { return param; } + 2 │ export const test2 = testFn([]); + · ──────── + ╰──── + help: Replace `string[]` with `Array`. + + ⚠ typescript-eslint(array-type): Array type using 'T[]' is forbidden. Use 'Array' instead. + ╭─[array_type.ts:2:29] + 1 │ function testFn(param: T) { return param; } + 2 │ export const test2 = testFn<(string | number)[]>([]); + · ─────────────────── + ╰──── + help: Replace `(string | number)[]` with `Array`. + + ⚠ typescript-eslint(array-type): Array type using 'readonly string[]' is forbidden. Use 'ReadonlyArray' instead. + ╭─[array_type.ts:2:29] + 1 │ function testFn(param: T) { return param; } + 2 │ export const test2 = testFn([]); + · ───────────────── + ╰──── + help: Replace `readonly string[]` with `ReadonlyArray`. + + ⚠ typescript-eslint(array-type): Array type using 'ReadonlyArray' is forbidden. Use 'readonly string[]' instead. + ╭─[array_type.ts:2:29] + 1 │ function testFn(param: T) { return param; } + 2 │ export const test2 = testFn>([]); + · ───────────────────── + ╰──── + help: Replace `ReadonlyArray` with `readonly string[]`.