no-trailing-commas.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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: "disallow trailing commas in array/object literals.",
  11. category: "ES5",
  12. recommended: false,
  13. url:
  14. "http://mysticatea.github.io/eslint-plugin-es/rules/no-trailing-commas.html",
  15. },
  16. fixable: null,
  17. messages: {
  18. forbidden:
  19. "ES5 trailing commas in array/object literals are forbidden.",
  20. },
  21. schema: [],
  22. type: "problem",
  23. },
  24. create(context) {
  25. const sourceCode = context.getSourceCode()
  26. return {
  27. "ArrayExpression, ArrayPattern, ObjectExpression, ObjectPattern"(
  28. node
  29. ) {
  30. const token = sourceCode.getLastToken(node, 1)
  31. if (isCommaToken(token)) {
  32. context.report({ node, messageId: "forbidden" })
  33. }
  34. },
  35. }
  36. },
  37. }