Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Jan 10, 2024
1 parent ad431e9 commit 8b2da74
Show file tree
Hide file tree
Showing 13 changed files with 469 additions and 227 deletions.
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
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
31 changes: 21 additions & 10 deletions test/get-property-name.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 { getPropertyName } from "../src/index.mjs"
import { newCompatLinter } from "./test-lib/eslint-compat.mjs"

describe("The 'getPropertyName' function", () => {
for (const { code, expected } of [
Expand Down Expand Up @@ -56,23 +57,33 @@ describe("The 'getPropertyName' function", () => {
: []),
]) {
it(`should return ${JSON.stringify(expected)} from ${code}`, () => {
const linter = new eslint.Linter()
const linter = newCompatLinter()

let actual = null
linter.defineRule("test", () => ({
"Property,PropertyDefinition,MethodDefinition,MemberExpression"(
node,
) {
actual = getPropertyName(node)
},
}))
const messages = linter.verify(code, {
parserOptions: {
languageOptions: {
ecmaVersion: semver.gte(eslint.Linter.version, "8.0.0")
? 2022
: 2020,
},
rules: { test: "error" },
rules: { "test/test": "error" },
plugins: {
test: {
rules: {
test: {
create(_context) {
return {
"Property,PropertyDefinition,MethodDefinition,MemberExpression"(
node,
) {
actual = getPropertyName(node)
},
}
},
},
},
},
},
})
assert.strictEqual(
messages.length,
Expand Down
Loading

0 comments on commit 8b2da74

Please sign in to comment.