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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * @fileoverview Rule to flag labels that are the same as an identifier
  3. * @author Ian Christian Myers
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("../util/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Rule Definition
  12. //------------------------------------------------------------------------------
  13. module.exports = {
  14. meta: {
  15. type: "suggestion",
  16. docs: {
  17. description: "disallow labels that share a name with a variable",
  18. category: "Variables",
  19. recommended: false,
  20. url: "https://eslint.org/docs/rules/no-label-var"
  21. },
  22. schema: []
  23. },
  24. create(context) {
  25. //--------------------------------------------------------------------------
  26. // Helpers
  27. //--------------------------------------------------------------------------
  28. /**
  29. * Check if the identifier is present inside current scope
  30. * @param {Object} scope current scope
  31. * @param {string} name To evaluate
  32. * @returns {boolean} True if its present
  33. * @private
  34. */
  35. function findIdentifier(scope, name) {
  36. return astUtils.getVariableByName(scope, name) !== null;
  37. }
  38. //--------------------------------------------------------------------------
  39. // Public API
  40. //--------------------------------------------------------------------------
  41. return {
  42. LabeledStatement(node) {
  43. // Fetch the innermost scope.
  44. const scope = context.getScope();
  45. /*
  46. * Recursively find the identifier walking up the scope, starting
  47. * with the innermost scope.
  48. */
  49. if (findIdentifier(scope, node.label.name)) {
  50. context.report({ node, message: "Found identifier with same name as label." });
  51. }
  52. }
  53. };
  54. }
  55. };