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.

semi-style.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * @fileoverview Rule to enforce location of semicolons.
  3. * @author Toru Nagashima
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("./utils/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Rule Definition
  12. //------------------------------------------------------------------------------
  13. const SELECTOR = `:matches(${
  14. [
  15. "BreakStatement", "ContinueStatement", "DebuggerStatement",
  16. "DoWhileStatement", "ExportAllDeclaration",
  17. "ExportDefaultDeclaration", "ExportNamedDeclaration",
  18. "ExpressionStatement", "ImportDeclaration", "ReturnStatement",
  19. "ThrowStatement", "VariableDeclaration"
  20. ].join(",")
  21. })`;
  22. /**
  23. * Get the child node list of a given node.
  24. * This returns `Program#body`, `BlockStatement#body`, or `SwitchCase#consequent`.
  25. * This is used to check whether a node is the first/last child.
  26. * @param {Node} node A node to get child node list.
  27. * @returns {Node[]|null} The child node list.
  28. */
  29. function getChildren(node) {
  30. const t = node.type;
  31. if (t === "BlockStatement" || t === "Program") {
  32. return node.body;
  33. }
  34. if (t === "SwitchCase") {
  35. return node.consequent;
  36. }
  37. return null;
  38. }
  39. /**
  40. * Check whether a given node is the last statement in the parent block.
  41. * @param {Node} node A node to check.
  42. * @returns {boolean} `true` if the node is the last statement in the parent block.
  43. */
  44. function isLastChild(node) {
  45. const t = node.parent.type;
  46. if (t === "IfStatement" && node.parent.consequent === node && node.parent.alternate) { // before `else` keyword.
  47. return true;
  48. }
  49. if (t === "DoWhileStatement") { // before `while` keyword.
  50. return true;
  51. }
  52. const nodeList = getChildren(node.parent);
  53. return nodeList !== null && nodeList[nodeList.length - 1] === node; // before `}` or etc.
  54. }
  55. module.exports = {
  56. meta: {
  57. type: "layout",
  58. docs: {
  59. description: "enforce location of semicolons",
  60. category: "Stylistic Issues",
  61. recommended: false,
  62. url: "https://eslint.org/docs/rules/semi-style"
  63. },
  64. schema: [{ enum: ["last", "first"] }],
  65. fixable: "whitespace",
  66. messages: {
  67. expectedSemiColon: "Expected this semicolon to be at {{pos}}."
  68. }
  69. },
  70. create(context) {
  71. const sourceCode = context.getSourceCode();
  72. const option = context.options[0] || "last";
  73. /**
  74. * Check the given semicolon token.
  75. * @param {Token} semiToken The semicolon token to check.
  76. * @param {"first"|"last"} expected The expected location to check.
  77. * @returns {void}
  78. */
  79. function check(semiToken, expected) {
  80. const prevToken = sourceCode.getTokenBefore(semiToken);
  81. const nextToken = sourceCode.getTokenAfter(semiToken);
  82. const prevIsSameLine = !prevToken || astUtils.isTokenOnSameLine(prevToken, semiToken);
  83. const nextIsSameLine = !nextToken || astUtils.isTokenOnSameLine(semiToken, nextToken);
  84. if ((expected === "last" && !prevIsSameLine) || (expected === "first" && !nextIsSameLine)) {
  85. context.report({
  86. loc: semiToken.loc,
  87. messageId: "expectedSemiColon",
  88. data: {
  89. pos: (expected === "last")
  90. ? "the end of the previous line"
  91. : "the beginning of the next line"
  92. },
  93. fix(fixer) {
  94. if (prevToken && nextToken && sourceCode.commentsExistBetween(prevToken, nextToken)) {
  95. return null;
  96. }
  97. const start = prevToken ? prevToken.range[1] : semiToken.range[0];
  98. const end = nextToken ? nextToken.range[0] : semiToken.range[1];
  99. const text = (expected === "last") ? ";\n" : "\n;";
  100. return fixer.replaceTextRange([start, end], text);
  101. }
  102. });
  103. }
  104. }
  105. return {
  106. [SELECTOR](node) {
  107. if (option === "first" && isLastChild(node)) {
  108. return;
  109. }
  110. const lastToken = sourceCode.getLastToken(node);
  111. if (astUtils.isSemicolonToken(lastToken)) {
  112. check(lastToken, option);
  113. }
  114. },
  115. ForStatement(node) {
  116. const firstSemi = node.init && sourceCode.getTokenAfter(node.init, astUtils.isSemicolonToken);
  117. const secondSemi = node.test && sourceCode.getTokenAfter(node.test, astUtils.isSemicolonToken);
  118. if (firstSemi) {
  119. check(firstSemi, "last");
  120. }
  121. if (secondSemi) {
  122. check(secondSemi, "last");
  123. }
  124. }
  125. };
  126. }
  127. };