Skip to content

Commit 1e67d40

Browse files
committed
refactor(linter/plugins): move scope-related SourceCode methods into separate file
1 parent 3781b22 commit 1e67d40

File tree

2 files changed

+53
-42
lines changed

2 files changed

+53
-42
lines changed

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/*
2+
* `SourceCode` methods related to scopes.
3+
*/
4+
15
import type * as ESTree from '../generated/types.d.ts';
26

37
import type { Node } from './types.ts';
@@ -82,3 +86,45 @@ export type DefinitionType =
8286
| 'ImportBinding'
8387
| 'Parameter'
8488
| 'Variable';
89+
90+
/**
91+
* Determine whether the given identifier node is a reference to a global variable.
92+
* @param node - `Identifier` node to check.
93+
* @returns `true` if the identifier is a reference to a global variable.
94+
*/
95+
// oxlint-disable-next-line no-unused-vars
96+
export function isGlobalReference(node: Node): boolean {
97+
throw new Error('`sourceCode.isGlobalReference` not implemented yet'); // TODO
98+
}
99+
100+
/**
101+
* Get the variables that `node` defines.
102+
* This is a convenience method that passes through to the same method on the `scopeManager`.
103+
* @param node - The node for which the variables are obtained.
104+
* @returns An array of variable nodes representing the variables that `node` defines.
105+
*/
106+
// oxlint-disable-next-line no-unused-vars
107+
export function getDeclaredVariables(node: Node): Variable[] {
108+
throw new Error('`sourceCode.getDeclaredVariables` not implemented yet'); // TODO
109+
}
110+
111+
/**
112+
* Get the scope for the given node
113+
* @param node - The node to get the scope of.
114+
* @returns The scope information for this node.
115+
*/
116+
// oxlint-disable-next-line no-unused-vars
117+
export function getScope(node: Node): Scope {
118+
throw new Error('`sourceCode.getScope` not implemented yet'); // TODO
119+
}
120+
121+
/**
122+
* Mark a variable as used in the current scope
123+
* @param name - The name of the variable to mark as used.
124+
* @param refNode? - The closest node to the variable reference.
125+
* @returns `true` if the variable was found and marked as used, `false` if not.
126+
*/
127+
// oxlint-disable-next-line no-unused-vars
128+
export function markVariableAsUsed(name: string, refNode: Node): boolean {
129+
throw new Error('`sourceCode.markVariableAsUsed` not implemented yet'); // TODO
130+
}

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

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ import {
2121
lines,
2222
resetLines,
2323
} from './location.js';
24+
import * as scopeMethods from './scope.js';
2425
import * as tokenMethods from './tokens.js';
2526

2627
import type { Program } from '../generated/types.d.ts';
27-
import type { Scope, ScopeManager, Variable } from './scope.ts';
28+
import type { ScopeManager } from './scope.ts';
2829
import type { BufferWithArrays, Node, NodeOrToken, Ranged } from './types.ts';
2930

3031
const { max } = Math;
@@ -182,15 +183,11 @@ export const SOURCE_CODE = Object.freeze({
182183
throw new Error('`sourceCode.isSpaceBetween` not implemented yet'); // TODO
183184
},
184185

185-
/**
186-
* Determine whether the given identifier node is a reference to a global variable.
187-
* @param node - `Identifier` node to check.
188-
* @returns `true` if the identifier is a reference to a global variable.
189-
*/
190-
// oxlint-disable-next-line no-unused-vars
191-
isGlobalReference(node: Node): boolean {
192-
throw new Error('`sourceCode.isGlobalReference` not implemented yet'); // TODO
193-
},
186+
// Scope methods
187+
isGlobalReference: scopeMethods.isGlobalReference,
188+
getDeclaredVariables: scopeMethods.getDeclaredVariables,
189+
getScope: scopeMethods.getScope,
190+
markVariableAsUsed: scopeMethods.markVariableAsUsed,
194191

195192
// Token methods
196193
getTokens: tokenMethods.getTokens,
@@ -223,38 +220,6 @@ export const SOURCE_CODE = Object.freeze({
223220
getIndexFromLoc: getOffsetFromLineColumn,
224221

225222
getAncestors,
226-
227-
/**
228-
* Get the variables that `node` defines.
229-
* This is a convenience method that passes through to the same method on the `scopeManager`.
230-
* @param node - The node for which the variables are obtained.
231-
* @returns An array of variable nodes representing the variables that `node` defines.
232-
*/
233-
// oxlint-disable-next-line no-unused-vars
234-
getDeclaredVariables(node: Node): Variable[] {
235-
throw new Error('`sourceCode.getDeclaredVariables` not implemented yet'); // TODO
236-
},
237-
238-
/**
239-
* Get the scope for the given node
240-
* @param node - The node to get the scope of.
241-
* @returns The scope information for this node.
242-
*/
243-
// oxlint-disable-next-line no-unused-vars
244-
getScope(node: Node): Scope {
245-
throw new Error('`sourceCode.getScope` not implemented yet'); // TODO
246-
},
247-
248-
/**
249-
* Mark a variable as used in the current scope
250-
* @param name - The name of the variable to mark as used.
251-
* @param refNode? - The closest node to the variable reference.
252-
* @returns `true` if the variable was found and marked as used, `false` if not.
253-
*/
254-
// oxlint-disable-next-line no-unused-vars
255-
markVariableAsUsed(name: string, refNode: Node): boolean {
256-
throw new Error('`sourceCode.markVariableAsUsed` not implemented yet'); // TODO
257-
},
258223
});
259224

260225
export type SourceCode = typeof SOURCE_CODE;

0 commit comments

Comments
 (0)