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

Fix: ConfigArrayFactory was ignoring the resolver option in some places #53

Merged
merged 2 commits into from
Sep 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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