Skip to content

Commit

Permalink
Fix: ConfigArrayFactory was ignoring the resolver option in some plac…
Browse files Browse the repository at this point in the history
…es (#53)

* Fix: ConfigArrayFactory was ignoring the resolver option in some places

* Add tests
  • Loading branch information
onigoetz authored Sep 8, 2021
1 parent be0f70d commit c5d4919
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/config-array-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -926,11 +926,11 @@ class ConfigArrayFactory {
_loadParser(nameOrPath, ctx) {
debug("Loading parser %j from %s", nameOrPath, ctx.filePath);

const { cwd } = internalSlotsMap.get(this);
const { cwd, resolver } = internalSlotsMap.get(this);
const relativeTo = ctx.filePath || path.join(cwd, "__placeholder__.js");

try {
const filePath = ModuleResolver.resolve(nameOrPath, relativeTo);
const filePath = resolver.resolve(nameOrPath, relativeTo);

writeDebugLogForLoading(nameOrPath, relativeTo, filePath);

Expand Down Expand Up @@ -977,7 +977,7 @@ class ConfigArrayFactory {
_loadPlugin(name, ctx) {
debug("Loading plugin %j from %s", name, ctx.filePath);

const { additionalPluginPool } = internalSlotsMap.get(this);
const { additionalPluginPool, resolver } = internalSlotsMap.get(this);
const request = naming.normalizePackageName(name, "eslint-plugin");
const id = naming.getShorthandName(request, "eslint-plugin");
const relativeTo = path.join(ctx.pluginBasePath, "__placeholder__.js");
Expand Down Expand Up @@ -1018,7 +1018,7 @@ class ConfigArrayFactory {
let error;

try {
filePath = ModuleResolver.resolve(request, relativeTo);
filePath = resolver.resolve(request, relativeTo);
} catch (resolveError) {
error = resolveError;
/* istanbul ignore else */
Expand Down
103 changes: 103 additions & 0 deletions tests/lib/config-array-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,109 @@ describe("ConfigArrayFactory", () => {
});
});

describe("custom 'resolver' details", () => {

const { prepare, cleanup, getPath } = createCustomTeardown({
cwd: tempDir,
files: {
"node_modules/custom-xxx-parser/index.js": "exports.name = 'xxx-parser';",
"node_modules/custom-eslint-plugin-xxx/index.js": "exports.configs = { name: 'eslint-plugin-xxx' };",
"node_modules/custom-eslint-config-foo/index.js": "exports.env = { browser: true }"
}
});

before(async () => {
await prepare();
factory = new ConfigArrayFactory({
cwd: getPath(),
resolver: {
resolve(find, relativeTo) {
return path.join(path.dirname(relativeTo), `node_modules/custom-${find}/index.js`);
}
}
});
});

after(cleanup);

describe("if the 'parser' property was a valid package, the first config array element", () => {
let element;

beforeEach(() => {
element = create({ parser: "xxx-parser" })[0];
});

it("should have the package ID at 'parser.id' property.", () => {
assert.strictEqual(element.parser.id, "xxx-parser");
});

it("should have the package object at 'parser.definition' property.", () => {
assert.deepStrictEqual(element.parser.definition, { name: "xxx-parser" });
});

it("should have the path to the package at 'parser.filePath' property.", () => {
assert.strictEqual(element.parser.filePath, path.join(getPath(), "node_modules/custom-xxx-parser/index.js"));
});
});

describe("if the 'plugins' property was a valid package, the first config array element", () => {
let element;

beforeEach(() => {
element = create({ plugins: ["xxx"] })[0];
});

it("should have 'plugins[id]' property.", () => {
assert.notStrictEqual(element.plugins.xxx, void 0);
});

it("should have the package ID at 'plugins[id].id' property.", () => {
assert.strictEqual(element.plugins.xxx.id, "xxx");
});

it("should have the package object at 'plugins[id].definition' property.", () => {
assertPluginDefinition(
element.plugins.xxx.definition,
{ configs: { name: "eslint-plugin-xxx" } }
);
});

it("should have the path to the package at 'plugins[id].filePath' property.", () => {
assert.strictEqual(element.plugins.xxx.filePath, path.join(getPath(), "node_modules/custom-eslint-plugin-xxx/index.js"));
});
});

describe("if 'extends' property was 'foo', the returned value", () => {
let configArray;

beforeEach(() => {
configArray = create(
{ extends: "foo", rules: { eqeqeq: 1 } },
{ name: ".eslintrc" }
);
});

it("should have two elements.", () => {
assert.strictEqual(configArray.length, 2);
});

it("should have the config data of 'eslint-config-foo' at the first element.", () => {
assertConfigArrayElement(configArray[0], {
name: ".eslintrc » eslint-config-foo",
filePath: path.join(getPath(), "node_modules/custom-eslint-config-foo/index.js"),
env: { browser: true }
});
});

it("should have the given config data at the second element.", () => {
assertConfigArrayElement(configArray[1], {
name: ".eslintrc",
rules: { eqeqeq: 1 }
});
});
});
});

describe("'parser' details", () => {

const { prepare, cleanup, getPath } = createCustomTeardown({
Expand Down

0 comments on commit c5d4919

Please sign in to comment.