forked from sindresorhus/eslint-plugin-unicorn
/
no-this-assignment.js
46 lines (40 loc) · 964 Bytes
/
no-this-assignment.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
'use strict';
const {matches} = require('./selectors/index.js');
const MESSAGE_ID = 'no-this-assignment';
const messages = {
[MESSAGE_ID]: 'Do not assign `this` to `{{name}}`.',
};
const variableDeclaratorSelector = [
'VariableDeclarator',
'[init.type="ThisExpression"]',
'[id.type="Identifier"]',
].join('');
const assignmentExpressionSelector = [
'AssignmentExpression',
'[right.type="ThisExpression"]',
'[left.type="Identifier"]',
].join('');
const selector = matches([variableDeclaratorSelector, assignmentExpressionSelector]);
const create = () => ({
[selector](node) {
const variable = node.type === 'AssignmentExpression' ? node.left : node.id;
return {
node,
data: {name: variable.name},
messageId: MESSAGE_ID,
};
},
});
/**
* @type {import('eslint').Rule.RuleModule}
*/
module.exports = {
create,
meta: {
type: 'suggestion',
docs: {
description: 'Disallow assigning `this` to a variable.',
},
messages,
},
};