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

feat: Avoid dirname for built-in configs. #71

Merged
merged 5 commits into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 0 additions & 12 deletions conf/eslint-all.cjs

This file was deleted.

12 changes: 0 additions & 12 deletions conf/eslint-recommended.cjs

This file was deleted.

23 changes: 18 additions & 5 deletions lib/cascading-config-array-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@
// Requirements
//------------------------------------------------------------------------------

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

import { ConfigArrayFactory } from "./config-array-factory.js";
import {
ConfigArray,
ConfigDependency,
IgnorePattern
} from "./config-array/index.js";
import ConfigValidator from "./shared/config-validator.js";
import { emitDeprecationWarning } from "./shared/deprecation-warnings.js";
import { ConfigArrayFactory } from "./config-array-factory.js";
import { ConfigArray, ConfigDependency, IgnorePattern } from "./config-array/index.js";
import debugOrig from "debug";

const debug = debugOrig("eslintrc:cascading-config-array-factory");

Expand Down Expand Up @@ -57,7 +62,9 @@ const debug = debugOrig("eslintrc:cascading-config-array-factory");
* @property {Map<string,Rule>} builtInRules The rules that are built in to ESLint.
* @property {Object} [resolver=ModuleResolver] The module resolver object.
* @property {string} eslintAllPath The path to the definitions for eslint:all.
* @property {Function} getEslintAllConfig Returns the config data for eslint:all.
* @property {string} eslintRecommendedPath The path to the definitions for eslint:recommended.
* @property {Function} getEslintRecommendedConfig Returns the config data for eslint:recommended.
*/

/**
Expand All @@ -78,7 +85,9 @@ const debug = debugOrig("eslintrc:cascading-config-array-factory");
* @property {Map<string,Rule>} builtInRules The rules that are built in to ESLint.
* @property {Object} [resolver=ModuleResolver] The module resolver object.
* @property {string} eslintAllPath The path to the definitions for eslint:all.
* @property {Function} getEslintAllConfig Returns the config data for eslint:all.
* @property {string} eslintRecommendedPath The path to the definitions for eslint:recommended.
* @property {Function} getEslintRecommendedConfig Returns the config data for eslint:recommended.
*/

