blob: 839ad03e292d1be273a0fd06ca52ff2cfd023c25 [file] [log] [blame]
Yang Guo4fd355c2019-09-19 10:59:03 +02001/**
2 * @fileoverview A rule to disallow modifying variables of class declarations
3 * @author Toru Nagashima
4 */
5
6"use strict";
7
8const astUtils = require("./utils/ast-utils");
9
10//------------------------------------------------------------------------------
11// Rule Definition
12//------------------------------------------------------------------------------
13
14module.exports = {
15 meta: {
16 type: "problem",
17
18 docs: {
19 description: "disallow reassigning class members",
Yang Guo4fd355c2019-09-19 10:59:03 +020020 recommended: true,
21 url: "https://eslint.org/docs/rules/no-class-assign"
22 },
23
24 schema: [],
25
26 messages: {
27 class: "'{{name}}' is a class."
28 }
29 },
30
31 create(context) {
32
33 /**
34 * Finds and reports references that are non initializer and writable.
Tim van der Lippec8f6ffd2020-04-06 13:42:00 +010035 * @param {Variable} variable A variable to check.
Yang Guo4fd355c2019-09-19 10:59:03 +020036 * @returns {void}
37 */
38 function checkVariable(variable) {
39 astUtils.getModifyingReferences(variable.references).forEach(reference => {
40 context.report({ node: reference.identifier, messageId: "class", data: { name: reference.identifier.name } });
41
42 });
43 }
44
45 /**
46 * Finds and reports references that are non initializer and writable.
Tim van der Lippec8f6ffd2020-04-06 13:42:00 +010047 * @param {ASTNode} node A ClassDeclaration/ClassExpression node to check.
Yang Guo4fd355c2019-09-19 10:59:03 +020048 * @returns {void}
49 */
50 function checkForClass(node) {
51 context.getDeclaredVariables(node).forEach(checkVariable);
52 }
53
54 return {
55 ClassDeclaration: checkForClass,
56 ClassExpression: checkForClass
57 };
58
59 }
60};