diff --git a/.eslintrc.js b/.eslintrc.js index 4483568..5c970d2 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -12,7 +12,7 @@ module.exports = { object: "context", property: "getScope", message: - "If you are using it in a test case, use test/test-lib/get-scope.mjs instead. Other than that, the API should also be compatible with ESLint v9.", + "If you are using it in a test case, use test/test-lib/eslint-compat.mjs#getScope instead. Other than that, the API should also be compatible with ESLint v9.", }, ], }, diff --git a/package.json b/package.json index 14352f6..d8f265d 100644 --- a/package.json +++ b/package.json @@ -54,6 +54,7 @@ }, "devDependencies": { "@eslint-community/eslint-plugin-mysticatea": "^15.6.1", + "@types/eslint": "^8.56.12", "c8": "^8.0.1", "dot-prop": "^7.2.0", "eslint": "^8.57.1", diff --git a/test/find-variable.mjs b/test/find-variable.mjs index 6b6e71c..849f1c1 100644 --- a/test/find-variable.mjs +++ b/test/find-variable.mjs @@ -1,7 +1,7 @@ import assert from "assert" import eslint from "eslint" import { findVariable } from "../src/index.mjs" -import { getScope } from "./test-lib/get-scope.mjs" +import { getScope } from "./test-lib/eslint-compat.mjs" describe("The 'findVariable' function", () => { function getVariable(code, selector, withString = null) { diff --git a/test/get-innermost-scope.mjs b/test/get-innermost-scope.mjs index 4370327..9c85bb0 100644 --- a/test/get-innermost-scope.mjs +++ b/test/get-innermost-scope.mjs @@ -1,7 +1,7 @@ import assert from "assert" import eslint from "eslint" import { getInnermostScope } from "../src/index.mjs" -import { getScope } from "./test-lib/get-scope.mjs" +import { getScope } from "./test-lib/eslint-compat.mjs" describe("The 'getInnermostScope' function", () => { let i = 0 diff --git a/test/get-static-value.mjs b/test/get-static-value.mjs index 9ca05c6..0bf1a2e 100644 --- a/test/get-static-value.mjs +++ b/test/get-static-value.mjs @@ -2,7 +2,7 @@ import assert from "assert" import eslint from "eslint" import semver from "semver" import { getStaticValue } from "../src/index.mjs" -import { getScope } from "./test-lib/get-scope.mjs" +import { getScope } from "./test-lib/eslint-compat.mjs" describe("The 'getStaticValue' function", () => { for (const { code, expected, noScope = false } of [ diff --git a/test/get-string-if-constant.mjs b/test/get-string-if-constant.mjs index 5d8cba0..f400fd6 100644 --- a/test/get-string-if-constant.mjs +++ b/test/get-string-if-constant.mjs @@ -1,7 +1,7 @@ import assert from "assert" import eslint from "eslint" import { getStringIfConstant } from "../src/index.mjs" -import { getScope } from "./test-lib/get-scope.mjs" +import { getScope } from "./test-lib/eslint-compat.mjs" describe("The 'getStringIfConstant' function", () => { for (const { code, expected } of [ diff --git a/test/reference-tracker.mjs b/test/reference-tracker.mjs index 6e9f14a..52993be 100644 --- a/test/reference-tracker.mjs +++ b/test/reference-tracker.mjs @@ -2,7 +2,7 @@ import assert from "assert" import eslint from "eslint" import semver from "semver" import { CALL, CONSTRUCT, ESM, READ, ReferenceTracker } from "../src/index.mjs" -import { getScope } from "./test-lib/get-scope.mjs" +import { getScope } from "./test-lib/eslint-compat.mjs" const config = { parserOptions: { diff --git a/test/test-lib/eslint-compat.mjs b/test/test-lib/eslint-compat.mjs new file mode 100644 index 0000000..abaf70b --- /dev/null +++ b/test/test-lib/eslint-compat.mjs @@ -0,0 +1,5 @@ +export const getScope = (context, node) => + getSourceCode(context).getScope?.(node) ?? context.getScope(); + +const getSourceCode = (context) => + context.sourceCode ?? context.getSourceCode(); diff --git a/test/test-lib/get-scope.mjs b/test/test-lib/get-scope.mjs deleted file mode 100644 index 765b6c8..0000000 --- a/test/test-lib/get-scope.mjs +++ /dev/null @@ -1,18 +0,0 @@ -export function getScope(context, node) { - const sourceCode = context.sourceCode || context.getSourceCode() - if (sourceCode.getScope) { - return sourceCode.getScope(node) - } - const scopeManager = sourceCode.scopeManager - const inner = node.type !== "Program" - for (let n = node; n; n = n.parent) { - const scope = scopeManager.acquire(n, inner) - if (scope) { - if (scope.type === "function-expression-name") { - return scope.childScopes[0] - } - return scope - } - } - return scopeManager.scopes[0] -}