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.

52 lines
1.4 KiB

4 years ago
  1. /**
  2. * @fileoverview Rule to disallow a duplicate case label.
  3. * @author Dieter Oberkofler
  4. * @author Burak Yigit Kaya
  5. */
  6. "use strict";
  7. //------------------------------------------------------------------------------
  8. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. module.exports = {
  11. meta: {
  12. type: "problem",
  13. docs: {
  14. description: "disallow duplicate case labels",
  15. category: "Possible Errors",
  16. recommended: true,
  17. url: "https://eslint.org/docs/rules/no-duplicate-case"
  18. },
  19. schema: [],
  20. messages: {
  21. unexpected: "Duplicate case label."
  22. }
  23. },
  24. create(context) {
  25. const sourceCode = context.getSourceCode();
  26. return {
  27. SwitchStatement(node) {
  28. const previousKeys = new Set();
  29. for (const switchCase of node.cases) {
  30. if (switchCase.test) {
  31. const key = sourceCode.getText(switchCase.test);
  32. if (previousKeys.has(key)) {
  33. context.report({ node: switchCase, messageId: "unexpected" });
  34. } else {
  35. previousKeys.add(key);
  36. }
  37. }
  38. }
  39. }
  40. };
  41. }
  42. };