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-unexpected-multiline.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * @fileoverview Rule to spot scenarios where a newline looks like it is ending a statement, but is not.
  3. * @author Glen Mailer
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("../util/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Rule Definition
  12. //------------------------------------------------------------------------------
  13. module.exports = {
  14. meta: {
  15. type: "problem",
  16. docs: {
  17. description: "disallow confusing multiline expressions",
  18. category: "Possible Errors",
  19. recommended: true,
  20. url: "https://eslint.org/docs/rules/no-unexpected-multiline"
  21. },
  22. schema: []
  23. },
  24. create(context) {
  25. const FUNCTION_MESSAGE = "Unexpected newline between function and ( of function call.";
  26. const PROPERTY_MESSAGE = "Unexpected newline between object and [ of property access.";
  27. const TAGGED_TEMPLATE_MESSAGE = "Unexpected newline between template tag and template literal.";
  28. const DIVISION_MESSAGE = "Unexpected newline between numerator and division operator.";
  29. const REGEX_FLAG_MATCHER = /^[gimsuy]+$/;
  30. const sourceCode = context.getSourceCode();
  31. /**
  32. * Check to see if there is a newline between the node and the following open bracket
  33. * line's expression
  34. * @param {ASTNode} node The node to check.
  35. * @param {string} msg The error message to use.
  36. * @returns {void}
  37. * @private
  38. */
  39. function checkForBreakAfter(node, msg) {
  40. const openParen = sourceCode.getTokenAfter(node, astUtils.isNotClosingParenToken);
  41. const nodeExpressionEnd = sourceCode.getTokenBefore(openParen);
  42. if (openParen.loc.start.line !== nodeExpressionEnd.loc.end.line) {
  43. context.report({ node, loc: openParen.loc.start, message: msg, data: { char: openParen.value } });
  44. }
  45. }
  46. //--------------------------------------------------------------------------
  47. // Public API
  48. //--------------------------------------------------------------------------
  49. return {
  50. MemberExpression(node) {
  51. if (!node.computed) {
  52. return;
  53. }
  54. checkForBreakAfter(node.object, PROPERTY_MESSAGE);
  55. },
  56. TaggedTemplateExpression(node) {
  57. if (node.tag.loc.end.line === node.quasi.loc.start.line) {
  58. return;
  59. }
  60. context.report({ node, loc: node.loc.start, message: TAGGED_TEMPLATE_MESSAGE });
  61. },
  62. CallExpression(node) {
  63. if (node.arguments.length === 0) {
  64. return;
  65. }
  66. checkForBreakAfter(node.callee, FUNCTION_MESSAGE);
  67. },
  68. "BinaryExpression[operator='/'] > BinaryExpression[operator='/'].left"(node) {
  69. const secondSlash = sourceCode.getTokenAfter(node, token => token.value === "/");
  70. const tokenAfterOperator = sourceCode.getTokenAfter(secondSlash);
  71. if (
  72. tokenAfterOperator.type === "Identifier" &&
  73. REGEX_FLAG_MATCHER.test(tokenAfterOperator.value) &&
  74. secondSlash.range[1] === tokenAfterOperator.range[0]
  75. ) {
  76. checkForBreakAfter(node.left, DIVISION_MESSAGE);
  77. }
  78. }
  79. };
  80. }
  81. };