项目原始demo,不改动
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

space-in-parens.js 9.4 KiB

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