no-spaced-func.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * @fileoverview Rule to check that spaced function application
  3. * @author Matt DuVall <http://www.mattduvall.com>
  4. * @deprecated in ESLint v3.3.0
  5. */
  6. "use strict";
  7. //------------------------------------------------------------------------------
  8. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. /** @type {import('../shared/types').Rule} */
  11. module.exports = {
  12. meta: {
  13. type: "layout",
  14. docs: {
  15. description: "Disallow spacing between function identifiers and their applications (deprecated)",
  16. recommended: false,
  17. url: "https://eslint.org/docs/rules/no-spaced-func"
  18. },
  19. deprecated: true,
  20. replacedBy: ["func-call-spacing"],
  21. fixable: "whitespace",
  22. schema: [],
  23. messages: {
  24. noSpacedFunction: "Unexpected space between function name and paren."
  25. }
  26. },
  27. create(context) {
  28. const sourceCode = context.getSourceCode();
  29. /**
  30. * Check if open space is present in a function name
  31. * @param {ASTNode} node node to evaluate
  32. * @returns {void}
  33. * @private
  34. */
  35. function detectOpenSpaces(node) {
  36. const lastCalleeToken = sourceCode.getLastToken(node.callee);
  37. let prevToken = lastCalleeToken,
  38. parenToken = sourceCode.getTokenAfter(lastCalleeToken);
  39. // advances to an open parenthesis.
  40. while (
  41. parenToken &&
  42. parenToken.range[1] < node.range[1] &&
  43. parenToken.value !== "("
  44. ) {
  45. prevToken = parenToken;
  46. parenToken = sourceCode.getTokenAfter(parenToken);
  47. }
  48. // look for a space between the callee and the open paren
  49. if (parenToken &&
  50. parenToken.range[1] < node.range[1] &&
  51. sourceCode.isSpaceBetweenTokens(prevToken, parenToken)
  52. ) {
  53. context.report({
  54. node,
  55. loc: lastCalleeToken.loc.start,
  56. messageId: "noSpacedFunction",
  57. fix(fixer) {
  58. return fixer.removeRange([prevToken.range[1], parenToken.range[0]]);
  59. }
  60. });
  61. }
  62. }
  63. return {
  64. CallExpression: detectOpenSpaces,
  65. NewExpression: detectOpenSpaces
  66. };
  67. }
  68. };