1 | /**
|
2 | * @fileoverview A rule to disallow modifying variables of class declarations
|
3 | * @author Toru Nagashima
|
4 | */
|
5 |
|
6 | ;
|
7 |
|
8 | const astUtils = require("../ast-utils");
|
9 |
|
10 | //------------------------------------------------------------------------------
|
11 | // Rule Definition
|
12 | //------------------------------------------------------------------------------
|
13 |
|
14 | module.exports = {
|
15 | meta: {
|
16 | docs: {
|
17 | description: "disallow reassigning class members",
|
18 | category: "ECMAScript 6",
|
19 | recommended: true,
|
20 | url: "https://eslint.org/docs/rules/no-class-assign"
|
21 | },
|
22 |
|
23 | schema: [],
|
24 |
|
25 | messages: {
|
26 | class: "'{{name}}' is a class."
|
27 | }
|
28 | },
|
29 |
|
30 | create(context) {
|
31 |
|
32 | /**
|
33 | * Finds and reports references that are non initializer and writable.
|
34 | * @param {Variable} variable - A variable to check.
|
35 | * @returns {void}
|
36 | */
|
37 | function checkVariable(variable) {
|
38 | astUtils.getModifyingReferences(variable.references).forEach(reference => {
|
39 | context.report({ node: reference.identifier, messageId: "class", data: { name: reference.identifier.name } });
|
40 |
|
41 | });
|
42 | }
|
43 |
|
44 | /**
|
45 | * Finds and reports references that are non initializer and writable.
|
46 | * @param {ASTNode} node - A ClassDeclaration/ClassExpression node to check.
|
47 | * @returns {void}
|
48 | */
|
49 | function checkForClass(node) {
|
50 | context.getDeclaredVariables(node).forEach(checkVariable);
|
51 | }
|
52 |
|
53 | return {
|
54 | ClassDeclaration: checkForClass,
|
55 | ClassExpression: checkForClass
|
56 | };
|
57 |
|
58 | }
|
59 | };
|