no-native.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. // Presumably can't pass this since the implicit `Promise` global
  11. // being checked here would always lack `defs`
  12. // istanbul ignore else
  13. if (!variable.defs || !variable.defs.length) {
  14. return false
  15. }
  16. // istanbul ignore next
  17. return true
  18. })
  19. }
  20. module.exports = {
  21. meta: {
  22. type: 'suggestion',
  23. docs: {
  24. url: getDocsUrl('no-native'),
  25. },
  26. messages: {
  27. name: '"{{name}}" is not defined.',
  28. },
  29. },
  30. create(context) {
  31. /**
  32. * Checks for and reports reassigned constants
  33. *
  34. * @param {Scope} scope - an escope Scope object
  35. * @returns {void}
  36. * @private
  37. */
  38. return {
  39. 'Program:exit'() {
  40. const scope = context.getScope()
  41. scope.implicit.left.forEach((ref) => {
  42. if (ref.identifier.name !== 'Promise') {
  43. return
  44. }
  45. // istanbul ignore else
  46. if (!isDeclared(scope, ref)) {
  47. context.report({
  48. node: ref.identifier,
  49. messageId: 'name',
  50. data: { name: ref.identifier.name },
  51. })
  52. }
  53. })
  54. },
  55. }
  56. },
  57. }