Skip to content

Commit 18d22b7

Browse files
nzakasmdjermanovic
andauthored
refactor: Upgrade ESLint (#178)
* chore: Upgrade ESLint * Add back eslintignore * Skip tests relying on project files * Update eslint.config.js Co-authored-by: Milos Djermanovic <[email protected]> * Update eslint.config.js Co-authored-by: Milos Djermanovic <[email protected]> * Update eslint.config.js Co-authored-by: Milos Djermanovic <[email protected]> * Update package.json Co-authored-by: Milos Djermanovic <[email protected]> * Add formatting config --------- Co-authored-by: Milos Djermanovic <[email protected]>
1 parent f05aea7 commit 18d22b7

30 files changed

+172
-136
lines changed

.eslintrc.cjs

-42
This file was deleted.

conf/environments.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function getDiff(current, prev) {
2323
const retv = {};
2424

2525
for (const [key, value] of Object.entries(current)) {
26-
if (!Object.hasOwnProperty.call(prev, key)) {
26+
if (!Object.hasOwn(prev, key)) {
2727
retv[key] = value;
2828
}
2929
}

eslint.config.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @fileoverview ESLint configuration file
3+
* @author Nicholas C. Zakas
4+
*/
5+
6+
//-----------------------------------------------------------------------------
7+
// Imports
8+
//-----------------------------------------------------------------------------
9+
10+
import eslintConfigESLint from "eslint-config-eslint";
11+
import eslintConfigESLintFormatting from "eslint-config-eslint/formatting";
12+
13+
//-----------------------------------------------------------------------------
14+
// Config
15+
//-----------------------------------------------------------------------------
16+
17+
export default [
18+
19+
{
20+
ignores: [
21+
"tests/fixtures",
22+
"coverage",
23+
"docs",
24+
"jsdoc",
25+
"dist"
26+
]
27+
},
28+
29+
...eslintConfigESLint,
30+
eslintConfigESLintFormatting,
31+
{
32+
files: ["tests/**/*"],
33+
languageOptions: {
34+
globals: {
35+
describe: "readonly",
36+
xdescribe: "readonly",
37+
it: "readonly",
38+
xit: "readonly",
39+
beforeEach: "readonly",
40+
afterEach: "readonly",
41+
before: "readonly",
42+
after: "readonly"
43+
}
44+
},
45+
rules: {
46+
"no-restricted-syntax": ["error", {
47+
selector: "CallExpression[callee.object.name='assert'][callee.property.name='doesNotThrow']",
48+
message: "`assert.doesNotThrow()` should be replaced with a comment next to the code."
49+
}]
50+
}
51+
}
52+
];

lib/cascading-config-array-factory.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
//------------------------------------------------------------------------------
2424

2525
import debugOrig from "debug";
26-
import os from "os";
27-
import path from "path";
26+
import os from "node:os";
27+
import path from "node:path";
2828

2929
import { ConfigArrayFactory } from "./config-array-factory.js";
3030
import {
@@ -193,7 +193,7 @@ function createCLIConfigArray({
193193
*/
194194
class ConfigurationNotFoundError extends Error {
195195

196-
// eslint-disable-next-line jsdoc/require-description
196+
197197
/**
198198
* @param {string} directoryPath The directory path.
199199
*/
@@ -345,6 +345,7 @@ class CascadingConfigArrayFactory {
345345
* @param {string} directoryPath The path to a leaf directory.
346346
* @param {boolean} configsExistInSubdirs `true` if configurations exist in subdirectories.
347347
* @returns {ConfigArray} The loaded config.
348+
* @throws {Error} If a config file is invalid.
348349
* @private
349350
*/
350351
_loadConfigInAncestors(directoryPath, configsExistInSubdirs = false) {
@@ -446,6 +447,7 @@ class CascadingConfigArrayFactory {
446447
* @param {string} directoryPath The path to the leaf directory to find config files.
447448
* @param {boolean} ignoreNotFoundError If `true` then it doesn't throw `ConfigurationNotFoundError`.
448449
* @returns {ConfigArray} The loaded config.
450+
* @throws {Error} If a config file is invalid.
449451
* @private
450452
*/
451453
_finalizeConfigArray(configArray, directoryPath, ignoreNotFoundError) {
@@ -482,7 +484,7 @@ class CascadingConfigArrayFactory {
482484
!directoryPath.startsWith(homePath)
483485
) {
484486
const lastElement =
485-
personalConfigArray[personalConfigArray.length - 1];
487+
personalConfigArray.at(-1);
486488

487489
emitDeprecationWarning(
488490
lastElement.filePath,

lib/config-array-factory.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@
3939
//------------------------------------------------------------------------------
4040

4141
import debugOrig from "debug";
42-
import fs from "fs";
42+
import fs from "node:fs";
4343
import importFresh from "import-fresh";
44-
import { createRequire } from "module";
45-
import path from "path";
44+
import { createRequire } from "node:module";
45+
import path from "node:path";
4646
import stripComments from "strip-json-comments";
4747

4848
import {
@@ -254,7 +254,7 @@ function loadPackageJSONConfigFile(filePath) {
254254
try {
255255
const packageData = loadJSONConfigFile(filePath);
256256

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

350351
try {
351352
const packageJsonPath = ModuleResolver.resolve(
@@ -510,6 +511,7 @@ class ConfigArrayFactory {
510511
* @param {Object} [options] The options.
511512
* @param {string} [options.basePath] The base path to resolve relative paths in `overrides[].files`, `overrides[].excludedFiles`, and `ignorePatterns`.
512513
* @param {string} [options.name] The config name.
514+
* @throws {Error} If the config file is invalid.
513515
* @returns {ConfigArray} Loaded config. An empty `ConfigArray` if any config doesn't exist.
514516
*/
515517
loadInDirectory(directoryPath, { basePath, name } = {}) {
@@ -595,6 +597,7 @@ class ConfigArrayFactory {
595597
/**
596598
* Load `.eslintignore` file in the current working directory.
597599
* @returns {ConfigArray} Loaded config. An empty `ConfigArray` if any config doesn't exist.
600+
* @throws {Error} If the ignore file is invalid.
598601
*/
599602
loadDefaultESLintIgnore() {
600603
const slots = internalSlotsMap.get(this);
@@ -607,7 +610,7 @@ class ConfigArrayFactory {
607610
if (fs.existsSync(packageJsonPath)) {
608611
const data = loadJSONConfigFile(packageJsonPath);
609612

610-
if (Object.hasOwnProperty.call(data, "eslintIgnore")) {
613+
if (Object.hasOwn(data, "eslintIgnore")) {
611614
if (!Array.isArray(data.eslintIgnore)) {
612615
throw new Error("Package.json eslintIgnore property requires an array of paths");
613616
}
@@ -796,6 +799,7 @@ class ConfigArrayFactory {
796799
* @param {string} extendName The name of a base config.
797800
* @param {ConfigArrayFactoryLoadingContext} ctx The loading context.
798801
* @returns {IterableIterator<ConfigArrayElement>} The normalized config.
802+
* @throws {Error} If the extended config file can't be loaded.
799803
* @private
800804
*/
801805
_loadExtends(extendName, ctx) {
@@ -819,6 +823,7 @@ class ConfigArrayFactory {
819823
* @param {string} extendName The name of a base config.
820824
* @param {ConfigArrayFactoryLoadingContext} ctx The loading context.
821825
* @returns {IterableIterator<ConfigArrayElement>} The normalized config.
826+
* @throws {Error} If the extended config file can't be loaded.
822827
* @private
823828
*/
824829
_loadExtendedBuiltInConfig(extendName, ctx) {
@@ -868,6 +873,7 @@ class ConfigArrayFactory {
868873
* @param {string} extendName The name of a base config.
869874
* @param {ConfigArrayFactoryLoadingContext} ctx The loading context.
870875
* @returns {IterableIterator<ConfigArrayElement>} The normalized config.
876+
* @throws {Error} If the extended config file can't be loaded.
871877
* @private
872878
*/
873879
_loadExtendedPluginConfig(extendName, ctx) {
@@ -905,6 +911,7 @@ class ConfigArrayFactory {
905911
* @param {string} extendName The name of a base config.
906912
* @param {ConfigArrayFactoryLoadingContext} ctx The loading context.
907913
* @returns {IterableIterator<ConfigArrayElement>} The normalized config.
914+
* @throws {Error} If the extended config file can't be loaded.
908915
* @private
909916
*/
910917
_loadExtendedShareableConfig(extendName, ctx) {

lib/config-array/config-array.js

+2
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ class PluginConflictError extends Error {
178178
* @param {Record<string, DependentPlugin>} target The destination to merge
179179
* @param {Record<string, DependentPlugin>|undefined} source The source to merge.
180180
* @returns {void}
181+
* @throws {PluginConflictError} When a plugin was conflicted.
181182
*/
182183
function mergePlugins(target, source) {
183184
if (!isNonNullObject(source)) {
@@ -258,6 +259,7 @@ function mergeRuleConfigs(target, source) {
258259
* @param {ConfigArray} instance The config elements.
259260
* @param {number[]} indices The indices to use.
260261
* @returns {ExtractedConfig} The extracted config.
262+
* @throws {Error} When a plugin is conflicted.
261263
*/
262264
function createConfig(instance, indices) {
263265
const config = new ExtractedConfig();

lib/config-array/config-dependency.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* @author Toru Nagashima <https://github.com/mysticatea>
1616
*/
1717

18-
import util from "util";
18+
import util from "node:util";
1919

2020
/**
2121
* The class is to store parsers or plugins.
@@ -88,8 +88,8 @@ class ConfigDependency {
8888
this.importerPath = importerPath;
8989
}
9090

91-
// eslint-disable-next-line jsdoc/require-description
9291
/**
92+
* Converts this instance to a JSON compatible object.
9393
* @returns {Object} a JSON compatible object.
9494
*/
9595
toJSON() {
@@ -103,14 +103,14 @@ class ConfigDependency {
103103
return obj;
104104
}
105105

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

lib/config-array/extracted-config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,10 @@ class ExtractedConfig {
120120
*/
121121
toCompatibleObjectAsConfigFileContent() {
122122
const {
123-
/* eslint-disable no-unused-vars */
123+
/* eslint-disable no-unused-vars -- needed to make `config` correct */
124124
configNameOfNoInlineConfig: _ignore1,
125125
processor: _ignore2,
126-
/* eslint-enable no-unused-vars */
126+
/* eslint-enable no-unused-vars -- needed to make `config` correct */
127127
ignores,
128128
...config
129129
} = this;

lib/config-array/ignore-pattern.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
// Requirements
3333
//------------------------------------------------------------------------------
3434

35-
import assert from "assert";
36-
import path from "path";
35+
import assert from "node:assert";
36+
import path from "node:path";
3737
import ignore from "ignore";
3838
import debugOrig from "debug";
3939

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

120+
/**
121+
*
122+
*/
120123
class IgnorePattern {
121124

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

155158
const basePath = getCommonAncestorPath(ignorePatterns.map(p => p.basePath));
156-
const patterns = [].concat(
157-
...ignorePatterns.map(p => p.getPatternsRelativeTo(basePath))
158-
);
159+
const patterns = ignorePatterns.flatMap(p => p.getPatternsRelativeTo(basePath));
159160
const ig = ignore({ allowRelativePaths: true }).add([...DotPatterns, ...patterns]);
160161
const dotIg = ignore({ allowRelativePaths: true }).add(patterns);
161162

lib/config-array/override-tester.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
* @author Toru Nagashima <https://github.com/mysticatea>
1818
*/
1919

20-
import assert from "assert";
21-
import path from "path";
22-
import util from "util";
20+
import assert from "node:assert";
21+
import path from "node:path";
22+
import util from "node:util";
2323
import minimatch from "minimatch";
2424

2525
const { Minimatch } = minimatch;
@@ -94,6 +94,7 @@ class OverrideTester {
9494
* @param {string|string[]} excludedFiles The glob patterns for excluded files.
9595
* @param {string} basePath The path to the base directory to test paths.
9696
* @returns {OverrideTester|null} The created instance or `null`.
97+
* @throws {Error} When invalid patterns are given.
9798
*/
9899
static create(files, excludedFiles, basePath) {
99100
const includePatterns = normalizePatterns(files);
@@ -183,6 +184,7 @@ class OverrideTester {
183184
* Test if a given path is matched or not.
184185
* @param {string} filePath The absolute path to the target file.
185186
* @returns {boolean} `true` if the path was matched.
187+
* @throws {Error} When invalid `filePath` is given.
186188
*/
187189
test(filePath) {
188190
if (typeof filePath !== "string" || !path.isAbsolute(filePath)) {
@@ -196,8 +198,8 @@ class OverrideTester {
196198
));
197199
}
198200

199-
// eslint-disable-next-line jsdoc/require-description
200201
/**
202+
* Converts this instance to a JSON compatible object.
201203
* @returns {Object} a JSON compatible object.
202204
*/
203205
toJSON() {
@@ -213,8 +215,8 @@ class OverrideTester {
213215
};
214216
}
215217

216-
// eslint-disable-next-line jsdoc/require-description
217218
/**
219+
* Custom inspect method for Node.js `console.log()`.
218220
* @returns {Object} an object to display by `console.log()`.
219221
*/
220222
[util.inspect.custom]() {

0 commit comments

Comments
 (0)