no-trailing-function-commas.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * @author Toru Nagashima <https://github.com/mysticatea>
  3. * See LICENSE file in root directory for full license.
  4. */
  5. "use strict"
  6. const { isCommaToken } = require("../utils")
  7. module.exports = {
  8. meta: {
  9. docs: {
  10. description:
  11. "disallow trailing commas in parameter/argument lists.",
  12. category: "ES2017",
  13. recommended: false,
  14. url:
  15. "http://mysticatea.github.io/eslint-plugin-es/rules/no-trailing-function-commas.html",
  16. },
  17. fixable: "code",
  18. messages: {
  19. forbidden:
  20. "ES2017 trailing commas in parameter/argument lists are forbidden.",
  21. },
  22. schema: [],
  23. type: "problem",
  24. },
  25. create(context) {
  26. const sourceCode = context.getSourceCode()
  27. return {
  28. ":function"(node) {
  29. const length = node.params.length
  30. if (length === 0) {
  31. return
  32. }
  33. const lastParam = node.params[length - 1]
  34. const token = sourceCode.getTokenAfter(lastParam)
  35. if (isCommaToken(token)) {
  36. context.report({
  37. loc: token.loc,
  38. messageId: "forbidden",
  39. fix(fixer) {
  40. return fixer.remove(token)
  41. },
  42. })
  43. }
  44. },
  45. "CallExpression, NewExpression"(node) {
  46. const token = sourceCode.getLastToken(node, 1)
  47. if (node.arguments.length >= 1 && isCommaToken(token)) {
  48. context.report({
  49. loc: token.loc,
  50. messageId: "forbidden",
  51. fix(fixer) {
  52. return fixer.remove(token)
  53. },
  54. })
  55. }
  56. },
  57. }
  58. },
  59. }