space-unary-ops.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /**
  2. * @fileoverview This rule shoud require or disallow spaces before or after unary operations.
  3. * @author Marcin Kumorek
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("../ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Rule Definition
  12. //------------------------------------------------------------------------------
  13. module.exports = {
  14. meta: {
  15. docs: {
  16. description: "enforce consistent spacing before or after unary operators",
  17. category: "Stylistic Issues",
  18. recommended: false,
  19. url: "https://eslint.org/docs/rules/space-unary-ops"
  20. },
  21. fixable: "whitespace",
  22. schema: [
  23. {
  24. type: "object",
  25. properties: {
  26. words: {
  27. type: "boolean"
  28. },
  29. nonwords: {
  30. type: "boolean"
  31. },
  32. overrides: {
  33. type: "object",
  34. additionalProperties: {
  35. type: "boolean"
  36. }
  37. }
  38. },
  39. additionalProperties: false
  40. }
  41. ]
  42. },
  43. create(context) {
  44. const options = context.options && Array.isArray(context.options) && context.options[0] || { words: true, nonwords: false };
  45. const sourceCode = context.getSourceCode();
  46. //--------------------------------------------------------------------------
  47. // Helpers
  48. //--------------------------------------------------------------------------
  49. /**
  50. * Check if the node is the first "!" in a "!!" convert to Boolean expression
  51. * @param {ASTnode} node AST node
  52. * @returns {boolean} Whether or not the node is first "!" in "!!"
  53. */
  54. function isFirstBangInBangBangExpression(node) {
  55. return node && node.type === "UnaryExpression" && node.argument.operator === "!" &&
  56. node.argument && node.argument.type === "UnaryExpression" && node.argument.operator === "!";
  57. }
  58. /**
  59. * Checks if an override exists for a given operator.
  60. * @param {string} operator Operator
  61. * @returns {boolean} Whether or not an override has been provided for the operator
  62. */
  63. function overrideExistsForOperator(operator) {
  64. return options.overrides && options.overrides.hasOwnProperty(operator);
  65. }
  66. /**
  67. * Gets the value that the override was set to for this operator
  68. * @param {string} operator Operator
  69. * @returns {boolean} Whether or not an override enforces a space with this operator
  70. */
  71. function overrideEnforcesSpaces(operator) {
  72. return options.overrides[operator];
  73. }
  74. /**
  75. * Verify Unary Word Operator has spaces after the word operator
  76. * @param {ASTnode} node AST node
  77. * @param {Object} firstToken first token from the AST node
  78. * @param {Object} secondToken second token from the AST node
  79. * @param {string} word The word to be used for reporting
  80. * @returns {void}
  81. */
  82. function verifyWordHasSpaces(node, firstToken, secondToken, word) {
  83. if (secondToken.range[0] === firstToken.range[1]) {
  84. context.report({
  85. node,
  86. message: "Unary word operator '{{word}}' must be followed by whitespace.",
  87. data: {
  88. word
  89. },
  90. fix(fixer) {
  91. return fixer.insertTextAfter(firstToken, " ");
  92. }
  93. });
  94. }
  95. }
  96. /**
  97. * Verify Unary Word Operator doesn't have spaces after the word operator
  98. * @param {ASTnode} node AST node
  99. * @param {Object} firstToken first token from the AST node
  100. * @param {Object} secondToken second token from the AST node
  101. * @param {string} word The word to be used for reporting
  102. * @returns {void}
  103. */
  104. function verifyWordDoesntHaveSpaces(node, firstToken, secondToken, word) {
  105. if (astUtils.canTokensBeAdjacent(firstToken, secondToken)) {
  106. if (secondToken.range[0] > firstToken.range[1]) {
  107. context.report({
  108. node,
  109. message: "Unexpected space after unary word operator '{{word}}'.",
  110. data: {
  111. word
  112. },
  113. fix(fixer) {
  114. return fixer.removeRange([firstToken.range[1], secondToken.range[0]]);
  115. }
  116. });
  117. }
  118. }
  119. }
  120. /**
  121. * Check Unary Word Operators for spaces after the word operator
  122. * @param {ASTnode} node AST node
  123. * @param {Object} firstToken first token from the AST node
  124. * @param {Object} secondToken second token from the AST node
  125. * @param {string} word The word to be used for reporting
  126. * @returns {void}
  127. */
  128. function checkUnaryWordOperatorForSpaces(node, firstToken, secondToken, word) {
  129. if (overrideExistsForOperator(word)) {
  130. if (overrideEnforcesSpaces(word)) {
  131. verifyWordHasSpaces(node, firstToken, secondToken, word);
  132. } else {
  133. verifyWordDoesntHaveSpaces(node, firstToken, secondToken, word);
  134. }
  135. } else if (options.words) {
  136. verifyWordHasSpaces(node, firstToken, secondToken, word);
  137. } else {
  138. verifyWordDoesntHaveSpaces(node, firstToken, secondToken, word);
  139. }
  140. }
  141. /**
  142. * Verifies YieldExpressions satisfy spacing requirements
  143. * @param {ASTnode} node AST node
  144. * @returns {void}
  145. */
  146. function checkForSpacesAfterYield(node) {
  147. const tokens = sourceCode.getFirstTokens(node, 3),
  148. word = "yield";
  149. if (!node.argument || node.delegate) {
  150. return;
  151. }
  152. checkUnaryWordOperatorForSpaces(node, tokens[0], tokens[1], word);
  153. }
  154. /**
  155. * Verifies AwaitExpressions satisfy spacing requirements
  156. * @param {ASTNode} node AwaitExpression AST node
  157. * @returns {void}
  158. */
  159. function checkForSpacesAfterAwait(node) {
  160. const tokens = sourceCode.getFirstTokens(node, 3);
  161. checkUnaryWordOperatorForSpaces(node, tokens[0], tokens[1], "await");
  162. }
  163. /**
  164. * Verifies UnaryExpression, UpdateExpression and NewExpression have spaces before or after the operator
  165. * @param {ASTnode} node AST node
  166. * @param {Object} firstToken First token in the expression
  167. * @param {Object} secondToken Second token in the expression
  168. * @returns {void}
  169. */
  170. function verifyNonWordsHaveSpaces(node, firstToken, secondToken) {
  171. if (node.prefix) {
  172. if (isFirstBangInBangBangExpression(node)) {
  173. return;
  174. }
  175. if (firstToken.range[1] === secondToken.range[0]) {
  176. context.report({
  177. node,
  178. message: "Unary operator '{{operator}}' must be followed by whitespace.",
  179. data: {
  180. operator: firstToken.value
  181. },
  182. fix(fixer) {
  183. return fixer.insertTextAfter(firstToken, " ");
  184. }
  185. });
  186. }
  187. } else {
  188. if (firstToken.range[1] === secondToken.range[0]) {
  189. context.report({
  190. node,
  191. message: "Space is required before unary expressions '{{token}}'.",
  192. data: {
  193. token: secondToken.value
  194. },
  195. fix(fixer) {
  196. return fixer.insertTextBefore(secondToken, " ");
  197. }
  198. });
  199. }
  200. }
  201. }
  202. /**
  203. * Verifies UnaryExpression, UpdateExpression and NewExpression don't have spaces before or after the operator
  204. * @param {ASTnode} node AST node
  205. * @param {Object} firstToken First token in the expression
  206. * @param {Object} secondToken Second token in the expression
  207. * @returns {void}
  208. */
  209. function verifyNonWordsDontHaveSpaces(node, firstToken, secondToken) {
  210. if (node.prefix) {
  211. if (secondToken.range[0] > firstToken.range[1]) {
  212. context.report({
  213. node,
  214. message: "Unexpected space after unary operator '{{operator}}'.",
  215. data: {
  216. operator: firstToken.value
  217. },
  218. fix(fixer) {
  219. if (astUtils.canTokensBeAdjacent(firstToken, secondToken)) {
  220. return fixer.removeRange([firstToken.range[1], secondToken.range[0]]);
  221. }
  222. return null;
  223. }
  224. });
  225. }
  226. } else {
  227. if (secondToken.range[0] > firstToken.range[1]) {
  228. context.report({
  229. node,
  230. message: "Unexpected space before unary operator '{{operator}}'.",
  231. data: {
  232. operator: secondToken.value
  233. },
  234. fix(fixer) {
  235. return fixer.removeRange([firstToken.range[1], secondToken.range[0]]);
  236. }
  237. });
  238. }
  239. }
  240. }
  241. /**
  242. * Verifies UnaryExpression, UpdateExpression and NewExpression satisfy spacing requirements
  243. * @param {ASTnode} node AST node
  244. * @returns {void}
  245. */
  246. function checkForSpaces(node) {
  247. const tokens = node.type === "UpdateExpression" && !node.prefix
  248. ? sourceCode.getLastTokens(node, 2)
  249. : sourceCode.getFirstTokens(node, 2);
  250. const firstToken = tokens[0];
  251. const secondToken = tokens[1];
  252. if ((node.type === "NewExpression" || node.prefix) && firstToken.type === "Keyword") {
  253. checkUnaryWordOperatorForSpaces(node, firstToken, secondToken, firstToken.value);
  254. return;
  255. }
  256. const operator = node.prefix ? tokens[0].value : tokens[1].value;
  257. if (overrideExistsForOperator(operator)) {
  258. if (overrideEnforcesSpaces(operator)) {
  259. verifyNonWordsHaveSpaces(node, firstToken, secondToken);
  260. } else {
  261. verifyNonWordsDontHaveSpaces(node, firstToken, secondToken);
  262. }
  263. } else if (options.nonwords) {
  264. verifyNonWordsHaveSpaces(node, firstToken, secondToken);
  265. } else {
  266. verifyNonWordsDontHaveSpaces(node, firstToken, secondToken);
  267. }
  268. }
  269. //--------------------------------------------------------------------------
  270. // Public
  271. //--------------------------------------------------------------------------
  272. return {
  273. UnaryExpression: checkForSpaces,
  274. UpdateExpression: checkForSpaces,
  275. NewExpression: checkForSpaces,
  276. YieldExpression: checkForSpacesAfterYield,
  277. AwaitExpression: checkForSpacesAfterAwait
  278. };
  279. }
  280. };