Skip to content

Commit

Permalink
Use requireModule('ember') instead of window.Ember (#1535)
Browse files Browse the repository at this point in the history
* Use require('ember') instead of `window.Ember`

* require('ember') || window.Ember

* Try requireModule

* Try require.has

* requireModule again

* requireModule again

* Fix issues with deprecation re-entry and debug class name logging

* Fix tests for 3.27+

* Wrap ember require in try/catch

* Skip mixin tests

Co-authored-by: Chris Garrett <[email protected]>
  • Loading branch information
RobbieTheWagner and Chris Garrett authored Apr 26, 2021
1 parent 15f49e5 commit 33b6124
Show file tree
Hide file tree
Showing 31 changed files with 2,072 additions and 2,033 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ module.exports = {
},
globals: {
basicContext: false,
require: false,
requireModule: false,
},
rules: {
'no-prototype-builtins': 'off',
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ export default Controller.extend({
}),

setActive: action(function (bool) {
this.set('active', bool);
schedule('afterRender', () => {
this.set('active', bool);
});
}),

setupContentElement: action(function (element) {
Expand Down
8 changes: 6 additions & 2 deletions ember_debug/adapters/basic.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/* globals requireModule */
/* eslint no-console: 0 */
const Ember = window.Ember;
let Ember;
try {
Ember = requireModule('ember')['default'];
} catch {
Ember = window.Ember;
}
const { A, computed, RSVP, Object: EmberObject } = Ember;
const { Promise, resolve } = RSVP;
import { onReady } from '../utils/on-ready';
Expand Down
9 changes: 7 additions & 2 deletions ember_debug/adapters/web-extension.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import BasicAdapter from './basic';
import { typeOf } from '../utils/type-check';

const Ember = window.Ember;
let Ember;
try {
Ember = requireModule('ember')['default'];
} catch {
Ember = window.Ember;
}
const { run } = Ember;
const { isArray } = Array;
const { keys } = Object;
Expand Down Expand Up @@ -48,7 +53,7 @@ export default BasicAdapter.extend({
// scope but not on the global object (i.e. `window.inspect`) does
// not work. This sometimes causes problems, because, e.g. if the
// page has a div with the ID `inspect`, `window.inspect` will point
// to that div and shadown the "global" inspect function with no way
// to that div and shadow the "global" inspect function with no way
// to get it back. That causes "`inspect` is not a function" errors.
//
// As it turns out, however, when the extension page evals, the
Expand Down
7 changes: 6 additions & 1 deletion ember_debug/adapters/websocket.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import BasicAdapter from './basic';
const Ember = window.Ember;
let Ember;
try {
Ember = requireModule('ember')['default'];
} catch {
Ember = window.Ember;
}
const {
computed,
run,
Expand Down
7 changes: 6 additions & 1 deletion ember_debug/container-debug.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// eslint-disable-next-line ember/no-mixins
import PortMixin from 'ember-debug/mixins/port-mixin';
const Ember = window.Ember;
let Ember;
try {
Ember = requireModule('ember')['default'];
} catch {
Ember = window.Ember;
}
const { Object: EmberObject, computed } = Ember;
const { readOnly } = computed;

Expand Down
7 changes: 6 additions & 1 deletion ember_debug/data-debug.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
// eslint-disable-next-line ember/no-mixins
import PortMixin from 'ember-debug/mixins/port-mixin';

const Ember = window.Ember;
let Ember;
try {
Ember = requireModule('ember')['default'];
} catch {
Ember = window.Ember;
}
const { Object: EmberObject, computed, guidFor, A, set } = Ember;
const { alias } = computed;

Expand Down
29 changes: 22 additions & 7 deletions ember_debug/deprecation-debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,27 @@
import PortMixin from 'ember-debug/mixins/port-mixin';
import SourceMap from 'ember-debug/libs/source-map';

const Ember = window.Ember;
const { Debug, Object: EmberObject, computed, guidFor, run, RSVP, A } = Ember;
let Ember;
try {
Ember = requireModule('ember')['default'];
} catch {
Ember = window.Ember;
}

const { Debug, Object: EmberObject, computed, guidFor, RSVP, A } = Ember;
const { resolve, all } = RSVP;
const { readOnly } = computed;
const { registerDeprecationHandler } = Debug;

let run;
try {
run = requireModule('@ember/runloop')['default'];
} catch {
run = Ember.run;
}

const { cancel, debounce } = run;

export default EmberObject.extend(PortMixin, {
portNamespace: 'deprecation',

Expand Down Expand Up @@ -174,7 +189,7 @@ export default EmberObject.extend(PortMixin, {
},

clear() {
run.cancel(this.debounce);
cancel(this.debounce);
this.deprecations.clear();
this.set('groupedDeprecations', {});
this.sendCount();
Expand All @@ -191,7 +206,7 @@ export default EmberObject.extend(PortMixin, {
},

willDestroy() {
run.cancel(this.debounce);
cancel(this.debounce);
return this._super(...arguments);
},

Expand Down Expand Up @@ -242,11 +257,11 @@ export default EmberObject.extend(PortMixin, {
// to catch deprecations
if (!this.namespace.IGNORE_DEPRECATIONS) {
this.deprecationsToSend.pushObject(deprecation);
run.cancel(this.debounce);
cancel(this.debounce);
if (this._watching) {
this.debounce = run.debounce(this, 'sendPending', 100);
this.debounce = debounce(this, 'sendPending', 100);
} else {
this.debounce = run.debounce(this, 'sendCount', 100);
this.debounce = debounce(this, 'sendCount', 100);
}
if (!this._warned) {
this.adapter.warn(
Expand Down
7 changes: 6 additions & 1 deletion ember_debug/general-debug.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
/* eslint no-empty:0 */
// eslint-disable-next-line ember/no-mixins
import PortMixin from 'ember-debug/mixins/port-mixin';
const Ember = window.Ember;
let Ember;
try {
Ember = requireModule('ember')['default'];
} catch {
Ember = window.Ember;
}
const { Object: EmberObject } = Ember;
let { libraries } = Ember;

Expand Down
7 changes: 6 additions & 1 deletion ember_debug/libs/capture-render-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { compareVersion } from '../utils/version';

/* eslint-disable no-console, no-inner-declarations */

const Ember = window.Ember;
let Ember;
try {
Ember = requireModule('ember')['default'];
} catch {
Ember = window.Ember;
}

let captureRenderTree;

Expand Down
7 changes: 6 additions & 1 deletion ember_debug/libs/promise-assembler.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@

import Promise from 'ember-debug/models/promise';

const Ember = window.Ember;
let Ember;
try {
Ember = requireModule('ember')['default'];
} catch {
Ember = window.Ember;
}
const { Object: EmberObject, Evented, A, computed, RSVP, isNone } = Ember;

let PromiseAssembler = EmberObject.extend(Evented, {
Expand Down
7 changes: 6 additions & 1 deletion ember_debug/libs/source-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
* A lot of the code is inspired by/taken from
* https://github.com/evanw/node-source-map-support
*/
const Ember = window.Ember;
let Ember;
try {
Ember = requireModule('ember')['default'];
} catch {
Ember = window.Ember;
}
const {
Object: EmberObject,
A,
Expand Down
7 changes: 6 additions & 1 deletion ember_debug/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import ContainerDebug from 'ember-debug/container-debug';
import DeprecationDebug from 'ember-debug/deprecation-debug';
import Session from 'ember-debug/services/session';

const Ember = window.Ember;
let Ember;
try {
Ember = requireModule('ember')['default'];
} catch {
Ember = window.Ember;
}
const {
Object: EmberObject,
run,
Expand Down
7 changes: 6 additions & 1 deletion ember_debug/mixins/port-mixin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
const Ember = window.Ember;
let Ember;
try {
Ember = requireModule('ember')['default'];
} catch {
Ember = window.Ember;
}
const { Mixin } = Ember;

// eslint-disable-next-line ember/no-new-mixins
Expand Down
7 changes: 6 additions & 1 deletion ember_debug/models/profile-manager.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import ProfileNode from './profile-node';
const Ember = window.Ember;
let Ember;
try {
Ember = requireModule('ember')['default'];
} catch {
Ember = window.Ember;
}
const {
run: { later, scheduleOnce },
} = Ember;
Expand Down
7 changes: 6 additions & 1 deletion ember_debug/models/profile-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
@class ProfileNode
**/
const Ember = window.Ember;
let Ember;
try {
Ember = requireModule('ember')['default'];
} catch {
Ember = window.Ember;
}
const { get, guidFor } = Ember;

const ProfileNode = function (start, payload, parent, now) {
Expand Down
7 changes: 6 additions & 1 deletion ember_debug/models/promise.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { typeOf } from '../utils/type-check';

const Ember = window.Ember;
let Ember;
try {
Ember = requireModule('ember')['default'];
} catch {
Ember = window.Ember;
}
const { Object: EmberObject, computed, A } = Ember;

const dateComputed = function () {
Expand Down
25 changes: 22 additions & 3 deletions ember_debug/object-inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import {
import { compareVersion } from 'ember-debug/utils/version';
import { typeOf } from './utils/type-check';

const Ember = window.Ember;
let Ember;
try {
Ember = requireModule('ember')['default'];
} catch {
Ember = window.Ember;
}
const {
Object: EmberObject,
inspect: emberInspect,
Expand Down Expand Up @@ -770,7 +775,21 @@ function getClassName(object) {
(emberNames.get(object.constructor) || object.constructor.name)) ||
'';

if ('toString' in object && object.toString !== Object.prototype.toString) {
if (object.constructor && object.constructor.prototype === object) {
let { constructor } = object;

if (
constructor.toString &&
constructor.toString !== Object.prototype.toString
) {
name = constructor.toString();
} else {
name = constructor.name;
}
} else if (
'toString' in object &&
object.toString !== Object.prototype.toString
) {
name = object.toString();
}

Expand All @@ -779,7 +798,7 @@ function getClassName(object) {
// with the class name. We check the length of the class name to prevent doing
// this when the value is minified.
if (
name.match(/<.*:ember\d+>/) &&
name.match(/<.*:.*>/) &&
!className.startsWith('_') &&
className.length > 2 &&
className !== 'Class'
Expand Down
7 changes: 6 additions & 1 deletion ember_debug/port.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
const Ember = window.Ember;
let Ember;
try {
Ember = requireModule('ember')['default'];
} catch {
Ember = window.Ember;
}
const { Object: EmberObject, computed, run } = Ember;
const { or, readOnly } = computed;

Expand Down
7 changes: 6 additions & 1 deletion ember_debug/promise-debug.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
// eslint-disable-next-line ember/no-mixins
import PortMixin from 'ember-debug/mixins/port-mixin';
import PromiseAssembler from 'ember-debug/libs/promise-assembler';
const Ember = window.Ember;
let Ember;
try {
Ember = requireModule('ember')['default'];
} catch {
Ember = window.Ember;
}
const { computed, Object: EmberObject, RSVP, A, run } = Ember;
const { readOnly } = computed;

Expand Down
7 changes: 6 additions & 1 deletion ember_debug/render-debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
import PortMixin from 'ember-debug/mixins/port-mixin';
import ProfileManager from './models/profile-manager';

const Ember = window.Ember;
let Ember;
try {
Ember = requireModule('ember')['default'];
} catch {
Ember = window.Ember;
}
const { subscribe, Object: EmberObject } = Ember;

// Initial setup, that has to occur before the EmberObject init for some reason
Expand Down
7 changes: 6 additions & 1 deletion ember_debug/route-debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import { compareVersion } from 'ember-debug/utils/version';
import classify from 'ember-debug/utils/classify';
import dasherize from 'ember-debug/utils/dasherize';

const Ember = window.Ember;
let Ember;
try {
Ember = requireModule('ember')['default'];
} catch {
Ember = window.Ember;
}
const {
computed,
observer,
Expand Down
7 changes: 6 additions & 1 deletion ember_debug/services/session.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
const Ember = window.Ember;
let Ember;
try {
Ember = requireModule('ember')['default'];
} catch {
Ember = window.Ember;
}
const { Object: EmberObject } = Ember;

const Session = EmberObject.extend({
Expand Down
7 changes: 6 additions & 1 deletion ember_debug/utils/type-check.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
const Ember = window.Ember;
let Ember;
try {
Ember = requireModule('ember')['default'];
} catch {
Ember = window.Ember;
}
const { ComputedProperty } = Ember;

/**
Expand Down
Loading

0 comments on commit 33b6124

Please sign in to comment.