Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve compatibility with ESLint v9 (take2) #2338

Merged
merged 2 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/rules/jsx-uses-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ module.exports = {
return
}

context.markVariableAsUsed(name)
const sourceCode = context.getSourceCode()
if (sourceCode.markVariableAsUsed) {
sourceCode.markVariableAsUsed(name, node)
} else {
context.markVariableAsUsed?.(name)
}
ota-meshi marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down
22 changes: 16 additions & 6 deletions lib/rules/script-setup-uses-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,33 @@ module.exports = {
}
}

/** @param {string} name */
function markVariableAsUsed(name) {
const sourceCode = context.getSourceCode()
if (sourceCode.markVariableAsUsed) {
sourceCode.markVariableAsUsed(name, sourceCode.ast)
} else {
context.markVariableAsUsed?.(name)
}
}

/**
* @see https://github.com/vuejs/vue-next/blob/2749c15170ad4913e6530a257db485d4e7ed2283/packages/compiler-core/src/transforms/transformElement.ts#L333
* @param {string} name
*/
function markSetupReferenceVariableAsUsed(name) {
if (scriptVariableNames.has(name)) {
context.markVariableAsUsed(name)
markVariableAsUsed(name)
return true
}
const camelName = camelize(name)
if (scriptVariableNames.has(camelName)) {
context.markVariableAsUsed(camelName)
markVariableAsUsed(camelName)
return true
}
const pascalName = casing.capitalize(camelName)
if (scriptVariableNames.has(pascalName)) {
context.markVariableAsUsed(pascalName)
markVariableAsUsed(pascalName)
return true
}
return false
Expand All @@ -83,7 +93,7 @@ module.exports = {
for (const ref of node.references.filter(
(ref) => ref.variable == null
)) {
context.markVariableAsUsed(ref.id.name)
markVariableAsUsed(ref.id.name)
}
},
VElement(node) {
Expand Down Expand Up @@ -115,7 +125,7 @@ module.exports = {
/** @param {VAttribute} node */
'VAttribute[directive=false]'(node) {
if (node.key.name === 'ref' && node.value) {
context.markVariableAsUsed(node.value.value)
markVariableAsUsed(node.value.value)
}
}
},
Expand All @@ -124,7 +134,7 @@ module.exports = {
const styleVars = getStyleVariablesContext(context)
if (styleVars) {
for (const ref of styleVars.references) {
context.markVariableAsUsed(ref.id.name)
markVariableAsUsed(ref.id.name)
}
}
}
Expand Down
19 changes: 11 additions & 8 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,12 @@ function wrapContextToOverrideTokenMethods(context, tokenStore, options) {
tokenStore
)

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

/**
* @param {ASTNode} node
* @returns {import('eslint').Scope.ScopeManager|null}
*/
function getContainerScope(node) {
const exprContainer = getVExpressionContainer(node)
Expand All @@ -260,9 +262,11 @@ function wrapContextToOverrideTokenMethods(context, tokenStore, options) {
return cache
}
const programNode = eslintSourceCode.ast
const parserOptions = context.parserOptions || {}
const parserOptions =
context.languageOptions?.parserOptions ?? context.parserOptions ?? {}
const ecmaFeatures = parserOptions.ecmaFeatures || {}
const ecmaVersion = parserOptions.ecmaVersion || 2020
const ecmaVersion =
context.languageOptions?.ecmaVersion ?? parserOptions.ecmaVersion ?? 2020
const sourceType = programNode.sourceType
try {
const eslintScope = createRequire(require.resolve('eslint'))(
Expand Down Expand Up @@ -297,7 +301,6 @@ function wrapContextToOverrideTokenMethods(context, tokenStore, options) {
getSourceCode() {
return sourceCode
},
// @ts-expect-error -- Added in ESLint v8.40
get sourceCode() {
return sourceCode
},
Expand All @@ -310,11 +313,11 @@ function wrapContextToOverrideTokenMethods(context, tokenStore, options) {
*/
function getDeclaredVariables(node) {
const scope = getContainerScope(node)
if (scope) {
return scope.getDeclaredVariables(node)
}

return context.getDeclaredVariables(node)
return (
scope?.getDeclaredVariables?.(node) ??
context.getDeclaredVariables?.(node) ??
[]
)
}
}

Expand Down
17 changes: 17 additions & 0 deletions tests/lib/rules/jsx-uses-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ const ruleTester = new RuleTester({
const linter = ruleTester.linter || eslint.linter
linter.defineRule('jsx-uses-vars', rule)

ruleTester.run('jsx-uses-vars', rule, {
// Visually check that there are no warnings in the console.
valid: [
`
import SomeComponent from './SomeComponent.jsx';
export default {
render () {
return (
<SomeComponent msg="Hello world"></SomeComponent>
)
},
};
`
],
invalid: []
})

describe('jsx-uses-vars', () => {
ruleTester.run('no-unused-vars', ruleNoUnusedVars, {
valid: [
Expand Down
15 changes: 15 additions & 0 deletions tests/lib/rules/script-setup-uses-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ const ruleTester = new RuleTester({
const linter = ruleTester.linter || eslint.linter
linter.defineRule('script-setup-uses-vars', rule)

ruleTester.run('script-setup-uses-vars', rule, {
// Visually check that there are no warnings in the console.
valid: [
`
<script setup>
import Foo from './Foo.vue'
</script>

<template>
<Foo />
</template>
`
],
invalid: []
})
describe('script-setup-uses-vars', () => {
ruleTester.run('no-unused-vars', ruleNoUnusedVars, {
valid: [
Expand Down
40 changes: 30 additions & 10 deletions typings/eslint/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export namespace Scope {
scopes: Scope[]
globalScope: Scope | null
acquire(node: VAST.ESNode | VAST.Program, inner?: boolean): Scope | null
getDeclaredVariables(node: VAST.ESNode): Variable[]
/** Added in ESLint v8.38.0 */
ota-meshi marked this conversation as resolved.
Show resolved Hide resolved
getDeclaredVariables?(node: VAST.ESNode): Variable[]
}
interface Scope {
type:
Expand Down Expand Up @@ -230,6 +231,11 @@ export class SourceCode /*extends ESLintSourceCode*/ {
getCommentsBefore(nodeOrToken: VNODE.HasLocation): VNODE.Comment[]
getCommentsAfter(nodeOrToken: VNODE.HasLocation): VNODE.Comment[]
getCommentsInside(node: VNODE.HasLocation): VNODE.Comment[]

/** Added in ESLint v8.39.0 */
markVariableAsUsed?(name: string, node: VNODE.HasLocation): void
/** Added in ESLint v8.37.0 */
getScope?(node: VNODE.HasLocation): Scope.Scope
}
export namespace SourceCode {
interface Config {
Expand Down Expand Up @@ -317,21 +323,35 @@ export namespace Rule {
id: string
options: ESLintRule.RuleContext['options']
settings: { [name: string]: any }
parserPath: string
parserOptions: any
parserServices: parserServices.ParserServices

getAncestors(): VAST.ESNode[]

getDeclaredVariables(node: VAST.ESNode): Scope.Variable[]
/** @deprecated removed in ESLint v10? */
parserPath?: string
/** @deprecated removed in ESLint v10? */
parserOptions?: ESLintLinter.ParserOptions
/** For flat config */
languageOptions?: ESLintLinter.FlatConfig['languageOptions']
/** @deprecated removed in ESLint v9 */
parserServices?: parserServices.ParserServices

/** @deprecated removed in ESLint v9 */
getAncestors?(): VAST.ESNode[]
/** @deprecated removed in ESLint v9 */
getDeclaredVariables?(node: VAST.ESNode): Scope.Variable[]
getFilename(): string
getScope(): Scope.Scope
/** Added in ESLint v8.40.0 */
filename?: string
/** @deprecated removed in ESLint v9 */
getScope?(): Scope.Scope
getSourceCode(): SourceCode
markVariableAsUsed(name: string): boolean
/** Added in ESLint v8.40.0 */
sourceCode?: SourceCode
/** @deprecated removed in ESLint v9 */
markVariableAsUsed?(name: string): boolean
report(descriptor: ReportDescriptor): void

// eslint@6 does not have this method.
getCwd?: () => string
/** Added in ESLint v8.40.0 */
cwd?: string
}

type ReportDescriptor =
Expand Down
Loading