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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * @fileoverview Rule to flag references to the undefined variable.
  3. * @author Michael Ficarra
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. type: "suggestion",
  12. docs: {
  13. description: "disallow the use of `undefined` as an identifier",
  14. category: "Variables",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/no-undefined"
  17. },
  18. schema: []
  19. },
  20. create(context) {
  21. /**
  22. * Report an invalid "undefined" identifier node.
  23. * @param {ASTNode} node The node to report.
  24. * @returns {void}
  25. */
  26. function report(node) {
  27. context.report({
  28. node,
  29. message: "Unexpected use of undefined."
  30. });
  31. }
  32. /**
  33. * Checks the given scope for references to `undefined` and reports
  34. * all references found.
  35. * @param {eslint-scope.Scope} scope The scope to check.
  36. * @returns {void}
  37. */
  38. function checkScope(scope) {
  39. const undefinedVar = scope.set.get("undefined");
  40. if (!undefinedVar) {
  41. return;
  42. }
  43. const references = undefinedVar.references;
  44. const defs = undefinedVar.defs;
  45. // Report non-initializing references (those are covered in defs below)
  46. references
  47. .filter(ref => !ref.init)
  48. .forEach(ref => report(ref.identifier));
  49. defs.forEach(def => report(def.name));
  50. }
  51. return {
  52. "Program:exit"() {
  53. const globalScope = context.getScope();
  54. const stack = [globalScope];
  55. while (stack.length) {
  56. const scope = stack.pop();
  57. stack.push(...scope.childScopes);
  58. checkScope(scope);
  59. }
  60. }
  61. };
  62. }
  63. };