Skip to content

Commit 5384550

Browse files
authored
build: enforce consistent readonly array type (#20290)
1 parent 92af5f1 commit 5384550

File tree

22 files changed

+47
-41
lines changed

22 files changed

+47
-41
lines changed

src/cdk-experimental/popover-edit/popover-edit.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {LEFT_ARROW, UP_ARROW, RIGHT_ARROW, DOWN_ARROW, TAB} from '@angular/cdk/k
33
import {CdkTableModule} from '@angular/cdk/table';
44
import {dispatchKeyboardEvent} from '@angular/cdk/testing/private';
55
import {CommonModule} from '@angular/common';
6-
import {Component, Directive, ElementRef, Type, ViewChild} from '@angular/core';
6+
import {Component, Directive, ElementRef, ViewChild} from '@angular/core';
77
import {ComponentFixture, fakeAsync, flush, TestBed, tick, inject} from '@angular/core/testing';
88
import {FormsModule, NgForm} from '@angular/forms';
99
import {BidiModule, Direction} from '@angular/cdk/bidi';
@@ -359,12 +359,12 @@ class CdkTableInCell extends BaseTestComponent {
359359
}
360360
}
361361

362-
const testCases: ReadonlyArray<[Type<BaseTestComponent>, string]> = [
362+
const testCases = [
363363
[VanillaTableOutOfCell, 'Vanilla HTML table; edit defined outside of cell'],
364364
[VanillaTableInCell, 'Vanilla HTML table; edit defined within cell'],
365365
[CdkFlexTableInCell, 'Flex cdk-table; edit defined within cell'],
366366
[CdkTableInCell, 'Table cdk-table; edit defined within cell'],
367-
];
367+
] as const;
368368

