@@ -9,7 +9,7 @@ const ruleTester = new RuleTester({
9
9
ruleTester . run ( 'array-type' , rule , {
10
10
valid : [
11
11
{
12
- code : 'let a = []' ,
12
+ code : 'let a: readonly any[] = []' ,
13
13
options : [ 'array' ] ,
14
14
} ,
15
15
{
@@ -826,31 +826,87 @@ let yyyy: Arr<Array<Array<Arr<string>>>> = [[[["2"]]]];`,
826
826
} ,
827
827
] ,
828
828
} ,
829
+
830
+ // readonly tests
831
+ {
832
+ code : 'const x: readonly number[] = [];' ,
833
+ output : 'const x: ReadonlyArray<number> = [];' ,
834
+ options : [ 'generic' ] ,
835
+ errors : [
836
+ {
837
+ messageId : 'errorStringGeneric' ,
838
+ data : { type : 'number' } ,
839
+ line : 1 ,
840
+ column : 10 ,
841
+ } ,
842
+ ] ,
843
+ } ,
844
+ {
845
+ code : 'const x: readonly (number | string | boolean)[] = [];' ,
846
+ output : 'const x: ReadonlyArray<number | string | boolean> = [];' ,
847
+ options : [ 'generic' ] ,
848
+ errors : [
849
+ {
850
+ messageId : 'errorStringGeneric' ,
851
+ data : { type : 'T' } ,
852
+ line : 1 ,
853
+ column : 10 ,
854
+ } ,
855
+ ] ,
856
+ } ,
857
+ {
858
+ code : 'const x: ReadonlyArray<number> = [];' ,
859
+ output : 'const x: readonly number[] = [];' ,
860
+ options : [ 'array' ] ,
861
+ errors : [
862
+ {
863
+ messageId : 'errorStringArray' ,
864
+ data : { type : 'number' } ,
865
+ line : 1 ,
866
+ column : 10 ,
867
+ } ,
868
+ ] ,
869
+ } ,
870
+ {
871
+ code : 'const x: ReadonlyArray<number | string | boolean> = [];' ,
872
+ output : 'const x: readonly (number | string | boolean)[] = [];' ,
873
+ options : [ 'array' ] ,
874
+ errors : [
875
+ {
876
+ messageId : 'errorStringArray' ,
877
+ data : { type : 'T' } ,
878
+ line : 1 ,
879
+ column : 10 ,
880
+ } ,
881
+ ] ,
882
+ } ,
829
883
] ,
830
884
} ) ;
831
885
832
886
// eslint rule tester is not working with multi-pass
833
887
// https://github.com/eslint/eslint/issues/11187
834
888
describe ( 'array-type (nested)' , ( ) => {
835
- it ( 'should fix correctly' , ( ) => {
889
+ describe ( 'should deeply fix correctly' , ( ) => {
836
890
function testOutput ( option : string , code : string , output : string ) : void {
837
- const linter = new Linter ( ) ;
891
+ it ( code , ( ) => {
892
+ const linter = new Linter ( ) ;
838
893
839
- linter . defineRule ( 'array-type' , Object . assign ( { } , rule ) as any ) ;
840
- const result = linter . verifyAndFix (
841
- code ,
842
- {
843
- rules : {
844
- 'array-type' : [ 2 , option ] ,
894
+ linter . defineRule ( 'array-type' , Object . assign ( { } , rule ) as any ) ;
895
+ const result = linter . verifyAndFix (
896
+ code ,
897
+ {
898
+ rules : {
899
+ 'array-type' : [ 2 , option ] ,
900
+ } ,
901
+ parser : '@typescript-eslint/parser' ,
845
902
} ,
846
- parser : '@typescript-eslint/parser' ,
847
- } ,
848
- {
849
- fix : true ,
850
- } ,
851
- ) ;
903
+ {
904
+ fix : true ,
905
+ } ,
906
+ ) ;
852
907
853
- expect ( output ) . toBe ( result . output ) ;
908
+ expect ( result . output ) . toBe ( output ) ;
909
+ } ) ;
854
910
}
855
911
856
912
testOutput (
@@ -894,5 +950,32 @@ class Foo<T = Bar[][]> extends Bar<T, T[]> implements Baz<T[]> {
894
950
`let yy: number[][] = [[4, 5], [6]];` ,
895
951
`let yy: Array<Array<number>> = [[4, 5], [6]];` ,
896
952
) ;
953
+
954
+ // readonly
955
+ testOutput (
956
+ 'generic' ,
957
+ `let x: readonly number[][]` ,
958
+ `let x: ReadonlyArray<Array<number>>` ,
959
+ ) ;
960
+ testOutput (
961
+ 'generic' ,
962
+ `let x: readonly (readonly number[])[]` ,
963
+ `let x: ReadonlyArray<ReadonlyArray<number>>` ,
964
+ ) ;
965
+ testOutput (
966
+ 'array' ,
967
+ `let x: ReadonlyArray<Array<number>>` ,
968
+ `let x: readonly number[][]` ,
969
+ ) ;
970
+ testOutput (
971
+ 'array' ,
972
+ `let x: ReadonlyArray<ReadonlyArray<number>>` ,
973
+ `let x: readonly (readonly number[])[]` ,
974
+ ) ;
975
+ testOutput (
976
+ 'array' ,
977
+ `let x: ReadonlyArray<readonly number[]>` ,
978
+ `let x: readonly (readonly number[])[]` ,
979
+ ) ;
897
980
} ) ;
898
981
} ) ;
0 commit comments