Skip to content

Commit 636e7ed

Browse files
committed
refactor(linter/plugins): shorten ScopeManager code (#15335)
Follow on after #14890. Pure refactor. Just shorten code but compacting `if` statements to a single line. Also move type import to after "real" imports.
1 parent 8b31daa commit 636e7ed

File tree

1 file changed

+8
-21
lines changed

1 file changed

+8
-21
lines changed

apps/oxlint/src-js/plugins/scope.ts

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
* `SourceCode` methods related to scopes.
33
*/
44

5-
import type * as ESTree from '../generated/types.d.ts';
6-
75
import {
86
analyze,
97
type AnalyzeOptions,
@@ -12,6 +10,8 @@ import {
1210
import { ast, initAst } from './source_code.js';
1311
import { assertIs } from './utils.js';
1412

13+
import type * as ESTree from '../generated/types.d.ts';
14+
1515
type Identifier =
1616
| ESTree.IdentifierName
1717
| ESTree.IdentifierReference
@@ -198,19 +198,12 @@ export type DefinitionType =
198198
*/
199199
export function isGlobalReference(node: ESTree.Node): boolean {
200200
// ref: https://github.com/eslint/eslint/blob/e7cda3bdf1bdd664e6033503a3315ad81736b200/lib/languages/js/source-code/source-code.js#L934-L962
201-
if (!node) {
202-
throw new TypeError('Missing required argument: node.');
203-
}
204-
205-
if (node.type !== 'Identifier') {
206-
return false;
207-
}
201+
if (!node) throw new TypeError('Missing required argument: node.');
202+
if (node.type !== 'Identifier') return false;
208203

209204
const { name } = node;
210205
// TODO: Is this check required? Isn't an `Identifier`'s `name` property always a string?
211-
if (typeof name !== 'string') {
212-
return false;
213-
}
206+
if (typeof name !== 'string') return false;
214207

215208
if (tsScopeManager === null) initTsScopeManager();
216209

@@ -222,17 +215,13 @@ export function isGlobalReference(node: ESTree.Node): boolean {
222215
const variable = globalScope.set.get(name);
223216

224217
// Global variables are not defined by any node, so they should have no definitions
225-
if (variable === undefined || variable.defs.length > 0) {
226-
return false;
227-
}
218+
if (variable === undefined || variable.defs.length > 0) return false;
228219

229220
// If there is a variable by the same name exists in the global scope,
230221
// we need to check our node is one of its references
231222
const { references } = variable;
232223
for (let i = 0, len = references.length; i < len; i++) {
233-
if (references[i].identifier === node) {
234-
return true;
235-
}
224+
if (references[i].identifier === node) return true;
236225
}
237226

238227
return false;
@@ -258,9 +247,7 @@ export function getDeclaredVariables(node: ESTree.Node): Variable[] {
258247
*/
259248
export function getScope(node: ESTree.Node): Scope {
260249
// ref: https://github.com/eslint/eslint/blob/e7cda3bdf1bdd664e6033503a3315ad81736b200/lib/languages/js/source-code/source-code.js#L862-L892
261-
if (!node) {
262-
throw new TypeError('Missing required argument: node.');
263-
}
250+
if (!node) throw new TypeError('Missing required argument: `node`');
264251

265252
if (tsScopeManager === null) initTsScopeManager();
266253

0 commit comments

Comments
 (0)