| 1 | /**
|
|---|
| 2 | * @fileoverview Restrict usage of specified globals.
|
|---|
| 3 | * @author Benoît Zugmeyer
|
|---|
| 4 | */
|
|---|
| 5 | "use strict";
|
|---|
| 6 |
|
|---|
| 7 | //------------------------------------------------------------------------------
|
|---|
| 8 | // Rule Definition
|
|---|
| 9 | //------------------------------------------------------------------------------
|
|---|
| 10 |
|
|---|
| 11 | /** @type {import('../shared/types').Rule} */
|
|---|
| 12 | module.exports = {
|
|---|
| 13 | meta: {
|
|---|
| 14 | type: "suggestion",
|
|---|
| 15 |
|
|---|
| 16 | docs: {
|
|---|
| 17 | description: "Disallow specified global variables",
|
|---|
| 18 | recommended: false,
|
|---|
| 19 | url: "https://eslint.org/docs/latest/rules/no-restricted-globals"
|
|---|
| 20 | },
|
|---|
| 21 |
|
|---|
| 22 | schema: {
|
|---|
| 23 | type: "array",
|
|---|
| 24 | items: {
|
|---|
| 25 | oneOf: [
|
|---|
| 26 | {
|
|---|
| 27 | type: "string"
|
|---|
| 28 | },
|
|---|
| 29 | {
|
|---|
| 30 | type: "object",
|
|---|
| 31 | properties: {
|
|---|
| 32 | name: { type: "string" },
|
|---|
| 33 | message: { type: "string" }
|
|---|
| 34 | },
|
|---|
| 35 | required: ["name"],
|
|---|
| 36 | additionalProperties: false
|
|---|
| 37 | }
|
|---|
| 38 | ]
|
|---|
| 39 | },
|
|---|
| 40 | uniqueItems: true,
|
|---|
| 41 | minItems: 0
|
|---|
| 42 | },
|
|---|
| 43 |
|
|---|
| 44 | messages: {
|
|---|
| 45 | defaultMessage: "Unexpected use of '{{name}}'.",
|
|---|
| 46 | // eslint-disable-next-line eslint-plugin/report-message-format -- Custom message might not end in a period
|
|---|
| 47 | customMessage: "Unexpected use of '{{name}}'. {{customMessage}}"
|
|---|
| 48 | }
|
|---|
| 49 | },
|
|---|
| 50 |
|
|---|
| 51 | create(context) {
|
|---|
| 52 |
|
|---|
| 53 | const sourceCode = context.sourceCode;
|
|---|
| 54 |
|
|---|
| 55 | // If no globals are restricted, we don't need to do anything
|
|---|
| 56 | if (context.options.length === 0) {
|
|---|
| 57 | return {};
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | const restrictedGlobalMessages = context.options.reduce((memo, option) => {
|
|---|
| 61 | if (typeof option === "string") {
|
|---|
| 62 | memo[option] = null;
|
|---|
| 63 | } else {
|
|---|
| 64 | memo[option.name] = option.message;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | return memo;
|
|---|
| 68 | }, {});
|
|---|
| 69 |
|
|---|
| 70 | /**
|
|---|
| 71 | * Report a variable to be used as a restricted global.
|
|---|
| 72 | * @param {Reference} reference the variable reference
|
|---|
| 73 | * @returns {void}
|
|---|
| 74 | * @private
|
|---|
| 75 | */
|
|---|
| 76 | function reportReference(reference) {
|
|---|
| 77 | const name = reference.identifier.name,
|
|---|
| 78 | customMessage = restrictedGlobalMessages[name],
|
|---|
| 79 | messageId = customMessage
|
|---|
| 80 | ? "customMessage"
|
|---|
| 81 | : "defaultMessage";
|
|---|
| 82 |
|
|---|
| 83 | context.report({
|
|---|
| 84 | node: reference.identifier,
|
|---|
| 85 | messageId,
|
|---|
| 86 | data: {
|
|---|
| 87 | name,
|
|---|
| 88 | customMessage
|
|---|
| 89 | }
|
|---|
| 90 | });
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | /**
|
|---|
| 94 | * Check if the given name is a restricted global name.
|
|---|
| 95 | * @param {string} name name of a variable
|
|---|
| 96 | * @returns {boolean} whether the variable is a restricted global or not
|
|---|
| 97 | * @private
|
|---|
| 98 | */
|
|---|
| 99 | function isRestricted(name) {
|
|---|
| 100 | return Object.prototype.hasOwnProperty.call(restrictedGlobalMessages, name);
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | return {
|
|---|
| 104 | Program(node) {
|
|---|
| 105 | const scope = sourceCode.getScope(node);
|
|---|
| 106 |
|
|---|
| 107 | // Report variables declared elsewhere (ex: variables defined as "global" by eslint)
|
|---|
| 108 | scope.variables.forEach(variable => {
|
|---|
| 109 | if (!variable.defs.length && isRestricted(variable.name)) {
|
|---|
| 110 | variable.references.forEach(reportReference);
|
|---|
| 111 | }
|
|---|
| 112 | });
|
|---|
| 113 |
|
|---|
| 114 | // Report variables not declared at all
|
|---|
| 115 | scope.through.forEach(reference => {
|
|---|
| 116 | if (isRestricted(reference.identifier.name)) {
|
|---|
| 117 | reportReference(reference);
|
|---|
| 118 | }
|
|---|
| 119 | });
|
|---|
| 120 |
|
|---|
| 121 | }
|
|---|
| 122 | };
|
|---|
| 123 | }
|
|---|
| 124 | };
|
|---|