nonblock-statement-body-position.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * @fileoverview enforce the location of single-line statements
  3. * @author Teddy Katz
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. const POSITION_SCHEMA = { enum: ["beside", "below", "any"] };
  10. module.exports = {
  11. meta: {
  12. type: "layout",
  13. docs: {
  14. description: "enforce the location of single-line statements",
  15. category: "Stylistic Issues",
  16. recommended: false,
  17. url: "https://eslint.org/docs/rules/nonblock-statement-body-position"
  18. },
  19. fixable: "whitespace",
  20. schema: [
  21. POSITION_SCHEMA,
  22. {
  23. properties: {
  24. overrides: {
  25. properties: {
  26. if: POSITION_SCHEMA,
  27. else: POSITION_SCHEMA,
  28. while: POSITION_SCHEMA,
  29. do: POSITION_SCHEMA,
  30. for: POSITION_SCHEMA
  31. },
  32. additionalProperties: false
  33. }
  34. },
  35. additionalProperties: false
  36. }
  37. ],
  38. messages: {
  39. expectNoLinebreak: "Expected no linebreak before this statement.",
  40. expectLinebreak: "Expected a linebreak before this statement."
  41. }
  42. },
  43. create(context) {
  44. const sourceCode = context.getSourceCode();
  45. //----------------------------------------------------------------------
  46. // Helpers
  47. //----------------------------------------------------------------------
  48. /**
  49. * Gets the applicable preference for a particular keyword
  50. * @param {string} keywordName The name of a keyword, e.g. 'if'
  51. * @returns {string} The applicable option for the keyword, e.g. 'beside'
  52. */
  53. function getOption(keywordName) {
  54. return context.options[1] && context.options[1].overrides && context.options[1].overrides[keywordName] ||
  55. context.options[0] ||
  56. "beside";
  57. }
  58. /**
  59. * Validates the location of a single-line statement
  60. * @param {ASTNode} node The single-line statement
  61. * @param {string} keywordName The applicable keyword name for the single-line statement
  62. * @returns {void}
  63. */
  64. function validateStatement(node, keywordName) {
  65. const option = getOption(keywordName);
  66. if (node.type === "BlockStatement" || option === "any") {
  67. return;
  68. }
  69. const tokenBefore = sourceCode.getTokenBefore(node);
  70. if (tokenBefore.loc.end.line === node.loc.start.line && option === "below") {
  71. context.report({
  72. node,
  73. messageId: "expectLinebreak",
  74. fix: fixer => fixer.insertTextBefore(node, "\n")
  75. });
  76. } else if (tokenBefore.loc.end.line !== node.loc.start.line && option === "beside") {
  77. context.report({
  78. node,
  79. messageId: "expectNoLinebreak",
  80. fix(fixer) {
  81. if (sourceCode.getText().slice(tokenBefore.range[1], node.range[0]).trim()) {
  82. return null;
  83. }
  84. return fixer.replaceTextRange([tokenBefore.range[1], node.range[0]], " ");
  85. }
  86. });
  87. }
  88. }
  89. //----------------------------------------------------------------------
  90. // Public
  91. //----------------------------------------------------------------------
  92. return {
  93. IfStatement(node) {
  94. validateStatement(node.consequent, "if");
  95. // Check the `else` node, but don't check 'else if' statements.
  96. if (node.alternate && node.alternate.type !== "IfStatement") {
  97. validateStatement(node.alternate, "else");
  98. }
  99. },
  100. WhileStatement: node => validateStatement(node.body, "while"),
  101. DoWhileStatement: node => validateStatement(node.body, "do"),
  102. ForStatement: node => validateStatement(node.body, "for"),
  103. ForInStatement: node => validateStatement(node.body, "for"),
  104. ForOfStatement: node => validateStatement(node.body, "for")
  105. };
  106. }
  107. };