Skip to content

Commit 2d6eb93

Browse files
committed
fix: check service import in order-in-models rule
1 parent e674272 commit 2d6eb93

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

lib/rules/order-in-models.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const ember = require('../utils/ember');
44
const propOrder = require('../utils/property-order');
5+
const { getImportIdentifier } = require('../utils/import');
56

67
const reportUnorderedProperties = propOrder.reportUnorderedProperties;
78
const addBackwardsPosition = propOrder.addBackwardsPosition;
@@ -52,13 +53,32 @@ module.exports = {
5253
: ORDER;
5354
const filePath = context.getFilename();
5455

56+
let importedInjectName;
57+
let importedEmberName;
58+
5559
return {
60+
ImportDeclaration(node) {
61+
if (node.source.value === 'ember') {
62+
importedEmberName = importedEmberName || getImportIdentifier(node, 'ember');
63+
}
64+
if (node.source.value === '@ember/service') {
65+
importedInjectName =
66+
importedInjectName || getImportIdentifier(node, '@ember/service', 'inject');
67+
}
68+
},
5669
CallExpression(node) {
5770
if (!ember.isDSModel(node, filePath)) {
5871
return;
5972
}
6073

61-
reportUnorderedProperties(node, context, 'model', order);
74+
reportUnorderedProperties(
75+
node,
76+
context,
77+
'model',
78+
order,
79+
importedEmberName,
80+
importedInjectName
81+
);
6282
},
6383
};
6484
},

tests/lib/rules/order-in-models.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,18 @@ eslintTester.run('order-in-models', rule, {
9090
},
9191
],
9292
},
93+
{
94+
code: `import {inject as service} from '@ember/service';
95+
export default DS.Model.extend({
96+
foo: service(),
97+
a: attr('string'),
98+
convertA(paramA) {
99+
},
100+
customProp: { a: 1 }
101+
});`,
102+
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
103+
options: [{ order: ['service', 'attribute', 'method'] }],
104+
},
93105
],
94106
invalid: [
95107
{

0 commit comments

Comments
 (0)