From 9e23712f9015959af7748b2b41623cadf9809038 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20De=20Boey?= Date: Tue, 15 Oct 2024 15:07:14 +0200 Subject: [PATCH] test: simplify `getScope` --- package.json | 1 + test/find-variable.mjs | 2 +- test/get-innermost-scope.mjs | 2 +- test/get-static-value.mjs | 2 +- test/get-string-if-constant.mjs | 2 +- test/reference-tracker.mjs | 2 +- test/test-lib/eslint-compat.mjs | 5 +++++ test/test-lib/get-scope.mjs | 18 ------------------ 8 files changed, 11 insertions(+), 23 deletions(-) create mode 100644 test/test-lib/eslint-compat.mjs delete mode 100644 test/test-lib/get-scope.mjs diff --git a/package.json b/package.json index 1a19e96..ccc1d65 100644 --- a/package.json +++ b/package.json @@ -54,6 +54,7 @@ }, "devDependencies": { "@eslint-community/eslint-plugin-mysticatea": "^15.5.1", + "@types/eslint": "^8.56.12", "c8": "^8.0.1", "dot-prop": "^7.2.0", "eslint": "^8.50.0", 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] -}