@@ -245,10 +245,12 @@ function wrapContextToOverrideTokenMethods(context, tokenStore, options) {
245
245
tokenStore
246
246
)
247
247
248
+ /** @type {WeakMap<ASTNode, import('eslint').Scope.ScopeManager> } */
248
249
const containerScopes = new WeakMap ( )
249
250
250
251
/**
251
252
* @param {ASTNode } node
253
+ * @returns {import('eslint').Scope.ScopeManager|null }
252
254
*/
253
255
function getContainerScope ( node ) {
254
256
const exprContainer = getVExpressionContainer ( node )
@@ -260,9 +262,11 @@ function wrapContextToOverrideTokenMethods(context, tokenStore, options) {
260
262
return cache
261
263
}
262
264
const programNode = eslintSourceCode . ast
263
- const parserOptions = context . parserOptions || { }
265
+ const parserOptions =
266
+ context . languageOptions ?. parserOptions ?? context . parserOptions ?? { }
264
267
const ecmaFeatures = parserOptions . ecmaFeatures || { }
265
- const ecmaVersion = parserOptions . ecmaVersion || 2020
268
+ const ecmaVersion =
269
+ context . languageOptions ?. ecmaVersion ?? parserOptions . ecmaVersion ?? 2020
266
270
const sourceType = programNode . sourceType
267
271
try {
268
272
const eslintScope = createRequire ( require . resolve ( 'eslint' ) ) (
@@ -297,7 +301,6 @@ function wrapContextToOverrideTokenMethods(context, tokenStore, options) {
297
301
getSourceCode ( ) {
298
302
return sourceCode
299
303
} ,
300
- // @ts -expect-error -- Added in ESLint v8.40
301
304
get sourceCode ( ) {
302
305
return sourceCode
303
306
} ,
@@ -310,11 +313,11 @@ function wrapContextToOverrideTokenMethods(context, tokenStore, options) {
310
313
*/
311
314
function getDeclaredVariables ( node ) {
312
315
const scope = getContainerScope ( node )
313
- if ( scope ) {
314
- return scope . getDeclaredVariables ( node )
315
- }
316
-
317
- return context . getDeclaredVariables ( node )
316
+ return (
317
+ scope ? .getDeclaredVariables ?. ( node ) ??
318
+ context . getDeclaredVariables ?. ( node ) ??
319
+ [ ]
320
+ )
318
321
}
319
322
}
320
323
@@ -1939,6 +1942,10 @@ module.exports = {
1939
1942
withinTypeNode,
1940
1943
findVariableByIdentifier,
1941
1944
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,
1942
1949
/**
1943
1950
* Checks whether the given node is in export default.
1944
1951
* @param {ASTNode } node
@@ -2562,6 +2569,26 @@ function isTypeScriptFile(path) {
2562
2569
return path . endsWith ( '.ts' ) || path . endsWith ( '.tsx' ) || path . endsWith ( '.mts' )
2563
2570
}
2564
2571
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
+
2565
2592
// ------------------------------------------------------------------------------
2566
2593
// Vue Helpers
2567
2594
// ------------------------------------------------------------------------------
0 commit comments