Skip to content

Commit

Permalink
Add new ember-data rule: require-async-inverse-relationship (#2155)
Browse files Browse the repository at this point in the history
  • Loading branch information
wozny1989 authored Aug 17, 2024
1 parent 8f485b2 commit af62351
Show file tree
Hide file tree
Showing 4 changed files with 330 additions and 4 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,11 @@ rules in templates can be disabled with eslint directives with mustache or html

### Ember Data

| Name                           | Description | 💼 | 🔧 | 💡 |
| :----------------------------------------------------------------------------- | :------------------------------------------------------------------- | :- | :- | :- |
| [no-empty-attrs](docs/rules/no-empty-attrs.md) | disallow usage of empty attributes in Ember Data models | | | |
| [use-ember-data-rfc-395-imports](docs/rules/use-ember-data-rfc-395-imports.md) | enforce usage of `@ember-data/` package imports instead `ember-data` || 🔧 | |
| Name                               | Description | 💼 | 🔧 | 💡 |
| :------------------------------------------------------------------------------------- | :-------------------------------------------------------------------- | :- | :- | :- |
| [no-empty-attrs](docs/rules/no-empty-attrs.md) | disallow usage of empty attributes in Ember Data models | | | |
| [require-async-inverse-relationship](docs/rules/require-async-inverse-relationship.md) | require inverse to be specified in @belongsTo and @hasMany decorators | | | |
| [use-ember-data-rfc-395-imports](docs/rules/use-ember-data-rfc-395-imports.md) | enforce usage of `@ember-data/` package imports instead `ember-data` || 🔧 | |

### Ember Object

Expand Down
41 changes: 41 additions & 0 deletions docs/rules/require-async-inverse-relationship.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# ember/require-async-inverse-relationship

<!-- end auto-generated rule header -->

This rule ensures that the `async` and `inverse` properties are specified in `@belongsTo` and `@hasMany` decorators in Ember Data models.

## Rule Details

This rule disallows:

- Using `@belongsTo` without specifying the `async` and `inverse` properties.
- Using `@hasMany` without specifying the `async` and `inverse` properties.

## Examples

Examples of **incorrect** code for this rule:

```js
import Model, { belongsTo, hasMany } from '@ember-data/model';

export default class FolderModel extends Model {
@hasMany('folder', { inverse: 'parent' }) children;
@belongsTo('folder', { inverse: 'children' }) parent;
}
```

Examples of **correct** code for this rule:

```js
import Model, { belongsTo, hasMany } from '@ember-data/model';

export default class FolderModel extends Model {
@hasMany('folder', { async: true, inverse: 'parent' }) children;
@belongsTo('folder', { async: true, inverse: 'children' }) parent;
}
```

## References

- [Deprecate Non Strict Relationships](https://deprecations.emberjs.com/id/ember-data-deprecate-non-strict-relationships)
- [Ember Data Relationships](https://guides.emberjs.com/release/models/relationships)
75 changes: 75 additions & 0 deletions lib/rules/require-async-inverse-relationship.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'require inverse to be specified in @belongsTo and @hasMany decorators',
category: 'Ember Data',
recommended: false,
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/require-async-inverse-relationship.md',
},
schema: [],
},

create(context) {
return {
CallExpression(node) {
const decorator =
node.parent.type === 'Decorator' &&
['belongsTo', 'hasMany'].includes(node.callee.name) &&
node;

if (decorator) {
const args = decorator.arguments;
const hasAsync = args.some(
(arg) =>
arg.type === 'ObjectExpression' &&
arg.properties.some((prop) => prop.key.name === 'async')
);
const hasBooleanAsync = args.some(
(arg) =>
arg.type === 'ObjectExpression' &&
arg.properties.some(
(prop) => prop.key.name === 'async' && typeof prop.value.value === 'boolean'
)
);
const hasInverse = args.some(
(arg) =>
arg.type === 'ObjectExpression' &&
arg.properties.some((prop) => prop.key.name === 'inverse')
);

if (!hasAsync) {
context.report({
node,
message: 'The @{{decorator}} decorator requires an `async` property to be specified.',
data: {
decorator: decorator.callee.name,
},
});
} else if (!hasBooleanAsync) {
context.report({
node,
message:
'The @{{decorator}} decorator requires an `async` property to be specified as a boolean.',
data: {
decorator: decorator.callee.name,
},
});
}

if (!hasInverse) {
context.report({
node,
message:
'The @{{decorator}} decorator requires an `inverse` property to be specified.',
data: {
decorator: decorator.callee.name,
},
});
}
}
},
};
},
};
209 changes: 209 additions & 0 deletions tests/lib/rules/require-async-inverse-relationship.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
'use strict';

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const rule = require('../../../lib/rules/require-async-inverse-relationship');
const RuleTester = require('eslint').RuleTester;

