Skip to content

Commit

Permalink
feat: strip deprecations with macros (#8280)
Browse files Browse the repository at this point in the history
* feat: strip deprecations with macros

* fixup check
  • Loading branch information
runspired authored Oct 30, 2022
1 parent 9cf1cba commit b14e586
Show file tree
Hide file tree
Showing 22 changed files with 461 additions and 300 deletions.
105 changes: 58 additions & 47 deletions packages/model/addon/-private/belongs-to.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import { lookupLegacySupport } from './model';
import { computedMacroWithOptionalParams } from './util';

function normalizeType(type) {
if (DEPRECATE_RELATIONSHIPS_WITHOUT_TYPE && !type) {
return;
if (DEPRECATE_RELATIONSHIPS_WITHOUT_TYPE) {
if (!type) {
return;
}
}

return dasherize(type);
Expand Down Expand Up @@ -127,60 +129,69 @@ function normalizeType(type) {
function belongsTo(modelName, options) {
let opts = options;
let userEnteredModelName = modelName;
if (DEPRECATE_RELATIONSHIPS_WITHOUT_TYPE && (typeof modelName !== 'string' || !modelName.length)) {
deprecate('belongsTo() must specify the string type of the related resource as the first parameter', false, {
id: 'ember-data:deprecate-non-strict-relationships',
for: 'ember-data',
until: '5.0',
since: { enabled: '4.7', available: '4.7' },
});

if (typeof modelName === 'object') {
opts = modelName;
userEnteredModelName = undefined;
} else {
opts = options;
userEnteredModelName = modelName;
}
if (DEPRECATE_RELATIONSHIPS_WITHOUT_TYPE) {
if (typeof modelName !== 'string' || !modelName.length) {
deprecate('belongsTo() must specify the string type of the related resource as the first parameter', false, {
id: 'ember-data:deprecate-non-strict-relationships',
for: 'ember-data',
until: '5.0',
since: { enabled: '4.7', available: '4.7' },
});

assert(
'The first argument to belongsTo must be a string representing a model type key, not an instance of ' +
typeof userEnteredModelName +
". E.g., to define a relation to the Person model, use belongsTo('person')",
typeof userEnteredModelName === 'string' || typeof userEnteredModelName === 'undefined'
);
}
if (typeof modelName === 'object') {
opts = modelName;
userEnteredModelName = undefined;
} else {
opts = options;
userEnteredModelName = modelName;
}

if (DEPRECATE_RELATIONSHIPS_WITHOUT_ASYNC && (!opts || typeof opts.async !== 'boolean')) {
opts = opts || {};
if (!('async' in opts)) {
opts.async = true;
assert(
'The first argument to belongsTo must be a string representing a model type key, not an instance of ' +
typeof userEnteredModelName +
". E.g., to define a relation to the Person model, use belongsTo('person')",
typeof userEnteredModelName === 'string' || typeof userEnteredModelName === 'undefined'
);
}
deprecate('belongsTo(<type>, <options>) must specify options.async as either `true` or `false`.', false, {
id: 'ember-data:deprecate-non-strict-relationships',
for: 'ember-data',
until: '5.0',
since: { enabled: '4.7', available: '4.7' },
});
} else {
assert(`Expected belongsTo options.async to be a boolean`, opts && typeof opts.async === 'boolean');
}

if (
DEPRECATE_RELATIONSHIPS_WITHOUT_INVERSE &&
opts.inverse !== null &&
(typeof opts.inverse !== 'string' || opts.inverse.length === 0)
) {
deprecate(
'belongsTo(<type>, <options>) must specify options.inverse as either `null` or string type of the related resource.',
false,
{
if (DEPRECATE_RELATIONSHIPS_WITHOUT_ASYNC) {
if (!opts || typeof opts.async !== 'boolean') {
opts = opts || {};
if (!('async' in opts)) {
opts.async = true;
}
deprecate('belongsTo(<type>, <options>) must specify options.async as either `true` or `false`.', false, {
id: 'ember-data:deprecate-non-strict-relationships',
for: 'ember-data',
until: '5.0',
since: { enabled: '4.7', available: '4.7' },
}
);
});
} else {
assert(`Expected belongsTo options.async to be a boolean`, opts && typeof opts.async === 'boolean');
}
} else {
assert(`Expected belongsTo options.async to be a boolean`, opts && typeof opts.async === 'boolean');
}

if (DEPRECATE_RELATIONSHIPS_WITHOUT_INVERSE) {
if (opts.inverse !== null && (typeof opts.inverse !== 'string' || opts.inverse.length === 0)) {
deprecate(
'belongsTo(<type>, <options>) must specify options.inverse as either `null` or string type of the related resource.',
false,
{
id: 'ember-data:deprecate-non-strict-relationships',
for: 'ember-data',
until: '5.0',
since: { enabled: '4.7', available: '4.7' },
}
);
} else {
assert(
`Expected belongsTo options.inverse to be either null or the string type of the related resource.`,
opts.inverse === null || (typeof opts.inverse === 'string' && opts.inverse.length > 0)
);
}
} else {
assert(
`Expected belongsTo options.inverse to be either null or the string type of the related resource.`,
Expand Down
100 changes: 53 additions & 47 deletions packages/model/addon/-private/has-many.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import { lookupLegacySupport } from './model';
import { computedMacroWithOptionalParams } from './util';

function normalizeType(type) {
if (DEPRECATE_RELATIONSHIPS_WITHOUT_TYPE && !type) {
return;
if (DEPRECATE_RELATIONSHIPS_WITHOUT_TYPE) {
if (!type) {
return;
}
}

return singularize(dasherize(type));
Expand Down Expand Up @@ -168,60 +170,64 @@ function normalizeType(type) {
@return {Ember.computed} relationship
*/
function hasMany(type, options) {
if (DEPRECATE_RELATIONSHIPS_WITHOUT_TYPE && (typeof type !== 'string' || !type.length)) {
deprecate(
'hasMany(<type>, <options>) must specify the string type of the related resource as the first parameter',
false,
{
id: 'ember-data:deprecate-non-strict-relationships',
for: 'ember-data',
until: '5.0',
since: { enabled: '4.7', available: '4.7' },
if (DEPRECATE_RELATIONSHIPS_WITHOUT_TYPE) {
if (typeof type !== 'string' || !type.length) {
deprecate(
'hasMany(<type>, <options>) must specify the string type of the related resource as the first parameter',
false,
{
id: 'ember-data:deprecate-non-strict-relationships',
for: 'ember-data',
until: '5.0',
since: { enabled: '4.7', available: '4.7' },
}
);
if (typeof type === 'object') {
options = type;
type = undefined;
}
);
if (typeof type === 'object') {
options = type;
type = undefined;
}

assert(
`The first argument to hasMany must be a string representing a model type key, not an instance of ${inspect(
type
)}. E.g., to define a relation to the Comment model, use hasMany('comment')`,
typeof type === 'string' || typeof type === 'undefined'
);
}

if (DEPRECATE_RELATIONSHIPS_WITHOUT_ASYNC && (!options || typeof options.async !== 'boolean')) {
options = options || {};
if (!('async' in options)) {
options.async = true;
assert(
`The first argument to hasMany must be a string representing a model type key, not an instance of ${inspect(
type
)}. E.g., to define a relation to the Comment model, use hasMany('comment')`,
typeof type === 'string' || typeof type === 'undefined'
);
}
deprecate('hasMany(<type>, <options>) must specify options.async as either `true` or `false`.', false, {
id: 'ember-data:deprecate-non-strict-relationships',
for: 'ember-data',
until: '5.0',
since: { enabled: '4.7', available: '4.7' },
});
} else {
assert(`Expected hasMany options.async to be a boolean`, options && typeof options.async === 'boolean');
}

if (
DEPRECATE_RELATIONSHIPS_WITHOUT_INVERSE &&
options.inverse !== null &&
(typeof options.inverse !== 'string' || options.inverse.length === 0)
) {
deprecate(
'hasMany(<type>, <options>) must specify options.inverse as either `null` or string type of the related resource.',
false,
{
if (DEPRECATE_RELATIONSHIPS_WITHOUT_ASYNC) {
if (!options || typeof options.async !== 'boolean') {
options = options || {};
if (!('async' in options)) {
options.async = true;
}
deprecate('hasMany(<type>, <options>) must specify options.async as either `true` or `false`.', false, {
id: 'ember-data:deprecate-non-strict-relationships',
for: 'ember-data',
until: '5.0',
since: { enabled: '4.7', available: '4.7' },
}
);
});
} else {
assert(`Expected hasMany options.async to be a boolean`, options && typeof options.async === 'boolean');
}
} else {
assert(`Expected hasMany options.async to be a boolean`, options && typeof options.async === 'boolean');
}

if (DEPRECATE_RELATIONSHIPS_WITHOUT_INVERSE) {
if (options.inverse !== null && (typeof options.inverse !== 'string' || options.inverse.length === 0)) {
deprecate(
'hasMany(<type>, <options>) must specify options.inverse as either `null` or string type of the related resource.',
false,
{
id: 'ember-data:deprecate-non-strict-relationships',
for: 'ember-data',
until: '5.0',
since: { enabled: '4.7', available: '4.7' },
}
);
}
}

// Metadata about relationships is stored on the meta of
Expand Down
8 changes: 5 additions & 3 deletions packages/model/addon/-private/legacy-data-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,11 @@ function inverseForRelationship(store, identifier, key) {
return null;
}

if (DEPRECATE_RELATIONSHIPS_WITHOUT_INVERSE && metaIsRelationshipDefinition(definition)) {
const modelClass = store.modelFor(identifier.type);
return definition._inverseKey(store, modelClass);
if (DEPRECATE_RELATIONSHIPS_WITHOUT_INVERSE) {
if (metaIsRelationshipDefinition(definition)) {
const modelClass = store.modelFor(identifier.type);
return definition._inverseKey(store, modelClass);
}
}
assert(
`Expected the relationship defintion to specify the inverse type or null.`,
Expand Down
42 changes: 22 additions & 20 deletions packages/model/addon/-private/legacy-relationships-support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -705,26 +705,28 @@ function extractIdentifierFromRecord(recordOrPromiseRecord: PromiseProxyRecord |
return null;
}

if (DEPRECATE_PROMISE_PROXIES && isPromiseRecord(recordOrPromiseRecord)) {
let content = recordOrPromiseRecord.content;
assert(
'You passed in a promise that did not originate from an EmberData relationship. You can only pass promises that come from a belongsTo or hasMany relationship to the get call.',
content !== undefined
);
deprecate(
`You passed in a PromiseProxy to a Relationship API that now expects a resolved value. await the value before setting it.`,
false,
{
id: 'ember-data:deprecate-promise-proxies',
until: '5.0',
since: {
enabled: '4.7',
available: '4.7',
},
for: 'ember-data',
}
);
return content ? recordIdentifierFor(content) : null;
if (DEPRECATE_PROMISE_PROXIES) {
if (isPromiseRecord(recordOrPromiseRecord)) {
let content = recordOrPromiseRecord.content;
assert(
'You passed in a promise that did not originate from an EmberData relationship. You can only pass promises that come from a belongsTo or hasMany relationship to the get call.',
content !== undefined
);
deprecate(
`You passed in a PromiseProxy to a Relationship API that now expects a resolved value. await the value before setting it.`,
false,
{
id: 'ember-data:deprecate-promise-proxies',
until: '5.0',
since: {
enabled: '4.7',
available: '4.7',
},
for: 'ember-data',
}
);
return content ? recordIdentifierFor(content) : null;
}
}

return recordIdentifierFor(recordOrPromiseRecord);
Expand Down
44 changes: 23 additions & 21 deletions packages/model/addon/-private/many-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,27 +367,29 @@ function extractIdentifiersFromRecords(records: RecordInstance[]): StableRecordI
}

function extractIdentifierFromRecord(recordOrPromiseRecord: PromiseProxyRecord | RecordInstance) {
if (DEPRECATE_PROMISE_PROXIES && isPromiseRecord(recordOrPromiseRecord)) {
let content = recordOrPromiseRecord.content;
assert(
'You passed in a promise that did not originate from an EmberData relationship. You can only pass promises that come from a belongsTo relationship.',
content !== undefined && content !== null
);
deprecate(
`You passed in a PromiseProxy to a Relationship API that now expects a resolved value. await the value before setting it.`,
false,
{
id: 'ember-data:deprecate-promise-proxies',
until: '5.0',
since: {
enabled: '4.7',
available: '4.7',
},
for: 'ember-data',
}
);
assertRecordPassedToHasMany(content);
return recordIdentifierFor(content);
if (DEPRECATE_PROMISE_PROXIES) {
if (isPromiseRecord(recordOrPromiseRecord)) {
let content = recordOrPromiseRecord.content;
assert(
'You passed in a promise that did not originate from an EmberData relationship. You can only pass promises that come from a belongsTo relationship.',
content !== undefined && content !== null
);
deprecate(
`You passed in a PromiseProxy to a Relationship API that now expects a resolved value. await the value before setting it.`,
false,
{
id: 'ember-data:deprecate-promise-proxies',
until: '5.0',
since: {
enabled: '4.7',
available: '4.7',
},
for: 'ember-data',
}
);
assertRecordPassedToHasMany(content);
return recordIdentifierFor(content);
}
}

assertRecordPassedToHasMany(recordOrPromiseRecord);
Expand Down
Loading

0 comments on commit b14e586

Please sign in to comment.