Skip to content

Commit

Permalink
refactor: isDescriptor() logic & syntax
Browse files Browse the repository at this point in the history
- otherwise the embroider test scenarios throw Error:
- "EmberObject.create no longer supports defining computed properties"
  • Loading branch information
Pixelik committed Oct 12, 2023
1 parent 8969d78 commit 4cb0c8b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
27 changes: 15 additions & 12 deletions addon/-private/ember-internals.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import __EMBER_METAL__ from '@ember/-internals/metal/index';
import * as EMBER_METAL from '@ember/-internals/metal/index';

const POSSIBLE_DECORATORS = ['AliasDecoratorImpl', 'ComputedDecoratorImpl'];

export function getDependentKeys(descriptorOrDecorator) {
if (__EMBER_METAL__ && __EMBER_METAL__.descriptorForDecorator) {
let descriptor = __EMBER_METAL__.descriptorForDecorator(
descriptorOrDecorator,
);
if (EMBER_METAL && EMBER_METAL.descriptorForDecorator) {
let descriptor = EMBER_METAL.descriptorForDecorator(descriptorOrDecorator);
return descriptor._dependentKeys || [descriptor.altKey];
} else {
return descriptorOrDecorator._dependentKeys;
}
}

export function isDescriptor(o) {
if (__EMBER_METAL__ && __EMBER_METAL__.isClassicDecorator) {
return __EMBER_METAL__.isClassicDecorator(o);
} else {
return (
o && (typeof o === 'object' || typeof o === 'function') && o.isDescriptor
);
}
const isClassicDecorator =
EMBER_METAL &&
EMBER_METAL.isClassicDecorator &&
EMBER_METAL.isClassicDecorator(o);
const _isDescriptor =
o && (typeof o === 'object' || typeof o === 'function') && o.isDescriptor;
const isOtherDecoratorImpl = POSSIBLE_DECORATORS.includes(
o?.constructor?.name,
);
return isClassicDecorator || _isDescriptor || isOtherDecoratorImpl;
}
5 changes: 4 additions & 1 deletion addon/-private/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ export default class Options {
constructor({ model, attribute, options = {} }) {
const optionKeys = keys(options);
const createParams = { [OPTION_KEYS]: optionKeys, model, attribute };
const someOptionsAreDescriptors = optionKeys.some((key) => {
return isDescriptor(options[key]);
});

// If any of the options is a CP, we need to create a custom class for it
if (optionKeys.some((key) => isDescriptor(options[key]))) {
if (someOptionsAreDescriptors) {
return OptionsObject.extend(options).create(createParams);
}

Expand Down

0 comments on commit 4cb0c8b

Please sign in to comment.