|
Last change
on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago |
|
Fix frontend appearance
|
-
Property mode
set to
100644
|
|
File size:
1.5 KB
|
| Line | |
|---|
| 1 | /**
|
|---|
| 2 | * @fileoverview A rule to disallow modifying variables that are declared using `const`
|
|---|
| 3 | * @author Toru Nagashima
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const astUtils = require("./utils/ast-utils");
|
|---|
| 9 |
|
|---|
| 10 | //------------------------------------------------------------------------------
|
|---|
| 11 | // Rule Definition
|
|---|
| 12 | //------------------------------------------------------------------------------
|
|---|
| 13 |
|
|---|
| 14 | /** @type {import('../shared/types').Rule} */
|
|---|
| 15 | module.exports = {
|
|---|
| 16 | meta: {
|
|---|
| 17 | type: "problem",
|
|---|
| 18 |
|
|---|
| 19 | docs: {
|
|---|
| 20 | description: "Disallow reassigning `const` variables",
|
|---|
| 21 | recommended: true,
|
|---|
| 22 | url: "https://eslint.org/docs/latest/rules/no-const-assign"
|
|---|
| 23 | },
|
|---|
| 24 |
|
|---|
| 25 | schema: [],
|
|---|
| 26 |
|
|---|
| 27 | messages: {
|
|---|
| 28 | const: "'{{name}}' is constant."
|
|---|
| 29 | }
|
|---|
| 30 | },
|
|---|
| 31 |
|
|---|
| 32 | create(context) {
|
|---|
| 33 |
|
|---|
| 34 | const sourceCode = context.sourceCode;
|
|---|
| 35 |
|
|---|
| 36 | /**
|
|---|
| 37 | * Finds and reports references that are non initializer and writable.
|
|---|
| 38 | * @param {Variable} variable A variable to check.
|
|---|
| 39 | * @returns {void}
|
|---|
| 40 | */
|
|---|
| 41 | function checkVariable(variable) {
|
|---|
| 42 | astUtils.getModifyingReferences(variable.references).forEach(reference => {
|
|---|
| 43 | context.report({ node: reference.identifier, messageId: "const", data: { name: reference.identifier.name } });
|
|---|
| 44 | });
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | return {
|
|---|
| 48 | VariableDeclaration(node) {
|
|---|
| 49 | if (node.kind === "const") {
|
|---|
| 50 | sourceCode.getDeclaredVariables(node).forEach(checkVariable);
|
|---|
| 51 | }
|
|---|
| 52 | }
|
|---|
| 53 | };
|
|---|
| 54 |
|
|---|
| 55 | }
|
|---|
| 56 | };
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.