Skip to content

Commit 8f3416b

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 37f4a7a commit 8f3416b

File tree

126 files changed

+438
-328
lines changed

Some content is hidden

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

126 files changed

+438
-328
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 = "typings",
50+
srcs = ["typings.d.ts"],
51+
)

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:typings",
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
@@ -161,7 +161,7 @@ export class CdkDialogContainer extends BasePortalOutlet implements OnDestroy {
161161
* @param portal Portal to be attached as the dialog content.
162162
*/
163163
attachComponentPortal<T>(portal: ComponentPortal<T>): ComponentRef<T> {
164-
if (this._portalHost.hasAttached()) {
164+
if (this._portalHost.hasAttached() && (typeof ngDevMode === 'undefined' || ngDevMode)) {
165165
throwDialogContentAlreadyAttachedError();
166166
}
167167

@@ -174,7 +174,7 @@ export class CdkDialogContainer extends BasePortalOutlet implements OnDestroy {
174174
* @param portal Portal to be attached as the dialog content.
175175
*/
176176
attachTemplatePortal<C>(portal: TemplatePortal<C>): EmbeddedViewRef<C> {
177-
if (this._portalHost.hasAttached()) {
177+
if (this._portalHost.hasAttached() && (typeof ngDevMode === 'undefined' || ngDevMode)) {
178178
throwDialogContentAlreadyAttachedError();
179179
}
180180

@@ -189,7 +189,7 @@ export class CdkDialogContainer extends BasePortalOutlet implements OnDestroy {
189189
* @breaking-change 10.0.0
190190
*/
191191
attachDomPortal = (portal: DomPortal) => {
192-
if (this._portalHost.hasAttached()) {
192+
if (this._portalHost.hasAttached() && (typeof ngDevMode === 'undefined' || ngDevMode)) {
193193
throwDialogContentAlreadyAttachedError();
194194
}
195195

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

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

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

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:typings",
1314
"//src/cdk/a11y",
1415
"//src/cdk/bidi",
1516
"//src/cdk/coercion",

src/cdk-experimental/menu/menu.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ export class CdkMenu extends CdkMenuGroup implements Menu, AfterContentInit, OnI
179179
const parent = this._getMenuPanel();
180180
if (parent) {
181181
parent._registerMenu(this);
182-
} else {
182+
} else if (typeof ngDevMode === 'undefined' || ngDevMode) {
183183
throwMissingMenuPanelError();
184184
}
185185
}

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:typings",
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/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+
"../typings.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", "../typings.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:typings",
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:typings",
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:typings",
1920
"//src/cdk/bidi",
2021
"//src/cdk/coercion",
2122
"//src/cdk/platform",

src/cdk/drag-drop/directives/drag.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import {
2727
OnChanges,
2828
SimpleChanges,
2929
ChangeDetectorRef,
30-
isDevMode,
3130
Self,
3231
} from '@angular/core';
3332
import {
@@ -326,7 +325,8 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
326325
const rootElement = this.rootElementSelector ?
327326
getClosestMatchingAncestor(element, this.rootElementSelector) : element;
328327

329-
if (rootElement && rootElement.nodeType !== this._document.ELEMENT_NODE) {
328+
if (rootElement && rootElement.nodeType !== this._document.ELEMENT_NODE &&
329+
(typeof ngDevMode === 'undefined' || ngDevMode)) {
330330
throw Error(`cdkDrag must be attached to an element node. ` +
331331
`Currently attached to "${rootElement.nodeName}".`);
332332
}
@@ -348,7 +348,8 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
348348

349349
const element = coerceElement(boundary);
350350

351-
if (isDevMode() && !element.contains(this.element.nativeElement)) {
351+
if ((typeof ngDevMode === 'undefined' || ngDevMode) &&
352+
!element.contains(this.element.nativeElement)) {
352353
throw Error('Draggable element is not inside of the node passed into cdkDragBoundary.');
353354
}
354355

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
SkipSelf,
2020
Inject,
2121
InjectionToken,
22-
isDevMode,
2322
} from '@angular/core';
2423
import {Directionality} from '@angular/cdk/bidi';
2524
import {ScrollDispatcher} from '@angular/cdk/scrolling';
@@ -256,7 +255,7 @@ export class CdkDropList<T = any> implements OnDestroy {
256255
if (typeof drop === 'string') {
257256
const correspondingDropList = CdkDropList._dropLists.find(list => list.id === drop);
258257

259-
if (!correspondingDropList && isDevMode()) {
258+
if (!correspondingDropList && (typeof ngDevMode === 'undefined' || ngDevMode)) {
260259
console.warn(`CdkDropList could not find connected drop list with id "${drop}"`);
261260
}
262261

src/cdk/overlay/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ ng_module(
2020
),
2121
module_name = "@angular/cdk/overlay",
2222
deps = [
23+
"//src:typings",
2324
"//src/cdk/bidi",
2425
"//src/cdk/coercion",
2526
"//src/cdk/keycodes",

src/cdk/overlay/position/flexible-connected-position-strategy.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
145145

146146
/** Attaches this position strategy to an overlay. */
147147
attach(overlayRef: OverlayReference): void {
148-
if (this._overlayRef && overlayRef !== this._overlayRef) {
148+
if (this._overlayRef && overlayRef !== this._overlayRef &&
149+
(typeof ngDevMode === 'undefined' || ngDevMode)) {
149150
throw Error('This position strategy is already attached to an overlay');
150151
}
151152

@@ -1066,18 +1067,20 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
10661067

10671068
/** Validates that the current position match the expected values. */
10681069
private _validatePositions(): void {
1069-
if (!this._preferredPositions.length) {
1070-
throw Error('FlexibleConnectedPositionStrategy: At least one position is required.');
1071-
}
1070+
if (typeof ngDevMode === 'undefined' || ngDevMode) {
1071+
if (!this._preferredPositions.length) {
1072+
throw Error('FlexibleConnectedPositionStrategy: At least one position is required.');
1073+
}
10721074

1073-
// TODO(crisbeto): remove these once Angular's template type
1074-
// checking is advanced enough to catch these cases.
1075-
this._preferredPositions.forEach(pair => {
1076-
validateHorizontalPosition('originX', pair.originX);
1077-
validateVerticalPosition('originY', pair.originY);
1078-
validateHorizontalPosition('overlayX', pair.overlayX);
1079-
validateVerticalPosition('overlayY', pair.overlayY);
1080-
});
1075+
// TODO(crisbeto): remove these once Angular's template type
1076+
// checking is advanced enough to catch these cases.
1077+
this._preferredPositions.forEach(pair => {
1078+
validateHorizontalPosition('originX', pair.originX);
1079+
validateVerticalPosition('originY', pair.originY);
1080+
validateHorizontalPosition('overlayX', pair.overlayX);
1081+
validateVerticalPosition('overlayY', pair.overlayY);
1082+
});
1083+
}
10811084
}
10821085

10831086
/** Adds a single CSS class or an array of classes on the overlay panel. */

src/cdk/overlay/scroll/close-scroll-strategy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class CloseScrollStrategy implements ScrollStrategy {
3535

3636
/** Attaches this scroll strategy to an overlay. */
3737
attach(overlayRef: OverlayReference) {
38-
if (this._overlayRef) {
38+
if (this._overlayRef && (typeof ngDevMode === 'undefined' || ngDevMode)) {
3939
throw getMatScrollStrategyAlreadyAttachedError();
4040
}
4141

src/cdk/overlay/scroll/reposition-scroll-strategy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class RepositionScrollStrategy implements ScrollStrategy {
3939

4040
/** Attaches this scroll strategy to an overlay. */
4141
attach(overlayRef: OverlayReference) {
42-
if (this._overlayRef) {
42+
if (this._overlayRef && (typeof ngDevMode === 'undefined' || ngDevMode)) {
4343
throw getMatScrollStrategyAlreadyAttachedError();
4444
}
4545

src/cdk/portal/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/portal",
1818
deps = [
19+
"//src:typings",
1920
"@npm//@angular/core",
2021
],
2122
)

src/cdk/portal/dom-portal-outlet.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,20 +115,20 @@ export class DomPortalOutlet extends BasePortalOutlet {
115115
attachDomPortal = (portal: DomPortal) => {
116116
// @breaking-change 10.0.0 Remove check and error once the
117117
// `_document` constructor parameter is required.
118-
if (!this._document) {
118+
if (!this._document && (typeof ngDevMode === 'undefined' || ngDevMode)) {
119119
throw Error('Cannot attach DOM portal without _document constructor parameter');
120120
}
121121

122122
const element = portal.element;
123-
if (!element.parentNode) {
123+
if (!element.parentNode && (typeof ngDevMode === 'undefined' || ngDevMode)) {
124124
throw Error('DOM portal content must be attached to a parent node.');
125125
}
126126

127127
// Anchor used to save the element's previous position so
128128
// that we can restore it when the portal is detached.
129129
const anchorNode = this._document.createComment('dom-portal');
130130

131-
element.parentNode.insertBefore(anchorNode, element);
131+
element.parentNode!.insertBefore(anchorNode, element);
132132
this.outletElement.appendChild(element);
133133

134134
super.setDisposeFn(() => {

src/cdk/portal/portal-directives.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,12 @@ export class CdkPortalOutlet extends BasePortalOutlet implements OnInit, OnDestr
198198
attachDomPortal = (portal: DomPortal) => {
199199
// @breaking-change 9.0.0 Remove check and error once the
200200
// `_document` constructor parameter is required.
201-
if (!this._document) {
201+
if (!this._document && (typeof ngDevMode === 'undefined' || ngDevMode)) {
202202
throw Error('Cannot attach DOM portal without _document constructor parameter');
203203
}
204204

205205
const element = portal.element;
206-
if (!element.parentNode) {
206+
if (!element.parentNode && (typeof ngDevMode === 'undefined' || ngDevMode)) {
207207
throw Error('DOM portal content must be attached to a parent node.');
208208
}
209209

@@ -212,7 +212,7 @@ export class CdkPortalOutlet extends BasePortalOutlet implements OnInit, OnDestr
212212
const anchorNode = this._document.createComment('dom-portal');
213213

214214
portal.setAttachedHost(this);
215-
element.parentNode.insertBefore(anchorNode, element);
215+
element.parentNode!.insertBefore(anchorNode, element);
216216
this._getRootNode().appendChild(element);
217217

218218
super.setDisposeFn(() => {

0 commit comments

Comments
 (0)