no-native.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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(function(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. docs: {
  19. url: getDocsUrl('no-native')
  20. }
  21. },
  22. create: function(context) {
  23. const MESSAGE = '"{{name}}" is not defined.'
  24. /**
  25. * Checks for and reports reassigned constants
  26. *
  27. * @param {Scope} scope - an escope Scope object
  28. * @returns {void}
  29. * @private
  30. */
  31. return {
  32. 'Program:exit': function() {
  33. const scope = context.getScope()
  34. scope.implicit.left.forEach(function(ref) {
  35. if (ref.identifier.name !== 'Promise') {
  36. return
  37. }
  38. if (!isDeclared(scope, ref)) {
  39. context.report({
  40. node: ref.identifier,
  41. message: MESSAGE,
  42. data: { name: ref.identifier.name }
  43. })
  44. }
  45. })
  46. }
  47. }
  48. }
  49. }