1/** 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", 20 category: "ECMAScript 6", 21 recommended: true, 22 url: "https://eslint.org/docs/rules/no-class-assign" 23 }, 24 25 schema: [], 26 27 messages: { 28 class: "'{{name}}' is a class." 29 } 30 }, 31 32 create(context) { 33 34 /** 35 * Finds and reports references that are non initializer and writable. 36 * @param {Variable} variable A variable to check. 37 * @returns {void} 38 */ 39 function checkVariable(variable) { 40 astUtils.getModifyingReferences(variable.references).forEach(reference => { 41 context.report({ node: reference.identifier, messageId: "class", data: { name: reference.identifier.name } }); 42 43 }); 44 } 45 46 /** 47 * Finds and reports references that are non initializer and writable. 48 * @param {ASTNode} node A ClassDeclaration/ClassExpression node to check. 49 * @returns {void} 50 */ 51 function checkForClass(node) { 52 context.getDeclaredVariables(node).forEach(checkVariable); 53 } 54 55 return { 56 ClassDeclaration: checkForClass, 57 ClassExpression: checkForClass 58 }; 59 60 } 61}; 62