forked from ember-cli/eslint-plugin-ember
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathavoid-leaking-state-in-ember-objects.js
More file actions
110 lines (97 loc) · 2.88 KB
/
avoid-leaking-state-in-ember-objects.js
File metadata and controls
110 lines (97 loc) · 2.88 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
'use strict';
const ember = require('../utils/ember');
const types = require('../utils/types');
const DEFAULT_IGNORED_PROPERTIES = [
'classNames',
'classNameBindings',
'actions',
'concatenatedProperties',
'mergedProperties',
'positionalParams',
'attributeBindings',
'queryParams',
'attrs',
];
function isAllowedTernary(value) {
return (
types.isConditionalExpression(value) &&
isAllowed(value.consequent) &&
isAllowed(value.alternate)
);
}
function isAllowedLogicalExpression(value) {
return types.isLogicalExpression(value) && isAllowed(value.left) && isAllowed(value.right);
}
function isAllowed(value) {
return (
ember.isFunctionExpression(value) ||
types.isLiteral(value) ||
types.isIdentifier(value) ||
types.isCallExpression(value) ||
types.isBinaryExpression(value) ||
types.isTemplateLiteral(value) ||
types.isTaggedTemplateExpression(value) ||
types.isMemberExpression(value) ||
types.isUnaryExpression(value) ||
isAllowedTernary(value) ||
isAllowedLogicalExpression(value)
);
}
//------------------------------------------------------------------------------
// Ember object rule - Avoid leaking state
// (Don't use arrays or objects as default props)
//------------------------------------------------------------------------------
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'disallow state leakage',
category: 'Ember Object',
recommended: true,
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/avoid-leaking-state-in-ember-objects.md',
},
fixable: null,
schema: [
{
type: 'array',
uniqueItems: true,
minItems: 1,
items: { type: 'string' },
},
],
},
create(context) {
const ignoredProperties = context.options[0]
? [...DEFAULT_IGNORED_PROPERTIES, ...context.options[0]]
: DEFAULT_IGNORED_PROPERTIES;
const report = function (node) {
const message =
'Only string, number, symbol, boolean, null, undefined, and function are allowed as default properties';
context.report({ node, message });
};
const sourceCode = context.sourceCode ?? context.getSourceCode();
const { scopeManager } = sourceCode;
return {
CallExpression(node) {
if (
!(
ember.isExtendObject(node) ||
ember.isReopenObject(node) ||
ember.isEmberMixin(context, node)
)
) {
return;
}
const properties = ember.getModuleProperties(node, scopeManager);
for (const property of properties.filter(
(property) => property.key && !ignoredProperties.includes(property.key.name)
)) {
if (!isAllowed(property.value)) {
report(property);
}
}
},
};
},
};