Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

no-implicit-globals.js 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. * @fileoverview Rule to check for implicit global variables and functions.
  3. * @author Joshua Peek
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. type: "suggestion",
  12. docs: {
  13. description: "disallow variable and `function` declarations in the global scope",
  14. category: "Best Practices",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/no-implicit-globals"
  17. },
  18. schema: []
  19. },
  20. create(context) {
  21. return {
  22. Program() {
  23. const scope = context.getScope();
  24. scope.variables.forEach(variable => {
  25. if (variable.writeable) {
  26. return;
  27. }
  28. variable.defs.forEach(def => {
  29. if (def.type === "FunctionName" || (def.type === "Variable" && def.parent.kind === "var")) {
  30. context.report({ node: def.node, message: "Implicit global variable, assign as global property instead." });
  31. }
  32. });
  33. });
  34. scope.implicit.variables.forEach(variable => {
  35. const scopeVariable = scope.set.get(variable.name);
  36. if (scopeVariable && scopeVariable.writeable) {
  37. return;
  38. }
  39. variable.defs.forEach(def => {
  40. context.report({ node: def.node, message: "Implicit global variable, assign as global property instead." });
  41. });
  42. });
  43. }
  44. };
  45. }
  46. };