semi-style.js 4.9 KB

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