Skip to content
Closed
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
1 change: 1 addition & 0 deletions crates/oxc_linter/src/generated/rule_runner_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2532,6 +2532,7 @@ impl RuleRunner for crate::rules::typescript::array_type::ArrayType {
AstType::TSConditionalType,
AstType::TSIndexedAccessType,
AstType::TSMappedType,
AstType::TSSatisfiesExpression,
AstType::TSTypeAliasDeclaration,
AstType::TSTypeAnnotation,
AstType::TSTypeParameterInstantiation,
Expand Down
49 changes: 49 additions & 0 deletions crates/oxc_linter/src/rules/typescript/array_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,15 @@ impl Rule for ArrayType {
ctx,
);
}
// for example: [] as const satisfies readonly string[];
AstKind::TSSatisfiesExpression(ts_satisfies_expression) => {
check(
&ts_satisfies_expression.type_annotation,
self.default_config(),
self.readonly_config(),
ctx,
);
}
AstKind::TSTypeReference(ts_type_reference)
if outermost_paren_parent(node, ctx).is_some_and(|x| match x.kind() {
AstKind::TSTypeAliasDeclaration(TSTypeAliasDeclaration {
Expand Down Expand Up @@ -965,6 +974,15 @@ const instance = new MyClass<number>(42);",
"let z: readonly factories.User[] = [];",
Some(serde_json::json!([{"readonly":"array-simple"}])),
),
// TSSatisfiesExpression
(
"const x = [] as const satisfies string[];",
Some(serde_json::json!([{"default":"array"}])),
),
(
"const x = [] as const satisfies ReadonlyArray<string>;",
Some(serde_json::json!([{"default":"array-simple","readonly":"generic"}])),
),
];

let fail = vec![
Expand Down Expand Up @@ -1456,6 +1474,20 @@ export const test8 = testFn<Array<string>, number[]>([]);",
"type MakeArrays<T> = { [K in keyof T]: T[K][] };",
Some(serde_json::json!([{"default":"generic"}])),
),
// TSSatisfiesExpression - https://github.com/oxc-project/oxc/issues/16897
(
"const x = [] as const satisfies readonly string[];",
Some(serde_json::json!([{"default":"array-simple","readonly":"generic"}])),
),
(
"const x = [] as const satisfies Array<string>;",
Some(serde_json::json!([{"default":"array"}])),
),
// nested: only outer Array<string[]> should be reported, inner string[] is correct
(
"const x = [] satisfies Array<string[]>;",
Some(serde_json::json!([{"default":"array"}])),
),
];

let fix: Vec<(&str, &str, Option<serde_json::Value>)> = vec![
Expand Down Expand Up @@ -2103,6 +2135,23 @@ export const test9 = testFn<ReadonlyArray<number>>([]);",
export const test9 = testFn<readonly number[]>([]);",
Some(serde_json::json!([{"default":"array-simple"}])),
),
// TSSatisfiesExpression - https://github.com/oxc-project/oxc/issues/16897
(
"const x = [] as const satisfies readonly string[];",
"const x = [] as const satisfies ReadonlyArray<string>;",
Some(serde_json::json!([{"default":"array-simple","readonly":"generic"}])),
),
(
"const x = [] as const satisfies Array<string>;",
"const x = [] as const satisfies string[];",
Some(serde_json::json!([{"default":"array"}])),
),
// nested: only outer Array<string[]> should be fixed
(
"const x = [] satisfies Array<string[]>;",
"const x = [] satisfies string[][];",
Some(serde_json::json!([{"default":"array"}])),
),
];

Tester::new(ArrayType::NAME, ArrayType::PLUGIN, pass, fail)
Expand Down
22 changes: 22 additions & 0 deletions crates/oxc_linter/src/snapshots/typescript_array_type.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: crates/oxc_linter/src/tester.rs
assertion_line: 394
---
⚠ typescript-eslint(array-type): Array type using 'factories.User[]' is forbidden. Use 'Array<factories.User>' instead.
╭─[array_type.ts:1:8]
Expand Down Expand Up @@ -901,3 +902,24 @@ source: crates/oxc_linter/src/tester.rs
· ──────
╰────
help: Replace `T[K][]` with `Array<T[K]>`.

⚠ typescript-eslint(array-type): Array type using 'readonly string[]' is forbidden. Use 'ReadonlyArray<string>' instead.
╭─[array_type.ts:1:33]
1 │ const x = [] as const satisfies readonly string[];
· ─────────────────
╰────
help: Replace `readonly string[]` with `ReadonlyArray<string>`.

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

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