nonblock-statement-body-position.js 4.3 KB

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