Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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.

default-case.js 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * @fileoverview require default case in switch statements
  3. * @author Aliaksei Shytkin
  4. */
  5. "use strict";
  6. const DEFAULT_COMMENT_PATTERN = /^no default$/iu;
  7. //------------------------------------------------------------------------------
  8. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. module.exports = {
  11. meta: {
  12. type: "suggestion",
  13. docs: {
  14. description: "require `default` cases in `switch` statements",
  15. category: "Best Practices",
  16. recommended: false,
  17. url: "https://eslint.org/docs/rules/default-case"
  18. },
  19. schema: [{
  20. type: "object",
  21. properties: {
  22. commentPattern: {
  23. type: "string"
  24. }
  25. },
  26. additionalProperties: false
  27. }],
  28. messages: {
  29. missingDefaultCase: "Expected a default case."
  30. }
  31. },
  32. create(context) {
  33. const options = context.options[0] || {};
  34. const commentPattern = options.commentPattern
  35. ? new RegExp(options.commentPattern, "u")
  36. : DEFAULT_COMMENT_PATTERN;
  37. const sourceCode = context.getSourceCode();
  38. //--------------------------------------------------------------------------
  39. // Helpers
  40. //--------------------------------------------------------------------------
  41. /**
  42. * Shortcut to get last element of array
  43. * @param {*[]} collection Array
  44. * @returns {*} Last element
  45. */
  46. function last(collection) {
  47. return collection[collection.length - 1];
  48. }
  49. //--------------------------------------------------------------------------
  50. // Public
  51. //--------------------------------------------------------------------------
  52. return {
  53. SwitchStatement(node) {
  54. if (!node.cases.length) {
  55. /*
  56. * skip check of empty switch because there is no easy way
  57. * to extract comments inside it now
  58. */
  59. return;
  60. }
  61. const hasDefault = node.cases.some(v => v.test === null);
  62. if (!hasDefault) {
  63. let comment;
  64. const lastCase = last(node.cases);
  65. const comments = sourceCode.getCommentsAfter(lastCase);
  66. if (comments.length) {
  67. comment = last(comments);
  68. }
  69. if (!comment || !commentPattern.test(comment.value.trim())) {
  70. context.report({ node, messageId: "missingDefaultCase" });
  71. }
  72. }
  73. }
  74. };
  75. }
  76. };