项目原始demo,不改动
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
このリポジトリはアーカイブされています。 ファイルの閲覧とクローンは可能ですが、プッシュや、課題・プルリクエストのオープンはできません。
 
 
 
 

161 行
6.6 KiB

  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("../ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Rule Definition
  12. //------------------------------------------------------------------------------
  13. module.exports = {
  14. meta: {
  15. docs: {
  16. description: "require or disallow spacing between function identifiers and their invocations",
  17. category: "Stylistic Issues",
  18. recommended: false,
  19. url: "https://eslint.org/docs/rules/func-call-spacing"
  20. },
  21. fixable: "whitespace",
  22. schema: {
  23. anyOf: [
  24. {
  25. type: "array",
  26. items: [
  27. {
  28. enum: ["never"]
  29. }
  30. ],
  31. minItems: 0,
  32. maxItems: 1
  33. },
  34. {
  35. type: "array",
  36. items: [
  37. {
  38. enum: ["always"]
  39. },
  40. {
  41. type: "object",
  42. properties: {
  43. allowNewlines: {
  44. type: "boolean"
  45. }
  46. },
  47. additionalProperties: false
  48. }
  49. ],
  50. minItems: 0,
  51. maxItems: 2
  52. }
  53. ]
  54. }
  55. },
  56. create(context) {
  57. const never = context.options[0] !== "always";
  58. const allowNewlines = !never && context.options[1] && context.options[1].allowNewlines;
  59. const sourceCode = context.getSourceCode();
  60. const text = sourceCode.getText();
  61. /**
  62. * Check if open space is present in a function name
  63. * @param {ASTNode} node node to evaluate
  64. * @returns {void}
  65. * @private
  66. */
  67. function checkSpacing(node) {
  68. const lastToken = sourceCode.getLastToken(node);
  69. const lastCalleeToken = sourceCode.getLastToken(node.callee);
  70. const parenToken = sourceCode.getFirstTokenBetween(lastCalleeToken, lastToken, astUtils.isOpeningParenToken);
  71. const prevToken = parenToken && sourceCode.getTokenBefore(parenToken);
  72. // Parens in NewExpression are optional
  73. if (!(parenToken && parenToken.range[1] < node.range[1])) {
  74. return;
  75. }
  76. const textBetweenTokens = text.slice(prevToken.range[1], parenToken.range[0]).replace(/\/\*.*?\*\//g, "");
  77. const hasWhitespace = /\s/.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: lastCalleeToken.loc.start,
  106. message: "Unexpected space between function name and paren.",
  107. fix(fixer) {
  108. /*
  109. * Only autofix if there is no newline
  110. * https://github.com/eslint/eslint/issues/7787
  111. */
  112. if (!hasNewline) {
  113. return fixer.removeRange([prevToken.range[1], parenToken.range[0]]);
  114. }
  115. return null;
  116. }
  117. });
  118. } else if (!never && !hasWhitespace) {
  119. context.report({
  120. node,
  121. loc: lastCalleeToken.loc.start,
  122. message: "Missing space between function name and paren.",
  123. fix(fixer) {
  124. return fixer.insertTextBefore(parenToken, " ");
  125. }
  126. });
  127. } else if (!never && !allowNewlines && hasNewline) {
  128. context.report({
  129. node,
  130. loc: lastCalleeToken.loc.start,
  131. message: "Unexpected newline between function name and paren.",
  132. fix(fixer) {
  133. return fixer.replaceTextRange([prevToken.range[1], parenToken.range[0]], " ");
  134. }
  135. });
  136. }
  137. }
  138. return {
  139. CallExpression: checkSpacing,
  140. NewExpression: checkSpacing
  141. };
  142. }
  143. };