Skip to content

Commit 1230ced

Browse files
authored
fix(no-native): fix to report Promise usage in eslint v9 or later (#619)
1 parent 6cf7adb commit 1230ced

3 files changed

Lines changed: 37 additions & 15 deletions

File tree

__tests__/no-native.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ for (const ruleTester of ruleTesters) {
5353
errors: [{ message: '"Promise" is not defined.' }],
5454
globals: { Promise: true },
5555
},
56+
{
57+
code: 'Promise.resolve()',
58+
errors: [{ message: '"Promise" is not defined.' }],
59+
globals: { Promise: 'off' },
60+
},
5661
],
5762
})
5863
}

package-lock.json

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rules/no-native.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
'use strict'
55

6-
const { getScope } = require('./lib/eslint-compat')
6+
const { getSourceCode } = require('./lib/eslint-compat')
77
const getDocsUrl = require('./lib/get-docs-url')
88

99
function isDeclared(scope, ref) {
@@ -46,22 +46,24 @@ module.exports = {
4646
* @private
4747
*/
4848
return {
49-
'Program:exit'(node) {
50-
const scope = getScope(context, node)
51-
const leftToBeResolved =
52-
scope.implicit.left ||
53-
/**
54-
* Fixes https://github.com/eslint-community/eslint-plugin-promise/issues/205.
55-
* The problem was that @typescript-eslint has a scope manager
56-
* which has `leftToBeResolved` instead of the default `left`.
57-
*/
58-
scope.implicit.leftToBeResolved
49+
'Program:exit'() {
50+
const sourceCode = getSourceCode(context)
51+
/** @type {import('eslint').Scope.Scope} */
52+
const scope = sourceCode.scopeManager.globalScope
5953

60-
leftToBeResolved.forEach((ref) => {
54+
for (const variable of scope.variables) {
55+
if (variable.name !== 'Promise') {
56+
continue
57+
}
58+
variable.references.forEach(validatePromiseReference)
59+
}
60+
for (const ref of scope.through) {
6161
if (ref.identifier.name !== 'Promise') {
62-
return
62+
continue
6363
}
64-
64+
validatePromiseReference(ref)
65+
}
66+
function validatePromiseReference(ref) {
6567
// istanbul ignore else
6668
if (!isDeclared(scope, ref)) {
6769
context.report({
@@ -70,7 +72,7 @@ module.exports = {
7072
data: { name: ref.identifier.name },
7173
})
7274
}
73-
})
75+
}
7476
},
7577
}
7678
},

0 commit comments

Comments
 (0)