Skip to content

Commit 346045a

Browse files
committed
feat(linter/id-length): add checkGeneric option (#19634)
Fixes #19617
1 parent bce92db commit 346045a

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

crates/oxc_linter/src/rules/eslint/id_length.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ pub struct IdLengthConfig {
6565
max: u64,
6666
/// The minimum number of graphemes required in an identifier.
6767
min: u64,
68+
/// Whether to check TypeScript generic type parameter names.
69+
/// Defaults to `true`.
70+
check_generic: bool,
6871
/// When set to `"never"`, property names are not checked for length.
6972
/// When set to `"always"` (default), property names are checked just like other identifiers.
7073
properties: PropertyKind,
@@ -77,6 +80,7 @@ impl Default for IdLengthConfig {
7780
exceptions: vec![],
7881
max: DEFAULT_MAX_LENGTH,
7982
min: DEFAULT_MIN_LENGTH,
83+
check_generic: true,
8084
properties: PropertyKind::default(),
8185
}
8286
}
@@ -193,6 +197,10 @@ impl Rule for IdLength {
193197
.and_then(|map| map.get("min"))
194198
.and_then(Value::as_u64)
195199
.unwrap_or(DEFAULT_MIN_LENGTH),
200+
check_generic: object
201+
.and_then(|map| map.get("checkGeneric"))
202+
.and_then(Value::as_bool)
203+
.unwrap_or(true),
196204
properties: object
197205
.and_then(|map| map.get("properties"))
198206
.and_then(Value::as_str)
@@ -245,6 +253,9 @@ impl IdLength {
245253
}
246254

247255
let parent_node = ctx.nodes().parent_node(node.id());
256+
if !self.check_generic && matches!(parent_node.kind(), AstKind::TSTypeParameter(_)) {
257+
return;
258+
}
248259

249260
match parent_node.kind() {
250261
AstKind::ImportSpecifier(import_specifier) => {
@@ -595,6 +606,10 @@ fn test() {
595606
("class Foo { #abc() {} }", Some(serde_json::json!([{ "max": 3 }]))), // { "ecmaVersion": 2022 },
596607
("class Foo { abc = 1 }", Some(serde_json::json!([{ "max": 3 }]))), // { "ecmaVersion": 2022 },
597608
("class Foo { #abc = 1 }", Some(serde_json::json!([{ "max": 3 }]))), // { "ecmaVersion": 2022 },
609+
(
610+
"export type Example<T> = T extends Array<infer U> ? U : never;",
611+
Some(serde_json::json!([{ "min": 2, "checkGeneric": false }])),
612+
),
598613
("var 𠮟 = 2", Some(serde_json::json!([{ "min": 1, "max": 1 }]))), // { "ecmaVersion": 6 },
599614
("var 葛󠄀 = 2", Some(serde_json::json!([{ "min": 1, "max": 1 }]))), // { "ecmaVersion": 6 },
600615
("var a = { 𐌘: 1 };", Some(serde_json::json!([{ "min": 1, "max": 1 }]))), // { "ecmaVersion": 6, },
@@ -713,6 +728,10 @@ fn test() {
713728
("class Foo { #abcdefg() {} }", Some(serde_json::json!([{ "max": 3 }]))), // { "ecmaVersion": 2022 },
714729
("class Foo { abcdefg = 1 }", Some(serde_json::json!([{ "max": 3 }]))), // { "ecmaVersion": 2022 },
715730
("class Foo { #abcdefg = 1 }", Some(serde_json::json!([{ "max": 3 }]))), // { "ecmaVersion": 2022 },
731+
(
732+
"export type Example<T> = T extends Array<infer U> ? U : never;",
733+
Some(serde_json::json!([{ "min": 2, "checkGeneric": true }])),
734+
),
716735
("var 𠮟 = 2", None), // { "ecmaVersion": 6 },
717736
("var 葛󠄀 = 2", None), // { "ecmaVersion": 6 },
718737
("var myObj = { 𐌘: 1 };", None), // { "ecmaVersion": 6, },

crates/oxc_linter/src/snapshots/eslint_id_length.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,18 @@ source: crates/oxc_linter/src/tester.rs
428428
· ────────
429429
╰────
430430

431+
eslint(id-length): Identifier name is too short (< 2).
432+
╭─[id_length.tsx:1:21]
433+
1export type Example<T> = T extends Array<infer U> ? U : never;
434+
· ─
435+
╰────
436+
437+
eslint(id-length): Identifier name is too short (< 2).
438+
╭─[id_length.tsx:1:48]
439+
1export type Example<T> = T extends Array<infer U> ? U : never;
440+
· ─
441+
╰────
442+
431443
eslint(id-length): Identifier name is too short (< 2).
432444
╭─[id_length.tsx:1:5]
433445
1var 𠮟 = 2

0 commit comments

Comments
 (0)