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.

template-tag-spacing.js 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * @fileoverview Rule to check spacing between template tags and their literals
  3. * @author Jonathan Wilsson
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. type: "layout",
  12. docs: {
  13. description: "require or disallow spacing between template tags and their literals",
  14. category: "Stylistic Issues",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/template-tag-spacing"
  17. },
  18. fixable: "whitespace",
  19. schema: [
  20. { enum: ["always", "never"] }
  21. ]
  22. },
  23. create(context) {
  24. const never = context.options[0] !== "always";
  25. const sourceCode = context.getSourceCode();
  26. /**
  27. * Check if a space is present between a template tag and its literal
  28. * @param {ASTNode} node node to evaluate
  29. * @returns {void}
  30. * @private
  31. */
  32. function checkSpacing(node) {
  33. const tagToken = sourceCode.getTokenBefore(node.quasi);
  34. const literalToken = sourceCode.getFirstToken(node.quasi);
  35. const hasWhitespace = sourceCode.isSpaceBetweenTokens(tagToken, literalToken);
  36. if (never && hasWhitespace) {
  37. context.report({
  38. node,
  39. loc: tagToken.loc.start,
  40. message: "Unexpected space between template tag and template literal.",
  41. fix(fixer) {
  42. const comments = sourceCode.getCommentsBefore(node.quasi);
  43. // Don't fix anything if there's a single line comment after the template tag
  44. if (comments.some(comment => comment.type === "Line")) {
  45. return null;
  46. }
  47. return fixer.replaceTextRange(
  48. [tagToken.range[1], literalToken.range[0]],
  49. comments.reduce((text, comment) => text + sourceCode.getText(comment), "")
  50. );
  51. }
  52. });
  53. } else if (!never && !hasWhitespace) {
  54. context.report({
  55. node,
  56. loc: tagToken.loc.start,
  57. message: "Missing space between template tag and template literal.",
  58. fix(fixer) {
  59. return fixer.insertTextAfter(tagToken, " ");
  60. }
  61. });
  62. }
  63. }
  64. return {
  65. TaggedTemplateExpression: checkSpacing
  66. };
  67. }
  68. };