Skip to content

Commit 9c8579d

Browse files
christopherthielenmergify[bot]
authored andcommitted
fix(IE9): Add safeConsole so IE9 doesn't break
It's 2020 I can't believe I'm adding IE9 compat :)
1 parent 243e548 commit 9c8579d

File tree

3 files changed

+51
-24
lines changed

3 files changed

+51
-24
lines changed

src/common/safeConsole.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/** workaround for missing console object in IE9 when dev tools haven't been opened o_O */
2+
/* tslint:disable:no-console */
3+
import { noop } from './common';
4+
5+
const noopConsoleStub = { log: noop, error: noop, table: noop };
6+
7+
function ie9Console(console) {
8+
const bound = (fn: Function) => Function.prototype.bind.call(fn, console);
9+
return {
10+
log: bound(console.log),
11+
error: bound(console.log),
12+
table: bound(console.log),
13+
};
14+
}
15+
16+
function fallbackConsole(console) {
17+
const log = console.log.bind(console);
18+
const error = console.error ? console.error.bind(console) : log;
19+
const table = console.table ? console.table.bind(console) : log;
20+
return { log, error, table };
21+
}
22+
23+
function getSafeConsole() {
24+
// @ts-ignore
25+
const isIE9 = document && document.documentMode && document.documentMode === 9;
26+
if (isIE9) {
27+
return window && window.console ? ie9Console(window.console) : noopConsoleStub;
28+
} else if (!console.table || !console.error) {
29+
return fallbackConsole(console);
30+
} else {
31+
return console;
32+
}
33+
}
34+
35+
export const safeConsole = getSafeConsole();

src/common/trace.ts

