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

test: fix it to not use context.getScope. #172

Merged
merged 4 commits into from
Dec 6, 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
9 changes: 9 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ module.exports = {
extends: ["plugin:@eslint-community/mysticatea/es2020"],
rules: {
"@eslint-community/mysticatea/prettier": "off",
"no-restricted-properties": [
"error",
{
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.",
},
],
},
overrides: [
{
Expand Down
4 changes: 4 additions & 0 deletions docs/api/ast-utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,8 @@ module.exports = {
return {
MemberExpression(node) {
const name = getPropertyName(node, context.getScope())
// When using ESLint>=8.37.0, write as follows:
// const name = getPropertyName(node, context.sourceCode.getScope(node))
MichaelDeBoey marked this conversation as resolved.
Show resolved Hide resolved
},
}
},
Expand Down Expand Up @@ -398,6 +400,8 @@ module.exports = {
return {
ExpressionStatement(node) {
const evaluated = getStaticValue(node, context.getScope())
// When using ESLint>=8.37.0, write as follows:
// const evaluated = getStaticValue(node, context.sourceCode.getScope(node))
if (evaluated) {
const staticValue = evaluated.value
// ...
Expand Down
10 changes: 10 additions & 0 deletions docs/api/scope-utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ module.exports = {
return {
Identifier(node) {
const variable = findVariable(context.getScope(), node)
// When using ESLint>=8.37.0, write as follows:
// const variable = findVariable(context.sourceCode.getScope(node), node)
MichaelDeBoey marked this conversation as resolved.
Show resolved Hide resolved
},
}
},
Expand Down Expand Up @@ -67,6 +69,8 @@ module.exports = {
return {
"Program"(node) {
const globalScope = context.getScope()
// When using ESLint>=8.37.0, write as follows:
// const globalScope = context.sourceCode.getScope(node)
MichaelDeBoey marked this conversation as resolved.
Show resolved Hide resolved
const maybeNodejsScope = getInnermostScope(globalScope, node)
},
}
Expand Down Expand Up @@ -129,6 +133,8 @@ module.exports = {
return {
"Program:exit"() {
const tracker = new ReferenceTracker(context.getScope())
// When using ESLint>=8.37.0, write as follows:
// const tracker = new ReferenceTracker(context.sourceCode.getScope(context.sourceCode.ast))
const traceMap = {
// Find `console.log`, `console.info`, `console.warn`, and `console.error`.
console: {
Expand Down Expand Up @@ -197,6 +203,8 @@ module.exports = {
return {
"Program:exit"() {
const tracker = new ReferenceTracker(context.getScope())
// When using ESLint>=8.37.0, write as follows:
// const tracker = new ReferenceTracker(context.sourceCode.getScope(context.sourceCode.ast))
const traceMap = {
// Find `Buffer()` and `new Buffer()` of `buffer` module.
buffer: {
Expand Down Expand Up @@ -266,6 +274,8 @@ module.exports = {
return {
"Program:exit"() {
const tracker = new ReferenceTracker(context.getScope())
// When using ESLint>=8.37.0, write as follows:
// const tracker = new ReferenceTracker(context.sourceCode.getScope(context.sourceCode.ast))
const traceMap = {
// Find `Buffer()` and `new Buffer()` of `buffer` module.
buffer: {
Expand Down
6 changes: 5 additions & 1 deletion test/find-variable.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import assert from "assert"
import eslint from "eslint"
import { findVariable } from "../src/index.mjs"
import { getScope } from "./test-lib/get-scope.mjs"

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

linter.defineRule("test", (context) => ({
[selector](node) {
variable = findVariable(context.getScope(), withString || node)
variable = findVariable(
getScope(context, node),
withString || node,
)
},
}))
linter.verify(code, {
Expand Down
3 changes: 2 additions & 1 deletion test/get-innermost-scope.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import assert from "assert"
import eslint from "eslint"
import { getInnermostScope } from "../src/index.mjs"
import { getScope } from "./test-lib/get-scope.mjs"

describe("The 'getInnermostScope' function", () => {
let i = 0
Expand Down Expand Up @@ -61,7 +62,7 @@ describe("The 'getInnermostScope' function", () => {
let expectedScope = null
linter.defineRule("test", (context) => ({
Program(node) {
const scope = context.getScope()
const scope = getScope(context, node)
actualScope = getInnermostScope(scope, selectNode(node))
expectedScope = selectScope(scope)
},
Expand Down
3 changes: 2 additions & 1 deletion test/get-static-value.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +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"

describe("The 'getStaticValue' function", () => {
for (const { code, expected, noScope = false } of [
Expand Down Expand Up @@ -405,7 +406,7 @@ const aMap = Object.freeze({
ExpressionStatement(node) {
actual = getStaticValue(
node,
noScope ? null : context.getScope(),
noScope ? null : getScope(context, node),
)
},
}))
Expand Down
6 changes: 5 additions & 1 deletion test/get-string-if-constant.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import assert from "assert"
import eslint from "eslint"
import { getStringIfConstant } from "../src/index.mjs"
import { getScope } from "./test-lib/get-scope.mjs"

describe("The 'getStringIfConstant' function", () => {
for (const { code, expected } of [
Expand Down Expand Up @@ -55,7 +56,10 @@ describe("The 'getStringIfConstant' function", () => {
let actual = null
linter.defineRule("test", (context) => ({
"Program > ExpressionStatement > *"(node) {
actual = getStringIfConstant(node, context.getScope())
actual = getStringIfConstant(
node,
getScope(context, node),
)
},
}))
linter.verify(code, {
Expand Down
19 changes: 13 additions & 6 deletions test/reference-tracker.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +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"

const config = {
parserOptions: {
Expand Down Expand Up @@ -520,8 +521,10 @@ describe("The 'ReferenceTracker' class:", () => {

let actual = null
linter.defineRule("test", (context) => ({
"Program:exit"() {
const tracker = new ReferenceTracker(context.getScope())
"Program:exit"(node) {
const tracker = new ReferenceTracker(
getScope(context, node),
)
actual = Array.from(
tracker.iterateGlobalReferences(traceMap),
).map((x) =>
Expand Down Expand Up @@ -684,8 +687,10 @@ describe("The 'ReferenceTracker' class:", () => {

let actual = null
linter.defineRule("test", (context) => ({
"Program:exit"() {
const tracker = new ReferenceTracker(context.getScope())
"Program:exit"(node) {
const tracker = new ReferenceTracker(
getScope(context, node),
)
actual = Array.from(
tracker.iterateCjsReferences(traceMap),
).map((x) =>
Expand Down Expand Up @@ -966,8 +971,10 @@ describe("The 'ReferenceTracker' class:", () => {

let actual = null
linter.defineRule("test", (context) => ({
"Program:exit"() {
const tracker = new ReferenceTracker(context.getScope())
"Program:exit"(node) {
const tracker = new ReferenceTracker(
getScope(context, node),
)
actual = Array.from(
tracker.iterateEsmReferences(traceMap),
).map((x) =>
Expand Down
18 changes: 18 additions & 0 deletions test/test-lib/get-scope.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export function getScope(context, node) {
const sourceCode = context.getSourceCode()
ota-meshi marked this conversation as resolved.
Show resolved Hide resolved
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]
}