Skip to content

Commit 3468f21

Browse files
committed
more passing tests
1 parent 2bf57d1 commit 3468f21

File tree

4 files changed

+115
-32
lines changed

4 files changed

+115
-32
lines changed

packages/@ember/-internals/metal/lib/decorator.ts

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,7 @@ export function makeComputedDecorator(
120120
): ExtendedMethodDecorator {
121121
let decorator = function COMPUTED_DECORATOR(...args: unknown[]): DecoratorPropertyDescriptor {
122122
if (isModernDecoratorArgs(args)) {
123-
return computedDecorator2023(
124-
args,
125-
desc,
126-
DecoratorClass
127-
) as unknown as DecoratorPropertyDescriptor;
123+
return computedDecorator2023(args, desc) as unknown as DecoratorPropertyDescriptor;
128124
}
129125

130126
let [target, key, propertyDesc, maybeMeta, isClassicDecorator] = args as [
@@ -135,25 +131,7 @@ export function makeComputedDecorator(
135131
boolean | undefined
136132
];
137133

138-
assert(
139-
`Only one computed property decorator can be applied to a class field or accessor, but '${key}' was decorated twice. You may have added the decorator to both a getter and setter, which is unnecessary.`,
140-
isClassicDecorator ||
141-
!propertyDesc ||
142-
!propertyDesc.get ||
143-
!COMPUTED_GETTERS.has(propertyDesc.get)
144-
);
145-
146-
let meta = arguments.length === 3 ? metaFor(target) : maybeMeta;
147-
desc.setup(target, key, propertyDesc, meta!);
148-
149-
let computedDesc: PropertyDescriptor = {
150-
enumerable: desc.enumerable,
151-
configurable: desc.configurable,
152-
get: DESCRIPTOR_GETTER_FUNCTION(key, desc),
153-
set: DESCRIPTOR_SETTER_FUNCTION(key, desc),
154-
};
155-
156-
return computedDesc;
134+
return makeDescriptor(desc, target, key, propertyDesc, maybeMeta, isClassicDecorator);
157135
};
158136

159137
setClassicDecorator(decorator, desc);
@@ -163,14 +141,49 @@ export function makeComputedDecorator(
163141
return decorator;
164142
}
165143

166-
function computedDecorator2023(
167-
args: Paramters<Decorator>,
144+
function makeDescriptor(
168145
desc: ComputedDescriptor,
169-
DecoratorClass: { prototype: object }
170-
) {
171-
let dec = identifyModernDecoratorArgs(args);
172-
console.log(`ignoring modern decorator on ${dec.kind} ${dec.context.name?.toString()}`);
173-
return;
146+
target: object,
147+
key: string,
148+
propertyDesc?: DecoratorPropertyDescriptor,
149+
maybeMeta?: Meta,
150+
isClassicDecorator?: boolean
151+
): PropertyDescriptor {
152+
assert(
153+
`Only one computed property decorator can be applied to a class field or accessor, but '${key}' was decorated twice. You may have added the decorator to both a getter and setter, which is unnecessary.`,
154+
isClassicDecorator ||
155+
!propertyDesc ||
156+
!propertyDesc.get ||
157+
!COMPUTED_GETTERS.has(propertyDesc.get)
158+
);
159+
160+
let meta = arguments.length === 3 ? metaFor(target) : maybeMeta;
161+
desc.setup(target, key, propertyDesc, meta!);
162+
163+
let computedDesc: PropertyDescriptor = {
164+
enumerable: desc.enumerable,
165+
configurable: desc.configurable,
166+
get: DESCRIPTOR_GETTER_FUNCTION(key, desc),
167+
set: DESCRIPTOR_SETTER_FUNCTION(key, desc),
168+
};
169+
return computedDesc;
170+
}
171+
172+
function computedDecorator2023(args: Parameters<Decorator>, desc: ComputedDescriptor) {
173+
const dec = identifyModernDecoratorArgs(args);
174+
switch (dec.kind) {
175+
case 'field':
176+
dec.context.addInitializer(function (this: any) {
177+
Object.defineProperty(
178+
this,
179+
dec.context.name,
180+
makeDescriptor(desc, this, dec.context.name as string)
181+
);
182+
});
183+
return;
184+
default:
185+
console.log(`unimplemented: injected on ${dec.kind} ${dec.context.name?.toString()}`);
186+
}
174187
}
175188

176189
/////////////

packages/@ember/-internals/metal/lib/injected_property.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import { computed } from './computed';
55
import type { DecoratorPropertyDescriptor, ElementDescriptor } from './decorator';
66
import { isElementDescriptor } from './decorator';
77
import { defineProperty } from './properties';
8+
import {
9+
type Decorator,
10+
identifyModernDecoratorArgs,
11+
isModernDecoratorArgs,
12+
} from './decorator-util';
813

914
export let DEBUG_INJECTION_FUNCTIONS: WeakMap<Function, any>;
1015

@@ -49,6 +54,10 @@ function inject(
4954
let elementDescriptor;
5055
let name: string | undefined;
5156

57+
if (isModernDecoratorArgs(args)) {
58+
return inject2023(type, undefined, args);
59+
}
60+
5261
if (isElementDescriptor(args)) {
5362
elementDescriptor = args;
5463
} else if (typeof args[0] === 'string') {
@@ -88,4 +97,40 @@ function inject(
8897
}
8998
}
9099

100+
function inject2023(type: string, name: string | undefined, args: Parameters<Decorator>) {
101+
const dec = identifyModernDecoratorArgs(args);
102+
switch (dec.kind) {
103+
case 'field':
104+
dec.context.addInitializer(function (this: any) {
105+
let getInjection = function (this: any) {
106+
let owner = getOwner(this) || this.container; // fallback to `container` for backwards compat
107+
108+
assert(
109+
`Attempting to lookup an injected property on an object without a container, ensure that the object was instantiated via a container.`,
110+
Boolean(owner)
111+
);
112+
113+
return owner.lookup(`${type}:${name || (dec.context.name as string)}`);
114+
};
115+
116+
if (DEBUG) {
117+
DEBUG_INJECTION_FUNCTIONS.set(getInjection, {
118+
type,
119+
name,
120+
});
121+
}
122+
123+
Object.defineProperty(this, dec.context.name, {
124+
get: getInjection,
125+
set(value) {
126+
defineProperty(this, dec.context.name as string, null, value);
127+
},
128+
});
129+
});
130+
return;
131+
default:
132+
throw new Error(`unimplemented: injected on ${dec.kind} ${dec.context.name?.toString()}`);
133+
}
134+
}
135+
91136
export default inject;

packages/@ember/object/index.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ import { setObservers } from '@ember/-internals/utils';
1111
import type { AnyFn } from '@ember/-internals/utility-types';
1212
import CoreObject from '@ember/object/core';
1313
import Observable from '@ember/object/observable';
14+
import {
15+
type Decorator,
16+
identifyModernDecoratorArgs,
17+
isModernDecoratorArgs,
18+
} from '@ember/-internals/metal/lib/decorator-util';
1419

1520
export {
1621
notifyPropertyChange,
@@ -181,6 +186,10 @@ export function action(desc: PropertyDescriptor): ExtendedMethodDecorator;
181186
export function action(
182187
...args: ElementDescriptor | [PropertyDescriptor]
183188
): PropertyDescriptor | ExtendedMethodDecorator {
189+
if (isModernDecoratorArgs(args)) {
190+
return action2023(args) as unknown as PropertyDescriptor;
191+
}
192+
184193
let actionFn: object | Function;
185194

186195
if (!isElementDescriptor(args)) {
@@ -309,3 +318,20 @@ export function observer<T extends AnyFn>(
309318
});
310319
return func;
311320
}
321+
322+
function action2023(args: Parameters<Decorator>) {
323+
const dec = identifyModernDecoratorArgs(args);
324+
switch (dec.kind) {
325+
case 'method':
326+
dec.context.addInitializer(function (this: any) {
327+
Object.defineProperty(
328+
this,
329+
dec.context.name,
330+
setupAction(this, dec.context.name, dec.value)
331+
);
332+
});
333+
break;
334+
default:
335+
throw new Error(`unimplemented: action on ${dec.kind} ${dec.context.name?.toString()}`);
336+
}
337+
}

packages/@ember/service/tests/service_test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ moduleFor(
5050
let owner = buildOwner();
5151

5252
class MainService extends Service {}
53-
5453
class Foo extends EmberObject {
5554
@inject main;
5655
}

0 commit comments

Comments
 (0)