Skip to content

Commit

Permalink
feat: Avoid dirname for built-in configs.
Browse files Browse the repository at this point in the history
Load eslint:recommended and eslint:all configs via import instead file paths.

Fixes: eslint/eslint#15575
  • Loading branch information
daidodo committed Feb 16, 2022
1 parent 8761efb commit 7ed74f7
Show file tree
Hide file tree
Showing 7 changed files with 3,276 additions and 1,439 deletions.
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
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
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) {
return this._normalizeConfigData(getEslintRecommendedConfig(), { ...ctx, name });
}
return this._loadConfigData({
...ctx,
filePath: eslintRecommendedPath,
name: `${ctx.name} » ${extendName}`
name,
filePath: eslintRecommendedPath
});
}
if (extendName === "eslint:all") {
const name = `${ctx.name} » ${extendName}`;

if (getEslintAllConfig) {
return this._normalizeConfigData(getEslintAllConfig(), { ...ctx, name });
}
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
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

0 comments on commit 7ed74f7

Please sign in to comment.