369369
describe('CDK Popover Edit', () => {
370370
for (const [componentClass, label] of testCases) {
@@ -381,7 +381,7 @@ describe('CDK Popover Edit', () => {
381381
inject([OverlayContainer], (oc: OverlayContainer) => {
382382
overlayContainer = oc;
383383
})();
384-
fixture = TestBed.createComponent(componentClass);
384+
fixture = TestBed.createComponent<BaseTestComponent>(componentClass);
385385
component = fixture.componentInstance;
386386
fixture.detectChanges();
387387
tick(10);

src/cdk-experimental/selection/selection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export class CdkSelection<T> implements OnInit, AfterContentChecked, CollectionV
9393
return;
9494
}
9595

96-
let dataStream: Observable<T[]|ReadonlyArray<T>>|undefined;
96+
let dataStream: Observable<readonly T[]>|undefined;
9797

9898
if (isDataSource(this._dataSource)) {
9999
dataStream = this._dataSource.connect(this);
@@ -219,4 +219,4 @@ export class CdkSelection<T> implements OnInit, AfterContentChecked, CollectionV
219219
}
220220

221221
type SelectAllState = 'all'|'none'|'partial';
222-
type TableDataSource<T> = DataSource<T>|Observable<ReadonlyArray<T>|T[]>|ReadonlyArray<T>|T[];
222+
type TableDataSource<T> = DataSource<T>|Observable<readonly T[]>|readonly T[];

src/cdk/collections/array-data-source.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import {DataSource} from './data-source';
1212

1313
/** DataSource wrapper for a native array. */
1414
export class ArrayDataSource<T> extends DataSource<T> {
15-
constructor(private _data: T[] | ReadonlyArray<T> | Observable<T[] | ReadonlyArray<T>>) {
15+
constructor(private _data: readonly T[] | Observable<readonly T[]>) {
1616
super();
1717
}
1818

19-
connect(): Observable<T[] | ReadonlyArray<T>> {
19+
connect(): Observable<readonly T[]> {
2020
return isObservable(this._data) ? this._data : observableOf(this._data);
2121
}
2222

src/cdk/collections/data-source.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export abstract class DataSource<T> {
1818
* data source.
1919
* @returns Observable that emits a new value when the data changes.
2020
*/
21-
abstract connect(collectionViewer: CollectionViewer): Observable<T[] | ReadonlyArray<T>>;
21+
abstract connect(collectionViewer: CollectionViewer): Observable<readonly T[]>;
2222

2323
/**
2424
* Disconnects a collection viewer (such as a data-table) from this data source. Can be used

src/cdk/drag-drop/drop-list-ref.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,10 @@ export class DropListRef<T = any> {
161161
private _previousSwap = {drag: null as DragRef | null, delta: 0, overlaps: false};
162162

163163
/** Draggable items in the container. */
164-
private _draggables: ReadonlyArray<DragRef> = [];
164+
private _draggables: readonly DragRef[] = [];
165165

166166
/** Drop lists that are connected to the current one. */
167-
private _siblings: ReadonlyArray<DropListRef> = [];
167+
private _siblings: readonly DropListRef[] = [];
168168

169169
/** Direction in which the list is oriented. */
170170
private _orientation: 'horizontal' | 'vertical' = 'vertical';
@@ -413,7 +413,7 @@ export class DropListRef<T = any> {
413413
}
414414

415415
/** Gets the scrollable parents that are registered with this drop container. */
416-
getScrollableParents(): ReadonlyArray<HTMLElement> {
416+
getScrollableParents(): readonly HTMLElement[] {
417417
return this._scrollableElements;
418418
}
419419

src/cdk/drag-drop/parent-position-tracker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export class ParentPositionTracker {
3131
}
3232

3333
/** Caches the positions. Should be called at the beginning of a drag sequence. */
34-
cache(elements: HTMLElement[] | ReadonlyArray<HTMLElement>) {
34+
cache(elements: readonly HTMLElement[]) {
3535
this.clear();
3636
this.positions.set(this._document, {
3737
scrollPosition: this._viewportRuler.getViewportScrollPosition(),

src/cdk/schematics/update-tool/utils/decorators.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export interface NgDecorator {
2424
* (e.g. "@angular/core") from a list of decorators.
2525
*/
2626
export function getAngularDecorators(
27-
typeChecker: ts.TypeChecker, decorators: ReadonlyArray<ts.Decorator>): NgDecorator[] {
27+
typeChecker: ts.TypeChecker, decorators: readonly ts.Decorator[]): readonly NgDecorator[] {
2828
return decorators.map(node => ({node, importData: getCallDecoratorImport(typeChecker, node)}))
2929
.filter(({importData}) => importData && importData.moduleName.startsWith('@angular/'))
3030
.map(

src/cdk/scrolling/virtual-for-of.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ export class CdkVirtualForOf<T> implements
151151
}
152152

153153
/** Emits whenever the data in the current DataSource changes. */
154-
dataStream: Observable<T[] | ReadonlyArray<T>> = this._dataSourceChanges
154+
dataStream: Observable<readonly T[]> = this._dataSourceChanges
155155
.pipe(
156156
// Start off with null `DataSource`.
157157
startWith(null),
@@ -168,7 +168,7 @@ export class CdkVirtualForOf<T> implements
168168
private _differ: IterableDiffer<T> | null = null;
169169

170170
/** The most recent data emitted from the DataSource. */
171-
private _data: T[] | ReadonlyArray<T>;
171+
private _data: readonly T[];
172172

173173
/** The currently rendered items. */
174174
private _renderedItems: T[];
@@ -299,7 +299,7 @@ export class CdkVirtualForOf<T> implements
299299

300300
/** Swap out one `DataSource` for another. */
301301
private _changeDataSource(oldDs: DataSource<T> | null, newDs: DataSource<T> | null):
302-
Observable<T[] | ReadonlyArray<T>> {
302+
Observable<readonly T[]> {
303303

304304
if (oldDs) {
305305
oldDs.disconnect(this);

src/cdk/scrolling/virtual-scroll-repeater.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ import {ListRange} from '@angular/cdk/collections';
1313
* An item to be repeated by the VirtualScrollViewport
1414
*/
1515
export interface CdkVirtualScrollRepeater<T> {
16-
dataStream: Observable<T[] | ReadonlyArray<T>>;
16+
dataStream: Observable<readonly T[]>;
1717
measureRangeSize(range: ListRange, orientation: 'horizontal' | 'vertical'): number;
1818
}

src/cdk/table/sticky-position-listener.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export type StickySize = number|null|undefined;
1616
export type StickyOffset = number|null|undefined;
1717

1818
export interface StickyUpdate {
19-
elements?: ReadonlyArray<HTMLElement[]|undefined>;
19+
elements?: readonly (HTMLElement[]|undefined)[];
2020
offsets?: StickyOffset[];
2121
sizes: StickySize[];
2222
}

src/cdk/table/table.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export interface RowOutlet {
106106
* @docs-private
107107
*/
108108
type CdkTableDataSourceInput<T> =
109-
DataSource<T>|Observable<ReadonlyArray<T>|T[]>|ReadonlyArray<T>|T[];
109+
readonly T[]|DataSource<T>|Observable<readonly T[]>;
110110

111111
/**
112112
* Provides a handle for the table to grab the view container's ng-container to insert data rows.
@@ -227,7 +227,7 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
227227
private _document: Document;
228228

229229
/** Latest data provided by the data source. */
230-
protected _data: T[]|ReadonlyArray<T>;
230+
protected _data: readonly T[];
231231

232232
/** Subject that emits when the component has been destroyed. */
233233
private _onDestroy = new Subject<void>();
@@ -953,7 +953,7 @@ export class CdkTable<T> implements AfterContentChecked, CollectionViewer, OnDes
953953
return;
954954
}
955955

956-
let dataStream: Observable<T[]|ReadonlyArray<T>>|undefined;
956+
let dataStream: Observable<readonly T[]>|undefined;
957957

958958
if (isDataSource(this.dataSource)) {
959959
dataStream = this.dataSource.connect(this);

src/cdk/tree/tree.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ export class CdkTree<T, K = T> implements AfterContentChecked, CollectionViewer,
200200

201201
/** Set up a subscription for the data provided by the data source. */
202202
private _observeRenderChanges() {
203-
let dataStream: Observable<T[] | ReadonlyArray<T>> | undefined;
203+
let dataStream: Observable<readonly T[]> | undefined;
204204

205205
if (isDataSource(this._dataSource)) {
206206
dataStream = this._dataSource.connect(this);
@@ -219,7 +219,7 @@ export class CdkTree<T, K = T> implements AfterContentChecked, CollectionViewer,
219219
}
220220

221221
/** Check for changes made in the data and render each change (node added/removed/moved). */
222-
renderNodeChanges(data: T[] | ReadonlyArray<T>, dataDiffer: IterableDiffer<T> = this._dataDiffer,
222+
renderNodeChanges(data: readonly T[], dataDiffer: IterableDiffer<T> = this._dataDiffer,
223223
viewContainer: ViewContainerRef = this._nodeOutlet.viewContainer,
224224
parentData?: T) {
225225
const changes = dataDiffer.diff(data);

src/components-examples/material/chips/chips-input/chips-input-example.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class ChipsInputExample {
1919
selectable = true;
2020
removable = true;
2121
addOnBlur = true;
22-
readonly separatorKeysCodes: number[] = [ENTER, COMMA];
22+
readonly separatorKeysCodes = [ENTER, COMMA] as const;
2323
fruits: Fruit[] = [
2424
{name: 'Lemon'},
2525
{name: 'Lime'},

src/material-experimental/column-resize/column-resize.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {
22
Component,
33
Directive,
44
ElementRef,
5-
Type,
65
ViewChild,
76
ChangeDetectionStrategy,
87
} from '@angular/core';
@@ -322,7 +321,7 @@ const approximateMatcher = {
322321
})
323322
};
324323

325-
const testCases: ReadonlyArray<[Type<object>, Type<BaseTestComponent>, string]> = [
324+
const testCases = [
326325
[MatColumnResizeModule, MatResizeTest, 'opt-in table-based mat-table'],
327326
[MatColumnResizeModule, MatResizeOnPushTest, 'inside OnPush component'],
328327
[MatColumnResizeModule, MatResizeFlexTest, 'opt-in flex-based mat-table'],
@@ -342,7 +341,7 @@ const testCases: ReadonlyArray<[Type<object>, Type<BaseTestComponent>, string]>
342341
MatDefaultEnabledColumnResizeModule, MatResizeDefaultFlexRtlTest,
343342
'default enabled rtl flex-based mat-table'
344343
],
345-
];
344+
] as const;
346345

347346
describe('Material Popover Edit', () => {
348347
for (const [resizeModule, componentClass, label] of testCases) {

src/material-experimental/popover-edit/popover-edit.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {LEFT_ARROW, UP_ARROW, RIGHT_ARROW, DOWN_ARROW, TAB} from '@angular/cdk/k
33
import {MatTableModule} from '@angular/material/table';
44
import {dispatchKeyboardEvent} from '@angular/cdk/testing/private';
55
import {CommonModule} from '@angular/common';
6-
import {Component, Directive, ElementRef, Type, ViewChild} from '@angular/core';
6+
import {Component, Directive, ElementRef, ViewChild} from '@angular/core';
77
import {ComponentFixture, fakeAsync, flush, TestBed, tick, inject} from '@angular/core/testing';
88
import {FormsModule, NgForm} from '@angular/forms';
99
import {OverlayContainer} from '@angular/cdk/overlay';
@@ -282,10 +282,10 @@ class MatTableInCell extends BaseTestComponent {
282282
dataSource = new ElementDataSource();
283283
}
284284

285-
const testCases: ReadonlyArray<[Type<BaseTestComponent>, string]> = [
285+
const testCases = [
286286
[MatFlexTableInCell, 'Flex mat-table; edit defined within cell'],
287287
[MatTableInCell, 'Table mat-table; edit defined within cell'],
288-
];
288+
] as const;
289289

290290
describe('Material Popover Edit', () => {
291291
for (const [componentClass, label] of testCases) {

src/material/tooltip/tooltip.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ export abstract class _MatTooltipBase<T extends _TooltipComponentBase> implement
660660
}
661661

662662
private _addListeners(
663-
listeners: ReadonlyArray<readonly [string, EventListenerOrEventListenerObject]>) {
663+
listeners: (readonly [string, EventListenerOrEventListenerObject])[]) {
664664
listeners.forEach(([event, listener]) => {
665665
this._elementRef.nativeElement.addEventListener(event, listener, passiveListenerOptions);
666666
});

tools/public_api_guard/cdk/collections.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ export declare const enum _ViewRepeaterOperation {
4646
}
4747

4848
export declare class ArrayDataSource<T> extends DataSource<T> {
49-
constructor(_data: T[] | ReadonlyArray<T> | Observable<T[] | ReadonlyArray<T>>);
50-
connect(): Observable<T[] | ReadonlyArray<T>>;
49+
constructor(_data: readonly T[] | Observable<readonly T[]>);
50+
connect(): Observable<readonly T[]>;
5151
disconnect(): void;
5252
}
5353

@@ -56,7 +56,7 @@ export interface CollectionViewer {
5656
}
5757

5858
export declare abstract class DataSource<T> {
59-
abstract connect(collectionViewer: CollectionViewer): Observable<T[] | ReadonlyArray<T>>;
59+
abstract connect(collectionViewer: CollectionViewer): Observable<readonly T[]>;
6060
abstract disconnect(collectionViewer: CollectionViewer): void;
6161
}
6262

tools/public_api_guard/cdk/drag-drop.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ export declare class DropListRef<T = any> {
406406
enter(item: DragRef, pointerX: number, pointerY: number, index?: number): void;
407407
exit(item: DragRef): void;
408408
getItemIndex(item: DragRef): number;
409-
getScrollableParents(): ReadonlyArray<HTMLElement>;
409+
getScrollableParents(): readonly HTMLElement[];
410410
isDragging(): boolean;
411411
isReceiving(): boolean;
412412
start(): void;

tools/public_api_guard/cdk/scrolling.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export declare class CdkVirtualForOf<T> implements CdkVirtualScrollRepeater<T>,
8484
set cdkVirtualForTemplateCacheSize(size: number);
8585
get cdkVirtualForTrackBy(): TrackByFunction<T> | undefined;
8686
set cdkVirtualForTrackBy(fn: TrackByFunction<T> | undefined);
87-
dataStream: Observable<T[] | ReadonlyArray<T>>;
87+
dataStream: Observable<readonly T[]>;
8888
viewChange: Subject<ListRange>;
8989
constructor(
9090
_viewContainerRef: ViewContainerRef,
@@ -112,7 +112,7 @@ export declare type CdkVirtualForOfContext<T> = {
112112
};
113113

114114
export interface CdkVirtualScrollRepeater<T> {
115-
dataStream: Observable<T[] | ReadonlyArray<T>>;
115+
dataStream: Observable<readonly T[]>;
116116
measureRangeSize(range: ListRange, orientation: 'horizontal' | 'vertical'): number;
117117
}
118118

tools/public_api_guard/cdk/table.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ export declare class CdkTable<T> implements AfterContentChecked, CollectionViewe
198198
_contentFooterRowDefs: QueryList<CdkFooterRowDef>;
199199
_contentHeaderRowDefs: QueryList<CdkHeaderRowDef>;
200200
_contentRowDefs: QueryList<CdkRowDef<T>>;
201-
protected _data: T[] | ReadonlyArray<T>;
201+
protected _data: readonly T[];
202202
protected readonly _differs: IterableDiffers;
203203
protected readonly _dir: Directionality;
204204
protected readonly _elementRef: ElementRef;
@@ -361,7 +361,7 @@ export declare class StickyStyler {
361361
}
362362

363363
export interface StickyUpdate {
364-
elements?: ReadonlyArray<HTMLElement[] | undefined>;
364+
elements?: readonly (HTMLElement[] | undefined)[];
365365
offsets?: StickyOffset[];
366366
sizes: StickySize[];
367367
}

tools/public_api_guard/cdk/tree.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export declare class CdkTree<T, K = T> implements AfterContentChecked, Collectio
5454
ngAfterContentChecked(): void;
5555
ngOnDestroy(): void;
5656
ngOnInit(): void;
57-
renderNodeChanges(data: T[] | ReadonlyArray<T>, dataDiffer?: IterableDiffer<T>, viewContainer?: ViewContainerRef, parentData?: T): void;
57+
renderNodeChanges(data: readonly T[], dataDiffer?: IterableDiffer<T>, viewContainer?: ViewContainerRef, parentData?: T): void;
5858
static ɵcmp: i0.ɵɵComponentDefWithMeta<CdkTree<any, any>, "cdk-tree", ["cdkTree"], { "dataSource": "dataSource"; "treeControl": "treeControl"; "trackBy": "trackBy"; }, {}, ["_nodeDefs"], never>;
5959
static ɵfac: i0.ɵɵFactoryDef<CdkTree<any, any>, never>;
6060
}

tslint.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
"node_modules/codelyzer"
99
],
1010
"rules": {
11+
"ban-types": [
12+
true,
13+
[
14+
"ReadonlyArray<.+>",
15+
"Use 'readonly T[]' instead."
16+
]
17+
],
1118
"max-line-length": [true, {
1219
"limit": 100,
1320
"check-strings": true,

0 commit comments

Comments
 (0)