no-subclassing-builtins.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. schema: [],
  18. messages: {
  19. forbidden: "ES2015 subclassing of '{{name}}' is forbidden.",
  20. },
  21. },
  22. create(context) {
  23. return {
  24. "Program:exit"() {
  25. const tracker = new ReferenceTracker(context.getScope())
  26. for (const { node, path } of tracker.iterateGlobalReferences({
  27. Array: { [READ]: true },
  28. Boolean: { [READ]: true },
  29. Error: { [READ]: true },
  30. RegExp: { [READ]: true },
  31. Function: { [READ]: true },
  32. Map: { [READ]: true },
  33. Number: { [READ]: true },
  34. Promise: { [READ]: true },
  35. Set: { [READ]: true },
  36. String: { [READ]: true },
  37. })) {
  38. if (
  39. node.parent.type.startsWith("Class") &&
  40. node.parent.superClass === node
  41. ) {
  42. context.report({
  43. node,
  44. messageId: "forbidden",
  45. data: { name: path.join(".") },
  46. })
  47. }
  48. }
  49. },
  50. }
  51. },
  52. }