Skip to content

Commit 17ea7ae

Browse files
committed
update
1 parent 88134b2 commit 17ea7ae

File tree

4 files changed

+37
-20
lines changed

4 files changed

+37
-20
lines changed

lib/rules/jsx-uses-vars.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ SOFTWARE.
3030
*/
3131
'use strict'
3232

33+
const utils = require('../utils')
34+
3335
module.exports = {
3436
// eslint-disable-next-line eslint-plugin/prefer-message-ids
3537
meta: {
@@ -63,12 +65,7 @@ module.exports = {
6365
return
6466
}
6567

66-
const sourceCode = context.getSourceCode()
67-
if (sourceCode.markVariableAsUsed) {
68-
sourceCode.markVariableAsUsed(name, node)
69-
} else {
70-
context.markVariableAsUsed?.(name)
71-
}
68+
utils.markVariableAsUsed(context, name, node)
7269
}
7370
}
7471
}

lib/rules/script-setup-uses-vars.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ module.exports = {
3939
if (!utils.isScriptSetup(context)) {
4040
return {}
4141
}
42+
const sourceCode = context.getSourceCode()
4243
/** @type {Set<string>} */
4344
const scriptVariableNames = new Set()
44-
const globalScope = context.getSourceCode().scopeManager.globalScope
45+
const globalScope = sourceCode.scopeManager.globalScope
4546
if (globalScope) {
4647
for (const variable of globalScope.variables) {
4748
scriptVariableNames.add(variable.name)
@@ -56,12 +57,7 @@ module.exports = {
5657

5758
/** @param {string} name */
5859
function markVariableAsUsed(name) {
59-
const sourceCode = context.getSourceCode()
60-
if (sourceCode.markVariableAsUsed) {
61-
sourceCode.markVariableAsUsed(name, sourceCode.ast)
62-
} else {
63-
context.markVariableAsUsed?.(name)
64-
}
60+
utils.markVariableAsUsed(context, name, sourceCode.ast)
6561
}
6662

6763
/**

lib/utils/index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,6 +1942,10 @@ module.exports = {
19421942
withinTypeNode,
19431943
findVariableByIdentifier,
19441944
getScope,
1945+
/**
1946+
* Marks a variable with the given name in the current scope as used. This affects the no-unused-vars rule.
1947+
*/
1948+
markVariableAsUsed,
19451949
/**
19461950
* Checks whether the given node is in export default.
19471951
* @param {ASTNode} node
@@ -2565,6 +2569,26 @@ function isTypeScriptFile(path) {
25652569
return path.endsWith('.ts') || path.endsWith('.tsx') || path.endsWith('.mts')
25662570
}
25672571

2572+
// ------------------------------------------------------------------------------
2573+
// ESLint Helpers
2574+
// ------------------------------------------------------------------------------
2575+
/**
2576+
* Marks a variable with the given name in the current scope as used. This affects the no-unused-vars rule.
2577+
* @param {RuleContext} context
2578+
* @param {string} name
2579+
* @param {ASTNode} node The node to get the current scope.
2580+
*/
2581+
function markVariableAsUsed(context, name, node) {
2582+
const sourceCode = context.getSourceCode()
2583+
if (sourceCode.markVariableAsUsed) {
2584+
sourceCode.markVariableAsUsed(name, node)
2585+
} else {
2586+
// This function does not use the given node, but the currently visited node.
2587+
// If we need to determine the scope of a given node, we need to implement it yourself.
2588+
context.markVariableAsUsed?.(name)
2589+
}
2590+
}
2591+
25682592
// ------------------------------------------------------------------------------
25692593
// Vue Helpers
25702594
// ------------------------------------------------------------------------------

typings/eslint/index.d.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export namespace Scope {
1818
scopes: Scope[]
1919
globalScope: Scope | null
2020
acquire(node: VAST.ESNode | VAST.Program, inner?: boolean): Scope | null
21-
/** Added in ESLint v8.38.0 */
21+
/** @since ESLint v8.38.0 */
2222
getDeclaredVariables?(node: VAST.ESNode): Variable[]
2323
}
2424
interface Scope {
@@ -232,9 +232,9 @@ export class SourceCode /*extends ESLintSourceCode*/ {
232232
getCommentsAfter(nodeOrToken: VNODE.HasLocation): VNODE.Comment[]
233233
getCommentsInside(node: VNODE.HasLocation): VNODE.Comment[]
234234

235-
/** Added in ESLint v8.39.0 */
236-
markVariableAsUsed?(name: string, node: VNODE.HasLocation): void
237-
/** Added in ESLint v8.37.0 */
235+
/** @since ESLint v8.39.0 */
236+
markVariableAsUsed?(name: string, node?: VNODE.HasLocation): void
237+
/** @since ESLint v8.37.0 */
238238
getScope?(node: VNODE.HasLocation): Scope.Scope
239239
}
240240
export namespace SourceCode {
@@ -337,20 +337,20 @@ export namespace Rule {
337337
/** @deprecated removed in ESLint v9 */
338338
getDeclaredVariables?(node: VAST.ESNode): Scope.Variable[]
339339
getFilename(): string
340-
/** Added in ESLint v8.40.0 */
340+
/** @since ESLint v8.40.0 */
341341
filename?: string
342342
/** @deprecated removed in ESLint v9 */
343343
getScope?(): Scope.Scope
344344
getSourceCode(): SourceCode
345-
/** Added in ESLint v8.40.0 */
345+
/** @since ESLint v8.40.0 */
346346
sourceCode?: SourceCode
347347
/** @deprecated removed in ESLint v9 */
348348
markVariableAsUsed?(name: string): boolean
349349
report(descriptor: ReportDescriptor): void
350350

351351
// eslint@6 does not have this method.
352352
getCwd?: () => string
353-
/** Added in ESLint v8.40.0 */
353+
/** @since ESLint v8.40.0 */
354354
cwd?: string
355355
}
356356

0 commit comments

Comments
 (0)