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.

multiline-ternary.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * @fileoverview Enforce newlines between operands of ternary expressions
  3. * @author Kai Cataldo
  4. */
  5. "use strict";
  6. const astUtils = require("../util/ast-utils");
  7. //------------------------------------------------------------------------------
  8. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. module.exports = {
  11. meta: {
  12. type: "layout",
  13. docs: {
  14. description: "enforce newlines between operands of ternary expressions",
  15. category: "Stylistic Issues",
  16. recommended: false,
  17. url: "https://eslint.org/docs/rules/multiline-ternary"
  18. },
  19. schema: [
  20. {
  21. enum: ["always", "always-multiline", "never"]
  22. }
  23. ]
  24. },
  25. create(context) {
  26. const option = context.options[0];
  27. const multiline = option !== "never";
  28. const allowSingleLine = option === "always-multiline";
  29. //--------------------------------------------------------------------------
  30. // Helpers
  31. //--------------------------------------------------------------------------
  32. /**
  33. * Tests whether node is preceded by supplied tokens
  34. * @param {ASTNode} node - node to check
  35. * @param {ASTNode} parentNode - parent of node to report
  36. * @param {boolean} expected - whether newline was expected or not
  37. * @returns {void}
  38. * @private
  39. */
  40. function reportError(node, parentNode, expected) {
  41. context.report({
  42. node,
  43. message: "{{expected}} newline between {{typeOfError}} of ternary expression.",
  44. data: {
  45. expected: expected ? "Expected" : "Unexpected",
  46. typeOfError: node === parentNode.test ? "test and consequent" : "consequent and alternate"
  47. }
  48. });
  49. }
  50. //--------------------------------------------------------------------------
  51. // Public
  52. //--------------------------------------------------------------------------
  53. return {
  54. ConditionalExpression(node) {
  55. const areTestAndConsequentOnSameLine = astUtils.isTokenOnSameLine(node.test, node.consequent);
  56. const areConsequentAndAlternateOnSameLine = astUtils.isTokenOnSameLine(node.consequent, node.alternate);
  57. if (!multiline) {
  58. if (!areTestAndConsequentOnSameLine) {
  59. reportError(node.test, node, false);
  60. }
  61. if (!areConsequentAndAlternateOnSameLine) {
  62. reportError(node.consequent, node, false);
  63. }
  64. } else {
  65. if (allowSingleLine && node.loc.start.line === node.loc.end.line) {
  66. return;
  67. }
  68. if (areTestAndConsequentOnSameLine) {
  69. reportError(node.test, node, true);
  70. }
  71. if (areConsequentAndAlternateOnSameLine) {
  72. reportError(node.consequent, node, true);
  73. }
  74. }
  75. }
  76. };
  77. }
  78. };