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 18, 2022
2 parents 32470f6 + 7ed74f7 commit b4803f0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
6 changes: 6 additions & 0 deletions lib/config-array-factory.js
Expand Up @@ -817,6 +817,9 @@ class ConfigArrayFactory {
const name = `${ctx.name} » ${extendName}`;

if (getEslintRecommendedConfig) {
if (typeof getEslintRecommendedConfig !== "function") {
throw new Error(`getEslintRecommendedConfig must be a function instead of '${getEslintRecommendedConfig}'`);
}
return this._normalizeConfigData(getEslintRecommendedConfig(), { ...ctx, name, filePath: "" });
}
return this._loadConfigData({
Expand All @@ -829,6 +832,9 @@ class ConfigArrayFactory {
const name = `${ctx.name} » ${extendName}`;

if (getEslintAllConfig) {
if (typeof getEslintAllConfig !== "function") {
throw new Error(`getEslintAllConfig must be a function instead of '${getEslintAllConfig}'`);
}
return this._normalizeConfigData(getEslintAllConfig(), { ...ctx, name, filePath: "" });
}
return this._loadConfigData({
Expand Down
47 changes: 36 additions & 11 deletions tests/lib/config-array-factory.js
Expand Up @@ -2143,17 +2143,6 @@ describe("ConfigArrayFactory", () => {
describe("with eslint built-in config callbacks", () => {
let factory;

beforeEach(async () => {
await prepare();
factory = new ConfigArrayFactory({
cwd: getPath(),
getEslintAllConfig,
getEslintRecommendedConfig
});
});

afterEach(cleanup);

/**
* Apply `extends` property.
* @param {Object} configData The config that has `extends` property.
Expand All @@ -2167,6 +2156,42 @@ describe("ConfigArrayFactory", () => {
.toCompatibleObjectAsConfigFileContent();
}

describe("with incorrect getConfig callbacks", () => {
beforeEach(async () => {
await prepare();
factory = new ConfigArrayFactory({
cwd: getPath(),
getEslintAllConfig: true,
getEslintRecommendedConfig: 12345
});
});

afterEach(cleanup);

it("should throw error when extends config includes 'eslint:all'", () => {
assert.throws(() => {
applyExtends({ extends: "eslint:all" });
}, /getEslintAllConfig must be a function instead of .*/u);
});

it("should throw error when extends config includes 'eslint:recommended'", () => {
assert.throws(() => {
applyExtends({ extends: "eslint:recommended" });
}, /getEslintRecommendedConfig must be a function instead of .*/u);
});
});

beforeEach(async () => {
await prepare();
factory = new ConfigArrayFactory({
cwd: getPath(),
getEslintAllConfig,
getEslintRecommendedConfig
});
});

afterEach(cleanup);

it("should apply extension 'foo' when specified from root directory config", () => {
const config = applyExtends({
extends: "foo",
Expand Down

0 comments on commit b4803f0

Please sign in to comment.