forked from ember-cli/eslint-plugin-ember
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathno-incorrect-computed-macros.js
More file actions
76 lines (69 loc) · 2.48 KB
/
no-incorrect-computed-macros.js
File metadata and controls
76 lines (69 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'use strict';
const { getImportIdentifier } = require('../utils/import');
const types = require('../utils/types');
const ERROR_MESSAGE_AND_OR = 'Computed property macro should be used with 2+ arguments';
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'disallow incorrect usage of computed property macros',
category: 'Computed Properties',
recommended: true,
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/no-incorrect-computed-macros.md',
},
fixable: 'code',
schema: [],
},
ERROR_MESSAGE_AND_OR,
create(context) {
let importNameAnd = undefined;
let importNameOr = undefined;
let importNameReadOnly = undefined;
return {
ImportDeclaration(node) {
if (node.source.value === '@ember/object/computed') {
// Gather the identifiers that these macros are imported under.
importNameAnd =
importNameAnd || getImportIdentifier(node, '@ember/object/computed', 'and');
importNameOr = importNameOr || getImportIdentifier(node, '@ember/object/computed', 'or');
importNameReadOnly =
importNameReadOnly || getImportIdentifier(node, '@ember/object/computed', 'readOnly');
}
},
CallExpression(node) {
if (
types.isIdentifier(node.callee) &&
[importNameAnd, importNameOr].includes(node.callee.name)
) {
if (
node.arguments.length === 1 &&
types.isStringLiteral(node.arguments[0]) &&
!node.arguments[0].value.includes('{')
) {
context.report({
node: node.callee,
message: ERROR_MESSAGE_AND_OR,
fix(fixer) {
if (importNameReadOnly) {
return fixer.replaceText(node.callee, importNameReadOnly);
} else {
const sourceCode = context.sourceCode ?? context.getSourceCode();
return [
fixer.insertTextBefore(
sourceCode.ast,
"import { readOnly } from '@ember/object/computed';\n"
),
fixer.replaceText(node.callee, 'readOnly'),
];
}
},
});
} else if (node.arguments.length === 0) {
context.report({ node, message: ERROR_MESSAGE_AND_OR });
}
}
},
};
},
};