Skip to content

Commit

Permalink
Merge branch 'master' into remove-EXTEND_PROTOTYPES
Browse files Browse the repository at this point in the history
  • Loading branch information
mixonic authored Jul 20, 2021
2 parents f9d62f1 + 023c3df commit 507a2c8
Show file tree
Hide file tree
Showing 45 changed files with 137 additions and 1,179 deletions.
46 changes: 21 additions & 25 deletions packages/@ember/-internals/container/lib/container.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Factory, LookupOptions, Owner, setOwner } from '@ember/-internals/owner';
import { dictionary, HAS_NATIVE_PROXY, HAS_NATIVE_SYMBOL, symbol } from '@ember/-internals/utils';
import { dictionary, symbol } from '@ember/-internals/utils';
import { assert } from '@ember/debug';
import { DEBUG } from '@glimmer/env';
import Registry, { DebugRegistry, Injection } from './registry';
Expand Down Expand Up @@ -234,30 +234,26 @@ if (DEBUG) {
* set on the manager.
*/
function wrapManagerInDeprecationProxy<T, C>(manager: FactoryManager<T, C>): FactoryManager<T, C> {
if (HAS_NATIVE_PROXY) {
let validator = {
set(_obj: T, prop: keyof T) {
throw new Error(
`You attempted to set "${prop}" on a factory manager created by container#factoryFor. A factory manager is a read-only construct.`
);
},
};

// Note:
// We have to proxy access to the manager here so that private property
// access doesn't cause the above errors to occur.
let m = manager;
let proxiedManager = {
class: m.class,
create(props?: { [prop: string]: any }) {
return m.create(props);
},
};

return new Proxy(proxiedManager, validator as any) as any;
}
let validator = {
set(_obj: T, prop: keyof T) {
throw new Error(
`You attempted to set "${prop}" on a factory manager created by container#factoryFor. A factory manager is a read-only construct.`
);
},
};

return manager;
// Note:
// We have to proxy access to the manager here so that private property
// access doesn't cause the above errors to occur.
let m = manager;
let proxiedManager = {
class: m.class,
create(props?: { [prop: string]: any }) {
return m.create(props);
},
};

return new Proxy(proxiedManager, validator as any) as any;
}

function isSingleton(container: Container, fullName: string) {
Expand Down Expand Up @@ -551,7 +547,7 @@ class FactoryManager<T, C> {
this.injections = undefined;
setFactoryFor(this, this);

if (factory && (HAS_NATIVE_SYMBOL || INIT_FACTORY in factory)) {
if (factory) {
setFactoryFor(factory, this);
}
}
Expand Down
9 changes: 1 addition & 8 deletions packages/@ember/-internals/environment/lib/env.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { FUNCTION_PROTOTYPE_EXTENSIONS } from '@ember/deprecated-features';
import { DEBUG } from '@glimmer/env';
import global from './global';

Expand Down Expand Up @@ -220,16 +219,10 @@ export const ENV = {
if (EXTEND_PROTOTYPES !== undefined) {
if (typeof EXTEND_PROTOTYPES === 'object' && EXTEND_PROTOTYPES !== null) {
ENV.EXTEND_PROTOTYPES.String = EXTEND_PROTOTYPES.String !== false;
if (FUNCTION_PROTOTYPE_EXTENSIONS) {
ENV.EXTEND_PROTOTYPES.Function = EXTEND_PROTOTYPES.Function !== false;
}
ENV.EXTEND_PROTOTYPES.Array = EXTEND_PROTOTYPES.Array !== false;
} else {
let isEnabled = EXTEND_PROTOTYPES !== false;
ENV.EXTEND_PROTOTYPES.String = isEnabled;
if (FUNCTION_PROTOTYPE_EXTENSIONS) {
ENV.EXTEND_PROTOTYPES.Function = isEnabled;
}
ENV.EXTEND_PROTOTYPES.Array = isEnabled;
}
}
Expand Down Expand Up @@ -259,6 +252,6 @@ export const ENV = {
}
})(global.EmberENV);

export function getENV() {
export function getENV(): object {
return ENV;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import { getOwner } from '@ember/-internals/owner';
import { _backburner } from '@ember/runloop';
import { get } from '@ember/-internals/metal';
import { dasherize } from '@ember/string';
import { HAS_NATIVE_SYMBOL } from '@ember/-internals/utils';
import { Namespace, Object as EmberObject, A as emberA } from '@ember/-internals/runtime';
import { consumeTag, createCache, getValue, tagFor, untrack } from '@glimmer/validator';

function iterate(arr, fn) {
if (HAS_NATIVE_SYMBOL && Symbol.iterator in arr) {
if (Symbol.iterator in arr) {
for (let item of arr) {
fn(item);
}
Expand Down
8 changes: 3 additions & 5 deletions packages/@ember/-internals/glimmer/lib/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ import {
} from './component-managers/curly';

// Keep track of which component classes have already been processed for lazy event setup.
// Using a WeakSet would be more appropriate here, but this can only be used when IE11 support is dropped.
// Thus the workaround using a WeakMap<object, true>
let lazyEventsProcessed = new WeakMap<EventDispatcher, WeakMap<object, true>>();
let lazyEventsProcessed = new WeakMap<EventDispatcher, WeakSet<object>>();

/**
@module @ember/component
Expand Down Expand Up @@ -671,7 +669,7 @@ const Component = CoreView.extend(
if (eventDispatcher) {
let lazyEventsProcessedForComponentClass = lazyEventsProcessed.get(eventDispatcher);
if (!lazyEventsProcessedForComponentClass) {
lazyEventsProcessedForComponentClass = new WeakMap<object, true>();
lazyEventsProcessedForComponentClass = new WeakSet<object>();
lazyEventsProcessed.set(eventDispatcher, lazyEventsProcessedForComponentClass);
}

Expand All @@ -685,7 +683,7 @@ const Component = CoreView.extend(
}
});

lazyEventsProcessedForComponentClass.set(proto, true);
lazyEventsProcessedForComponentClass.add(proto);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import InternalComponent, {
handleDeprecatedEventArguments,
InternalComponentConstructor,
jQueryEventShim,
ObjectEntries,
ObjectValues,
} from './internal';

const UNINITIALIZED: unknown = Object.freeze({});
Expand Down Expand Up @@ -304,7 +302,7 @@ export function handleDeprecatedFeatures(

handleDeprecatedAttributeArguments(target, attributeBindings);

handleDeprecatedEventArguments(target, ObjectEntries(virtualEvents));
handleDeprecatedEventArguments(target, Object.entries(virtualEvents));

{
let superIsVirtualEventListener = prototype['isVirtualEventListener'];
Expand All @@ -318,7 +316,7 @@ export function handleDeprecatedFeatures(
listener: Function
): listener is VirtualEventListener {
return (
ObjectValues(virtualEvents).indexOf(name) !== -1 ||
Object.values(virtualEvents).indexOf(name) !== -1 ||
superIsVirtualEventListener.call(this, name, listener)
);
},
Expand Down
24 changes: 3 additions & 21 deletions packages/@ember/-internals/glimmer/lib/components/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,6 @@ import InternalModifier, { InternalModifierManager } from '../modifiers/internal

function NOOP(): void {}

// TODO: remove me when IE11 support is EOL
export let ObjectEntries = ((): typeof Object['entries'] => {
if (typeof Object.entries === 'function') {
return Object.entries;
} else {
return (obj: {}) => Object.keys(obj).map((key) => [key, obj[key]] as [string, unknown]);
}
})();

// TODO: remove me when IE11 support is EOL
export let ObjectValues = ((): typeof Object['values'] => {
if (typeof Object.values === 'function') {
return Object.values;
} else {
return (obj: {}) => Object.keys(obj).map((key) => obj[key]);
}
})();

export type EventListener = (event: Event) => void;

export default class InternalComponent {
Expand Down Expand Up @@ -444,7 +426,7 @@ if (EMBER_MODERNIZED_BUILT_IN_COMPONENTS) {
name: string
): boolean {
let events = [
...ObjectValues(getEventsMap(this.owner)),
...Object.values(getEventsMap(this.owner)),
'focus-in',
'focus-out',
'key-press',
Expand All @@ -470,7 +452,7 @@ if (EMBER_MODERNIZED_BUILT_IN_COMPONENTS) {
let { element, component, listenerFor, listeners } = this;

let entries: [event: string, argument: string][] = [
...ObjectEntries(getEventsMap(this.owner)),
...Object.entries(getEventsMap(this.owner)),
...extraEvents,
];

Expand All @@ -489,7 +471,7 @@ if (EMBER_MODERNIZED_BUILT_IN_COMPONENTS) {
remove(): void {
let { element, listeners } = this;

for (let [event, listener] of ObjectEntries(listeners)) {
for (let [event, listener] of Object.entries(listeners)) {
element.removeEventListener(event, listener);
}

Expand Down
7 changes: 3 additions & 4 deletions packages/@ember/-internals/glimmer/lib/utils/iterator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { objectAt } from '@ember/-internals/metal';
import { _contentFor } from '@ember/-internals/runtime';
import { EmberArray, HAS_NATIVE_SYMBOL, isEmberArray, isObject } from '@ember/-internals/utils';
import { EmberArray, isEmberArray, isObject } from '@ember/-internals/utils';
import { Option } from '@glimmer/interfaces';
import { IteratorDelegate } from '@glimmer/reference';
import { consumeTag, isTracking, tagFor } from '@glimmer/validator';
Expand All @@ -21,7 +20,7 @@ function toEachInIterator(iterable: unknown) {

if (Array.isArray(iterable) || isEmberArray(iterable)) {
return ObjectIterator.fromIndexable(iterable);
} else if (HAS_NATIVE_SYMBOL && isNativeIterable<[unknown, unknown]>(iterable)) {
} else if (isNativeIterable<[unknown, unknown]>(iterable)) {
return MapLikeNativeIterator.from(iterable);
} else if (hasForEach(iterable)) {
return ObjectIterator.fromForEachable(iterable);
Expand All @@ -39,7 +38,7 @@ function toEachIterator(iterable: unknown) {
return ArrayIterator.from(iterable);
} else if (isEmberArray(iterable)) {
return EmberArrayIterator.from(iterable);
} else if (HAS_NATIVE_SYMBOL && isNativeIterable(iterable)) {
} else if (isNativeIterable(iterable)) {
return ArrayLikeNativeIterator.from(iterable);
} else if (hasForEach(iterable)) {
return ArrayIterator.fromForEachable(iterable);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { DEBUG } from '@glimmer/env';
import { moduleFor, RenderingTestCase, runTask } from 'internal-test-helpers';

import { HAS_NATIVE_SYMBOL } from '@ember/-internals/utils';

import { setComponentTemplate, getComponentTemplate } from '@glimmer/manager';
import { Component, compile } from '../../utils/helpers';

Expand Down Expand Up @@ -68,11 +66,9 @@ moduleFor(
setComponentTemplate(compile('foo'), 'foo');
}, /Cannot call `setComponentTemplate` on `foo`/);

if (HAS_NATIVE_SYMBOL) {
assert.throws(() => {
setComponentTemplate(compile('foo'), Symbol('foo'));
}, /Cannot call `setComponentTemplate` on `Symbol\(foo\)`/);
}
assert.throws(() => {
setComponentTemplate(compile('foo'), Symbol('foo'));
}, /Cannot call `setComponentTemplate` on `Symbol\(foo\)`/);
}

'@test calling it twice on the same object asserts'(assert) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { DEBUG } from '@glimmer/env';
import { alias, set, get, observer, on, computed, tracked } from '@ember/-internals/metal';
import Service, { inject as injectService } from '@ember/service';
import { Object as EmberObject, A as emberA } from '@ember/-internals/runtime';
import { jQueryDisabled } from '@ember/-internals/views';

import { Component, compile, htmlSafe } from '../../utils/helpers';
import { backtrackingMessageFor } from '../../utils/debug-stack';
Expand Down Expand Up @@ -3882,108 +3881,3 @@ moduleFor(
}
}
);

if (jQueryDisabled) {
moduleFor(
'Components test: curly components: jQuery disabled',
class extends RenderingTestCase {
['@test jQuery proxy is not available without jQuery']() {
let instance;

let FooBarComponent = Component.extend({
init() {
this._super();
instance = this;
},
});

this.registerComponent('foo-bar', {
ComponentClass: FooBarComponent,
template: 'hello',
});

this.render('{{foo-bar}}');

expectAssertion(() => {
instance.$()[0];
}, 'You cannot access this.$() with `jQuery` disabled.');
}
}
);
} else {
moduleFor(
'Components test: curly components: jQuery enabled',
class extends RenderingTestCase {
['@test it has a jQuery proxy to the element']() {
let instance;
let element1;
let element2;

let FooBarComponent = Component.extend({
init() {
this._super();
instance = this;
},
});

this.registerComponent('foo-bar', {
ComponentClass: FooBarComponent,
template: 'hello',
});

this.render('{{foo-bar}}');

expectDeprecation(() => {
element1 = instance.$()[0];
}, 'Using this.$() in a component has been deprecated, consider using this.element');

this.assertComponentElement(element1, { content: 'hello' });

runTask(() => this.rerender());

expectDeprecation(() => {
element2 = instance.$()[0];
}, 'Using this.$() in a component has been deprecated, consider using this.element');

this.assertComponentElement(element2, { content: 'hello' });

this.assertSameNode(element2, element1);
}

['@test it scopes the jQuery proxy to the component element'](assert) {
let instance;
let $span;

let FooBarComponent = Component.extend({
init() {
this._super();
instance = this;
},
});

this.registerComponent('foo-bar', {
ComponentClass: FooBarComponent,
template: '<span class="inner">inner</span>',
});

this.render('<span class="outer">outer</span>{{foo-bar}}');

expectDeprecation(() => {
$span = instance.$('span');
}, 'Using this.$() in a component has been deprecated, consider using this.element');

assert.equal($span.length, 1);
assert.equal($span.attr('class'), 'inner');

runTask(() => this.rerender());

expectDeprecation(() => {
$span = instance.$('span');
}, 'Using this.$() in a component has been deprecated, consider using this.element');

assert.equal($span.length, 1);
assert.equal($span.attr('class'), 'inner');
}
}
);
}
Loading

0 comments on commit 507a2c8

Please sign in to comment.