Skip to content

Commit 1723421

Browse files
arturovtkirjs
authored andcommitted
refactor(platform-browser): remove Console from Hammer gestures (angular#59409)
Injecting the `Console` is redundant because it directly calls the global `console` object. There is no reason to reference this class in Hammer gestures, as it is only used in development mode. We can safely call the `console` object directly. PR Close angular#59409
1 parent e194573 commit 1723421

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

packages/platform-browser/src/dom/events/hammer_gestures.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import {
1111
Inject,
1212
Injectable,
1313
InjectionToken,
14+
Injector,
1415
NgModule,
1516
Optional,
16-
Provider,
1717
ɵConsole as Console,
1818
} from '@angular/core';
1919

@@ -178,7 +178,7 @@ export class HammerGesturesPlugin extends EventManagerPlugin {
178178
constructor(
179179
@Inject(DOCUMENT) doc: any,
180180
@Inject(HAMMER_GESTURE_CONFIG) private _config: HammerGestureConfig,
181-
private console: Console,
181+
private _injector: Injector,
182182
@Optional() @Inject(HAMMER_LOADER) private loader?: HammerLoader | null,
183183
) {
184184
super(doc);
@@ -191,7 +191,10 @@ export class HammerGesturesPlugin extends EventManagerPlugin {
191191

192192
if (!(window as any).Hammer && !this.loader) {
193193
if (typeof ngDevMode === 'undefined' || ngDevMode) {
194-
this.console.warn(
194+
// Get a `Console` through an injector to tree-shake the
195+
// class when it is unused in production.
196+
const _console = this._injector.get(Console);
197+
_console.warn(
195198
`The "${eventName}" event cannot be bound because Hammer.JS is not ` +
196199
`loaded and no custom loader has been specified.`,
197200
);
@@ -223,9 +226,8 @@ export class HammerGesturesPlugin extends EventManagerPlugin {
223226
// If Hammer isn't actually loaded when the custom loader resolves, give up.
224227
if (!(window as any).Hammer) {
225228
if (typeof ngDevMode === 'undefined' || ngDevMode) {
226-
this.console.warn(
227-
`The custom HAMMER_LOADER completed, but Hammer.JS is not present.`,
228-
);
229+
const _console = this._injector.get(Console);
230+
_console.warn(`The custom HAMMER_LOADER completed, but Hammer.JS is not present.`);
229231
}
230232
deregister = () => {};
231233
return;
@@ -239,7 +241,8 @@ export class HammerGesturesPlugin extends EventManagerPlugin {
239241
}
240242
}).catch(() => {
241243
if (typeof ngDevMode === 'undefined' || ngDevMode) {
242-
this.console.warn(
244+
const _console = this._injector.get(Console);
245+
_console.warn(
243246
`The "${eventName}" event cannot be bound because the custom ` +
244247
`Hammer.JS loader failed.`,
245248
);
@@ -297,7 +300,7 @@ export class HammerGesturesPlugin extends EventManagerPlugin {
297300
provide: EVENT_MANAGER_PLUGINS,
298301
useClass: HammerGesturesPlugin,
299302
multi: true,
300-
deps: [DOCUMENT, HAMMER_GESTURE_CONFIG, Console, [new Optional(), HAMMER_LOADER]],
303+
deps: [DOCUMENT, HAMMER_GESTURE_CONFIG, Injector, [new Optional(), HAMMER_LOADER]],
301304
},
302305
{provide: HAMMER_GESTURE_CONFIG, useClass: HammerGestureConfig, deps: []},
303306
],

packages/platform-browser/test/dom/events/hammer_gestures_spec.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,22 @@ import {
1515

1616
describe('HammerGesturesPlugin', () => {
1717
let plugin: HammerGesturesPlugin;
18-
let fakeConsole: any;
1918

2019
if (isNode) {
2120
// Jasmine will throw if there are no tests.
2221
it('should pass', () => {});
2322
return;
2423
}
2524

26-
beforeEach(() => {
27-
fakeConsole = {warn: jasmine.createSpy('console.warn')};
28-
});
29-
3025
describe('with no custom loader', () => {
3126
beforeEach(() => {
32-
plugin = new HammerGesturesPlugin(document, new HammerGestureConfig(), fakeConsole);
27+
plugin = new HammerGesturesPlugin(document, new HammerGestureConfig(), TestBed);
3328
});
3429

3530
it('should warn user and do nothing when Hammer.js not loaded', () => {
31+
const warnSpy = spyOn(console, 'warn');
3632
expect(plugin.supports('swipe')).toBe(false);
37-
expect(fakeConsole.warn).toHaveBeenCalledWith(
33+
expect(warnSpy).toHaveBeenCalledWith(
3834
`The "swipe" event cannot be bound because Hammer.JS is not ` +
3935
`loaded and no custom loader has been specified.`,
4036
);
@@ -90,7 +86,7 @@ describe('HammerGesturesPlugin', () => {
9086
const hammerConfig = new HammerGestureConfig();
9187
spyOn(hammerConfig, 'buildHammer').and.returnValue(fakeHammerInstance);
9288

93-
plugin = new HammerGesturesPlugin(document, hammerConfig, fakeConsole, loader);
89+
plugin = new HammerGesturesPlugin(document, hammerConfig, TestBed, loader);
9490

9591
// Use a fake EventManager that has access to the NgZone.
9692
plugin.manager = {getZone: () => ngZone} as EventManager;
@@ -114,8 +110,9 @@ describe('HammerGesturesPlugin', () => {
114110
});
115111

116112
it('should not log a warning when HammerJS is not loaded', () => {
113+
const warnSpy = spyOn(console, 'warn');
117114
plugin.addEventListener(someElement, 'swipe', () => {});
118-
expect(fakeConsole.warn).not.toHaveBeenCalled();
115+
expect(warnSpy).not.toHaveBeenCalled();
119116
});
120117

121118
it('should defer registering an event until Hammer is loaded', fakeAsync(() => {
@@ -152,21 +149,25 @@ describe('HammerGesturesPlugin', () => {
152149
}));
153150

154151
it('should log a warning when the loader fails', fakeAsync(() => {
152+
const warnSpy = spyOn(console, 'warn');
153+
155154
plugin.addEventListener(someElement, 'swipe', () => {});
156155
failLoader();
157156
tick();
158157

159-
expect(fakeConsole.warn).toHaveBeenCalledWith(
158+
expect(warnSpy).toHaveBeenCalledWith(
160159
`The "swipe" event cannot be bound because the custom Hammer.JS loader failed.`,
161160
);
162161
}));
163162

164163
it('should load a warning if the loader resolves and Hammer is not present', fakeAsync(() => {
164+
const warnSpy = spyOn(console, 'warn');
165+
165166
plugin.addEventListener(someElement, 'swipe', () => {});
166167
resolveLoader();
167168
tick();
168169

169-
expect(fakeConsole.warn).toHaveBeenCalledWith(
170+
expect(warnSpy).toHaveBeenCalledWith(
170171
`The custom HAMMER_LOADER completed, but Hammer.JS is not present.`,
171172
);
172173
}));

0 commit comments

Comments
 (0)