no-keyword-properties.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * @author Toru Nagashima <https://github.com/mysticatea>
  3. * See LICENSE file in root directory for full license.
  4. */
  5. "use strict"
  6. // https://www-archive.mozilla.org/js/language/E262-3.pdf
  7. const keywords = new Set([
  8. "abstract",
  9. "boolean",
  10. "break",
  11. "byte",
  12. "case",
  13. "catch",
  14. "char",
  15. "class",
  16. "const",
  17. "continue",
  18. "debugger",
  19. "default",
  20. "delete",
  21. "do",
  22. "double",
  23. "else",
  24. "enum",
  25. "export",
  26. "extends",
  27. "false",
  28. "final",
  29. "finally",
  30. "float",
  31. "for",
  32. "function",
  33. "goto",
  34. "if",
  35. "implements",
  36. "import",
  37. "in",
  38. "instanceof",
  39. "int",
  40. "interface",
  41. "long",
  42. "native",
  43. "new",
  44. "null",
  45. "package",
  46. "private",
  47. "protected",
  48. "public",
  49. "return",
  50. "short",
  51. "static",
  52. "super",
  53. "switch",
  54. "synchronized",
  55. "this",
  56. "throw",
  57. "throws",
  58. "transient",
  59. "true",
  60. "try",
  61. "typeof",
  62. "var",
  63. "void",
  64. "volatile",
  65. "while",
  66. "with",
  67. ])
  68. module.exports = {
  69. meta: {
  70. docs: {
  71. description: "disallow reserved words as property names.",
  72. category: "ES5",
  73. recommended: false,
  74. url:
  75. "http://mysticatea.github.io/eslint-plugin-es/rules/no-keyword-properties.html",
  76. },
  77. fixable: null,
  78. messages: {
  79. forbidden: "ES5 reserved words as property names are forbidden.",
  80. },
  81. schema: [],
  82. type: "problem",
  83. },
  84. create(context) {
  85. return {
  86. Property(node) {
  87. if (
  88. !node.computed &&
  89. node.key.type === "Identifier" &&
  90. keywords.has(node.key.name)
  91. ) {
  92. context.report({ node, messageId: "forbidden" })
  93. }
  94. },
  95. MemberExpression(node) {
  96. if (
  97. !node.computed &&
  98. node.property.type === "Identifier" &&
  99. keywords.has(node.property.name)
  100. ) {
  101. context.report({ node, messageId: "forbidden" })
  102. }
  103. },
  104. }
  105. },
  106. }