项目原始demo,不改动
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

преди 4 години
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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("../ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Rule Definition
  12. //------------------------------------------------------------------------------
  13. module.exports = {
  14. meta: {
  15. docs: {
  16. description: "disallow labels that share a name with a variable",
  17. category: "Variables",
  18. recommended: false,
  19. url: "https://eslint.org/docs/rules/no-label-var"
  20. },
  21. schema: []
  22. },
  23. create(context) {
  24. //--------------------------------------------------------------------------
  25. // Helpers
  26. //--------------------------------------------------------------------------
  27. /**
  28. * Check if the identifier is present inside current scope
  29. * @param {Object} scope current scope
  30. * @param {string} name To evaluate
  31. * @returns {boolean} True if its present
  32. * @private
  33. */
  34. function findIdentifier(scope, name) {
  35. return astUtils.getVariableByName(scope, name) !== null;
  36. }
  37. //--------------------------------------------------------------------------
  38. // Public API
  39. //--------------------------------------------------------------------------
  40. return {
  41. LabeledStatement(node) {
  42. // Fetch the innermost scope.
  43. const scope = context.getScope();
  44. /*
  45. * Recursively find the identifier walking up the scope, starting
  46. * with the innermost scope.
  47. */
  48. if (findIdentifier(scope, node.label.name)) {
  49. context.report({ node, message: "Found identifier with same name as label." });
  50. }
  51. }
  52. };
  53. }
  54. };