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-undef.js 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * @fileoverview Rule to flag references to undeclared variables.
  3. * @author Mark Macdonald
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Helpers
  8. //------------------------------------------------------------------------------
  9. /**
  10. * Checks if the given node is the argument of a typeof operator.
  11. * @param {ASTNode} node The AST node being checked.
  12. * @returns {boolean} Whether or not the node is the argument of a typeof operator.
  13. */
  14. function hasTypeOfOperator(node) {
  15. const parent = node.parent;
  16. return parent.type === "UnaryExpression" && parent.operator === "typeof";
  17. }
  18. //------------------------------------------------------------------------------
  19. // Rule Definition
  20. //------------------------------------------------------------------------------
  21. module.exports = {
  22. meta: {
  23. type: "problem",
  24. docs: {
  25. description: "disallow the use of undeclared variables unless mentioned in `/*global */` comments",
  26. category: "Variables",
  27. recommended: true,
  28. url: "https://eslint.org/docs/rules/no-undef"
  29. },
  30. schema: [
  31. {
  32. type: "object",
  33. properties: {
  34. typeof: {
  35. type: "boolean"
  36. }
  37. },
  38. additionalProperties: false
  39. }
  40. ]
  41. },
  42. create(context) {
  43. const options = context.options[0];
  44. const considerTypeOf = options && options.typeof === true || false;
  45. return {
  46. "Program:exit"(/* node */) {
  47. const globalScope = context.getScope();
  48. globalScope.through.forEach(ref => {
  49. const identifier = ref.identifier;
  50. if (!considerTypeOf && hasTypeOfOperator(identifier)) {
  51. return;
  52. }
  53. context.report({
  54. node: identifier,
  55. message: "'{{name}}' is not defined.",
  56. data: identifier
  57. });
  58. });
  59. }
  60. };
  61. }
  62. };