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.

computed-property-spacing.js 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /**
  2. * @fileoverview Disallows or enforces spaces inside computed properties.
  3. * @author Jamund Ferguson
  4. */
  5. "use strict";
  6. const astUtils = require("../util/ast-utils");
  7. //------------------------------------------------------------------------------
  8. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. module.exports = {
  11. meta: {
  12. type: "layout",
  13. docs: {
  14. description: "enforce consistent spacing inside computed property brackets",
  15. category: "Stylistic Issues",
  16. recommended: false,
  17. url: "https://eslint.org/docs/rules/computed-property-spacing"
  18. },
  19. fixable: "whitespace",
  20. schema: [
  21. {
  22. enum: ["always", "never"]
  23. }
  24. ],
  25. messages: {
  26. unexpectedSpaceBefore: "There should be no space before '{{tokenValue}}'.",
  27. unexpectedSpaceAfter: "There should be no space after '{{tokenValue}}'.",
  28. missingSpaceBefore: "A space is required before '{{tokenValue}}'.",
  29. missingSpaceAfter: "A space is required after '{{tokenValue}}'."
  30. }
  31. },
  32. create(context) {
  33. const sourceCode = context.getSourceCode();
  34. const propertyNameMustBeSpaced = context.options[0] === "always"; // default is "never"
  35. //--------------------------------------------------------------------------
  36. // Helpers
  37. //--------------------------------------------------------------------------
  38. /**
  39. * Reports that there shouldn't be a space after the first token
  40. * @param {ASTNode} node - The node to report in the event of an error.
  41. * @param {Token} token - The token to use for the report.
  42. * @param {Token} tokenAfter - The token after `token`.
  43. * @returns {void}
  44. */
  45. function reportNoBeginningSpace(node, token, tokenAfter) {
  46. context.report({
  47. node,
  48. loc: token.loc.start,
  49. messageId: "unexpectedSpaceAfter",
  50. data: {
  51. tokenValue: token.value
  52. },
  53. fix(fixer) {
  54. return fixer.removeRange([token.range[1], tokenAfter.range[0]]);
  55. }
  56. });
  57. }
  58. /**
  59. * Reports that there shouldn't be a space before the last token
  60. * @param {ASTNode} node - The node to report in the event of an error.
  61. * @param {Token} token - The token to use for the report.
  62. * @param {Token} tokenBefore - The token before `token`.
  63. * @returns {void}
  64. */
  65. function reportNoEndingSpace(node, token, tokenBefore) {
  66. context.report({
  67. node,
  68. loc: token.loc.start,
  69. messageId: "unexpectedSpaceBefore",
  70. data: {
  71. tokenValue: token.value
  72. },
  73. fix(fixer) {
  74. return fixer.removeRange([tokenBefore.range[1], token.range[0]]);
  75. }
  76. });
  77. }
  78. /**
  79. * Reports that there should be a space after the first token
  80. * @param {ASTNode} node - The node to report in the event of an error.
  81. * @param {Token} token - The token to use for the report.
  82. * @returns {void}
  83. */
  84. function reportRequiredBeginningSpace(node, token) {
  85. context.report({
  86. node,
  87. loc: token.loc.start,
  88. messageId: "missingSpaceAfter",
  89. data: {
  90. tokenValue: token.value
  91. },
  92. fix(fixer) {
  93. return fixer.insertTextAfter(token, " ");
  94. }
  95. });
  96. }
  97. /**
  98. * Reports that there should be a space before the last token
  99. * @param {ASTNode} node - The node to report in the event of an error.
  100. * @param {Token} token - The token to use for the report.
  101. * @returns {void}
  102. */
  103. function reportRequiredEndingSpace(node, token) {
  104. context.report({
  105. node,
  106. loc: token.loc.start,
  107. messageId: "missingSpaceBefore",
  108. data: {
  109. tokenValue: token.value
  110. },
  111. fix(fixer) {
  112. return fixer.insertTextBefore(token, " ");
  113. }
  114. });
  115. }
  116. /**
  117. * Returns a function that checks the spacing of a node on the property name
  118. * that was passed in.
  119. * @param {string} propertyName The property on the node to check for spacing
  120. * @returns {Function} A function that will check spacing on a node
  121. */
  122. function checkSpacing(propertyName) {
  123. return function(node) {
  124. if (!node.computed) {
  125. return;
  126. }
  127. const property = node[propertyName];
  128. const before = sourceCode.getTokenBefore(property),
  129. first = sourceCode.getFirstToken(property),
  130. last = sourceCode.getLastToken(property),
  131. after = sourceCode.getTokenAfter(property);
  132. if (astUtils.isTokenOnSameLine(before, first)) {
  133. if (propertyNameMustBeSpaced) {
  134. if (!sourceCode.isSpaceBetweenTokens(before, first) && astUtils.isTokenOnSameLine(before, first)) {
  135. reportRequiredBeginningSpace(node, before);
  136. }
  137. } else {
  138. if (sourceCode.isSpaceBetweenTokens(before, first)) {
  139. reportNoBeginningSpace(node, before, first);
  140. }
  141. }
  142. }
  143. if (astUtils.isTokenOnSameLine(last, after)) {
  144. if (propertyNameMustBeSpaced) {
  145. if (!sourceCode.isSpaceBetweenTokens(last, after) && astUtils.isTokenOnSameLine(last, after)) {
  146. reportRequiredEndingSpace(node, after);
  147. }
  148. } else {
  149. if (sourceCode.isSpaceBetweenTokens(last, after)) {
  150. reportNoEndingSpace(node, after, last);
  151. }
  152. }
  153. }
  154. };
  155. }
  156. //--------------------------------------------------------------------------
  157. // Public
  158. //--------------------------------------------------------------------------
  159. return {
  160. Property: checkSpacing("key"),
  161. MemberExpression: checkSpacing("property")
  162. };
  163. }
  164. };