UNPKG

1.55 kBJavaScriptView Raw
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("../ast-utils");
9
10//------------------------------------------------------------------------------
11// Rule Definition
12//------------------------------------------------------------------------------
13
14module.exports = {
15 meta: {
16 docs: {
17 description: "disallow reassigning class members",
18 category: "ECMAScript 6",
19 recommended: true
20 },
21
22 schema: []
23 },
24
25 create(context) {
26
27 /**
28 * Finds and reports references that are non initializer and writable.
29 * @param {Variable} variable - A variable to check.
30 * @returns {void}
31 */
32 function checkVariable(variable) {
33 astUtils.getModifyingReferences(variable.references).forEach(reference => {
34 context.report({ node: reference.identifier, message: "'{{name}}' is a class.", data: { name: reference.identifier.name } });
35
36 });
37 }
38
39 /**
40 * Finds and reports references that are non initializer and writable.
41 * @param {ASTNode} node - A ClassDeclaration/ClassExpression node to check.
42 * @returns {void}
43 */
44 function checkForClass(node) {
45 context.getDeclaredVariables(node).forEach(checkVariable);
46 }
47
48 return {
49 ClassDeclaration: checkForClass,
50 ClassExpression: checkForClass
51 };
52
53 }
54};