+14-22
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@
3232
*
3333
* @publicapi @module trace
3434
*/
35-
/* tslint:disable:no-console */
3635
import { parse } from '../common/hof';
3736
import { isFunction, isNumber } from '../common/predicates';
3837
import { Transition } from '../transition/transition';
3938
import { ViewTuple } from '../view';
4039
import { ActiveUIView, ViewConfig, ViewContext } from '../view/interface';
4140
import { stringify, functionToString, maxLength, padString } from './strings';
41+
import { safeConsole } from './safeConsole';
4242
import { Resolvable } from '../resolve/resolvable';
4343
import { PathNode } from '../path/pathNode';
4444
import { PolicyWhen } from '../resolve/interface';
@@ -57,22 +57,14 @@ function uiViewString(uiview: ActiveUIView) {
5757
const viewConfigString = (viewConfig: ViewConfig) => {
5858
const view = viewConfig.viewDecl;
5959
const state = view.$context.name || '(root)';
60-
return `[View#${viewConfig.$id} from '${state}' state]: target ui-view: '${view.$uiViewName}@${
61-
view.$uiViewContextAnchor
62-
}'`;
60+
return `[View#${viewConfig.$id} from '${state}' state]: target ui-view: '${view.$uiViewName}@${view.$uiViewContextAnchor}'`;
6361
};
6462

6563
/** @hidden */
6664
function normalizedCat(input: Category | string): string {
6765
return isNumber(input) ? Category[input] : Category[Category[input]];
6866
}
6967

70-
/** @hidden */
71-
const consoleLog = Function.prototype.bind.call(console.log, console);
72-
73-
/** @hidden */
74-
const consoletable = isFunction(console.table) ? console.table.bind(console) : consoleLog.bind(console);
75-
7668
/**
7769
* Trace categories Enum
7870
*
@@ -176,13 +168,13 @@ export class Trace {
176168
/** @internalapi called by ui-router code */
177169
traceTransitionStart(trans: Transition) {
178170
if (!this.enabled(Category.TRANSITION)) return;
179-
console.log(`${transLbl(trans)}: Started -> ${stringify(trans)}`);
171+
safeConsole.log(`${transLbl(trans)}: Started -> ${stringify(trans)}`);
180172
}
181173

182174
/** @internalapi called by ui-router code */
183175
traceTransitionIgnored(trans: Transition) {
184176
if (!this.enabled(Category.TRANSITION)) return;
185-
console.log(`${transLbl(trans)}: Ignored <> ${stringify(trans)}`);
177+
safeConsole.log(`${transLbl(trans)}: Ignored <> ${stringify(trans)}`);
186178
}
187179

188180
/** @internalapi called by ui-router code */
@@ -191,45 +183,45 @@ export class Trace {
191183
const event = parse('traceData.hookType')(options) || 'internal',
192184
context = parse('traceData.context.state.name')(options) || parse('traceData.context')(options) || 'unknown',
193185
name = functionToString((step as any).registeredHook.callback);
194-
console.log(`${transLbl(trans)}: Hook -> ${event} context: ${context}, ${maxLength(200, name)}`);
186+
safeConsole.log(`${transLbl(trans)}: Hook -> ${event} context: ${context}, ${maxLength(200, name)}`);
195187
}
196188

197189
/** @internalapi called by ui-router code */
198190
traceHookResult(hookResult: HookResult, trans: Transition, transitionOptions: any) {
199191
if (!this.enabled(Category.HOOK)) return;
200-
console.log(`${transLbl(trans)}: <- Hook returned: ${maxLength(200, stringify(hookResult))}`);
192+
safeConsole.log(`${transLbl(trans)}: <- Hook returned: ${maxLength(200, stringify(hookResult))}`);
201193
}
202194

203195
/** @internalapi called by ui-router code */
204196
traceResolvePath(path: PathNode[], when: PolicyWhen, trans?: Transition) {
205197
if (!this.enabled(Category.RESOLVE)) return;
206-
console.log(`${transLbl(trans)}: Resolving ${path} (${when})`);
198+
safeConsole.log(`${transLbl(trans)}: Resolving ${path} (${when})`);
207199
}
208200

209201
/** @internalapi called by ui-router code */
210202
traceResolvableResolved(resolvable: Resolvable, trans?: Transition) {
211203
if (!this.enabled(Category.RESOLVE)) return;
212-
console.log(
204+
safeConsole.log(
213205
`${transLbl(trans)}: <- Resolved ${resolvable} to: ${maxLength(200, stringify(resolvable.data))}`
214206
);
215207
}
216208

217209
/** @internalapi called by ui-router code */
218210
traceError(reason: any, trans: Transition) {
219211
if (!this.enabled(Category.TRANSITION)) return;
220-
console.log(`${transLbl(trans)}: <- Rejected ${stringify(trans)}, reason: ${reason}`);
212+
safeConsole.log(`${transLbl(trans)}: <- Rejected ${stringify(trans)}, reason: ${reason}`);
221213
}
222214

223215
/** @internalapi called by ui-router code */
224216
traceSuccess(finalState: StateObject, trans: Transition) {
225217
if (!this.enabled(Category.TRANSITION)) return;
226-
console.log(`${transLbl(trans)}: <- Success ${stringify(trans)}, final state: ${finalState.name}`);
218+
safeConsole.log(`${transLbl(trans)}: <- Success ${stringify(trans)}, final state: ${finalState.name}`);
227219
}
228220

229221
/** @internalapi called by ui-router code */
230222
traceUIViewEvent(event: string, viewData: ActiveUIView, extra = '') {
231223
if (!this.enabled(Category.UIVIEW)) return;
232-
console.log(`ui-view: ${padString(30, event)} ${uiViewString(viewData)}${extra}`);
224+
safeConsole.log(`ui-view: ${padString(30, event)} ${uiViewString(viewData)}${extra}`);
233225
}
234226

235227
/** @internalapi called by ui-router code */
@@ -257,19 +249,19 @@ export class Trace {
257249
})
258250
.sort((a, b) => (a[uivheader] || '').localeCompare(b[uivheader] || ''));
259251

260-
consoletable(mapping);
252+
safeConsole.table(mapping);
261253
}
262254

263255
/** @internalapi called by ui-router code */
264256
traceViewServiceEvent(event: string, viewConfig: ViewConfig) {
265257
if (!this.enabled(Category.VIEWCONFIG)) return;
266-
console.log(`VIEWCONFIG: ${event} ${viewConfigString(viewConfig)}`);
258+
safeConsole.log(`VIEWCONFIG: ${event} ${viewConfigString(viewConfig)}`);
267259
}
268260

269261
/** @internalapi called by ui-router code */
270262
traceViewServiceUIViewEvent(event: string, viewData: ActiveUIView) {
271263
if (!this.enabled(Category.VIEWCONFIG)) return;
272-
console.log(`VIEWCONFIG: ${event} ${uiViewString(viewData)}`);
264+
safeConsole.log(`VIEWCONFIG: ${event} ${uiViewString(viewData)}`);
273265
}
274266
}
275267

src/state/stateMatcher.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { isString } from '../common/predicates';
33
import { StateOrName } from './interface';
44
import { StateObject } from './stateObject';
55
import { values } from '../common/common';
6+
import { safeConsole } from '../common/safeConsole';
67

78
export class StateMatcher {
89
constructor(private _states: { [key: string]: StateObject }) {}
@@ -29,8 +30,7 @@ export class StateMatcher {
2930
);
3031

3132
if (matches.length > 1) {
32-
// tslint:disable-next-line:no-console
33-
console.log(
33+
safeConsole.error(
3434
`stateMatcher.find: Found multiple matches for ${name} using glob: `,
3535
matches.map(match => match.name)
3636
);

0 commit comments

Comments
 (0)