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.

nonblock-statement-body-position.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. },
  39. create(context) {
  40. const sourceCode = context.getSourceCode();
  41. //----------------------------------------------------------------------
  42. // Helpers
  43. //----------------------------------------------------------------------
  44. /**
  45. * Gets the applicable preference for a particular keyword
  46. * @param {string} keywordName The name of a keyword, e.g. 'if'
  47. * @returns {string} The applicable option for the keyword, e.g. 'beside'
  48. */
  49. function getOption(keywordName) {
  50. return context.options[1] && context.options[1].overrides && context.options[1].overrides[keywordName] ||
  51. context.options[0] ||
  52. "beside";
  53. }
  54. /**
  55. * Validates the location of a single-line statement
  56. * @param {ASTNode} node The single-line statement
  57. * @param {string} keywordName The applicable keyword name for the single-line statement
  58. * @returns {void}
  59. */
  60. function validateStatement(node, keywordName) {
  61. const option = getOption(keywordName);
  62. if (node.type === "BlockStatement" || option === "any") {
  63. return;
  64. }
  65. const tokenBefore = sourceCode.getTokenBefore(node);
  66. if (tokenBefore.loc.end.line === node.loc.start.line && option === "below") {
  67. context.report({
  68. node,
  69. message: "Expected a linebreak before this statement.",
  70. fix: fixer => fixer.insertTextBefore(node, "\n")
  71. });
  72. } else if (tokenBefore.loc.end.line !== node.loc.start.line && option === "beside") {
  73. context.report({
  74. node,
  75. message: "Expected no linebreak before this statement.",
  76. fix(fixer) {
  77. if (sourceCode.getText().slice(tokenBefore.range[1], node.range[0]).trim()) {
  78. return null;
  79. }
  80. return fixer.replaceTextRange([tokenBefore.range[1], node.range[0]], " ");
  81. }
  82. });
  83. }
  84. }
  85. //----------------------------------------------------------------------
  86. // Public
  87. //----------------------------------------------------------------------
  88. return {
  89. IfStatement(node) {
  90. validateStatement(node.consequent, "if");
  91. // Check the `else` node, but don't check 'else if' statements.
  92. if (node.alternate && node.alternate.type !== "IfStatement") {
  93. validateStatement(node.alternate, "else");
  94. }
  95. },
  96. WhileStatement: node => validateStatement(node.body, "while"),
  97. DoWhileStatement: node => validateStatement(node.body, "do"),
  98. ForStatement: node => validateStatement(node.body, "for"),
  99. ForInStatement: node => validateStatement(node.body, "for"),
  100. ForOfStatement: node => validateStatement(node.body, "for")
  101. };
  102. }
  103. };