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

refactor: Upgrade ESLint #178

Merged
merged 8 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
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
76 changes: 76 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* @fileoverview ESLint configuration file
* @author Nicholas C. Zakas
*/

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

import eslintConfigESLint from "eslint-config-eslint";
import n from "eslint-plugin-n";
//-----------------------------------------------------------------------------
// Config
//-----------------------------------------------------------------------------

export default [

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

...eslintConfigESLint,
{
plugins: {
n,
},
languageOptions: {
ecmaVersion: 2020
},

/*
* it fixes eslint-plugin-jsdoc's reports: "Invalid JSDoc tag name "template" jsdoc/check-tag-names"
* refs: /~https://github.com/gajus/eslint-plugin-jsdoc#check-tag-names
*/
settings: {
jsdoc: {
mode: "typescript"
}
},
},
{
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."
}],

// Overcome /~https://github.com/mysticatea/eslint-plugin-node/issues/250
"n/no-unsupported-features/es-syntax": ["error", {
ignores: [
"modules",
"dynamicImport"
]
}]
}
}
];
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
Loading