no-native.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. schema: [],
  30. },
  31. create(context) {
  32. /**
  33. * Checks for and reports reassigned constants
  34. *
  35. * @param {Scope} scope - an eslint-scope Scope object
  36. * @returns {void}
  37. * @private
  38. */
  39. return {
  40. 'Program:exit'() {
  41. const scope = context.getScope()
  42. const leftToBeResolved =
  43. scope.implicit.left ||
  44. /**
  45. * Fixes https://github.com/eslint-community/eslint-plugin-promise/issues/205.
  46. * The problem was that @typescript-eslint has a scope manager
  47. * which has `leftToBeResolved` instead of the default `left`.
  48. */
  49. scope.implicit.leftToBeResolved
  50. leftToBeResolved.forEach((ref) => {
  51. if (ref.identifier.name !== 'Promise') {
  52. return
  53. }
  54. // istanbul ignore else
  55. if (!isDeclared(scope, ref)) {
  56. context.report({
  57. node: ref.identifier,
  58. messageId: 'name',
  59. data: { name: ref.identifier.name },
  60. })
  61. }
  62. })
  63. },
  64. }
  65. },
  66. }