no-multi-str.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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('../shared/types').Rule} */
  14. module.exports = {
  15. meta: {
  16. type: "suggestion",
  17. docs: {
  18. description: "Disallow multiline strings",
  19. recommended: false,
  20. url: "https://eslint.org/docs/rules/no-multi-str"
  21. },
  22. schema: [],
  23. messages: {
  24. multilineString: "Multiline support is limited to browsers supporting ES5 only."
  25. }
  26. },
  27. create(context) {
  28. /**
  29. * Determines if a given node is part of JSX syntax.
  30. * @param {ASTNode} node The node to check.
  31. * @returns {boolean} True if the node is a JSX node, false if not.
  32. * @private
  33. */
  34. function isJSXElement(node) {
  35. return node.type.indexOf("JSX") === 0;
  36. }
  37. //--------------------------------------------------------------------------
  38. // Public API
  39. //--------------------------------------------------------------------------
  40. return {
  41. Literal(node) {
  42. if (astUtils.LINEBREAK_MATCHER.test(node.raw) && !isJSXElement(node.parent)) {
  43. context.report({
  44. node,
  45. messageId: "multilineString"
  46. });
  47. }
  48. }
  49. };
  50. }
  51. };