no-multi-str.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * @fileoverview Rule to flag when using multiline strings
  3. * @author Ilya Volodin
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("./utils/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Rule Definition
  12. //------------------------------------------------------------------------------
  13. /** @type {import('../types').Rule.RuleModule} */
  14. module.exports = {
  15. meta: {
  16. type: "suggestion",
  17. docs: {
  18. description: "Disallow multiline strings",
  19. recommended: false,
  20. frozen: true,
  21. url: "https://eslint.org/docs/latest/rules/no-multi-str",
  22. },
  23. schema: [],
  24. messages: {
  25. multilineString:
  26. "Multiline support is limited to browsers supporting ES5 only.",
  27. },
  28. },
  29. create(context) {
  30. /**
  31. * Determines if a given node is part of JSX syntax.
  32. * @param {ASTNode} node The node to check.
  33. * @returns {boolean} True if the node is a JSX node, false if not.
  34. * @private
  35. */
  36. function isJSXElement(node) {
  37. return node.type.indexOf("JSX") === 0;
  38. }
  39. //--------------------------------------------------------------------------
  40. // Public API
  41. //--------------------------------------------------------------------------
  42. return {
  43. Literal(node) {
  44. if (
  45. astUtils.LINEBREAK_MATCHER.test(node.raw) &&
  46. !isJSXElement(node.parent)
  47. ) {
  48. context.report({
  49. node,
  50. messageId: "multilineString",
  51. });
  52. }
  53. },
  54. };
  55. },
  56. };