Skip to content

Commit

Permalink
Deprecate importing inject from @ember/service
Browse files Browse the repository at this point in the history
  • Loading branch information
bertdeblock committed Aug 18, 2023
1 parent 1cd116d commit db7b2eb
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 43 deletions.
17 changes: 17 additions & 0 deletions packages/@ember/service/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { deprecate } from '@ember/debug';
import { FrameworkObject } from '@ember/object/-internals';
import type { DecoratorPropertyDescriptor, ElementDescriptor } from '@ember/-internals/metal';
import { inject as metalInject } from '@ember/-internals/metal';
Expand All @@ -16,6 +17,7 @@ import { inject as metalInject } from '@ember/-internals/metal';
the property's name
@return {ComputedDecorator} injection decorator instance
@public
@deprecated Please import `service` instead.
*/
export function inject(name: string): PropertyDecorator;
export function inject(...args: [ElementDescriptor[0], ElementDescriptor[1]]): void;
Expand All @@ -24,6 +26,21 @@ export function inject(): PropertyDecorator;
export function inject(
...args: [] | [name: string] | ElementDescriptor
): PropertyDecorator | DecoratorPropertyDescriptor | void {
deprecate(
'Importing `inject` from `@ember/service` is deprecated. Please import `service` instead.',
false,
{
for: 'ember-source',
id: 'importing-inject-from-ember-service',
since: {
available: '5.4.0',
enabled: '5.4.0',
},
until: '6.0.0',
url: 'TODO',
}
);

return metalInject('service', ...args);
}

Expand Down
46 changes: 25 additions & 21 deletions packages/@ember/service/tests/service_test.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,51 @@
import Service, { inject, service } from '@ember/service';
import EmberObject from '@ember/object';
import { buildOwner, runDestroy } from 'internal-test-helpers';
import { moduleFor, AbstractTestCase } from 'internal-test-helpers';
import { moduleFor, AbstractTestCase, expectDeprecation } from 'internal-test-helpers';

moduleFor(
'inject - decorator',
class extends AbstractTestCase {
['@test works with native decorators'](assert) {
let owner = buildOwner();
expectDeprecation(() => {
let owner = buildOwner();

class MainService extends Service {}
class MainService extends Service {}

class Foo extends EmberObject {
@inject('main') main;
}
class Foo extends EmberObject {
@inject('main') main;
}

owner.register('service:main', MainService);
owner.register('foo:main', Foo);
owner.register('service:main', MainService);
owner.register('foo:main', Foo);

let foo = owner.lookup('foo:main');
let foo = owner.lookup('foo:main');

assert.ok(foo.main instanceof Service, 'service injected correctly');
assert.ok(foo.main instanceof Service, 'service injected correctly');

runDestroy(owner);
runDestroy(owner);
}, 'Importing `inject` from `@ember/service` is deprecated. Please import `service` instead.');
}

['@test uses the decorated property key if not provided'](assert) {
let owner = buildOwner();
expectDeprecation(() => {
let owner = buildOwner();

class MainService extends Service {}
class MainService extends Service {}

class Foo extends EmberObject {
@inject main;
}
class Foo extends EmberObject {
@inject main;
}

owner.register('service:main', MainService);
owner.register('foo:main', Foo);
owner.register('service:main', MainService);
owner.register('foo:main', Foo);

let foo = owner.lookup('foo:main');
let foo = owner.lookup('foo:main');

assert.ok(foo.main instanceof Service, 'service injected correctly');
assert.ok(foo.main instanceof Service, 'service injected correctly');

runDestroy(owner);
runDestroy(owner);
}, 'Importing `inject` from `@ember/service` is deprecated. Please import `service` instead.');
}
}
);
Expand Down
46 changes: 25 additions & 21 deletions packages/@ember/service/tests/service_ts_test.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,51 @@
import Service, { inject, service } from '@ember/service';
import EmberObject from '@ember/object';
import { buildOwner, runDestroy } from 'internal-test-helpers';
import { moduleFor, AbstractTestCase } from 'internal-test-helpers';
import { moduleFor, AbstractTestCase, expectDeprecation } from 'internal-test-helpers';

moduleFor(
'inject - decorator (TS)',
class extends AbstractTestCase {
['@test works with native decorators'](assert: QUnit['assert']) {
let owner = buildOwner();
expectDeprecation(() => {
let owner = buildOwner();

class MainService extends Service {}
class MainService extends Service {}

class Foo extends EmberObject {
@inject('main') declare main: MainService;
}
class Foo extends EmberObject {
@inject('main') declare main: MainService;
}

owner.register('service:main', MainService);
owner.register('foo:main', Foo);
owner.register('service:main', MainService);
owner.register('foo:main', Foo);

let foo = owner.lookup('foo:main') as Foo;
let foo = owner.lookup('foo:main') as Foo;

assert.ok(foo.main instanceof Service, 'service injected correctly');
assert.ok(foo.main instanceof Service, 'service injected correctly');

runDestroy(owner);
runDestroy(owner);
}, 'Importing `inject` from `@ember/service` is deprecated. Please import `service` instead.');
}

['@test uses the decorated property key if not provided'](assert: QUnit['assert']) {
let owner = buildOwner();
expectDeprecation(() => {
let owner = buildOwner();

class MainService extends Service {}
class MainService extends Service {}

class Foo extends EmberObject {
@inject declare main: MainService;
}
class Foo extends EmberObject {
@inject declare main: MainService;
}

owner.register('service:main', MainService);
owner.register('foo:main', Foo);
owner.register('service:main', MainService);
owner.register('foo:main', Foo);

let foo = owner.lookup('foo:main') as Foo;
let foo = owner.lookup('foo:main') as Foo;

assert.ok(foo.main instanceof Service, 'service injected correctly');
assert.ok(foo.main instanceof Service, 'service injected correctly');

runDestroy(owner);
runDestroy(owner);
}, 'Importing `inject` from `@ember/service` is deprecated. Please import `service` instead.');
}
}
);
Expand Down
2 changes: 1 addition & 1 deletion type-tests/@ember/routing-test/router.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Router from '@ember/routing/router';
import Service, { inject as service } from '@ember/service';
import Service, { service } from '@ember/service';
import EmberObject, { get } from '@ember/object';
import RouterService from '@ember/routing/router-service';
import RouteInfo, { RouteInfoWithAttributes } from '@ember/routing/route-info';
Expand Down

0 comments on commit db7b2eb

Please sign in to comment.