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.

no-lonely-if.js 3.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * @fileoverview Rule to disallow if as the only statmenet in an else block
  3. * @author Brandon Mills
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. type: "suggestion",
  12. docs: {
  13. description: "disallow `if` statements as the only statement in `else` blocks",
  14. category: "Stylistic Issues",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/no-lonely-if"
  17. },
  18. schema: [],
  19. fixable: "code"
  20. },
  21. create(context) {
  22. const sourceCode = context.getSourceCode();
  23. return {
  24. IfStatement(node) {
  25. const ancestors = context.getAncestors(),
  26. parent = ancestors.pop(),
  27. grandparent = ancestors.pop();
  28. if (parent && parent.type === "BlockStatement" &&
  29. parent.body.length === 1 && grandparent &&
  30. grandparent.type === "IfStatement" &&
  31. parent === grandparent.alternate) {
  32. context.report({
  33. node,
  34. message: "Unexpected if as the only statement in an else block.",
  35. fix(fixer) {
  36. const openingElseCurly = sourceCode.getFirstToken(parent);
  37. const closingElseCurly = sourceCode.getLastToken(parent);
  38. const elseKeyword = sourceCode.getTokenBefore(openingElseCurly);
  39. const tokenAfterElseBlock = sourceCode.getTokenAfter(closingElseCurly);
  40. const lastIfToken = sourceCode.getLastToken(node.consequent);
  41. const sourceText = sourceCode.getText();
  42. if (sourceText.slice(openingElseCurly.range[1],
  43. node.range[0]).trim() || sourceText.slice(node.range[1], closingElseCurly.range[0]).trim()) {
  44. // Don't fix if there are any non-whitespace characters interfering (e.g. comments)
  45. return null;
  46. }
  47. if (
  48. node.consequent.type !== "BlockStatement" && lastIfToken.value !== ";" && tokenAfterElseBlock &&
  49. (
  50. node.consequent.loc.end.line === tokenAfterElseBlock.loc.start.line ||
  51. /^[([/+`-]/.test(tokenAfterElseBlock.value) ||
  52. lastIfToken.value === "++" ||
  53. lastIfToken.value === "--"
  54. )
  55. ) {
  56. /*
  57. * If the `if` statement has no block, and is not followed by a semicolon, make sure that fixing
  58. * the issue would not change semantics due to ASI. If this would happen, don't do a fix.
  59. */
  60. return null;
  61. }
  62. return fixer.replaceTextRange(
  63. [openingElseCurly.range[0], closingElseCurly.range[1]],
  64. (elseKeyword.range[1] === openingElseCurly.range[0] ? " " : "") + sourceCode.getText(node)
  65. );
  66. }
  67. });
  68. }
  69. }
  70. };
  71. }
  72. };