Skip to content

Commit ff4d29b

Browse files
committed
progress
1 parent e86df83 commit ff4d29b

File tree

1 file changed

+32
-23
lines changed

1 file changed

+32
-23
lines changed

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

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -169,43 +169,52 @@ function makeDescriptor(
169169

170170
function computedDecorator2023(args: Parameters<Decorator>, desc: ComputedDescriptor) {
171171
const dec = identifyModernDecoratorArgs(args);
172+
173+
let setup: undefined | ((prototype: any, propDesc: any) => void) = function (
174+
prototype,
175+
propDesc
176+
) {
177+
desc.setup(prototype, dec.context.name as string, propDesc, metaFor(prototype));
178+
setup = undefined;
179+
};
180+
172181
switch (dec.kind) {
173182
case 'field':
174183
dec.context.addInitializer(function (this: any) {
175-
desc.setup(this, dec.context.name as string, undefined, metaFor(this));
184+
setup?.(this.constructor.prototype, undefined);
176185
Object.defineProperty(
177186
this,
178187
dec.context.name,
179188
makeDescriptor(desc, dec.context.name as string)
180189
);
181190
});
182191
break;
183-
case 'getter':
192+
case 'getter': {
193+
let propDesc = {
194+
get: dec.value as any,
195+
};
184196
dec.context.addInitializer(function (this: any) {
185-
let propDesc = {
186-
get: dec.value as any,
187-
};
188-
desc.setup(this, dec.context.name as string, propDesc, metaFor(this));
189-
Object.defineProperty(
190-
this,
191-
dec.context.name,
192-
makeDescriptor(desc, dec.context.name as string, propDesc)
193-
);
197+
setup?.(this.constructor.prototype, propDesc);
194198
});
195-
break;
196-
case 'setter':
199+
return makeDescriptor(desc, dec.context.name as string, propDesc).get;
200+
}
201+
case 'setter': {
202+
let propDesc = {
203+
set: dec.value as any,
204+
};
197205
dec.context.addInitializer(function (this: any) {
198-
let propDesc = {
199-
set: dec.value as any,
200-
};
201-
desc.setup(this, dec.context.name as string, propDesc, metaFor(this));
202-
Object.defineProperty(
203-
this,
204-
dec.context.name,
205-
makeDescriptor(desc, dec.context.name as string, propDesc)
206-
);
206+
setup?.(this.constructor.prototype, propDesc);
207207
});
208-
break;
208+
return makeDescriptor(desc, dec.context.name as string, propDesc).set;
209+
}
210+
case 'method':
211+
assert(
212+
`@computed can only be used on accessors or fields, attempted to use it with ${dec.context.name.toString()} but that was a method. Try converting it to a getter (e.g. \`get ${dec.context.name.toString()}() {}\`)`,
213+
false
214+
);
215+
// TS knows "assert()" is terminal and will complain about unreachable code if
216+
// I use a break here. ESLint complains if I *don't* use a break here.
217+
// eslint-disable-next-line no-fallthrough
209218
default:
210219
throw new Error(
211220
`unimplemented: computedDecorator on ${dec.kind} ${dec.context.name?.toString()}`

0 commit comments

Comments
 (0)