Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
fix(rome_js_analyze): false positive useShorthandArrayTypes (#3119)
Browse files Browse the repository at this point in the history
* feat: 🎸 finsish

* fix: 🐛 cr issues
  • Loading branch information
IWANABETHATGUY authored Sep 2, 2022
1 parent f016baf commit a1a5cd3
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,16 @@ fn convert_to_array_type(type_arguments: TsTypeArguments) -> Option<TsType> {
.filter_map(|param| {
let param = param.ok()?;
let element_type = match &param {
TsType::TsUnionType(_) => None,
TsType::TsTypeOperatorType(_) => None,
// Intersection or higher types
TsType::TsUnionType(_)
| TsType::TsIntersectionType(_)
| TsType::TsFunctionType(_)
| TsType::TsConstructorType(_)
| TsType::TsConditionalType(_)
| TsType::TsTypeOperatorType(_)
| TsType::TsInferType(_)
| TsType::TsMappedType(_) => None,

TsType::TsReferenceType(ty) if is_array_reference(ty).unwrap_or(false) => {
if let Some(type_arguments) = ty.type_arguments() {
convert_to_array_type(type_arguments)
Expand Down
13 changes: 13 additions & 0 deletions crates/rome_js_analyze/tests/specs/ts/useShorthandArrayType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,16 @@ let invalid2: Promise<Array<string>>;
let invalid3: Array<Foo<Bar>>;
let invalid4: Array<[number, number]>;

// valid
let valid5: Array<string & number>;
let valid6: Array<() => string>;
type valid7<T> = Array<T extends string ? string : number>
type valid8 = Array<new (string, number) => string>
// valid end

//parenthesized type
let valid8: Array<(string & number)>;
// infer type
type valid9<T> = T extends Array<infer R> ? R : any;
// mapped type
type valid10<T> = { [K in keyof T]: T[K] };
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: crates/rome_js_analyze/tests/spec_tests.rs
assertion_line: 97
expression: useShorthandArrayType.ts
---
# Input
Expand All @@ -15,6 +16,19 @@ let invalid2: Promise<Array<string>>;
let invalid3: Array<Foo<Bar>>;
let invalid4: Array<[number, number]>;
// valid
let valid5: Array<string & number>;
let valid6: Array<() => string>;
type valid7<T> = Array<T extends string ? string : number>
type valid8 = Array<new (string, number) => string>
// valid end
//parenthesized type
let valid8: Array<(string & number)>;
// infer type
type valid9<T> = T extends Array<infer R> ? R : any;
// mapped type
type valid10<T> = { [K in keyof T]: T[K] };
```
Expand Down Expand Up @@ -111,14 +125,15 @@ warning[ts/useShorthandArrayType]: Use shorthand T[] syntax instead of Array<T>
│ ---------------
Suggested fix: Use shorthand T[] syntax to replace
| @@ -6,6 +6,6 @@
| @@ -6,7 +6,7 @@
5 5 | let valid: Array<foo>;
6 6 | let invalid1: Array<foo, Array<string>>;
7 7 | let invalid2: Promise<Array<string>>;
8 | - let invalid3: Array<Foo<Bar>>;
8 | + let invalid3: Foo<Bar>[];
9 9 | let invalid4: Array<[number, number]>;
10 10 |
11 11 | // valid
```
Expand All @@ -131,13 +146,36 @@ warning[ts/useShorthandArrayType]: Use shorthand T[] syntax instead of Array<T>
│ -----------------------
Suggested fix: Use shorthand T[] syntax to replace
| @@ -7,5 +7,5 @@
| @@ -7,7 +7,7 @@
6 6 | let invalid1: Array<foo, Array<string>>;
7 7 | let invalid2: Promise<Array<string>>;
8 8 | let invalid3: Array<Foo<Bar>>;
9 | - let invalid4: Array<[number, number]>;
9 | + let invalid4: [number, number][];
10 10 |
11 11 | // valid
12 12 | let valid5: Array<string & number>;
```
```
warning[ts/useShorthandArrayType]: Use shorthand T[] syntax instead of Array<T> syntax.
┌─ useShorthandArrayType.ts:20:13
20 │ let valid8: Array<(string & number)>;
│ ------------------------
Suggested fix: Use shorthand T[] syntax to replace
| @@ -17,7 +17,7 @@
16 16 | // valid end
17 17 |
18 18 | //parenthesized type
19 | - let valid8: Array<(string & number)>;
19 | + let valid8: (string & number)[];
20 20 | // infer type
21 21 | type valid9<T> = T extends Array<infer R> ? R : any;
22 22 | // mapped type
```
Expand Down

0 comments on commit a1a5cd3

Please sign in to comment.