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.

semi-style.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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("../util/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. },
  67. create(context) {
  68. const sourceCode = context.getSourceCode();
  69. const option = context.options[0] || "last";
  70. /**
  71. * Check the given semicolon token.
  72. * @param {Token} semiToken The semicolon token to check.
  73. * @param {"first"|"last"} expected The expected location to check.
  74. * @returns {void}
  75. */
  76. function check(semiToken, expected) {
  77. const prevToken = sourceCode.getTokenBefore(semiToken);
  78. const nextToken = sourceCode.getTokenAfter(semiToken);
  79. const prevIsSameLine = !prevToken || astUtils.isTokenOnSameLine(prevToken, semiToken);
  80. const nextIsSameLine = !nextToken || astUtils.isTokenOnSameLine(semiToken, nextToken);
  81. if ((expected === "last" && !prevIsSameLine) || (expected === "first" && !nextIsSameLine)) {
  82. context.report({
  83. loc: semiToken.loc,
  84. message: "Expected this semicolon to be at {{pos}}.",
  85. data: {
  86. pos: (expected === "last")
  87. ? "the end of the previous line"
  88. : "the beginning of the next line"
  89. },
  90. fix(fixer) {
  91. if (prevToken && nextToken && sourceCode.commentsExistBetween(prevToken, nextToken)) {
  92. return null;
  93. }
  94. const start = prevToken ? prevToken.range[1] : semiToken.range[0];
  95. const end = nextToken ? nextToken.range[0] : semiToken.range[1];
  96. const text = (expected === "last") ? ";\n" : "\n;";
  97. return fixer.replaceTextRange([start, end], text);
  98. }
  99. });
  100. }
  101. }
  102. return {
  103. [SELECTOR](node) {
  104. if (option === "first" && isLastChild(node)) {
  105. return;
  106. }
  107. const lastToken = sourceCode.getLastToken(node);
  108. if (astUtils.isSemicolonToken(lastToken)) {
  109. check(lastToken, option);
  110. }
  111. },
  112. ForStatement(node) {
  113. const firstSemi = node.init && sourceCode.getTokenAfter(node.init, astUtils.isSemicolonToken);
  114. const secondSemi = node.test && sourceCode.getTokenAfter(node.test, astUtils.isSemicolonToken);
  115. if (firstSemi) {
  116. check(firstSemi, "last");
  117. }
  118. if (secondSemi) {
  119. check(secondSemi, "last");
  120. }
  121. }
  122. };
  123. }
  124. };