Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
77 changes: 77 additions & 0 deletions crates/oxc_linter/src/rules/typescript/array_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
_ => {}
}
}
Expand Down Expand Up @@ -1256,6 +1261,36 @@ fn test() {
("type x = Array<number>[]", None),
("const arr: Array<Array<number>>[] = [];", None),
("export function fn4(arr: Array<number>[]) { return arr; }", None),
(
"function testFn<T>(param: T) { return param; }
export const test2 = testFn<{name: string}[]>([]);",
Some(serde_json::json!([{"default":"array-simple"}])),
),
(
"function testFn<T>(param: T) { return param; }
export const test2 = testFn<Array<{name: string}>>([]);",
Some(serde_json::json!([{"default":"array"}])),
),
(
"function testFn<T>(param: T) { return param; }
export const test2 = testFn<string[]>([]);",
Some(serde_json::json!([{"default":"generic"}])),
),
(
"function testFn<T>(param: T) { return param; }
export const test2 = testFn<(string | number)[]>([]);",
Some(serde_json::json!([{"default":"generic"}])),
),
(
"function testFn<T>(param: T) { return param; }
export const test2 = testFn<readonly string[]>([]);",
Some(serde_json::json!([{"default":"generic"}])),
),
(
"function testFn<T>(param: T) { return param; }
export const test2 = testFn<ReadonlyArray<string>>([]);",
Some(serde_json::json!([{"default":"array"}])),
),
];

let fix: Vec<(&str, &str, Option<serde_json::Value>)> = vec![
Expand Down Expand Up @@ -1788,6 +1823,48 @@ fn test() {
"let a: Promise<Array<string>> = Promise.resolve([]);",
Some(serde_json::json!([{"default": "generic"}])),
),
(
"function testFn<T>(param: T) { return param; }
export const test2 = testFn<{name: string}[]>([]);",
"function testFn<T>(param: T) { return param; }
export const test2 = testFn<Array<{name: string}>>([]);",
Some(serde_json::json!([{"default":"array-simple"}])),
),
(
"function testFn<T>(param: T) { return param; }
export const test2 = testFn<Array<{name: string}>>([]);",
"function testFn<T>(param: T) { return param; }
export const test2 = testFn<{name: string}[]>([]);",
Some(serde_json::json!([{"default":"array"}])),
),
(
"function testFn<T>(param: T) { return param; }
export const test2 = testFn<string[]>([]);",
"function testFn<T>(param: T) { return param; }
export const test2 = testFn<Array<string>>([]);",
Some(serde_json::json!([{"default":"generic"}])),
),
(
"function testFn<T>(param: T) { return param; }
export const test2 = testFn<(string | number)[]>([]);",
"function testFn<T>(param: T) { return param; }
export const test2 = testFn<Array<string | number>>([]);",
Some(serde_json::json!([{"default":"generic"}])),
),
(
"function testFn<T>(param: T) { return param; }
export const test2 = testFn<readonly string[]>([]);",
"function testFn<T>(param: T) { return param; }
export const test2 = testFn<ReadonlyArray<string>>([]);",
Some(serde_json::json!([{"default":"generic"}])),
),
(
"function testFn<T>(param: T) { return param; }
export const test2 = testFn<ReadonlyArray<string>>([]);",
"function testFn<T>(param: T) { return param; }
export const test2 = testFn<readonly string[]>([]);",
Some(serde_json::json!([{"default":"array"}])),
),
];

Tester::new(ArrayType::NAME, ArrayType::PLUGIN, pass, fail)
Expand Down
62 changes: 62 additions & 0 deletions crates/oxc_linter/src/snapshots/typescript_array_type.snap
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,13 @@ source: crates/oxc_linter/src/tester.rs
╰────
help: Replace `string[]` with `Array<string>`.

⚠ typescript-eslint(array-type): Array type using 'string[]' is forbidden. Use 'Array<string>' instead.
╭─[array_type.ts:1:16]
1 │ let a: Promise<string[]> = Promise.resolve([]);
· ────────
╰────
help: Replace `string[]` with `Array<string>`.

⚠ typescript-eslint(array-type): Array type using 'Array<number>' is forbidden. Use 'number[]' instead.
╭─[array_type.ts:1:10]
1 │ type x = Array<number>[]
Expand All @@ -666,9 +673,64 @@ source: crates/oxc_linter/src/tester.rs
╰────
help: Replace `Array<Array<number>>` with `Array<number>[]`.

⚠ typescript-eslint(array-type): Array type using 'Array<number>' is forbidden. Use 'number[]' instead.
╭─[array_type.ts:1:18]
1 │ const arr: Array<Array<number>>[] = [];
· ─────────────
╰────
help: Replace `Array<number>` with `number[]`.

⚠ typescript-eslint(array-type): Array type using 'Array<number>' is forbidden. Use 'number[]' instead.
╭─[array_type.ts:1:26]
1 │ export function fn4(arr: Array<number>[]) { return arr; }
· ─────────────
╰────
help: Replace `Array<number>` with `number[]`.

⚠ typescript-eslint(array-type): Array type using 'T[]' is forbidden for non-simple types. Use 'Array<T>' instead.
╭─[array_type.ts:2:29]
1 │ function testFn<T>(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<T>' is forbidden. Use 'T[]' instead.
╭─[array_type.ts:2:29]
1 │ function testFn<T>(param: T) { return param; }
2 │ export const test2 = testFn<Array<{name: string}>>([]);
· ─────────────────────
╰────
help: Replace `Array<{name: string}>` with `{name: string}[]`.

⚠ typescript-eslint(array-type): Array type using 'string[]' is forbidden. Use 'Array<string>' instead.
╭─[array_type.ts:2:29]
1 │ function testFn<T>(param: T) { return param; }
2 │ export const test2 = testFn<string[]>([]);
· ────────
╰────
help: Replace `string[]` with `Array<string>`.

⚠ typescript-eslint(array-type): Array type using 'T[]' is forbidden. Use 'Array<T>' instead.
╭─[array_type.ts:2:29]
1 │ function testFn<T>(param: T) { return param; }
2 │ export const test2 = testFn<(string | number)[]>([]);
· ───────────────────
╰────
help: Replace `(string | number)[]` with `Array<string | number>`.

⚠ typescript-eslint(array-type): Array type using 'readonly string[]' is forbidden. Use 'ReadonlyArray<string>' instead.
╭─[array_type.ts:2:29]
1 │ function testFn<T>(param: T) { return param; }
2 │ export const test2 = testFn<readonly string[]>([]);
· ─────────────────
╰────
help: Replace `readonly string[]` with `ReadonlyArray<string>`.

⚠ typescript-eslint(array-type): Array type using 'ReadonlyArray<string>' is forbidden. Use 'readonly string[]' instead.
╭─[array_type.ts:2:29]
1 │ function testFn<T>(param: T) { return param; }
2 │ export const test2 = testFn<ReadonlyArray<string>>([]);
· ─────────────────────
╰────
help: Replace `ReadonlyArray<string>` with `readonly string[]`.
Loading