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: add test for ESLint v9 #186

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
},
],
},
Expand Down
21 changes: 17 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,30 @@ jobs:
matrix.os }})
strategy:
matrix:
eslint: [8]
node: [12.22.0, 12, 14.17.0, 14, 16, 18, 20]
eslint: [9]
node: [18, 20]
os: [ubuntu-latest]
include:
# On other platforms
- os: windows-latest
eslint: 8
eslint: 9
node: 18
- os: macos-latest
eslint: 8
eslint: 9
node: 18
# On old Node versions & ESLint v8
- eslint: 8
node: 12.22.0
os: ubuntu-latest
- eslint: 8
node: 12
os: ubuntu-latest
- eslint: 8
node: 14.17.0
os: ubuntu-latest
- eslint: 8
node: 16
os: ubuntu-latest
# On old ESLint versions
- eslint: 7
node: 18
Expand Down
35 changes: 22 additions & 13 deletions test/find-variable.mjs
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
import assert from "assert"
import eslint from "eslint"
import { findVariable } from "../src/index.mjs"
import { getScope } from "./test-lib/get-scope.mjs"
import { getScope, newCompatLinter } from "./test-lib/eslint-compat.mjs"

describe("The 'findVariable' function", () => {
function getVariable(code, selector, withString = null) {
const linter = new eslint.Linter()
const linter = newCompatLinter()
let variable = null

linter.defineRule("test", (context) => ({
[selector](node) {
variable = findVariable(
getScope(context, node),
withString || node,
)
},
}))
linter.verify(code, {
parserOptions: { ecmaVersion: 2020 },
rules: { test: "error" },
languageOptions: { ecmaVersion: 2020 },
rules: { "test/test": "error" },
plugins: {
test: {
rules: {
test: {
create(context) {
return {
[selector](node) {
variable = findVariable(
getScope(context, node),
withString || node,
)
},
}
},
},
},
},
},
})

return variable
Expand Down
34 changes: 23 additions & 11 deletions test/get-function-head-location.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 { getFunctionHeadLocation } from "../src/index.mjs"
import { newCompatLinter } from "./test-lib/eslint-compat.mjs"

describe("The 'getFunctionHeadLocation' function", () => {
const expectedResults = {
Expand Down Expand Up @@ -103,26 +104,37 @@ describe("The 'getFunctionHeadLocation' function", () => {
it(`should return "${JSON.stringify(
expectedLoc,
)}" for "${key}".`, () => {
const linter = new eslint.Linter()
const linter = newCompatLinter()

let actualLoc = null
linter.defineRule("test", (context) => ({
":function"(node) {
actualLoc = getFunctionHeadLocation(
node,
context.getSourceCode(),
)
},
}))
const messages = linter.verify(
key,
{
rules: { test: "error" },
parserOptions: {
rules: { "test/test": "error" },
languageOptions: {
ecmaVersion: semver.gte(eslint.Linter.version, "8.0.0")
? 2022
: 2020,
},
plugins: {
test: {
rules: {
test: {
create(context) {
return {
":function"(node) {
actualLoc =
getFunctionHeadLocation(
node,
context.getSourceCode(),
)
},
}
},
},
},
},
},
},
"test.js",
true,
Expand Down
61 changes: 42 additions & 19 deletions test/get-function-name-with-kind.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 { getFunctionNameWithKind } from "../src/index.mjs"
import { newCompatLinter } from "./test-lib/eslint-compat.mjs"

describe("The 'getFunctionNameWithKind' function", () => {
const expectedResults = {
Expand Down Expand Up @@ -133,22 +134,33 @@ describe("The 'getFunctionNameWithKind' function", () => {
const expectedResult2 = expectedResults[key]

it(`should return "${expectedResult1}" for "${key}".`, () => {
const linter = new eslint.Linter()
const linter = newCompatLinter()

let actualResult = null
linter.defineRule("test", () => ({
":function"(node) {
actualResult = getFunctionNameWithKind(node)
},
}))
const messages = linter.verify(key, {
rules: { test: "error" },
parserOptions: {
rules: { "test/test": "error" },
languageOptions: {
ecmaVersion: semver.gte(eslint.Linter.version, "8.0.0")
? 2022
: 2020,
sourceType: "module",
},
plugins: {
test: {
rules: {
test: {
create(_context) {
return {
":function"(node) {
actualResult =
getFunctionNameWithKind(node)
},
}
},
},
},
},
},
})

assert.strictEqual(
Expand All @@ -160,25 +172,36 @@ describe("The 'getFunctionNameWithKind' function", () => {
})

it(`should return "${expectedResult2}" for "${key}" if sourceCode is present.`, () => {
const linter = new eslint.Linter()
const linter = newCompatLinter()

let actualResult = null
linter.defineRule("test", (context) => ({
":function"(node) {
actualResult = getFunctionNameWithKind(
node,
context.getSourceCode(),
)
},
}))
const messages = linter.verify(key, {
rules: { test: "error" },
parserOptions: {
rules: { "test/test": "error" },
languageOptions: {
ecmaVersion: semver.gte(eslint.Linter.version, "8.0.0")
? 2022
: 2020,
sourceType: "module",
},
plugins: {
test: {
rules: {
test: {
create(context) {
return {
":function"(node) {
actualResult =
getFunctionNameWithKind(
node,
context.getSourceCode(),
)
},
}
},
},
},
},
},
})

assert.strictEqual(
Expand Down
63 changes: 42 additions & 21 deletions test/get-innermost-scope.mjs
Original file line number Diff line number Diff line change
@@ -1,75 +1,96 @@
import assert from "assert"
import eslint from "eslint"
import { getInnermostScope } from "../src/index.mjs"
import { getScope } from "./test-lib/get-scope.mjs"
import { getScope, newCompatLinter } from "./test-lib/eslint-compat.mjs"

describe("The 'getInnermostScope' function", () => {
let i = 0
for (const { code, parserOptions, selectNode, selectScope } of [
for (const { code, languageOptions, selectNode, selectScope } of [
{
code: "let a = 0",
parserOptions: {},
languageOptions: {},
selectNode: (node) => node,
selectScope: (scope) => scope,
},
{
code: "let a = 0",
parserOptions: { ecmaFeatures: { globalReturn: true } },
languageOptions: {
parserOptions: { ecmaFeatures: { globalReturn: true } },
},
selectNode: (node) => node,
selectScope: (scope) => scope.childScopes[0],
},
{
code: "let a = 0",
parserOptions: { sourceType: "module" },
languageOptions: { sourceType: "module" },
selectNode: (node) => node,
selectScope: (scope) => scope.childScopes[0],
},
{
code: "a; { b; { c; } d; } e;",
parserOptions: {},
languageOptions: {},
selectNode: (node) => node.body[0],
selectScope: (scope) => scope,
},
{
code: "a; { b; { c; } d; } e;",
parserOptions: {},
languageOptions: {},
selectNode: (node) => node.body[2],
selectScope: (scope) => scope,
},
{
code: "a; { b; { c; } d; } e;",
parserOptions: {},
languageOptions: {},
selectNode: (node) => node.body[1].body[0],
selectScope: (scope) => scope.childScopes[0],
},
{
code: "a; { b; { c; } d; } e;",
parserOptions: {},
languageOptions: {},
selectNode: (node) => node.body[1].body[2],
selectScope: (scope) => scope.childScopes[0],
},
{
code: "a; { b; { c; } d; } e;",
parserOptions: {},
languageOptions: {},
selectNode: (node) => node.body[1].body[1].body[0],
selectScope: (scope) => scope.childScopes[0].childScopes[0],
},
]) {
it(`should return the innermost scope (${++i})`, () => {
const linter = new eslint.Linter()
const linter = newCompatLinter()

let actualScope = null
let expectedScope = null
linter.defineRule("test", (context) => ({
Program(node) {
const scope = getScope(context, node)
actualScope = getInnermostScope(scope, selectNode(node))
expectedScope = selectScope(scope)
},
}))
linter.verify(code, {
parserOptions: { ecmaVersion: 2020, ...parserOptions },
rules: { test: "error" },
languageOptions: {
ecmaVersion: 2020,
sourceType: "script",
...languageOptions,
},
rules: { "test/test": "error" },
plugins: {
test: {
rules: {
test: {
create(context) {
return {
Program(node) {
const scope = getScope(
context,
node,
)
actualScope = getInnermostScope(
scope,
selectNode(node),
)
expectedScope = selectScope(scope)
},
}
},
},
},
},
},
})

assert.notStrictEqual(expectedScope, null)
Expand Down
Loading
Loading