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-curly-spacing.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * @fileoverview Rule to enforce spacing around embedded expressions of template strings
  3. * @author Toru Nagashima
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("../util/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Helpers
  12. //------------------------------------------------------------------------------
  13. const OPEN_PAREN = /\$\{$/;
  14. const CLOSE_PAREN = /^\}/;
  15. //------------------------------------------------------------------------------
  16. // Rule Definition
  17. //------------------------------------------------------------------------------
  18. module.exports = {
  19. meta: {
  20. type: "layout",
  21. docs: {
  22. description: "require or disallow spacing around embedded expressions of template strings",
  23. category: "ECMAScript 6",
  24. recommended: false,
  25. url: "https://eslint.org/docs/rules/template-curly-spacing"
  26. },
  27. fixable: "whitespace",
  28. schema: [
  29. { enum: ["always", "never"] }
  30. ]
  31. },
  32. create(context) {
  33. const sourceCode = context.getSourceCode();
  34. const always = context.options[0] === "always";
  35. const prefix = always ? "Expected" : "Unexpected";
  36. /**
  37. * Checks spacing before `}` of a given token.
  38. * @param {Token} token - A token to check. This is a Template token.
  39. * @returns {void}
  40. */
  41. function checkSpacingBefore(token) {
  42. const prevToken = sourceCode.getTokenBefore(token);
  43. if (prevToken &&
  44. CLOSE_PAREN.test(token.value) &&
  45. astUtils.isTokenOnSameLine(prevToken, token) &&
  46. sourceCode.isSpaceBetweenTokens(prevToken, token) !== always
  47. ) {
  48. context.report({
  49. loc: token.loc.start,
  50. message: "{{prefix}} space(s) before '}'.",
  51. data: {
  52. prefix
  53. },
  54. fix(fixer) {
  55. if (always) {
  56. return fixer.insertTextBefore(token, " ");
  57. }
  58. return fixer.removeRange([
  59. prevToken.range[1],
  60. token.range[0]
  61. ]);
  62. }
  63. });
  64. }
  65. }
  66. /**
  67. * Checks spacing after `${` of a given token.
  68. * @param {Token} token - A token to check. This is a Template token.
  69. * @returns {void}
  70. */
  71. function checkSpacingAfter(token) {
  72. const nextToken = sourceCode.getTokenAfter(token);
  73. if (nextToken &&
  74. OPEN_PAREN.test(token.value) &&
  75. astUtils.isTokenOnSameLine(token, nextToken) &&
  76. sourceCode.isSpaceBetweenTokens(token, nextToken) !== always
  77. ) {
  78. context.report({
  79. loc: {
  80. line: token.loc.end.line,
  81. column: token.loc.end.column - 2
  82. },
  83. message: "{{prefix}} space(s) after '${'.",
  84. data: {
  85. prefix
  86. },
  87. fix(fixer) {
  88. if (always) {
  89. return fixer.insertTextAfter(token, " ");
  90. }
  91. return fixer.removeRange([
  92. token.range[1],
  93. nextToken.range[0]
  94. ]);
  95. }
  96. });
  97. }
  98. }
  99. return {
  100. TemplateElement(node) {
  101. const token = sourceCode.getFirstToken(node);
  102. checkSpacingBefore(token);
  103. checkSpacingAfter(token);
  104. }
  105. };
  106. }
  107. };