forked from ember-cli/eslint-plugin-ember
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate-no-array-prototype-extensions.js
More file actions
112 lines (101 loc) · 3.24 KB
/
template-no-array-prototype-extensions.js
File metadata and controls
112 lines (101 loc) · 3.24 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
111
112
const FIRST_OBJECT_PROP_NAME = 'firstObject';
const LAST_OBJECT_PROP_NAME = 'lastObject';
const ERROR_MESSAGES = {
LAST_OBJECT: 'Array prototype extension property lastObject usage is disallowed.',
FIRST_OBJECT:
"Array prototype extension property firstObject usage is disallowed. Please use Ember's get helper instead, e.g. `(get @list '0')`.",
};
/**
* Check if the path should be allowed. `@firstObject.test`, `@lastObject`, and
* `this.firstObject` are allowed (they are property names, not extensions).
*/
function isAllowed(originalStr, matchedStr) {
// allow `@firstObject.test`, `@lastObject`
if (originalStr.startsWith(`@${matchedStr}`)) {
return true;
}
const originalParts = originalStr.split('.');
const matchStrIndex = originalParts.indexOf(matchedStr);
// if not found
if (matchStrIndex === -1) {
return true;
}
// allow this.firstObject (direct property, not extension)
return !matchStrIndex || originalParts[matchStrIndex - 1] === 'this';
}
/**
* Check if current node is a `get` helper and its string literal contains matchedStr.
* For example `{{get this 'list.firstObject'}}` returns true,
* but `{{get this 'firstObject'}}` returns false (that's a direct property).
*/
function isGetHelperWithMatchedLiteral(node, matchedStr) {
if (node.original !== 'get') {
return false;
}
const parent = node.parent;
if (
parent &&
(parent.type === 'GlimmerMustacheStatement' || parent.type === 'GlimmerSubExpression') &&
parent.params &&
parent.params[1] &&
parent.params[1].type === 'GlimmerStringLiteral'
) {
const literal = parent.params[1].value || parent.params[1].original;
const parts = literal.split('.');
const matchStrIndex = parts.indexOf(matchedStr);
// matchedStr is found and not the `{{get this 'firstObject'}}` case
return (
matchStrIndex !== -1 &&
!(matchStrIndex === 0 && parent.params[0] && parent.params[0].original === 'this')
);
}
return false;
}
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'disallow usage of Ember Array prototype extensions',
category: 'Best Practices',
strictGjs: true,
strictGts: true,
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-array-prototype-extensions.md',
},
fixable: null,
schema: [],
messages: {
lastObject: ERROR_MESSAGES.LAST_OBJECT,
firstObject: ERROR_MESSAGES.FIRST_OBJECT,
},
},
create(context) {
return {
GlimmerPathExpression(node) {
if (!node.original) {
return;
}
// Handle lastObject — no fixer available
if (
!isAllowed(node.original, LAST_OBJECT_PROP_NAME) ||
isGetHelperWithMatchedLiteral(node, LAST_OBJECT_PROP_NAME)
) {
context.report({
node,
messageId: 'lastObject',
});
}
// Handle firstObject
if (
!isAllowed(node.original, FIRST_OBJECT_PROP_NAME) ||
isGetHelperWithMatchedLiteral(node, FIRST_OBJECT_PROP_NAME)
) {
context.report({
node,
messageId: 'firstObject',
});
}
},
};
},
};