const parserOptions = { ecmaVersion: 2022, sourceType: 'module' };

const ruleTester = new RuleTester({
parserOptions,
parser: require.resolve('@babel/eslint-parser'),
});

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

ruleTester.run('require-async-inverse-relationship', rule, {
valid: [
`import Model, { belongsTo } from '@ember-data/model';
export default class extends Model {
@belongsTo('post', { async: true, inverse: 'comments' }) post;
}`,
`import Model, { hasMany } from '@ember-data/model';
export default class extends Model {
@hasMany('comment', { async: true, inverse: 'post' }) comments;
}`,
`import Model, { belongsTo, hasMany } from '@ember-data/model';
export default class extends Model {
@belongsTo('post', { async: false, inverse: 'comments' }) post;
@hasMany('user', { async: true, inverse: null }) owner;
}`,
],

invalid: [
{
code: `import Model, { belongsTo } from '@ember-data/model';
export default class extends Model {
@belongsTo('post') post;
}`,
output: null,
errors: [
{
message: 'The @belongsTo decorator requires an `async` property to be specified.',
type: 'CallExpression',
},
{
message: 'The @belongsTo decorator requires an `inverse` property to be specified.',
type: 'CallExpression',
},
],
},
{
code: `import Model, { belongsTo } from '@ember-data/model';
export default class extends Model {
@belongsTo('post', {}) post;
}`,
output: null,
errors: [
{
message: 'The @belongsTo decorator requires an `async` property to be specified.',
type: 'CallExpression',
},
{
message: 'The @belongsTo decorator requires an `inverse` property to be specified.',
type: 'CallExpression',
},
],
},
{
code: `import Model, { belongsTo } from '@ember-data/model';
export default class extends Model {
@belongsTo('post', { async: 'comments'}) post;
}`,
output: null,
errors: [
{
message:
'The @belongsTo decorator requires an `async` property to be specified as a boolean.',
type: 'CallExpression',
},
{
message: 'The @belongsTo decorator requires an `inverse` property to be specified.',
type: 'CallExpression',
},
],
},
{
code: `import Model, { belongsTo } from '@ember-data/model';
export default class extends Model {
@belongsTo('post', { async: true }) post;
}`,
output: null,
errors: [
{
message: 'The @belongsTo decorator requires an `inverse` property to be specified.',
type: 'CallExpression',
},
],
},
{
code: `import Model, { belongsTo } from '@ember-data/model';
export default class extends Model {
@belongsTo('post', { inverse: 'comments' }) post;
}`,
output: null,
errors: [
{
message: 'The @belongsTo decorator requires an `async` property to be specified.',
type: 'CallExpression',
},
],
},
{
code: `import Model, { hasMany } from '@ember-data/model';
export default class extends Model {
@hasMany('comment') comments;
}`,
output: null,
errors: [
{
message: 'The @hasMany decorator requires an `async` property to be specified.',
type: 'CallExpression',
},
{
message: 'The @hasMany decorator requires an `inverse` property to be specified.',
type: 'CallExpression',
},
],
},
{
code: `import Model, { hasMany } from '@ember-data/model';
export default class extends Model {
@hasMany('comment', {}) comments;
}`,
output: null,
errors: [
{
message: 'The @hasMany decorator requires an `async` property to be specified.',
type: 'CallExpression',
},
{
message: 'The @hasMany decorator requires an `inverse` property to be specified.',
type: 'CallExpression',
},
],
},
{
code: `import Model, { hasMany } from '@ember-data/model';
export default class extends Model {
@hasMany('comment', { async: 'comments'}) comments;
}`,
output: null,
errors: [
{
message:
'The @hasMany decorator requires an `async` property to be specified as a boolean.',
type: 'CallExpression',
},
{
message: 'The @hasMany decorator requires an `inverse` property to be specified.',
type: 'CallExpression',
},
],
},
{
code: `import Model, { hasMany } from '@ember-data/model';
export default class extends Model {
@hasMany('comment', { async: true }) comments;
}`,
output: null,
errors: [
{
message: 'The @hasMany decorator requires an `inverse` property to be specified.',
type: 'CallExpression',
},
],
},
{
code: `import Model, { hasMany } from '@ember-data/model';
export default class extends Model {
@hasMany('comment', { inverse: 'post' }) comments;
}`,
output: null,
errors: [
{
message: 'The @hasMany decorator requires an `async` property to be specified.',
type: 'CallExpression',
},
],
},
],
});

0 comments on commit af62351

Please sign in to comment.