Skip to content

Commit fa2c69a

Browse files
test: fix it to not use context.getScope. (#172)
Co-authored-by: 唯然 <weiran.zsd@outlook.com>
1 parent 8d69bc4 commit fa2c69a

File tree

9 files changed

+84
-17
lines changed

9 files changed

+84
-17
lines changed

.eslintrc.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ module.exports = {
66
extends: ["plugin:@eslint-community/mysticatea/es2020"],
77
rules: {
88
"@eslint-community/mysticatea/prettier": "off",
9+
"no-restricted-properties": [
10+
"error",
11+
{
12+
object: "context",
13+
property: "getScope",
14+
message:
15+
"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.",
16+
},
17+
],
918
},
1019
overrides: [
1120
{

docs/api/ast-utils.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,9 @@ module.exports = {
341341
create(context) {
342342
return {
343343
MemberExpression(node) {
344-
const name = getPropertyName(node, context.getScope())
344+
const name = getPropertyName(node, context.sourceCode.getScope(node))
345+
// When using ESLint<8.37.0, write as follows:
346+
// const name = getPropertyName(node, context.getScope())
345347
},
346348
}
347349
},
@@ -397,7 +399,9 @@ module.exports = {
397399
create(context) {
398400
return {
399401
ExpressionStatement(node) {
400-
const evaluated = getStaticValue(node, context.getScope())
402+
const evaluated = getStaticValue(node, context.sourceCode.getScope(node))
403+
// When using ESLint<8.37.0, write as follows:
404+
// const evaluated = getStaticValue(node, context.getScope())
401405
if (evaluated) {
402406
const staticValue = evaluated.value
403407
// ...

docs/api/scope-utils.md

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ module.exports = {
2929
create(context) {
3030
return {
3131
Identifier(node) {
32-
const variable = findVariable(context.getScope(), node)
32+
const variable = findVariable(context.sourceCode.getScope(node), node)
33+
// When using ESLint<8.37.0, write as follows:
34+
// const variable = findVariable(context.getScope(), node)
3335
},
3436
}
3537
},
@@ -66,7 +68,9 @@ module.exports = {
6668
create(context) {
6769
return {
6870
"Program"(node) {
69-
const globalScope = context.getScope()
71+
const globalScope = context.sourceCode.getScope(node)
72+
// When using ESLint<8.37.0, write as follows:
73+
// const globalScope = context.getScope()
7074
const maybeNodejsScope = getInnermostScope(globalScope, node)
7175
},
7276
}
@@ -128,7 +132,12 @@ module.exports = {
128132
create(context) {
129133
return {
130134
"Program:exit"() {
131-
const tracker = new ReferenceTracker(context.getScope())
135+
const tracker = new ReferenceTracker(
136+
context.sourceCode.getScope(context.sourceCode.ast),
137+
)
138+
// When using ESLint<8.37.0, write as follows:
139+
// const tracker = new ReferenceTracker(context.getScope())
140+
132141
const traceMap = {
133142
// Find `console.log`, `console.info`, `console.warn`, and `console.error`.
134143
console: {
@@ -196,7 +205,12 @@ module.exports = {
196205
create(context) {
197206
return {
198207
"Program:exit"() {
199-
const tracker = new ReferenceTracker(context.getScope())
208+
const tracker = new ReferenceTracker(
209+
context.sourceCode.getScope(context.sourceCode.ast),
210+
)
211+
// When using ESLint<8.37.0, write as follows:
212+
// const tracker = new ReferenceTracker(context.getScope())
213+
200214
const traceMap = {
201215
// Find `Buffer()` and `new Buffer()` of `buffer` module.
202216
buffer: {
@@ -265,7 +279,12 @@ module.exports = {
265279
create(context) {
266280
return {
267281
"Program:exit"() {
268-
const tracker = new ReferenceTracker(context.getScope())
282+
const tracker = new ReferenceTracker(
283+
context.sourceCode.getScope(context.sourceCode.ast),
284+
)
285+
// When using ESLint<8.37.0, write as follows:
286+
// const tracker = new ReferenceTracker(context.getScope())
287+
269288
const traceMap = {
270289
// Find `Buffer()` and `new Buffer()` of `buffer` module.
271290
buffer: {

test/find-variable.mjs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import assert from "assert"
22
import eslint from "eslint"
33
import { findVariable } from "../src/index.mjs"
4+
import { getScope } from "./test-lib/get-scope.mjs"
45

56
describe("The 'findVariable' function", () => {
67
function getVariable(code, selector, withString = null) {
@@ -9,7 +10,10 @@ describe("The 'findVariable' function", () => {
910

1011
linter.defineRule("test", (context) => ({
1112
[selector](node) {
12-
variable = findVariable(context.getScope(), withString || node)
13+
variable = findVariable(
14+
getScope(context, node),
15+
withString || node,
16+
)
1317
},
1418
}))
1519
linter.verify(code, {

test/get-innermost-scope.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import assert from "assert"
22
import eslint from "eslint"
33
import { getInnermostScope } from "../src/index.mjs"
4+
import { getScope } from "./test-lib/get-scope.mjs"
45

56
describe("The 'getInnermostScope' function", () => {
67
let i = 0
@@ -61,7 +62,7 @@ describe("The 'getInnermostScope' function", () => {
6162
let expectedScope = null
6263
linter.defineRule("test", (context) => ({
6364
Program(node) {
64-
const scope = context.getScope()
65+
const scope = getScope(context, node)
6566
actualScope = getInnermostScope(scope, selectNode(node))
6667
expectedScope = selectScope(scope)
6768
},

test/get-static-value.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import assert from "assert"
22
import eslint from "eslint"
33
import semver from "semver"
44
import { getStaticValue } from "../src/index.mjs"
5+
import { getScope } from "./test-lib/get-scope.mjs"
56

67
describe("The 'getStaticValue' function", () => {
78
for (const { code, expected, noScope = false } of [
@@ -405,7 +406,7 @@ const aMap = Object.freeze({
405406
ExpressionStatement(node) {
406407
actual = getStaticValue(
407408
node,
408-
noScope ? null : context.getScope(),
409+
noScope ? null : getScope(context, node),
409410
)
410411
},
411412
}))

test/get-string-if-constant.mjs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import assert from "assert"
22
import eslint from "eslint"
33
import { getStringIfConstant } from "../src/index.mjs"
4+
import { getScope } from "./test-lib/get-scope.mjs"
45

56
describe("The 'getStringIfConstant' function", () => {
67
for (const { code, expected } of [
@@ -55,7 +56,10 @@ describe("The 'getStringIfConstant' function", () => {
5556
let actual = null
5657
linter.defineRule("test", (context) => ({
5758
"Program > ExpressionStatement > *"(node) {
58-
actual = getStringIfConstant(node, context.getScope())
59+
actual = getStringIfConstant(
60+
node,
61+
getScope(context, node),
62+
)
5963
},
6064
}))
6165
linter.verify(code, {

test/reference-tracker.mjs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import assert from "assert"
22
import eslint from "eslint"
33
import semver from "semver"
44
import { CALL, CONSTRUCT, ESM, READ, ReferenceTracker } from "../src/index.mjs"
5+
import { getScope } from "./test-lib/get-scope.mjs"
56

67
const config = {
78
parserOptions: {
@@ -520,8 +521,10 @@ describe("The 'ReferenceTracker' class:", () => {
520521

521522
let actual = null
522523
linter.defineRule("test", (context) => ({
523-
"Program:exit"() {
524-
const tracker = new ReferenceTracker(context.getScope())
524+
"Program:exit"(node) {
525+
const tracker = new ReferenceTracker(
526+
getScope(context, node),
527+
)
525528
actual = Array.from(
526529
tracker.iterateGlobalReferences(traceMap),
527530
).map((x) =>
@@ -684,8 +687,10 @@ describe("The 'ReferenceTracker' class:", () => {
684687

685688
let actual = null
686689
linter.defineRule("test", (context) => ({
687-
"Program:exit"() {
688-
const tracker = new ReferenceTracker(context.getScope())
690+
"Program:exit"(node) {
691+
const tracker = new ReferenceTracker(
692+
getScope(context, node),
693+
)
689694
actual = Array.from(
690695
tracker.iterateCjsReferences(traceMap),
691696
).map((x) =>
@@ -966,8 +971,10 @@ describe("The 'ReferenceTracker' class:", () => {
966971

967972
let actual = null
968973
linter.defineRule("test", (context) => ({
969-
"Program:exit"() {
970-
const tracker = new ReferenceTracker(context.getScope())
974+
"Program:exit"(node) {
975+
const tracker = new ReferenceTracker(
976+
getScope(context, node),
977+
)
971978
actual = Array.from(
972979
tracker.iterateEsmReferences(traceMap),
973980
).map((x) =>

test/test-lib/get-scope.mjs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export function getScope(context, node) {
2+
const sourceCode = context.sourceCode || context.getSourceCode()
3+
if (sourceCode.getScope) {
4+
return sourceCode.getScope(node)
5+
}
6+
const scopeManager = sourceCode.scopeManager
7+
const inner = node.type !== "Program"
8+
for (let n = node; n; n = n.parent) {
9+
const scope = scopeManager.acquire(n, inner)
10+
if (scope) {
11+
if (scope.type === "function-expression-name") {
12+
return scope.childScopes[0]
13+
}
14+
return scope
15+
}
16+
}
17+
return scopeManager.scopes[0]
18+
}

0 commit comments

Comments
 (0)