1 | /**
|
2 | * @fileoverview A rule to disallow modifying variables that are declared using `const`
|
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 `const` variables",
|
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(function(reference) {
|
34 | context.report(
|
35 | reference.identifier,
|
36 | "'{{name}}' is constant.",
|
37 | {name: reference.identifier.name});
|
38 | });
|
39 | }
|
40 |
|
41 | return {
|
42 | VariableDeclaration(node) {
|
43 | if (node.kind === "const") {
|
44 | context.getDeclaredVariables(node).forEach(checkVariable);
|
45 | }
|
46 | }
|
47 | };
|
48 |
|
49 | }
|
50 | };
|