Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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.

func-call-spacing.js 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /**
  2. * @fileoverview Rule to control spacing within function calls
  3. * @author Matt DuVall <http://www.mattduvall.com>
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("./utils/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Rule Definition
  12. //------------------------------------------------------------------------------
  13. module.exports = {
  14. meta: {
  15. type: "layout",
  16. docs: {
  17. description: "require or disallow spacing between function identifiers and their invocations",
  18. category: "Stylistic Issues",
  19. recommended: false,
  20. url: "https://eslint.org/docs/rules/func-call-spacing"
  21. },
  22. fixable: "whitespace",
  23. schema: {
  24. anyOf: [
  25. {
  26. type: "array",
  27. items: [
  28. {
  29. enum: ["never"]
  30. }
  31. ],
  32. minItems: 0,
  33. maxItems: 1
  34. },
  35. {
  36. type: "array",
  37. items: [
  38. {
  39. enum: ["always"]
  40. },
  41. {
  42. type: "object",
  43. properties: {
  44. allowNewlines: {
  45. type: "boolean"
  46. }
  47. },
  48. additionalProperties: false
  49. }
  50. ],
  51. minItems: 0,
  52. maxItems: 2
  53. }
  54. ]
  55. },
  56. messages: {
  57. unexpectedWhitespace: "Unexpected whitespace between function name and paren.",
  58. unexpectedNewline: "Unexpected newline between function name and paren.",
  59. missing: "Missing space between function name and paren."
  60. }
  61. },
  62. create(context) {
  63. const never = context.options[0] !== "always";
  64. const allowNewlines = !never && context.options[1] && context.options[1].allowNewlines;
  65. const sourceCode = context.getSourceCode();
  66. const text = sourceCode.getText();
  67. /**
  68. * Check if open space is present in a function name
  69. * @param {ASTNode} node node to evaluate
  70. * @param {Token} leftToken The last token of the callee. This may be the closing parenthesis that encloses the callee.
  71. * @param {Token} rightToken Tha first token of the arguments. this is the opening parenthesis that encloses the arguments.
  72. * @returns {void}
  73. * @private
  74. */
  75. function checkSpacing(node, leftToken, rightToken) {
  76. const textBetweenTokens = text.slice(leftToken.range[1], rightToken.range[0]).replace(/\/\*.*?\*\//gu, "");
  77. const hasWhitespace = /\s/u.test(textBetweenTokens);
  78. const hasNewline = hasWhitespace && astUtils.LINEBREAK_MATCHER.test(textBetweenTokens);
  79. /*
  80. * never allowNewlines hasWhitespace hasNewline message
  81. * F F F F Missing space between function name and paren.
  82. * F F F T (Invalid `!hasWhitespace && hasNewline`)
  83. * F F T T Unexpected newline between function name and paren.
  84. * F F T F (OK)
  85. * F T T F (OK)
  86. * F T T T (OK)
  87. * F T F T (Invalid `!hasWhitespace && hasNewline`)
  88. * F T F F Missing space between function name and paren.
  89. * T T F F (Invalid `never && allowNewlines`)
  90. * T T F T (Invalid `!hasWhitespace && hasNewline`)
  91. * T T T T (Invalid `never && allowNewlines`)
  92. * T T T F (Invalid `never && allowNewlines`)
  93. * T F T F Unexpected space between function name and paren.
  94. * T F T T Unexpected space between function name and paren.
  95. * T F F T (Invalid `!hasWhitespace && hasNewline`)
  96. * T F F F (OK)
  97. *
  98. * T T Unexpected space between function name and paren.
  99. * F F Missing space between function name and paren.
  100. * F F T Unexpected newline between function name and paren.
  101. */
  102. if (never && hasWhitespace) {
  103. context.report({
  104. node,
  105. loc: {
  106. start: leftToken.loc.end,
  107. end: {
  108. line: rightToken.loc.start.line,
  109. column: rightToken.loc.start.column - 1
  110. }
  111. },
  112. messageId: "unexpectedWhitespace",
  113. fix(fixer) {
  114. // Don't remove comments.
  115. if (sourceCode.commentsExistBetween(leftToken, rightToken)) {
  116. return null;
  117. }
  118. // If `?.` exists, it doesn't hide no-unexpected-multiline errors
  119. if (node.optional) {
  120. return fixer.replaceTextRange([leftToken.range[1], rightToken.range[0]], "?.");
  121. }
  122. /*
  123. * Only autofix if there is no newline
  124. * https://github.com/eslint/eslint/issues/7787
  125. */
  126. if (hasNewline) {
  127. return null;
  128. }
  129. return fixer.removeRange([leftToken.range[1], rightToken.range[0]]);
  130. }
  131. });
  132. } else if (!never && !hasWhitespace) {
  133. context.report({
  134. node,
  135. loc: {
  136. start: {
  137. line: leftToken.loc.end.line,
  138. column: leftToken.loc.end.column - 1
  139. },
  140. end: rightToken.loc.start
  141. },
  142. messageId: "missing",
  143. fix(fixer) {
  144. if (node.optional) {
  145. return null; // Not sure if inserting a space to either before/after `?.` token.
  146. }
  147. return fixer.insertTextBefore(rightToken, " ");
  148. }
  149. });
  150. } else if (!never && !allowNewlines && hasNewline) {
  151. context.report({
  152. node,
  153. loc: {
  154. start: leftToken.loc.end,
  155. end: rightToken.loc.start
  156. },
  157. messageId: "unexpectedNewline",
  158. fix(fixer) {
  159. /*
  160. * Only autofix if there is no newline
  161. * https://github.com/eslint/eslint/issues/7787
  162. * But if `?.` exists, it doesn't hide no-unexpected-multiline errors
  163. */
  164. if (!node.optional) {
  165. return null;
  166. }
  167. // Don't remove comments.
  168. if (sourceCode.commentsExistBetween(leftToken, rightToken)) {
  169. return null;
  170. }
  171. const range = [leftToken.range[1], rightToken.range[0]];
  172. const qdToken = sourceCode.getTokenAfter(leftToken);
  173. if (qdToken.range[0] === leftToken.range[1]) {
  174. return fixer.replaceTextRange(range, "?. ");
  175. }
  176. if (qdToken.range[1] === rightToken.range[0]) {
  177. return fixer.replaceTextRange(range, " ?.");
  178. }
  179. return fixer.replaceTextRange(range, " ?. ");
  180. }
  181. });
  182. }
  183. }
  184. return {
  185. "CallExpression, NewExpression"(node) {
  186. const lastToken = sourceCode.getLastToken(node);
  187. const lastCalleeToken = sourceCode.getLastToken(node.callee);
  188. const parenToken = sourceCode.getFirstTokenBetween(lastCalleeToken, lastToken, astUtils.isOpeningParenToken);
  189. const prevToken = parenToken && sourceCode.getTokenBefore(parenToken, astUtils.isNotQuestionDotToken);
  190. // Parens in NewExpression are optional
  191. if (!(parenToken && parenToken.range[1] < node.range[1])) {
  192. return;
  193. }
  194. checkSpacing(node, prevToken, parenToken);
  195. },
  196. ImportExpression(node) {
  197. const leftToken = sourceCode.getFirstToken(node);
  198. const rightToken = sourceCode.getTokenAfter(leftToken);
  199. checkSpacing(node, leftToken, rightToken);
  200. }
  201. };
  202. }
  203. };