Skip to content

Commit 2263fb6

Browse files
committed
refactor: replace usages of context.getScope
1 parent 837281e commit 2263fb6

File tree

6 files changed

+16
-10
lines changed

6 files changed

+16
-10
lines changed

src/rules/newline-after-import.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import isStaticRequire from '../core/staticRequire';
77
import docsUrl from '../docsUrl';
88

99
import debug from 'debug';
10-
import { getFilename } from '../context';
10+
import { getFilename, getScope } from '../context';
1111
const log = debug('eslint-plugin-import:rules:newline-after-import');
1212

1313
//------------------------------------------------------------------------------
@@ -195,10 +195,12 @@ module.exports = {
195195
},
196196
'Program:exit'() {
197197
log('exit processing for', context.getPhysicalFilename ? context.getPhysicalFilename() : getFilename(context));
198-
const scopeBody = getScopeBody(context.getScope());
199-
log('got scope:', scopeBody);
200198

201199
requireCalls.forEach((node, index) => {
200+
// todo: this probably isn't correct...
201+
const scopeBody = getScopeBody(getScope(context, node));
202+
log('got scope:', scopeBody);
203+
202204
const nodePosition = findNodeIndexInScopeBody(scopeBody, node);
203205
log('node position in scope:', nodePosition);
204206

src/rules/no-amd.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
import docsUrl from '../docsUrl';
7+
import { getScope } from '../context';
78

89
//------------------------------------------------------------------------------
910
// Rule Definition
@@ -23,7 +24,7 @@ module.exports = {
2324
create(context) {
2425
return {
2526
CallExpression(node) {
26-
if (context.getScope().type !== 'module') { return; }
27+
if (getScope(context, node).type !== 'module') { return; }
2728

2829
if (node.callee.type !== 'Identifier') { return; }
2930
if (node.callee.name !== 'require' && node.callee.name !== 'define') { return; }

src/rules/no-commonjs.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
import docsUrl from '../docsUrl';
7+
import { getScope } from '../context';
78

89
const EXPORT_MESSAGE = 'Expected "export" or "export default"';
910
const IMPORT_MESSAGE = 'Expected "import" instead of "require()"';
@@ -107,7 +108,7 @@ module.exports = {
107108

108109
// exports.
109110
if (node.object.name === 'exports') {
110-
const isInScope = context.getScope()
111+
const isInScope = getScope(context, node)
111112
.variables
112113
.some((variable) => variable.name === 'exports');
113114
if (!isInScope) {
@@ -117,7 +118,7 @@ module.exports = {
117118

118119
},
119120
CallExpression(call) {
120-
if (!validateScope(context.getScope())) { return; }
121+
if (!validateScope(getScope(context, call))) { return; }
121122

122123
if (call.callee.type !== 'Identifier') { return; }
123124
if (call.callee.name !== 'require') { return; }

src/rules/no-mutable-exports.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import docsUrl from '../docsUrl';
2+
import { getScope } from '../context';
23

34
module.exports = {
45
meta: {
@@ -32,15 +33,15 @@ module.exports = {
3233
}
3334

3435
function handleExportDefault(node) {
35-
const scope = context.getScope();
36+
const scope = getScope(context, node);
3637

3738
if (node.declaration.name) {
3839
checkDeclarationsInScope(scope, node.declaration.name);
3940
}
4041
}
4142

4243
function handleExportNamed(node) {
43-
const scope = context.getScope();
44+
const scope = getScope(context, node);
4445

4546
if (node.declaration) {
4647
checkDeclaration(node.declaration);

src/rules/no-namespace.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import minimatch from 'minimatch';
77
import docsUrl from '../docsUrl';
8-
import { getSourceCode } from '../context';
8+
import { getScope, getSourceCode } from '../context';
99

1010
/**
1111
* @param {MemberExpression} memberExpression
@@ -109,7 +109,7 @@ module.exports = {
109109
return;
110110
}
111111

112-
const scopeVariables = context.getScope().variables;
112+
const scopeVariables = getScope(context, node).variables;
113113
const namespaceVariable = scopeVariables.find((variable) => variable.defs[0].node === node);
114114
const namespaceReferences = namespaceVariable.references;
115115
const namespaceIdentifiers = namespaceReferences.map((reference) => reference.identifier);

utils/declaredScope.js

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ exports.__esModule = true;
44

55
/** @type {import('./declaredScope').default} */
66
exports.default = function declaredScope(context, name) {
7+
// todo: what should be done about this...
78
const references = context.getScope().references;
89
const reference = references.find((x) => x.identifier.name === name);
910
if (!reference || !reference.resolved) { return undefined; }

0 commit comments

Comments
 (0)