no-subclassing-builtins.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 { READ, ReferenceTracker } = require("eslint-utils")
  7. module.exports = {
  8. meta: {
  9. docs: {
  10. description: "disallow the subclassing of the built-in classes.",
  11. category: "ES2015",
  12. recommended: false,
  13. url:
  14. "http://mysticatea.github.io/eslint-plugin-es/rules/no-subclassing-builtins.html",
  15. },
  16. fixable: null,
  17. messages: {
  18. forbidden: "ES2015 subclassing of '{{name}}' is forbidden.",
  19. },
  20. schema: [],
  21. type: "problem",
  22. },
  23. create(context) {
  24. return {
  25. "Program:exit"() {
  26. const tracker = new ReferenceTracker(context.getScope())
  27. for (const { node, path } of tracker.iterateGlobalReferences({
  28. Array: { [READ]: true },
  29. Boolean: { [READ]: true },
  30. Error: { [READ]: true },
  31. RegExp: { [READ]: true },
  32. Function: { [READ]: true },
  33. Map: { [READ]: true },
  34. Number: { [READ]: true },
  35. Promise: { [READ]: true },
  36. Set: { [READ]: true },
  37. String: { [READ]: true },
  38. })) {
  39. if (
  40. node.parent.type.startsWith("Class") &&
  41. node.parent.superClass === node
  42. ) {
  43. context.report({
  44. node,
  45. messageId: "forbidden",
  46. data: { name: path.join(".") },
  47. })
  48. }
  49. }
  50. },
  51. }
  52. },
  53. }