/** @type {WeakMap<CascadingConfigArrayFactory, CascadingConfigArrayFactoryInternalSlots>} */
Expand Down Expand Up @@ -219,7 +228,9 @@ class CascadingConfigArrayFactory {
loadRules,
resolver,
eslintRecommendedPath,
eslintAllPath
getEslintRecommendedConfig,
eslintAllPath,
getEslintAllConfig
} = {}) {
const configArrayFactory = new ConfigArrayFactory({
additionalPluginPool,
Expand All @@ -228,7 +239,9 @@ class CascadingConfigArrayFactory {
builtInRules,
resolver,
eslintRecommendedPath,
eslintAllPath
getEslintRecommendedConfig,
eslintAllPath,
getEslintAllConfig
});

internalSlotsMap.set(this, {
Expand Down
50 changes: 37 additions & 13 deletions lib/config-array-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,23 @@
// Requirements
//------------------------------------------------------------------------------

import debugOrig from "debug";
import fs from "fs";
import path from "path";
import importFresh from "import-fresh";
import { createRequire } from "module";
import path from "path";
import stripComments from "strip-json-comments";
import ConfigValidator from "./shared/config-validator.js";
import * as naming from "./shared/naming.js";
import * as ModuleResolver from "./shared/relative-module-resolver.js";

import {
ConfigArray,
ConfigDependency,
IgnorePattern,
OverrideTester
} from "./config-array/index.js";
import debugOrig from "debug";
import ConfigValidator from "./shared/config-validator.js";
import * as naming from "./shared/naming.js";
import * as ModuleResolver from "./shared/relative-module-resolver.js";

import { createRequire } from "module";
const require = createRequire(import.meta.url);

const debug = debugOrig("eslintrc:config-array-factory");
Expand Down Expand Up @@ -90,7 +91,9 @@ const configFilenames = [
* @property {Map<string,Rule>} builtInRules The rules that are built in to ESLint.
* @property {Object} [resolver=ModuleResolver] The module resolver object.
* @property {string} eslintAllPath The path to the definitions for eslint:all.
* @property {Function} getEslintAllConfig Returns the config data for eslint:all.
* @property {string} eslintRecommendedPath The path to the definitions for eslint:recommended.
* @property {Function} getEslintRecommendedConfig Returns the config data for eslint:recommended.
*/

/**
Expand All @@ -101,7 +104,9 @@ const configFilenames = [
* @property {Map<string,Rule>} builtInRules The rules that are built in to ESLint.
* @property {Object} [resolver=ModuleResolver] The module resolver object.
* @property {string} eslintAllPath The path to the definitions for eslint:all.
* @property {Function} getEslintAllConfig Returns the config data for eslint:all.
* @property {string} eslintRecommendedPath The path to the definitions for eslint:recommended.
* @property {Function} getEslintRecommendedConfig Returns the config data for eslint:recommended.
*/

/**
Expand Down Expand Up @@ -428,7 +433,9 @@ class ConfigArrayFactory {
builtInRules,
resolver = ModuleResolver,
eslintAllPath,
eslintRecommendedPath
getEslintAllConfig,
eslintRecommendedPath,
getEslintRecommendedConfig
} = {}) {
internalSlotsMap.set(this, {
additionalPluginPool,
Expand All @@ -439,7 +446,9 @@ class ConfigArrayFactory {
builtInRules,
resolver,
eslintAllPath,
eslintRecommendedPath
getEslintAllConfig,
eslintRecommendedPath,
getEslintRecommendedConfig
});
}

Expand Down Expand Up @@ -797,20 +806,35 @@ class ConfigArrayFactory {
* @private
*/
_loadExtendedBuiltInConfig(extendName, ctx) {
const { eslintAllPath, eslintRecommendedPath } = internalSlotsMap.get(this);
const {
eslintAllPath,
getEslintAllConfig,
eslintRecommendedPath,
getEslintRecommendedConfig
} = internalSlotsMap.get(this);

if (extendName === "eslint:recommended") {
const name = `${ctx.name} » ${extendName}`;

if (getEslintRecommendedConfig) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add in a check here to make sure getEslintRecommendedConfig is a function, and throw an error if not? I can picture this being difficult to debug if someone passes a nonfunction accidentally.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

return this._normalizeConfigData(getEslintRecommendedConfig(), { ...ctx, name });
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
}
return this._loadConfigData({
...ctx,
filePath: eslintRecommendedPath,
name: `${ctx.name} » ${extendName}`
name,
filePath: eslintRecommendedPath
});
}
if (extendName === "eslint:all") {
const name = `${ctx.name} » ${extendName}`;

if (getEslintAllConfig) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for getEslintAllConfig

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

return this._normalizeConfigData(getEslintAllConfig(), { ...ctx, name });
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
}
return this._loadConfigData({
...ctx,
filePath: eslintAllPath,
name: `${ctx.name} » ${extendName}`
name,
filePath: eslintAllPath
});
}

Expand Down
11 changes: 4 additions & 7 deletions lib/flat-compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@
// Requirements
//-----------------------------------------------------------------------------

import path from "path";
import { fileURLToPath } from "url";
import createDebug from "debug";
import path from "path";

import { ConfigArrayFactory } from "./config-array-factory.js";
import environments from "../conf/environments.js";

const dirname = path.dirname(fileURLToPath(import.meta.url));
import { ConfigArrayFactory } from "./config-array-factory.js";

//-----------------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -225,8 +222,8 @@ class FlatCompat {
this[cafactory] = new ConfigArrayFactory({
cwd: baseDirectory,
resolvePluginsRelativeTo,
eslintAllPath: path.resolve(dirname, "../conf/eslint-all.cjs"),
eslintRecommendedPath: path.resolve(dirname, "../conf/eslint-recommended.cjs")
getEslintAllConfig: () => ({ settings: { "eslint:all": true } }),
getEslintRecommendedConfig: () => ({ settings: { "eslint:recommended": true } })
});
}

Expand Down
Loading