Skip to content

Commit 88134b2

Browse files
committed
Improve compatibility with ESLint v9 (take2)
1 parent 0b2edb7 commit 88134b2

File tree

6 files changed

+95
-25
lines changed

6 files changed

+95
-25
lines changed

lib/rules/jsx-uses-vars.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@ module.exports = {
6363
return
6464
}
6565

66-
context.markVariableAsUsed(name)
66+
const sourceCode = context.getSourceCode()
67+
if (sourceCode.markVariableAsUsed) {
68+
sourceCode.markVariableAsUsed(name, node)
69+
} else {
70+
context.markVariableAsUsed?.(name)
71+
}
6772
}
6873
}
6974
}

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,33 @@ module.exports = {
5454
}
5555
}
5656

57+
/** @param {string} name */
58+
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+
}
65+
}
66+
5767
/**
5868
* @see https://github.com/vuejs/vue-next/blob/2749c15170ad4913e6530a257db485d4e7ed2283/packages/compiler-core/src/transforms/transformElement.ts#L333
5969
* @param {string} name
6070
*/
6171
function markSetupReferenceVariableAsUsed(name) {
6272
if (scriptVariableNames.has(name)) {
63-
context.markVariableAsUsed(name)
73+
markVariableAsUsed(name)
6474
return true
6575
}
6676
const camelName = camelize(name)
6777
if (scriptVariableNames.has(camelName)) {
68-
context.markVariableAsUsed(camelName)
78+
markVariableAsUsed(camelName)
6979
return true
7080
}
7181
const pascalName = casing.capitalize(camelName)
7282
if (scriptVariableNames.has(pascalName)) {
73-
context.markVariableAsUsed(pascalName)
83+
markVariableAsUsed(pascalName)
7484
return true
7585
}
7686
return false
@@ -83,7 +93,7 @@ module.exports = {
8393
for (const ref of node.references.filter(
8494
(ref) => ref.variable == null
8595
)) {
86-
context.markVariableAsUsed(ref.id.name)
96+
markVariableAsUsed(ref.id.name)
8797
}
8898
},
8999
VElement(node) {
@@ -115,7 +125,7 @@ module.exports = {
115125
/** @param {VAttribute} node */
116126
'VAttribute[directive=false]'(node) {
117127
if (node.key.name === 'ref' && node.value) {
118-
context.markVariableAsUsed(node.value.value)
128+
markVariableAsUsed(node.value.value)
119129
}
120130
}
121131
},
@@ -124,7 +134,7 @@ module.exports = {
124134
const styleVars = getStyleVariablesContext(context)
125135
if (styleVars) {
126136
for (const ref of styleVars.references) {
127-
context.markVariableAsUsed(ref.id.name)
137+
markVariableAsUsed(ref.id.name)
128138
}
129139
}
130140
}

lib/utils/index.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,12 @@ function wrapContextToOverrideTokenMethods(context, tokenStore, options) {
245245
tokenStore
246246
)
247247

248+
/** @type {WeakMap<ASTNode, import('eslint').Scope.ScopeManager>} */
248249
const containerScopes = new WeakMap()
249250

250251
/**
251252
* @param {ASTNode} node
253+
* @returns {import('eslint').Scope.ScopeManager|null}
252254
*/
253255
function getContainerScope(node) {
254256
const exprContainer = getVExpressionContainer(node)
@@ -260,9 +262,11 @@ function wrapContextToOverrideTokenMethods(context, tokenStore, options) {
260262
return cache
261263
}
262264
const programNode = eslintSourceCode.ast
263-
const parserOptions = context.parserOptions || {}
265+
const parserOptions =
266+
context.languageOptions?.parserOptions ?? context.parserOptions ?? {}
264267
const ecmaFeatures = parserOptions.ecmaFeatures || {}
265-
const ecmaVersion = parserOptions.ecmaVersion || 2020
268+
const ecmaVersion =
269+
context.languageOptions?.ecmaVersion ?? parserOptions.ecmaVersion ?? 2020
266270
const sourceType = programNode.sourceType
267271
try {
268272
const eslintScope = createRequire(require.resolve('eslint'))(
@@ -297,7 +301,6 @@ function wrapContextToOverrideTokenMethods(context, tokenStore, options) {
297301
getSourceCode() {
298302
return sourceCode
299303
},
300-
// @ts-expect-error -- Added in ESLint v8.40
301304
get sourceCode() {
302305
return sourceCode
303306
},
@@ -310,11 +313,11 @@ function wrapContextToOverrideTokenMethods(context, tokenStore, options) {
310313
*/
311314
function getDeclaredVariables(node) {
312315
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+
)
318321
}
319322
}
320323

tests/lib/rules/jsx-uses-vars.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@ const ruleTester = new RuleTester({
2424
const linter = ruleTester.linter || eslint.linter
2525
linter.defineRule('jsx-uses-vars', rule)
2626

27+
ruleTester.run('jsx-uses-vars', rule, {
28+
// Visually check that there are no warnings in the console.
29+
valid: [
30+
`
31+
import SomeComponent from './SomeComponent.jsx';
32+
export default {
33+
render () {
34+
return (
35+
<SomeComponent msg="Hello world"></SomeComponent>
36+
)
37+
},
38+
};
39+
`
40+
],
41+
invalid: []
42+
})
43+
2744
describe('jsx-uses-vars', () => {
2845
ruleTester.run('no-unused-vars', ruleNoUnusedVars, {
2946
valid: [

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@ const ruleTester = new RuleTester({
2323
const linter = ruleTester.linter || eslint.linter
2424
linter.defineRule('script-setup-uses-vars', rule)
2525

26+
ruleTester.run('script-setup-uses-vars', rule, {
27+
// Visually check that there are no warnings in the console.
28+
valid: [
29+
`
30+
<script setup>
31+
import Foo from './Foo.vue'
32+
</script>
33+
34+
<template>
35+
<Foo />
36+
</template>
37+
`
38+
],
39+
invalid: []
40+
})
2641
describe('script-setup-uses-vars', () => {
2742
ruleTester.run('no-unused-vars', ruleNoUnusedVars, {
2843
valid: [

typings/eslint/index.d.ts

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ export namespace Scope {
1818
scopes: Scope[]
1919
globalScope: Scope | null
2020
acquire(node: VAST.ESNode | VAST.Program, inner?: boolean): Scope | null
21-
getDeclaredVariables(node: VAST.ESNode): Variable[]
21+
/** Added in ESLint v8.38.0 */
22+
getDeclaredVariables?(node: VAST.ESNode): Variable[]
2223
}
2324
interface Scope {
2425
type:
@@ -230,6 +231,11 @@ export class SourceCode /*extends ESLintSourceCode*/ {
230231
getCommentsBefore(nodeOrToken: VNODE.HasLocation): VNODE.Comment[]
231232
getCommentsAfter(nodeOrToken: VNODE.HasLocation): VNODE.Comment[]
232233
getCommentsInside(node: VNODE.HasLocation): VNODE.Comment[]
234+
235+
/** Added in ESLint v8.39.0 */
236+
markVariableAsUsed?(name: string, node: VNODE.HasLocation): void
237+
/** Added in ESLint v8.37.0 */
238+
getScope?(node: VNODE.HasLocation): Scope.Scope
233239
}
234240
export namespace SourceCode {
235241
interface Config {
@@ -317,21 +323,35 @@ export namespace Rule {
317323
id: string
318324
options: ESLintRule.RuleContext['options']
319325
settings: { [name: string]: any }
320-
parserPath: string
321-
parserOptions: any
322-
parserServices: parserServices.ParserServices
323-
324-
getAncestors(): VAST.ESNode[]
325-
326-
getDeclaredVariables(node: VAST.ESNode): Scope.Variable[]
326+
/** @deprecated removed in ESLint v10? */
327+
parserPath?: string
328+
/** @deprecated removed in ESLint v10? */
329+
parserOptions?: ESLintLinter.ParserOptions
330+
/** For flat config */
331+
languageOptions?: ESLintLinter.FlatConfig['languageOptions']
332+
/** @deprecated removed in ESLint v9 */
333+
parserServices?: parserServices.ParserServices
334+
335+
/** @deprecated removed in ESLint v9 */
336+
getAncestors?(): VAST.ESNode[]
337+
/** @deprecated removed in ESLint v9 */
338+
getDeclaredVariables?(node: VAST.ESNode): Scope.Variable[]
327339
getFilename(): string
328-
getScope(): Scope.Scope
340+
/** Added in ESLint v8.40.0 */
341+
filename?: string
342+
/** @deprecated removed in ESLint v9 */
343+
getScope?(): Scope.Scope
329344
getSourceCode(): SourceCode
330-
markVariableAsUsed(name: string): boolean
345+
/** Added in ESLint v8.40.0 */
346+
sourceCode?: SourceCode
347+
/** @deprecated removed in ESLint v9 */
348+
markVariableAsUsed?(name: string): boolean
331349
report(descriptor: ReportDescriptor): void
332350

333351
// eslint@6 does not have this method.
334352
getCwd?: () => string
353+
/** Added in ESLint v8.40.0 */
354+
cwd?: string
335355
}
336356

337357
type ReportDescriptor =

0 commit comments

Comments
 (0)