UNPKG

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