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
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
56 changes: 56 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: const arr = [] 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"}])),
),
// https://github.com/oxc-project/oxc/issues/16897 - satisfies expression
(
"const arr = [] as const satisfies ReadonlyArray<string>;",
Some(serde_json::json!([{"default":"array-simple","readonly":"generic"}])),
),
(
"const arr = [] as const satisfies readonly string[];",
Some(serde_json::json!([{"default":"array-simple","readonly":"array"}])),
),
];

let fail = vec![
Expand Down Expand Up @@ -1456,6 +1474,23 @@ export const test8 = testFn<Array<string>, number[]>([]);",
"type MakeArrays<T> = { [K in keyof T]: T[K][] };",
Some(serde_json::json!([{"default":"generic"}])),
),
// https://github.com/oxc-project/oxc/issues/16897 - satisfies expression
(
"const arr = [] as const satisfies readonly string[];",
Some(serde_json::json!([{"default":"array-simple","readonly":"generic"}])),
),
(
"const arr = [] as const satisfies readonly SupportedDomainName[];",
Some(serde_json::json!([{"default":"array-simple","readonly":"generic"}])),
),
(
"const arr = [] as const satisfies ReadonlyArray<string>;",
Some(serde_json::json!([{"default":"array-simple","readonly":"array"}])),
),
(
"const arr = [] as const satisfies string[];",
Some(serde_json::json!([{"default":"generic"}])),
),
];

let fix: Vec<(&str, &str, Option<serde_json::Value>)> = vec![
Expand Down Expand Up @@ -2103,6 +2138,27 @@ export const test9 = testFn<ReadonlyArray<number>>([]);",
export const test9 = testFn<readonly number[]>([]);",
Some(serde_json::json!([{"default":"array-simple"}])),
),
// https://github.com/oxc-project/oxc/issues/16897 - satisfies expression
(
"const arr = [] as const satisfies readonly string[];",
"const arr = [] as const satisfies ReadonlyArray<string>;",
Some(serde_json::json!([{"default":"array-simple","readonly":"generic"}])),
),
(
"const arr = [] as const satisfies readonly SupportedDomainName[];",
"const arr = [] as const satisfies ReadonlyArray<SupportedDomainName>;",
Some(serde_json::json!([{"default":"array-simple","readonly":"generic"}])),
),
(
"const arr = [] as const satisfies ReadonlyArray<string>;",
"const arr = [] as const satisfies readonly string[];",
Some(serde_json::json!([{"default":"array-simple","readonly":"array"}])),
),
(
"const arr = [] as const satisfies string[];",
"const arr = [] as const satisfies Array<string>;",
Some(serde_json::json!([{"default":"generic"}])),
),
];

Tester::new(ArrayType::NAME, ArrayType::PLUGIN, pass, fail)
Expand Down
28 changes: 28 additions & 0 deletions crates/oxc_linter/src/snapshots/typescript_array_type.snap
Original file line number Diff line number Diff line change
Expand Up @@ -901,3 +901,31 @@ 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:35]
1 │ const arr = [] as const satisfies readonly string[];
· ─────────────────
╰────
help: Replace `readonly string[]` with `ReadonlyArray<string>`.

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

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

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