UNPKG

1.83 kBJavaScriptView Raw
1// Borrowed from here:
2// https://github.com/colonyamerican/eslint-plugin-cah/issues/3
3
4'use strict'
5
6const getDocsUrl = require('./lib/get-docs-url')
7
8function isDeclared(scope, ref) {
9 return scope.variables.some((variable) => {
10 if (variable.name !== ref.identifier.name) {
11 return false
12 }
13
14 // Presumably can't pass this since the implicit `Promise` global
15 // being checked here would always lack `defs`
16 // istanbul ignore else
17 if (!variable.defs || !variable.defs.length) {
18 return false
19 }
20
21 // istanbul ignore next
22 return true
23 })
24}
25
26module.exports = {
27 meta: {
28 type: 'suggestion',
29 docs: {
30 url: getDocsUrl('no-native'),
31 },
32 messages: {
33 name: '"{{name}}" is not defined.',
34 },
35 schema: [],
36 },
37 create(context) {
38 /**
39 * Checks for and reports reassigned constants
40 *
41 * @param {Scope} scope - an eslint-scope Scope object
42 * @returns {void}
43 * @private
44 */
45 return {
46 'Program:exit'() {
47 const scope = context.getScope()
48 const leftToBeResolved =
49 scope.implicit.left ||
50 /**
51 * Fixes https://github.com/eslint-community/eslint-plugin-promise/issues/205.
52 * The problem was that @typescript-eslint has a scope manager
53 * which has `leftToBeResolved` instead of the default `left`.
54 */
55 scope.implicit.leftToBeResolved
56
57 leftToBeResolved.forEach((ref) => {
58 if (ref.identifier.name !== 'Promise') {
59 return
60 }
61
62 // istanbul ignore else
63 if (!isDeclared(scope, ref)) {
64 context.report({
65 node: ref.identifier,
66 messageId: 'name',
67 data: { name: ref.identifier.name },
68 })
69 }
70 })
71 },
72 }
73 },
74}