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.

space-in-parens.js 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /**
  2. * @fileoverview Disallows or enforces spaces inside of parentheses.
  3. * @author Jonathan Rajavuori
  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 parentheses",
  15. category: "Stylistic Issues",
  16. recommended: false,
  17. url: "https://eslint.org/docs/rules/space-in-parens"
  18. },
  19. fixable: "whitespace",
  20. schema: [
  21. {
  22. enum: ["always", "never"]
  23. },
  24. {
  25. type: "object",
  26. properties: {
  27. exceptions: {
  28. type: "array",
  29. items: {
  30. enum: ["{}", "[]", "()", "empty"]
  31. },
  32. uniqueItems: true
  33. }
  34. },
  35. additionalProperties: false
  36. }
  37. ]
  38. },
  39. create(context) {
  40. const MISSING_SPACE_MESSAGE = "There must be a space inside this paren.",
  41. REJECTED_SPACE_MESSAGE = "There should be no spaces inside this paren.",
  42. ALWAYS = context.options[0] === "always",
  43. exceptionsArrayOptions = (context.options[1] && context.options[1].exceptions) || [],
  44. options = {};
  45. let exceptions;
  46. if (exceptionsArrayOptions.length) {
  47. options.braceException = exceptionsArrayOptions.indexOf("{}") !== -1;
  48. options.bracketException = exceptionsArrayOptions.indexOf("[]") !== -1;
  49. options.parenException = exceptionsArrayOptions.indexOf("()") !== -1;
  50. options.empty = exceptionsArrayOptions.indexOf("empty") !== -1;
  51. }
  52. /**
  53. * Produces an object with the opener and closer exception values
  54. * @param {Object} opts The exception options
  55. * @returns {Object} `openers` and `closers` exception values
  56. * @private
  57. */
  58. function getExceptions() {
  59. const openers = [],
  60. closers = [];
  61. if (options.braceException) {
  62. openers.push("{");
  63. closers.push("}");
  64. }
  65. if (options.bracketException) {
  66. openers.push("[");
  67. closers.push("]");
  68. }
  69. if (options.parenException) {
  70. openers.push("(");
  71. closers.push(")");
  72. }
  73. if (options.empty) {
  74. openers.push(")");
  75. closers.push("(");
  76. }
  77. return {
  78. openers,
  79. closers
  80. };
  81. }
  82. //--------------------------------------------------------------------------
  83. // Helpers
  84. //--------------------------------------------------------------------------
  85. const sourceCode = context.getSourceCode();
  86. /**
  87. * Determines if a token is one of the exceptions for the opener paren
  88. * @param {Object} token The token to check
  89. * @returns {boolean} True if the token is one of the exceptions for the opener paren
  90. */
  91. function isOpenerException(token) {
  92. return token.type === "Punctuator" && exceptions.openers.indexOf(token.value) >= 0;
  93. }
  94. /**
  95. * Determines if a token is one of the exceptions for the closer paren
  96. * @param {Object} token The token to check
  97. * @returns {boolean} True if the token is one of the exceptions for the closer paren
  98. */
  99. function isCloserException(token) {
  100. return token.type === "Punctuator" && exceptions.closers.indexOf(token.value) >= 0;
  101. }
  102. /**
  103. * Determines if an opener paren should have a missing space after it
  104. * @param {Object} left The paren token
  105. * @param {Object} right The token after it
  106. * @returns {boolean} True if the paren should have a space
  107. */
  108. function shouldOpenerHaveSpace(left, right) {
  109. if (sourceCode.isSpaceBetweenTokens(left, right)) {
  110. return false;
  111. }
  112. if (ALWAYS) {
  113. if (astUtils.isClosingParenToken(right)) {
  114. return false;
  115. }
  116. return !isOpenerException(right);
  117. }
  118. return isOpenerException(right);
  119. }
  120. /**
  121. * Determines if an closer paren should have a missing space after it
  122. * @param {Object} left The token before the paren
  123. * @param {Object} right The paren token
  124. * @returns {boolean} True if the paren should have a space
  125. */
  126. function shouldCloserHaveSpace(left, right) {
  127. if (astUtils.isOpeningParenToken(left)) {
  128. return false;
  129. }
  130. if (sourceCode.isSpaceBetweenTokens(left, right)) {
  131. return false;
  132. }
  133. if (ALWAYS) {
  134. return !isCloserException(left);
  135. }
  136. return isCloserException(left);
  137. }
  138. /**
  139. * Determines if an opener paren should not have an existing space after it
  140. * @param {Object} left The paren token
  141. * @param {Object} right The token after it
  142. * @returns {boolean} True if the paren should reject the space
  143. */
  144. function shouldOpenerRejectSpace(left, right) {
  145. if (right.type === "Line") {
  146. return false;
  147. }
  148. if (!astUtils.isTokenOnSameLine(left, right)) {
  149. return false;
  150. }
  151. if (!sourceCode.isSpaceBetweenTokens(left, right)) {
  152. return false;
  153. }
  154. if (ALWAYS) {
  155. return isOpenerException(right);
  156. }
  157. return !isOpenerException(right);
  158. }
  159. /**
  160. * Determines if an closer paren should not have an existing space after it
  161. * @param {Object} left The token before the paren
  162. * @param {Object} right The paren token
  163. * @returns {boolean} True if the paren should reject the space
  164. */
  165. function shouldCloserRejectSpace(left, right) {
  166. if (astUtils.isOpeningParenToken(left)) {
  167. return false;
  168. }
  169. if (!astUtils.isTokenOnSameLine(left, right)) {
  170. return false;
  171. }
  172. if (!sourceCode.isSpaceBetweenTokens(left, right)) {
  173. return false;
  174. }
  175. if (ALWAYS) {
  176. return isCloserException(left);
  177. }
  178. return !isCloserException(left);
  179. }
  180. //--------------------------------------------------------------------------
  181. // Public
  182. //--------------------------------------------------------------------------
  183. return {
  184. Program: function checkParenSpaces(node) {
  185. exceptions = getExceptions();
  186. const tokens = sourceCode.tokensAndComments;
  187. tokens.forEach((token, i) => {
  188. const prevToken = tokens[i - 1];
  189. const nextToken = tokens[i + 1];
  190. if (!astUtils.isOpeningParenToken(token) && !astUtils.isClosingParenToken(token)) {
  191. return;
  192. }
  193. if (token.value === "(" && shouldOpenerHaveSpace(token, nextToken)) {
  194. context.report({
  195. node,
  196. loc: token.loc.start,
  197. message: MISSING_SPACE_MESSAGE,
  198. fix(fixer) {
  199. return fixer.insertTextAfter(token, " ");
  200. }
  201. });
  202. } else if (token.value === "(" && shouldOpenerRejectSpace(token, nextToken)) {
  203. context.report({
  204. node,
  205. loc: token.loc.start,
  206. message: REJECTED_SPACE_MESSAGE,
  207. fix(fixer) {
  208. return fixer.removeRange([token.range[1], nextToken.range[0]]);
  209. }
  210. });
  211. } else if (token.value === ")" && shouldCloserHaveSpace(prevToken, token)) {
  212. // context.report(node, token.loc.start, MISSING_SPACE_MESSAGE);
  213. context.report({
  214. node,
  215. loc: token.loc.start,
  216. message: MISSING_SPACE_MESSAGE,
  217. fix(fixer) {
  218. return fixer.insertTextBefore(token, " ");
  219. }
  220. });
  221. } else if (token.value === ")" && shouldCloserRejectSpace(prevToken, token)) {
  222. context.report({
  223. node,
  224. loc: token.loc.start,
  225. message: REJECTED_SPACE_MESSAGE,
  226. fix(fixer) {
  227. return fixer.removeRange([prevToken.range[1], token.range[0]]);
  228. }
  229. });
  230. }
  231. });
  232. }
  233. };
  234. }
  235. };