no-trailing-function-commas.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. schema: [],
  19. messages: {
  20. forbidden:
  21. "ES2017 trailing commas in parameter/argument lists are forbidden.",
  22. },
  23. },
  24. create(context) {
  25. const sourceCode = context.getSourceCode()
  26. return {
  27. ":function"(node) {
  28. const length = node.params.length
  29. if (length === 0) {
  30. return
  31. }
  32. const lastParam = node.params[length - 1]
  33. const token = sourceCode.getTokenAfter(lastParam)
  34. if (isCommaToken(token)) {
  35. context.report({
  36. loc: token.loc,
  37. messageId: "forbidden",
  38. fix(fixer) {
  39. return fixer.remove(token)
  40. },
  41. })
  42. }
  43. },
  44. "CallExpression, NewExpression"(node) {
  45. const token = sourceCode.getLastToken(node, 1)
  46. if (node.arguments.length >= 1 && isCommaToken(token)) {
  47. context.report({
  48. loc: token.loc,
  49. messageId: "forbidden",
  50. fix(fixer) {
  51. return fixer.remove(token)
  52. },
  53. })
  54. }
  55. },
  56. }
  57. },
  58. }