Skip to content

Commit

Permalink
feat: add ESLint v9 compatibility layer
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDeBoey committed Oct 16, 2024
1 parent 3ee07fc commit 9e8e0e8
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 83 deletions.
23 changes: 23 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,29 @@ module.exports = {
"plugin:@eslint-community/mysticatea/es2015",
"plugin:@eslint-community/mysticatea/+eslint-plugin",
],
rules: {
"no-restricted-properties": [
"error",
{
object: "context",
property: "getDeclaredVariables",
message:
"If you are using it in a test case, use test/test-lib/eslint-compat.mjs#getDeclaredVariables instead. Other than that, the API should also be compatible with ESLint v9.",
},
{
object: "context",
property: "getSourceCode",
message:
"If you are using it in a test case, use test/test-lib/eslint-compat.mjs#getSourceCode instead. Other than that, the API should also be compatible with ESLint v9.",
},
{
object: "context",
property: "getScope",
message:
"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.",
},
],
},
overrides: [
{
files: ["lib/utils.js", "scripts/*.js"],
Expand Down
24 changes: 24 additions & 0 deletions lib/eslint-compat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* eslint-disable no-restricted-properties */

Check failure on line 1 in lib/eslint-compat.js

View workflow job for this annotation

GitHub Actions / ⬣ Lint (ESLint@6)

Requires 'eslint-enable' directive for 'no-restricted-properties'

Check failure on line 1 in lib/eslint-compat.js

View workflow job for this annotation

GitHub Actions / ⬣ Lint (ESLint@7)

Requires 'eslint-enable' directive for 'no-restricted-properties'

Check failure on line 1 in lib/eslint-compat.js

View workflow job for this annotation

GitHub Actions / ⬣ Lint (ESLint@8)

Requires 'eslint-enable' directive for 'no-restricted-properties'

"use strict"

function getDeclaredVariables(context, node) {
return (
getSourceCode(context).getDeclaredVariables?.(node) ??
context.getDeclaredVariables(node)
)
}

function getSourceCode(context) {
return context.sourceCode ?? context.getSourceCode()
}

function getScope(context, node) {
return getSourceCode(context).getScope?.(node) ?? context.getScope()
}

module.exports = {
getDeclaredVariables,
getSourceCode,
getScope,
}
23 changes: 9 additions & 14 deletions lib/rules/arrow-parens.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
/**
* @author Toru Nagashima
* @copyright 2015 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
"use strict"

const { getSourceCode } = require("../eslint-compat")

/**
* Checks whether or not a given token is `(`.
* Checks whether a given token is `(`.
* @param {Token} token - A token to check.
* @returns {boolean} `true` when the token is `(`.
*/
Expand All @@ -15,7 +12,7 @@ function isOpenParen(token) {
}

/**
* Checks whether or not given two tokens are at a same line.
* Checks whether given two tokens are at a same line.
* @param {Token} a - A left token.
* @param {Token} b - A right token.
* @returns {boolean} `true` when the tokens are at a same line.
Expand Down Expand Up @@ -43,12 +40,11 @@ module.exports = {
type: "suggestion",
},
create(context) {
const sourceCode = getSourceCode(context)
return {
ArrowFunctionExpression(node) {
const first = context
.getSourceCode()
.getFirstToken(node, node.async ? 1 : 0)
const before = context.getSourceCode().getTokenBefore(first)
const first = sourceCode.getFirstToken(node, node.async ? 1 : 0)
const before = sourceCode.getTokenBefore(first)

if (isOpenParen(first)) {
if (
Expand All @@ -63,9 +59,8 @@ module.exports = {
fix(fixer) {
const id = node.params[0]
const begin = first.range[0]
const end = context
.getSourceCode()
.getTokenAfter(id).range[1]
const end =
sourceCode.getTokenAfter(id).range[1]

return fixer.replaceTextRange(
[begin, end],
Expand Down
17 changes: 7 additions & 10 deletions lib/rules/block-scoped-var.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/**
* @author Toru Nagashima
* @copyright 2015 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
"use strict"

const { getDeclaredVariables } = require("../eslint-compat")

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
Expand All @@ -15,7 +12,7 @@ const containerNodeType =
/^(?:For(?:In|Of)?Statement|(?:Arrow)?Function(?:Declaration|Expression))$/u

/**
* Checks whether or not a given definition should be skipped.
* Checks whether a given definition should be skipped.
* @param {escope.Variable.DefEntry} def - A definition to check.
* @param {escope.Variable.DefEntry[]} defs - A definition list which includes `def`.
* @param {escope.Variable} variable - A variable which is defined by the definition.
Expand Down Expand Up @@ -180,7 +177,7 @@ class PseudoScope {
}

/**
* Turns an used flag on.
* Turns a used flag on.
* @returns {void}
*/
markAsUsed() {
Expand Down Expand Up @@ -213,12 +210,12 @@ module.exports = {
},
create(context) {
/**
* Finds and reports references which are outside of valid scopes.
* Finds and reports references which are outside valid scopes.
* @param {ASTNode} node - A node to get variables.
* @returns {void}
*/
function checkForVariables(node) {
const variables = context.getDeclaredVariables(node)
const variables = getDeclaredVariables(context, node)
for (const variable of variables) {
const defs = variable.defs
const lastDef = defs[defs.length - 1]
Expand All @@ -235,7 +232,7 @@ module.exports = {
continue
}

// Check whether or not any reading reference exists.
// Check whether any reading reference exists.
// And while it does, warn references which does not belong to any
// scope.
let hasReadRef = false
Expand Down
13 changes: 5 additions & 8 deletions lib/rules/no-instanceof-array.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/**
* @author Toru Nagashima
* @copyright 2016 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
"use strict"

const { getScope, getSourceCode } = require("../eslint-compat")

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand All @@ -26,7 +23,7 @@ module.exports = {
},

create(context) {
const sourceCode = context.getSourceCode()
const sourceCode = getSourceCode(context)

/**
* Checks whether the given node is RHS of instanceof.
Expand All @@ -43,8 +40,8 @@ module.exports = {
}

return {
"Program:exit"() {
const globalScope = context.getScope()
"Program:exit"(globalNode) {
const globalScope = getScope(context, globalNode)
const variable = globalScope.set.get("Array")

// Skip if undefined or shadowed
Expand Down
13 changes: 5 additions & 8 deletions lib/rules/no-instanceof-wrapper.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/**
* @author Toru Nagashima
* @copyright 2016 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
"use strict"

const { getScope, getSourceCode } = require("../eslint-compat")

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand All @@ -26,7 +23,7 @@ module.exports = {
},

create(context) {
const sourceCode = context.getSourceCode()
const sourceCode = getSourceCode(context)
const targetTypes = [
"Boolean",
"Number",
Expand All @@ -51,8 +48,8 @@ module.exports = {
}

return {
"Program:exit"() {
const globalScope = context.getScope()
"Program:exit"(globalNode) {
const globalScope = getScope(context, globalNode)

for (const ctorName of targetTypes) {
const typeName = ctorName.toLowerCase()
Expand Down
11 changes: 4 additions & 7 deletions lib/rules/no-this-in-static.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/**
* @author Toru Nagashima
* @copyright 2016 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
"use strict"

const { getSourceCode } = require("../eslint-compat")

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand All @@ -25,7 +22,7 @@ module.exports = {
},

create(context) {
const sourceCode = context.getSourceCode()
const sourceCode = getSourceCode(context)
let funcInfo = null

/**
Expand Down Expand Up @@ -65,7 +62,7 @@ module.exports = {
}

/**
* Reports the `this`/`super` node if this is inside of a static method.
* Reports the `this`/`super` node if this is inside a static method.
*
* @param {ASTNode} node - The node to report.
* @returns {void}
Expand Down
10 changes: 4 additions & 6 deletions lib/rules/no-use-ignored-vars.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/**
* @fileoverview Rule to disallow a use of ignored variables.
* @author Toru Nagashima
*/
"use strict"

const { getScope } = require("../eslint-compat")

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -84,8 +82,8 @@ module.exports = {
}

return {
"Program:exit"() {
const queue = [context.getScope()]
"Program:exit"(node) {
const queue = [getScope(context, node)]
let scope = null

while ((scope = queue.pop()) != null) {
Expand Down
19 changes: 8 additions & 11 deletions lib/rules/no-useless-rest-spread.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/**
* @fileoverview Rule to disallow unnecessary spread operators.
* @author Toru Nagashima
*/
"use strict"

const { getSourceCode } = require("../eslint-compat")

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -109,7 +107,7 @@ module.exports = {
},

create(context) {
const sourceCode = context.getSourceCode()
const sourceCode = getSourceCode(context)

/**
* Verify the given SpreadElement or RestElement.
Expand All @@ -133,12 +131,11 @@ module.exports = {
const isRestParameter =
nodeType === "RestElement" && argumentType !== parentType
const type1 = nodeType === "RestElement" ? "rest" : "spread"
const type2 =
/* eslint-disable @eslint-community/mysticatea/prettier */
isRestParameter ? "parameter" :
isArray ? "element" :
/* otherwise */ "property"
/* eslint-enable @eslint-community/mysticatea/prettier */
const type2 = isRestParameter
? "parameter"
: isArray
? "element"
: /* otherwise */ "property"

context.report({
node,
Expand Down
Loading

0 comments on commit 9e8e0e8

Please sign in to comment.