forked from ember-cli/eslint-plugin-ember
-
Notifications
You must be signed in to change notification settings - Fork 0
/
no-array-prototype-extensions.js
150 lines (136 loc) · 4.27 KB
/
no-array-prototype-extensions.js
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
'use strict';
const ERROR_MESSAGE = "Don't use Ember's array prototype extensions";
const EXTENSION_METHODS = new Set([
/**
* https://api.emberjs.com/ember/release/classes/EmberArray
* EmberArray methods excluding native functions like reduce, filter etc.
* */
'any',
'compact',
'filterBy',
'findBy',
'getEach',
'invoke',
'isAny',
'isEvery',
'mapBy',
'objectAt',
'objectsAt',
'reject',
'rejectBy',
'setEach',
'sortBy',
'toArray',
'uniq',
'uniqBy',
'without',
/**
* https://api.emberjs.com/ember/release/classes/MutableArray
* MutableArray methods excluding `replace`. `replace` is handled differently as it's also part of String.prototype.
* */
'addObject',
'addObjects',
'clear',
'insertAt',
'popObject',
'pushObject',
'pushObjects',
'removeAt',
'removeObject',
'removeObjects',
'reverseObjects',
'setObject',
'shiftObject',
'unshiftObject',
'unshiftObjects',
]);
const REPLACE_METHOD = 'replace';
/**
* https://api.emberjs.com/ember/release/classes/EmberArray
* EmberArray properties excluding native props: [], length.
* */
const EXTENSION_PROPERTIES = new Set(['lastObject', 'firstObject']);
//----------------------------------------------------------------------------------------------
// General rule - Don't use Ember's array prototype extensions like .any(), .pushObject() or .firstObject
//----------------------------------------------------------------------------------------------
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: "disallow usage of Ember's `Array` prototype extensions",
category: 'Deprecations',
recommended: false,
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/no-array-prototype-extensions.md',
},
fixable: null,
schema: [],
messages: {
main: ERROR_MESSAGE,
},
},
create(context) {
return {
/**
* Cover cases when `EXTENSION_METHODS` is getting called.
* Example: something.filterBy();
* @param {Object} node
*/
CallExpression(node) {
// Skip case: filterBy();
if (node.callee.type !== 'MemberExpression') {
return;
}
// Skip case: this.filterBy();
if (node.callee.object.type === 'ThisExpression') {
return;
}
if (node.callee.property.type !== 'Identifier') {
return;
}
if (EXTENSION_METHODS.has(node.callee.property.name)) {
context.report({ node, messageId: 'main' });
}
// Example: someArray.replace(1, 2, [1, 2, 3]);
// We can differentiate String.prototype.replace and Array.prototype.replace by arguments length
// String.prototype.replace can only have 2 arguments, Array.prototype.replace needs to have exact 3 arguments
if (node.callee.property.name === REPLACE_METHOD && node.arguments.length === 3) {
context.report({ node, messageId: 'main' });
}
},
/**
* Cover cases when `EXTENSION_PROPERTIES` is accessed like:
* foo.firstObject;
* bar.lastObject.bar;
* @param {Object} node
*/
MemberExpression(node) {
// Skip case when EXTENSION_PROPERTIES is accessed through callee.
// Example: something.firstObject()
if (node.parent.type === 'CallExpression') {
return;
}
if (node.property.type !== 'Identifier') {
return;
}
if (EXTENSION_PROPERTIES.has(node.property.name)) {
context.report({ node, messageId: 'main' });
}
},
/**
* Cover cases when `EXTENSION_PROPERTIES` is accessed through literals like:
* get(something, 'foo.firstObject');
* set(something, 'lastObject.bar', 'something');
* @param {Object} node
*/
Literal(node) {
// Generate regexp for extension properties.
// new RegExp(`${[...EXTENSION_PROPERTIES].map(prop => `(\.|^)${prop}(\.|$)`).join('|')}`) won't generate \. correctly
const regexp = /(\.|^)firstObject(\.|$)|(\.|^)lastObject(\.|$)/;
if (typeof node.value === 'string' && regexp.test(node.value)) {
context.report({ node, messageId: 'main' });
}
},
};
},
};