no-native.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Borrowed from here:
  2. // https://github.com/colonyamerican/eslint-plugin-cah/issues/3
  3. 'use strict'
  4. const getDocsUrl = require('./lib/get-docs-url')
  5. function isDeclared(scope, ref) {
  6. return scope.variables.some((variable) => {
  7. if (variable.name !== ref.identifier.name) {
  8. return false
  9. }
  10. if (!variable.defs || !variable.defs.length) {
  11. return false
  12. }
  13. return true
  14. })
  15. }
  16. module.exports = {
  17. meta: {
  18. type: 'suggestion',
  19. docs: {
  20. url: getDocsUrl('no-native'),
  21. },
  22. messages: {
  23. name: '"{{name}}" is not defined.',
  24. },
  25. },
  26. create(context) {
  27. /**
  28. * Checks for and reports reassigned constants
  29. *
  30. * @param {Scope} scope - an escope Scope object
  31. * @returns {void}
  32. * @private
  33. */
  34. return {
  35. 'Program:exit'() {
  36. const scope = context.getScope()
  37. scope.implicit.left.forEach((ref) => {
  38. if (ref.identifier.name !== 'Promise') {
  39. return
  40. }
  41. if (!isDeclared(scope, ref)) {
  42. context.report({
  43. node: ref.identifier,
  44. messageId: 'name',
  45. data: { name: ref.identifier.name },
  46. })
  47. }
  48. })
  49. },
  50. }
  51. },
  52. }