Skip to content

Commit 1ea40c8

Browse files
committed
perf: allow assertions to be removed in production mode
Wraps all assertions in `ngDevMode` checks so that they can be stripped away in production mode. Furthermore, changes all the places where we were using the old `isDevMode` check.
1 parent 578d4ef commit 1ea40c8

File tree

138 files changed

+453
-345
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

138 files changed

+453
-345
lines changed

src/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ load("@npm_bazel_typescript//:index.bzl", "ts_config")
22
load("//src/cdk:config.bzl", "CDK_ENTRYPOINTS")
33
load("//src/material:config.bzl", "MATERIAL_ENTRYPOINTS", "MATERIAL_TESTING_ENTRYPOINTS")
44
load("//tools/dgeni:index.bzl", "dgeni_api_docs")
5+
load("//tools:defaults.bzl", "ts_library")
56

67
package(default_visibility = ["//visibility:public"])
78

@@ -43,3 +44,8 @@ dgeni_api_docs(
4344
},
4445
tags = ["docs-package"],
4546
)
47+
48+
ts_library(
49+
name = "dev_mode_types",
50+
srcs = ["dev-mode-types.d.ts"],
51+
)

src/cdk-experimental/combobox/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ ng_module(
1010
),
1111
module_name = "@angular/cdk-experimental/combobox",
1212
deps = [
13+
"//src:dev_mode_types",
1314
"//src/cdk/a11y",
1415
"//src/cdk/bidi",
1516
"//src/cdk/collections",

src/cdk-experimental/combobox/combobox.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
Directive,
1616
ElementRef,
1717
EventEmitter,
18-
Input, isDevMode,
18+
Input,
1919
OnDestroy,
2020
Optional,
2121
Output, ViewContainerRef
@@ -273,7 +273,8 @@ export class CdkCombobox<T = unknown> implements OnDestroy, AfterContentInit {
273273

274274
private _coerceOpenActionProperty(input: string | OpenAction[]): OpenAction[] {
275275
let actions = typeof input === 'string' ? input.trim().split(/[ ,]+/) : input;
276-
if (isDevMode() && actions.some(a => allowedOpenActions.indexOf(a) === -1)) {
276+
if ((typeof ngDevMode === 'undefined' || ngDevMode) &&
277+
actions.some(a => allowedOpenActions.indexOf(a) === -1)) {
277278
throw Error(`${input} is not a support open action for CdkCombobox`);
278279
}
279280
return actions as OpenAction[];

src/cdk-experimental/dialog/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ ng_module(
1111
assets = [":dialog-container.css"] + glob(["**/*.html"]),
1212
module_name = "@angular/cdk-experimental/dialog",
1313
deps = [
14+
"//src:dev_mode_types",
1415
"//src/cdk/a11y",
1516
"//src/cdk/bidi",
1617
"//src/cdk/keycodes",

src/cdk-experimental/dialog/dialog-container.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export class CdkDialogContainer extends BasePortalOutlet implements OnDestroy {
171171
* @param portal Portal to be attached as the dialog content.
172172
*/
173173
attachComponentPortal<T>(portal: ComponentPortal<T>): ComponentRef<T> {
174-
if (this._portalHost.hasAttached()) {
174+
if (this._portalHost.hasAttached() && (typeof ngDevMode === 'undefined' || ngDevMode)) {
175175
throwDialogContentAlreadyAttachedError();
176176
}
177177

@@ -183,7 +183,7 @@ export class CdkDialogContainer extends BasePortalOutlet implements OnDestroy {
183183
* @param portal Portal to be attached as the dialog content.
184184
*/
185185
attachTemplatePortal<C>(portal: TemplatePortal<C>): EmbeddedViewRef<C> {
186-
if (this._portalHost.hasAttached()) {
186+
if (this._portalHost.hasAttached() && (typeof ngDevMode === 'undefined' || ngDevMode)) {
187187
throwDialogContentAlreadyAttachedError();
188188
}
189189

@@ -197,7 +197,7 @@ export class CdkDialogContainer extends BasePortalOutlet implements OnDestroy {
197197
* @breaking-change 10.0.0
198198
*/
199199
attachDomPortal = (portal: DomPortal) => {
200-
if (this._portalHost.hasAttached()) {
200+
if (this._portalHost.hasAttached() && (typeof ngDevMode === 'undefined' || ngDevMode)) {
201201
throwDialogContentAlreadyAttachedError();
202202
}
203203

src/cdk-experimental/dialog/dialog.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export class Dialog implements OnDestroy {
106106
openFromComponent<T>(component: ComponentType<T>, config?: DialogConfig): DialogRef<any> {
107107
config = this._applyConfigDefaults(config);
108108

109-
if (config.id && this.getById(config.id)) {
109+
if (config.id && this.getById(config.id) && (typeof ngDevMode === 'undefined' || ngDevMode)) {
110110
throw Error(`Dialog with id "${config.id}" exists already. The dialog id must be unique.`);
111111
}
112112

@@ -125,7 +125,7 @@ export class Dialog implements OnDestroy {
125125
openFromTemplate<T>(template: TemplateRef<T>, config?: DialogConfig): DialogRef<any> {
126126
config = this._applyConfigDefaults(config);
127127

128-
if (config.id && this.getById(config.id)) {
128+
if (config.id && this.getById(config.id) && (typeof ngDevMode === 'undefined' || ngDevMode)) {
129129
throw Error(`Dialog with id "${config.id}" exists already. The dialog id must be unique.`);
130130
}
131131

src/cdk-experimental/menu/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ ng_module(
1010
),
1111
module_name = "@angular/cdk-experimental/menu",
1212
deps = [
13+
"//src:dev_mode_types",
1314
"//src/cdk/a11y",
1415
"//src/cdk/bidi",
1516
"//src/cdk/coercion",

src/cdk-experimental/menu/context-menu.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,7 @@ export class CdkContextMenuTrigger implements OnDestroy {
110110
return this._menuPanel;
111111
}
112112
set menuPanel(panel: CdkMenuPanel) {
113-
// If the provided panel already has a stack, that means it already has a trigger configured
114-
// TODO refactor once https://github.com/angular/components/pull/20146 lands
115-
if (isDevMode() && panel._menuStack) {
113+
if ((typeof ngDevMode === 'undefined' || ngDevMode) && panel._menuStack) {
116114
throwExistingMenuStackError();
117115
}
118116
this._menuPanel = panel;

src/cdk-experimental/menu/menu-item-trigger.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ export class CdkMenuItemTrigger implements OnDestroy {
6464
// If the provided panel already has a stack, that means it already has a trigger configured.
6565
// Note however that there are some edge cases where two triggers **may** share the same menu,
6666
// e.g. two triggers in two separate menus.
67-
// TODO refactor once https://github.com/angular/components/pull/20146 lands
68-
if (isDevMode() && panel?._menuStack) {
67+
if ((typeof ngDevMode === 'undefined' || ngDevMode) && panel?._menuStack) {
6968
throwExistingMenuStackError();
7069
}
7170

src/cdk-experimental/scrolling/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ ng_module(
1111
),
1212
module_name = "@angular/cdk-experimental/scrolling",
1313
deps = [
14+
"//src:dev_mode_types",
1415
"//src/cdk/coercion",
1516
"//src/cdk/collections",
1617
"//src/cdk/scrolling",

src/cdk-experimental/scrolling/auto-size-virtual-scroll.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,10 @@ export class AutoSizeVirtualScrollStrategy implements VirtualScrollStrategy {
7272
/** @docs-private Implemented as part of VirtualScrollStrategy. */
7373
scrolledIndexChange = new Observable<number>(() => {
7474
// TODO(mmalerba): Implement.
75-
throw Error('cdk-virtual-scroll: scrolledIndexChange is currently not supported for the' +
76-
' autosize scroll strategy');
75+
if (typeof ngDevMode === 'undefined' || ngDevMode) {
76+
throw Error('cdk-virtual-scroll: scrolledIndexChange is currently not supported for the' +
77+
' autosize scroll strategy');
78+
}
7779
});
7880

7981
/** The attached viewport. */
@@ -164,9 +166,11 @@ export class AutoSizeVirtualScrollStrategy implements VirtualScrollStrategy {
164166

165167
/** Scroll to the offset for the given index. */
166168
scrollToIndex(): void {
167-
// TODO(mmalerba): Implement.
168-
throw Error('cdk-virtual-scroll: scrollToIndex is currently not supported for the autosize'
169-
+ ' scroll strategy');
169+
if (typeof ngDevMode === 'undefined' || ngDevMode) {
170+
// TODO(mmalerba): Implement.
171+
throw Error('cdk-virtual-scroll: scrollToIndex is currently not supported for the autosize'
172+
+ ' scroll strategy');
173+
}
170174
}
171175

172176
/**

src/cdk-experimental/selection/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ ng_module(
1010
),
1111
module_name = "@angular/cdk-experimental/selection",
1212
deps = [
13+
"//src:dev_mode_types",
1314
"//src/cdk/coercion",
1415
"//src/cdk/collections",
1516
"//src/cdk/table",

src/cdk-experimental/selection/select-all.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Directive, Inject, isDevMode, OnDestroy, OnInit, Optional, Self} from '@angular/core';
9+
import {Directive, Inject, OnDestroy, OnInit, Optional, Self} from '@angular/core';
1010
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
1111
import {Observable, of as observableOf, Subject} from 'rxjs';
1212
import {switchMap, takeUntil} from 'rxjs/operators';
@@ -90,11 +90,11 @@ export class CdkSelectAll<T> implements OnDestroy, OnInit {
9090
}
9191

9292
private _assertValidParentSelection() {
93-
if (!this._selection && isDevMode()) {
93+
if (!this._selection && (typeof ngDevMode === 'undefined' || ngDevMode)) {
9494
throw Error('CdkSelectAll: missing CdkSelection in the parent');
9595
}
9696

97-
if (!this._selection.multiple && isDevMode()) {
97+
if (!this._selection.multiple && (typeof ngDevMode === 'undefined' || ngDevMode)) {
9898
throw Error('CdkSelectAll: CdkSelection must have cdkSelectionMultiple set to true');
9999
}
100100
}

src/cdk-experimental/selection/selection-column.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {CdkCellDef, CdkColumnDef, CdkHeaderCellDef, CdkTable} from '@angular/cdk
1010
import {
1111
Component,
1212
Input,
13-
isDevMode,
1413
OnDestroy,
1514
OnInit,
1615
Optional,
@@ -77,7 +76,7 @@ export class CdkSelectionColumn<T> implements OnInit, OnDestroy {
7776
) {}
7877

7978
ngOnInit() {
80-
if (!this.selection && isDevMode()) {
79+
if (!this.selection && (typeof ngDevMode === 'undefined' || ngDevMode)) {
8180
throw Error('CdkSelectionColumn: missing CdkSelection in the parent');
8281
}
8382

@@ -87,10 +86,8 @@ export class CdkSelectionColumn<T> implements OnInit, OnDestroy {
8786
this._columnDef.cell = this._cell;
8887
this._columnDef.headerCell = this._headerCell;
8988
this._table.addColumnDef(this._columnDef);
90-
} else {
91-
if (isDevMode()) {
92-
throw Error('CdkSelectionColumn: missing parent table');
93-
}
89+
} else if ((typeof ngDevMode === 'undefined' || ngDevMode)) {
90+
throw Error('CdkSelectionColumn: missing parent table');
9491
}
9592
}
9693

src/cdk-experimental/selection/selection-set.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {isDevMode, TrackByFunction} from '@angular/core';
9+
import {TrackByFunction} from '@angular/core';
1010
import {Subject} from 'rxjs';
1111

1212
/**
@@ -55,7 +55,7 @@ export class SelectionSet<T> implements TrackBySelection<T> {
5555
}
5656

5757
select(...selects: SelectableWithIndex<T>[]) {
58-
if (!this._multiple && selects.length > 1 && isDevMode()) {
58+
if (!this._multiple && selects.length > 1 && (typeof ngDevMode === 'undefined' || ngDevMode)) {
5959
throw Error('SelectionSet: not multiple selection');
6060
}
6161

@@ -81,7 +81,7 @@ export class SelectionSet<T> implements TrackBySelection<T> {
8181
}
8282

8383
deselect(...selects: SelectableWithIndex<T>[]) {
84-
if (!this._multiple && selects.length > 1 && isDevMode()) {
84+
if (!this._multiple && selects.length > 1 && (typeof ngDevMode === 'undefined' || ngDevMode)) {
8585
throw Error('SelectionSet: not multiple selection');
8686
}
8787

@@ -114,7 +114,7 @@ export class SelectionSet<T> implements TrackBySelection<T> {
114114
return select.value;
115115
}
116116

117-
if (select.index == null && isDevMode()) {
117+
if (select.index == null && (typeof ngDevMode === 'undefined' || ngDevMode)) {
118118
throw Error('SelectionSet: index required when trackByFn is used.');
119119
}
120120

src/cdk-experimental/selection/selection-toggle.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
Directive,
1212
Inject,
1313
Input,
14-
isDevMode,
1514
OnDestroy,
1615
OnInit,
1716
Optional,
@@ -77,7 +76,7 @@ export class CdkSelectionToggle<T> implements OnDestroy, OnInit {
7776
}
7877

7978
private _assertValidParentSelection() {
80-
if (!this._selection && isDevMode()) {
79+
if (!this._selection && (typeof ngDevMode === 'undefined' || ngDevMode)) {
8180
throw Error('CdkSelectAll: missing CdkSelection in the parent');
8281
}
8382
}

src/cdk-experimental/selection/selection.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
Directive,
1414
EventEmitter,
1515
Input,
16-
isDevMode,
1716
OnDestroy,
1817
OnInit,
1918
Output,
@@ -104,7 +103,7 @@ export class CdkSelection<T> implements OnInit, AfterContentChecked, CollectionV
104103
dataStream = observableOf(this._dataSource);
105104
}
106105

107-
if (dataStream == null && isDevMode()) {
106+
if (dataStream == null && (typeof ngDevMode === 'undefined' || ngDevMode)) {
108107
throw Error('Unknown data source');
109108
}
110109

@@ -139,7 +138,7 @@ export class CdkSelection<T> implements OnInit, AfterContentChecked, CollectionV
139138

140139
/** Toggles selection for a given value. `index` is required if `trackBy` is used. */
141140
toggleSelection(value: T, index?: number) {
142-
if (this.trackByFn && index == null && isDevMode()) {
141+
if (this.trackByFn && index == null && (typeof ngDevMode === 'undefined' || ngDevMode)) {
143142
throw Error('CdkSelection: index required when trackBy is used');
144143
}
145144

@@ -155,7 +154,7 @@ export class CdkSelection<T> implements OnInit, AfterContentChecked, CollectionV
155154
* values are selected, de-select all values.
156155
*/
157156
toggleSelectAll() {
158-
if (!this._multiple && isDevMode()) {
157+
if (!this._multiple && (typeof ngDevMode === 'undefined' || ngDevMode)) {
159158
throw Error('CdkSelection: multiple selection not enabled');
160159
}
161160

@@ -168,7 +167,7 @@ export class CdkSelection<T> implements OnInit, AfterContentChecked, CollectionV
168167

169168
/** Checks whether a value is selected. `index` is required if `trackBy` is used. */
170169
isSelected(value: T, index?: number) {
171-
if (this.trackByFn && index == null && isDevMode()) {
170+
if (this.trackByFn && index == null && (typeof ngDevMode === 'undefined' || ngDevMode)) {
172171
throw Error('CdkSelection: index required when trackBy is used');
173172
}
174173

src/cdk-experimental/tsconfig-tests.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
},
2222
"include": [
2323
"**/index.ts",
24-
"**/*.spec.ts"
24+
"**/*.spec.ts",
25+
"../dev-mode-types.d.ts"
2526
],
2627
"exclude": [
2728
"**/*.e2e.spec.ts"

src/cdk-experimental/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
"@angular/cdk-experimental/*": ["../cdk-experimental/*"]
1010
}
1111
},
12-
"include": ["./**/*.ts"]
12+
"include": ["./**/*.ts", "../dev-mode-types.d.ts"]
1313
}

src/cdk/a11y/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ ng_module(
1818
),
1919
module_name = "@angular/cdk/a11y",
2020
deps = [
21+
"//src:dev_mode_types",
2122
"//src/cdk/coercion",
2223
"//src/cdk/keycodes",
2324
"//src/cdk/observers",

src/cdk/a11y/focus-trap/focus-trap.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {
1818
NgZone,
1919
OnDestroy,
2020
DoCheck,
21-
isDevMode,
2221
SimpleChanges,
2322
OnChanges,
2423
} from '@angular/core';
@@ -213,7 +212,8 @@ export class FocusTrap {
213212

214213
// Warn the consumer if the element they've pointed to
215214
// isn't focusable, when not in production mode.
216-
if (isDevMode() && !this._checker.isFocusable(redirectToElement)) {
215+
if ((typeof ngDevMode === 'undefined' || ngDevMode) &&
216+
!this._checker.isFocusable(redirectToElement)) {
217217
console.warn(`Element matching '[cdkFocusInitial]' is not focusable.`, redirectToElement);
218218
}
219219

src/cdk/a11y/key-manager/list-key-manager.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ export class ListKeyManager<T extends ListKeyManagerOption> {
140140
* @param debounceInterval Time to wait after the last keystroke before setting the active item.
141141
*/
142142
withTypeAhead(debounceInterval: number = 200): this {
143-
if (this._items.length && this._items.some(item => typeof item.getLabel !== 'function')) {
143+
if ((typeof ngDevMode === 'undefined' || ngDevMode) && (this._items.length &&
144+
this._items.some(item => typeof item.getLabel !== 'function'))) {
144145
throw Error('ListKeyManager items in typeahead mode must implement the `getLabel` method.');
145146
}
146147

src/cdk/collections/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ ng_module(
1616
),
1717
module_name = "@angular/cdk/collections",
1818
deps = [
19+
"//src:dev_mode_types",
1920
"@npm//@angular/core",
2021
"@npm//rxjs",
2122
],

src/cdk/collections/selection-model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ export class SelectionModel<T> {
178178
* including multiple values while the selection model is not supporting multiple values.
179179
*/
180180
private _verifyValueAssignment(values: T[]) {
181-
if (values.length > 1 && !this._multiple) {
181+
if (values.length > 1 && !this._multiple && (typeof ngDevMode === 'undefined' || ngDevMode)) {
182182
throw getMultipleValuesInSingleSelectionError();
183183
}
184184
}

src/cdk/drag-drop/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ ng_module(
1616
),
1717
module_name = "@angular/cdk/drag-drop",
1818
deps = [
19+
"//src:dev_mode_types",
1920
"//src/cdk/bidi",
2021
"//src/cdk/coercion",
2122
"//src/cdk/platform",

0 commit comments

Comments
 (0)