项目原始demo,不改动
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

multiline-ternary.js 3.1 KiB

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