Skip to content

Commit 47ade95

Browse files
committed
feat(core): allow more granular control over sanity checks
Allows consumers to disable individual sanity checks, rather than the all-or-nothing setup that we have at the moment. Fixes #16617.
1 parent 1c74518 commit 47ade95

File tree

3 files changed

+52
-14
lines changed

3 files changed

+52
-14
lines changed

src/material/core/common-behaviors/common-module.ts

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,27 @@ import {VERSION as CDK_VERSION} from '@angular/cdk';
1818
const VERSION = new Version('0.0.0-PLACEHOLDER');
1919

2020
/** @docs-private */
21-
export function MATERIAL_SANITY_CHECKS_FACTORY(): boolean {
21+
export function MATERIAL_SANITY_CHECKS_FACTORY(): SanityChecks {
2222
return true;
2323
}
2424

2525
/** Injection token that configures whether the Material sanity checks are enabled. */
26-
export const MATERIAL_SANITY_CHECKS = new InjectionToken<boolean>('mat-sanity-checks', {
26+
export const MATERIAL_SANITY_CHECKS = new InjectionToken<SanityChecks>('mat-sanity-checks', {
2727
providedIn: 'root',
2828
factory: MATERIAL_SANITY_CHECKS_FACTORY,
2929
});
3030

31+
/**
32+
* Possible sanity checks that can be enabled. If set to
33+
* true/false, all checks will be enabled/disabled.
34+
*/
35+
export type SanityChecks = boolean | {
36+
doctype: boolean;
37+
theme: boolean;
38+
version: boolean;
39+
hammer: boolean;
40+
};
41+
3142
/**
3243
* Module that captures anything that should be loaded and/or run for *all* Angular Material
3344
* components. This includes Bidi, etc.
@@ -51,21 +62,28 @@ export class MatCommonModule {
5162
/** Reference to the global 'window' object. */
5263
private _window = typeof window === 'object' && window ? window : null;
5364

65+
/** Configured sanity checks. */
66+
private _sanityChecks: SanityChecks;
67+
5468
constructor(
55-
@Optional() @Inject(MATERIAL_SANITY_CHECKS) private _sanityChecksEnabled: boolean,
69+
@Optional() @Inject(MATERIAL_SANITY_CHECKS) sanityChecks: any,
5670
@Optional() @Inject(HAMMER_LOADER) private _hammerLoader?: HammerLoader) {
5771

58-
if (this._areChecksEnabled() && !this._hasDoneGlobalChecks) {
72+
// Note that `_sanityChecks` is typed to `any`, because AoT
73+
// throws an error if we use the `SanityChecks` type directly.
74+
this._sanityChecks = sanityChecks;
75+
76+
if (!this._hasDoneGlobalChecks) {
5977
this._checkDoctypeIsDefined();
6078
this._checkThemeIsPresent();
6179
this._checkCdkVersionMatch();
6280
this._hasDoneGlobalChecks = true;
6381
}
6482
}
6583

66-
/** Whether any sanity checks are enabled */
67-
private _areChecksEnabled(): boolean {
68-
return this._sanityChecksEnabled && isDevMode() && !this._isTestEnv();
84+
/** Whether any sanity checks are enabled. */
85+
private _checksAreEnabled(): boolean {
86+
return isDevMode() && !this._isTestEnv();
6987
}
7088

7189
/** Whether the code is running in tests. */
@@ -75,7 +93,10 @@ export class MatCommonModule {
7593
}
7694

7795
private _checkDoctypeIsDefined(): void {
78-
if (this._document && !this._document.doctype) {
96+
const isEnabled = this._checksAreEnabled() &&
97+
(typeof this._sanityChecks === 'boolean' ? this._sanityChecks : this._sanityChecks.doctype);
98+
99+
if (isEnabled && this._document && !this._document.doctype) {
79100
console.warn(
80101
'Current document does not have a doctype. This may cause ' +
81102
'some Angular Material components not to behave as expected.'
@@ -86,7 +107,11 @@ export class MatCommonModule {
86107
private _checkThemeIsPresent(): void {
87108
// We need to assert that the `body` is defined, because these checks run very early
88109
// and the `body` won't be defined if the consumer put their scripts in the `head`.
89-
if (!this._document || !this._document.body || typeof getComputedStyle !== 'function') {
110+
const isDisabled = !this._checksAreEnabled() ||
111+
(typeof this._sanityChecks === 'boolean' ? !this._sanityChecks : !this._sanityChecks.theme);
112+
113+
if (isDisabled || !this._document || !this._document.body ||
114+
typeof getComputedStyle !== 'function') {
90115
return;
91116
}
92117

@@ -113,7 +138,10 @@ export class MatCommonModule {
113138

114139
/** Checks whether the material version matches the cdk version */
115140
private _checkCdkVersionMatch(): void {
116-
if (VERSION.full !== CDK_VERSION.full) {
141+
const isEnabled = this._checksAreEnabled() &&
142+
(typeof this._sanityChecks === 'boolean' ? this._sanityChecks : this._sanityChecks.version);
143+
144+
if (isEnabled && VERSION.full !== CDK_VERSION.full) {
117145
console.warn(
118146
'The Angular Material version (' + VERSION.full + ') does not match ' +
119147
'the Angular CDK version (' + CDK_VERSION.full + ').\n' +
@@ -128,7 +156,10 @@ export class MatCommonModule {
128156
return;
129157
}
130158

131-
if (this._areChecksEnabled() && !(this._window as any)['Hammer'] && !this._hammerLoader) {
159+
const isEnabled = this._checksAreEnabled() &&
160+
(typeof this._sanityChecks === 'boolean' ? this._sanityChecks : this._sanityChecks.hammer);
161+
162+
if (isEnabled && !(this._window as any)['Hammer'] && !this._hammerLoader) {
132163
console.warn(
133164
'Could not find HammerJS. Certain Angular Material components may not work correctly.');
134165
}

src/material/core/common-behaviors/index.ts

Lines changed: 1 addition & 1 deletion
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-
export {MatCommonModule, MATERIAL_SANITY_CHECKS} from './common-module';
9+
export {MatCommonModule, MATERIAL_SANITY_CHECKS, SanityChecks} from './common-module';
1010
export {CanDisable, CanDisableCtor, mixinDisabled} from './disabled';
1111
export {CanColor, CanColorCtor, mixinColor, ThemePalette} from './color';
1212
export {CanDisableRipple, CanDisableRippleCtor, mixinDisableRipple} from './disable-ripple';

tools/public_api_guard/material/core.d.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ export declare const MAT_OPTION_PARENT_COMPONENT: InjectionToken<MatOptionParent
190190
export declare const MAT_RIPPLE_GLOBAL_OPTIONS: InjectionToken<RippleGlobalOptions>;
191191

192192
export declare class MatCommonModule {
193-
constructor(_sanityChecksEnabled: boolean, _hammerLoader?: HammerLoader | undefined);
193+
constructor(sanityChecks: any, _hammerLoader?: HammerLoader | undefined);
194194
_checkHammerIsAvailable(): void;
195195
}
196196

@@ -206,7 +206,7 @@ export declare type MatDateFormats = {
206206
};
207207
};
208208

209-
export declare const MATERIAL_SANITY_CHECKS: InjectionToken<boolean>;
209+
export declare const MATERIAL_SANITY_CHECKS: InjectionToken<SanityChecks>;
210210

211211
export declare class MatLine {
212212
}
@@ -412,6 +412,13 @@ export interface RippleTarget {
412412
rippleDisabled: boolean;
413413
}
414414

415+
export declare type SanityChecks = boolean | {
416+
doctype: boolean;
417+
theme: boolean;
418+
version: boolean;
419+
hammer: boolean;
420+
};
421+
415422
export declare const JAN = 0, FEB = 1, MAR = 2, APR = 3, MAY = 4, JUN = 5, JUL = 6, AUG = 7, SEP = 8, OCT = 9, NOV = 10, DEC = 11;
416423

417424
export declare function setLines(lines: QueryList<MatLine>, element: ElementRef<HTMLElement>): void;

0 commit comments

Comments
 (0)