Skip to content

Commit

Permalink
refactor: Upgrade ESLint (#178)
Browse files Browse the repository at this point in the history
* chore: Upgrade ESLint

* Add back eslintignore

* Skip tests relying on project files

* Update eslint.config.js

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* Update eslint.config.js

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* Update eslint.config.js

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* Update package.json

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* Add formatting config

---------

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
  • Loading branch information
nzakas and mdjermanovic authored Feb 14, 2025
1 parent f05aea7 commit 18d22b7
Show file tree
Hide file tree
Showing 30 changed files with 172 additions and 136 deletions.
42 changes: 0 additions & 42 deletions .eslintrc.cjs

This file was deleted.

2 changes: 1 addition & 1 deletion conf/environments.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function getDiff(current, prev) {
const retv = {};

for (const [key, value] of Object.entries(current)) {
if (!Object.hasOwnProperty.call(prev, key)) {
if (!Object.hasOwn(prev, key)) {
retv[key] = value;
}
}
Expand Down
52 changes: 52 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @fileoverview ESLint configuration file
* @author Nicholas C. Zakas
*/

//-----------------------------------------------------------------------------
// Imports
//-----------------------------------------------------------------------------

import eslintConfigESLint from "eslint-config-eslint";
import eslintConfigESLintFormatting from "eslint-config-eslint/formatting";

//-----------------------------------------------------------------------------
// Config
//-----------------------------------------------------------------------------

export default [

{
ignores: [
"tests/fixtures",
"coverage",
"docs",
"jsdoc",
"dist"
]
},

...eslintConfigESLint,
eslintConfigESLintFormatting,
{
files: ["tests/**/*"],
languageOptions: {
globals: {
describe: "readonly",
xdescribe: "readonly",
it: "readonly",
xit: "readonly",
beforeEach: "readonly",
afterEach: "readonly",
before: "readonly",
after: "readonly"
}
},
rules: {
"no-restricted-syntax": ["error", {
selector: "CallExpression[callee.object.name='assert'][callee.property.name='doesNotThrow']",
message: "`assert.doesNotThrow()` should be replaced with a comment next to the code."
}]
}
}
];
10 changes: 6 additions & 4 deletions lib/cascading-config-array-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
//------------------------------------------------------------------------------

import debugOrig from "debug";
import os from "os";
import path from "path";
import os from "node:os";
import path from "node:path";

import { ConfigArrayFactory } from "./config-array-factory.js";
import {
Expand Down Expand Up @@ -193,7 +193,7 @@ function createCLIConfigArray({
*/
class ConfigurationNotFoundError extends Error {

// eslint-disable-next-line jsdoc/require-description

/**
* @param {string} directoryPath The directory path.
*/
Expand Down Expand Up @@ -345,6 +345,7 @@ class CascadingConfigArrayFactory {
* @param {string} directoryPath The path to a leaf directory.
* @param {boolean} configsExistInSubdirs `true` if configurations exist in subdirectories.
* @returns {ConfigArray} The loaded config.
* @throws {Error} If a config file is invalid.
* @private
*/
_loadConfigInAncestors(directoryPath, configsExistInSubdirs = false) {
Expand Down Expand Up @@ -446,6 +447,7 @@ class CascadingConfigArrayFactory {
* @param {string} directoryPath The path to the leaf directory to find config files.
* @param {boolean} ignoreNotFoundError If `true` then it doesn't throw `ConfigurationNotFoundError`.
* @returns {ConfigArray} The loaded config.
* @throws {Error} If a config file is invalid.
* @private
*/
_finalizeConfigArray(configArray, directoryPath, ignoreNotFoundError) {
Expand Down Expand Up @@ -482,7 +484,7 @@ class CascadingConfigArrayFactory {
!directoryPath.startsWith(homePath)
) {
const lastElement =
personalConfigArray[personalConfigArray.length - 1];
personalConfigArray.at(-1);

emitDeprecationWarning(
lastElement.filePath,
Expand Down
19 changes: 13 additions & 6 deletions lib/config-array-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@
//------------------------------------------------------------------------------

import debugOrig from "debug";
import fs from "fs";
import fs from "node:fs";
import importFresh from "import-fresh";
import { createRequire } from "module";
import path from "path";
import { createRequire } from "node:module";
import path from "node:path";
import stripComments from "strip-json-comments";

import {
Expand Down Expand Up @@ -254,7 +254,7 @@ function loadPackageJSONConfigFile(filePath) {
try {
const packageData = loadJSONConfigFile(filePath);

if (!Object.hasOwnProperty.call(packageData, "eslintConfig")) {
if (!Object.hasOwn(packageData, "eslintConfig")) {
throw Object.assign(
new Error("package.json file doesn't have 'eslintConfig' field."),
{ code: "ESLINT_CONFIG_FIELD_NOT_FOUND" }
Expand All @@ -273,6 +273,7 @@ function loadPackageJSONConfigFile(filePath) {
* Loads a `.eslintignore` from a file.
* @param {string} filePath The filename to load.
* @returns {string[]} The ignore patterns from the file.
* @throws {Error} If the file cannot be read.
* @private
*/
function loadESLintIgnoreFile(filePath) {
Expand Down Expand Up @@ -345,7 +346,7 @@ function loadConfigFile(filePath) {
function writeDebugLogForLoading(request, relativeTo, filePath) {
/* istanbul ignore next */
if (debug.enabled) {
let nameAndVersion = null;
let nameAndVersion = null; // eslint-disable-line no-useless-assignment -- known bug in the rule

try {
const packageJsonPath = ModuleResolver.resolve(
Expand Down Expand Up @@ -510,6 +511,7 @@ class ConfigArrayFactory {
* @param {Object} [options] The options.
* @param {string} [options.basePath] The base path to resolve relative paths in `overrides[].files`, `overrides[].excludedFiles`, and `ignorePatterns`.
* @param {string} [options.name] The config name.
* @throws {Error} If the config file is invalid.
* @returns {ConfigArray} Loaded config. An empty `ConfigArray` if any config doesn't exist.
*/
loadInDirectory(directoryPath, { basePath, name } = {}) {
Expand Down Expand Up @@ -595,6 +597,7 @@ class ConfigArrayFactory {
/**
* Load `.eslintignore` file in the current working directory.
* @returns {ConfigArray} Loaded config. An empty `ConfigArray` if any config doesn't exist.
* @throws {Error} If the ignore file is invalid.
*/
loadDefaultESLintIgnore() {
const slots = internalSlotsMap.get(this);
Expand All @@ -607,7 +610,7 @@ class ConfigArrayFactory {
if (fs.existsSync(packageJsonPath)) {
const data = loadJSONConfigFile(packageJsonPath);

if (Object.hasOwnProperty.call(data, "eslintIgnore")) {
if (Object.hasOwn(data, "eslintIgnore")) {
if (!Array.isArray(data.eslintIgnore)) {
throw new Error("Package.json eslintIgnore property requires an array of paths");
}
Expand Down Expand Up @@ -796,6 +799,7 @@ class ConfigArrayFactory {
* @param {string} extendName The name of a base config.
* @param {ConfigArrayFactoryLoadingContext} ctx The loading context.
* @returns {IterableIterator<ConfigArrayElement>} The normalized config.
* @throws {Error} If the extended config file can't be loaded.
* @private
*/
_loadExtends(extendName, ctx) {
Expand All @@ -819,6 +823,7 @@ class ConfigArrayFactory {
* @param {string} extendName The name of a base config.
* @param {ConfigArrayFactoryLoadingContext} ctx The loading context.
* @returns {IterableIterator<ConfigArrayElement>} The normalized config.
* @throws {Error} If the extended config file can't be loaded.
* @private
*/
_loadExtendedBuiltInConfig(extendName, ctx) {
Expand Down Expand Up @@ -868,6 +873,7 @@ class ConfigArrayFactory {
* @param {string} extendName The name of a base config.
* @param {ConfigArrayFactoryLoadingContext} ctx The loading context.
* @returns {IterableIterator<ConfigArrayElement>} The normalized config.
* @throws {Error} If the extended config file can't be loaded.
* @private
*/
_loadExtendedPluginConfig(extendName, ctx) {
Expand Down Expand Up @@ -905,6 +911,7 @@ class ConfigArrayFactory {
* @param {string} extendName The name of a base config.
* @param {ConfigArrayFactoryLoadingContext} ctx The loading context.
* @returns {IterableIterator<ConfigArrayElement>} The normalized config.
* @throws {Error} If the extended config file can't be loaded.
* @private
*/
_loadExtendedShareableConfig(extendName, ctx) {
Expand Down
2 changes: 2 additions & 0 deletions lib/config-array/config-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ class PluginConflictError extends Error {
* @param {Record<string, DependentPlugin>} target The destination to merge
* @param {Record<string, DependentPlugin>|undefined} source The source to merge.
* @returns {void}
* @throws {PluginConflictError} When a plugin was conflicted.
*/
function mergePlugins(target, source) {
if (!isNonNullObject(source)) {
Expand Down Expand Up @@ -258,6 +259,7 @@ function mergeRuleConfigs(target, source) {
* @param {ConfigArray} instance The config elements.
* @param {number[]} indices The indices to use.
* @returns {ExtractedConfig} The extracted config.
* @throws {Error} When a plugin is conflicted.
*/
function createConfig(instance, indices) {
const config = new ExtractedConfig();
Expand Down
10 changes: 5 additions & 5 deletions lib/config-array/config-dependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @author Toru Nagashima <https://github.com/mysticatea>
*/

import util from "util";
import util from "node:util";

/**
* The class is to store parsers or plugins.
Expand Down Expand Up @@ -88,8 +88,8 @@ class ConfigDependency {
this.importerPath = importerPath;
}

// eslint-disable-next-line jsdoc/require-description
/**
* Converts this instance to a JSON compatible object.
* @returns {Object} a JSON compatible object.
*/
toJSON() {
Expand All @@ -103,14 +103,14 @@ class ConfigDependency {
return obj;
}

// eslint-disable-next-line jsdoc/require-description
/**
* Custom inspect method for Node.js `console.log()`.
* @returns {Object} an object to display by `console.log()`.
*/
[util.inspect.custom]() {
const {
definition: _ignore1, // eslint-disable-line no-unused-vars
original: _ignore2, // eslint-disable-line no-unused-vars
definition: _ignore1, // eslint-disable-line no-unused-vars -- needed to make `obj` correct
original: _ignore2, // eslint-disable-line no-unused-vars -- needed to make `obj` correct
...obj
} = this;

Expand Down
4 changes: 2 additions & 2 deletions lib/config-array/extracted-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ class ExtractedConfig {
*/
toCompatibleObjectAsConfigFileContent() {
const {
/* eslint-disable no-unused-vars */
/* eslint-disable no-unused-vars -- needed to make `config` correct */
configNameOfNoInlineConfig: _ignore1,
processor: _ignore2,
/* eslint-enable no-unused-vars */
/* eslint-enable no-unused-vars -- needed to make `config` correct */
ignores,
...config
} = this;
Expand Down
11 changes: 6 additions & 5 deletions lib/config-array/ignore-pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
// Requirements
//------------------------------------------------------------------------------

import assert from "assert";
import path from "path";
import assert from "node:assert";
import path from "node:path";
import ignore from "ignore";
import debugOrig from "debug";

Expand Down Expand Up @@ -117,6 +117,9 @@ const DotPatterns = Object.freeze([".*", "!.eslintrc.*", "!../"]);
// Public
//------------------------------------------------------------------------------

/**
*
*/
class IgnorePattern {

/**
Expand Down Expand Up @@ -153,9 +156,7 @@ class IgnorePattern {
debug("Create with: %o", ignorePatterns);

const basePath = getCommonAncestorPath(ignorePatterns.map(p => p.basePath));
const patterns = [].concat(
...ignorePatterns.map(p => p.getPatternsRelativeTo(basePath))
);
const patterns = ignorePatterns.flatMap(p => p.getPatternsRelativeTo(basePath));
const ig = ignore({ allowRelativePaths: true }).add([...DotPatterns, ...patterns]);
const dotIg = ignore({ allowRelativePaths: true }).add(patterns);

Expand Down
12 changes: 7 additions & 5 deletions lib/config-array/override-tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
* @author Toru Nagashima <https://github.com/mysticatea>
*/

import assert from "assert";
import path from "path";
import util from "util";
import assert from "node:assert";
import path from "node:path";
import util from "node:util";
import minimatch from "minimatch";

const { Minimatch } = minimatch;
Expand Down Expand Up @@ -94,6 +94,7 @@ class OverrideTester {
* @param {string|string[]} excludedFiles The glob patterns for excluded files.
* @param {string} basePath The path to the base directory to test paths.
* @returns {OverrideTester|null} The created instance or `null`.
* @throws {Error} When invalid patterns are given.
*/
static create(files, excludedFiles, basePath) {
const includePatterns = normalizePatterns(files);
Expand Down Expand Up @@ -183,6 +184,7 @@ class OverrideTester {
* Test if a given path is matched or not.
* @param {string} filePath The absolute path to the target file.
* @returns {boolean} `true` if the path was matched.
* @throws {Error} When invalid `filePath` is given.
*/
test(filePath) {
if (typeof filePath !== "string" || !path.isAbsolute(filePath)) {
Expand All @@ -196,8 +198,8 @@ class OverrideTester {
));
}

// eslint-disable-next-line jsdoc/require-description
/**
* Converts this instance to a JSON compatible object.
* @returns {Object} a JSON compatible object.
*/
toJSON() {
Expand All @@ -213,8 +215,8 @@ class OverrideTester {
};
}

// eslint-disable-next-line jsdoc/require-description
/**
* Custom inspect method for Node.js `console.log()`.
* @returns {Object} an object to display by `console.log()`.
*/
[util.inspect.custom]() {
Expand Down
Loading

0 comments on commit 18d22b7

Please # to comment.