Skip to content

Commit

Permalink
Merge pull request #16523 from emberjs/more-ts
Browse files Browse the repository at this point in the history
Convert ember-environment to typescript.
  • Loading branch information
rwjblue authored Apr 16, 2018
2 parents bf9bf81 + 4849f56 commit 90cc5c0
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 92 deletions.
16 changes: 0 additions & 16 deletions packages/ember-environment/index.d.ts

This file was deleted.

3 changes: 3 additions & 0 deletions packages/ember-environment/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './lib/context';
export * from './lib/env';
export * from './lib/has-dom';
32 changes: 32 additions & 0 deletions packages/ember-environment/lib/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import global from './global';

export interface GlobalContext {
imports: object;
exports: object;
lookup: object;
}

// legacy imports/exports/lookup stuff (should we keep this??)
export const context = (function(
global: object,
Ember: Partial<GlobalContext> | undefined
): GlobalContext {
return Ember === undefined
? { imports: global, exports: global, lookup: global }
: {
// import jQuery
imports: Ember.imports || global,
// export Ember
exports: Ember.exports || global,
// search for Namespaces
lookup: Ember.lookup || global,
};
})(global, global.Ember);

export function getLookup(): object {
return context.lookup;
}

export function setLookup(value: object): void {
context.lookup = value;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,44 @@
import global from './lib/global';
import { defaultFalse, defaultTrue, normalizeExtendPrototypes } from './lib/utils';
import global from './global';
import { defaultFalse, defaultTrue, normalizeExtendPrototypes } from './utils';

export interface Environment {
ENABLE_ALL_FEATURES: boolean;
ENABLE_OPTIONAL_FEATURES: boolean;
EXTEND_PROTOTYPES: {
Array: boolean;
Function: boolean;
String: boolean;
};
LOG_STACKTRACE_ON_DEPRECATION: boolean;
LOG_VERSION: boolean;
RAISE_ON_DEPRECATION: boolean;
_APPLICATION_TEMPLATE_WRAPPER: boolean;
_TEMPLATE_ONLY_GLIMMER_COMPONENTS: boolean;
_ENABLE_EMBER_K_SUPPORT: boolean;
_ENABLE_SAFE_STRING_SUPPORT: boolean;
_ENABLE_ENUMERABLE_CONTAINS_SUPPORT: boolean;
_ENABLE_UNDERSCORE_ACTIONS_SUPPORT: boolean;
_ENABLE_REVERSED_OBSERVER_SUPPORT: boolean;
_ENABLE_INITIALIZER_ARGUMENTS_SUPPORT: boolean;
_ENABLE_ROUTER_RESOURCE: boolean;
_ENABLE_CURRENT_WHEN_SUPPORT: boolean;
_ENABLE_CONTROLLER_WRAPPED_SUPPORT: boolean;
_ENABLE_DEPRECATED_REGISTRY_SUPPORT: boolean;
_ENABLE_IMMEDIATE_OBSERVER_SUPPORT: boolean;
_ENABLE_STRING_FMT_SUPPORT: boolean;
_ENABLE_FREEZABLE_SUPPORT: boolean;
_ENABLE_COMPONENT_DEFAULTLAYOUT_SUPPORT: boolean;
_ENABLE_BINDING_SUPPORT: boolean;
_ENABLE_INPUT_TRANSFORM_SUPPORT: boolean;
_ENABLE_DEPRECATION_OPTIONS_SUPPORT: boolean;
_ENABLE_ORPHANED_OUTLETS_SUPPORT: boolean;
_ENABLE_WARN_OPTIONS_SUPPORT: boolean;
_ENABLE_RESOLVER_FUNCTION_SUPPORT: boolean;
_ENABLE_DID_INIT_ATTRS_SUPPORT: boolean;
_ENABLE_RENDER_SUPPORT: boolean;
_ENABLE_PROPERTY_REQUIRED_SUPPORT: boolean;
}

/**
The hash of environment variables used to control various configuration
settings. To specify your own or override default settings, add the
Expand All @@ -11,7 +50,7 @@ import { defaultFalse, defaultTrue, normalizeExtendPrototypes } from './lib/util
@type Object
@public
*/
export const ENV =
export const ENV: Environment =
(typeof global.EmberENV === 'object' && global.EmberENV) ||
(typeof global.ENV === 'object' && global.ENV) ||
{};
Expand Down Expand Up @@ -101,52 +140,3 @@ ENV._APPLICATION_TEMPLATE_WRAPPER = defaultTrue(ENV._APPLICATION_TEMPLATE_WRAPPE
@private
*/
ENV._TEMPLATE_ONLY_GLIMMER_COMPONENTS = defaultFalse(ENV._TEMPLATE_ONLY_GLIMMER_COMPONENTS);

// check if window exists and actually is the global
const hasDOM =
typeof window !== 'undefined' &&
window === global &&
window.document &&
window.document.createElement &&
!ENV.disableBrowserEnvironment; // is this a public thing?

// legacy imports/exports/lookup stuff (should we keep this??)
const originalContext = global.Ember || {};

export const context = {
// import jQuery
imports: originalContext.imports || global,
// export Ember
exports: originalContext.exports || global,
// search for Namespaces
lookup: originalContext.lookup || global,
};

export function getLookup() {
return context.lookup;
}

export function setLookup(value) {
context.lookup = value;
}

// TODO: cleanup single source of truth issues with this stuff
export const environment = hasDOM
? {
hasDOM: true,
isChrome: !!window.chrome && !window.opera,
isFirefox: typeof InstallTrigger !== 'undefined',
location: window.location,
history: window.history,
userAgent: window.navigator.userAgent,
window,
}
: {
hasDOM: false,
isChrome: false,
isFirefox: false,
location: null,
history: null,
userAgent: 'Lynx (textmode)',
window: null,
};
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
/* globals global, window, self, mainContext */
declare const mainContext: object | undefined;

// from lodash to catch fake globals
function checkGlobal(value) {
function checkGlobal(value: any | null | undefined): value is object {
return value && value.Object === Object ? value : undefined;
}

// element ids can ruin global miss checks
function checkElementIdShadowing(value) {
function checkElementIdShadowing(value: any | null | undefined) {
return value && value.nodeType === undefined ? value : undefined;
}

// export real global
export default checkGlobal(checkElementIdShadowing(typeof global === 'object' && global)) ||
checkGlobal(typeof self === 'object' && self) ||
checkGlobal(typeof window === 'object' && window) ||
mainContext || // set before strict mode in Ember loader/wrapper
(typeof mainContext !== 'undefined' && mainContext) || // set before strict mode in Ember loader/wrapper
new Function('return this')(); // eval outside of strict mode
33 changes: 33 additions & 0 deletions packages/ember-environment/lib/has-dom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ENV } from './env';
import global from './global';

// check if window exists and actually is the global
const hasDOM =
typeof window !== 'undefined' &&
window === global &&
window.document &&
window.document.createElement &&
!(ENV as any).disableBrowserEnvironment; // is this a public thing?

declare const InstallTrigger: any | undefined;

// TODO: cleanup single source of truth issues with this stuff
export const environment = hasDOM
? {
hasDOM: true,
isChrome: !!(window as any).chrome && !(window as any).opera,
isFirefox: typeof InstallTrigger !== 'undefined',
location: window.location,
history: window.history,
userAgent: window.navigator.userAgent,
window,
}
: {
hasDOM: false,
isChrome: false,
isFirefox: false,
location: null,
history: null,
userAgent: 'Lynx (textmode)',
window: null,
};
21 changes: 0 additions & 21 deletions packages/ember-environment/lib/utils.js

This file was deleted.

21 changes: 21 additions & 0 deletions packages/ember-environment/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export function defaultTrue(v: any | undefined | null) {
return v === false ? false : true;
}

export function defaultFalse(v: any | undefined | null) {
return v === true ? true : false;
}

export function normalizeExtendPrototypes(obj: any | undefined | null) {
if (obj === false) {
return { String: false, Array: false, Function: false };
}
if (obj === undefined || obj === null || obj === true) {
return { String: true, Array: true, Function: true };
}
return {
String: defaultTrue(obj.String),
Array: defaultTrue(obj.Array),
Function: defaultTrue(obj.Function),
};
}

0 comments on commit 90cc5c0

Please sign in to comment.