1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const astUtils = require("../ast-utils");
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | module.exports = {
|
15 | meta: {
|
16 | docs: {
|
17 | description: "disallow reassigning `const` variables",
|
18 | category: "ECMAScript 6",
|
19 | recommended: true,
|
20 | url: "https://eslint.org/docs/rules/no-const-assign"
|
21 | },
|
22 |
|
23 | schema: [],
|
24 |
|
25 | messages: {
|
26 | const: "'{{name}}' is constant."
|
27 | }
|
28 | },
|
29 |
|
30 | create(context) {
|
31 |
|
32 | |
33 |
|
34 |
|
35 |
|
36 |
|
37 | function checkVariable(variable) {
|
38 | astUtils.getModifyingReferences(variable.references).forEach(reference => {
|
39 | context.report({ node: reference.identifier, messageId: "const", data: { name: reference.identifier.name } });
|
40 | });
|
41 | }
|
42 |
|
43 | return {
|
44 | VariableDeclaration(node) {
|
45 | if (node.kind === "const") {
|
46 | context.getDeclaredVariables(node).forEach(checkVariable);
|
47 | }
|
48 | }
|
49 | };
|
50 |
|
51 | }
|
52 | };
|