Commit 9e7ea3e2 authored by David Sveningsson's avatar David Sveningsson

fix(config): keep track of plugin original name

parent bc61a6be
......@@ -705,7 +705,9 @@ describe("config", () => {
plugins: ["mock-plugin"],
});
config.init();
expect(config.getPlugins()).toEqual([{ name: "mock-plugin" }]);
expect(config.getPlugins()).toEqual([
expect.objectContaining({ name: "mock-plugin" }),
]);
});
});
......
......@@ -18,6 +18,14 @@ interface TransformerEntry {
fn: Transformer;
}
/**
* Internal interface for a loaded plugin.
*/
interface LoadedPlugin extends Plugin {
name: string;
originalName: string;
}
const recommended = require("./recommended");
const document = require("./document");
let rootDirCache: string = null;
......@@ -79,7 +87,7 @@ export class Config {
private initialized: boolean;
protected metaTable: MetaTable;
protected plugins: Plugin[];
protected plugins: LoadedPlugin[];
protected transformers: TransformerEntry[];
protected rootDir: string;
......@@ -311,15 +319,16 @@ export class Config {
return this.plugins;
}
private loadPlugins(plugins: string[]): Plugin[] {
private loadPlugins(plugins: string[]): LoadedPlugin[] {
return plugins.map((moduleName: string) => {
try {
// eslint-disable-next-line security/detect-non-literal-require
const plugin = require(moduleName.replace(
"<rootDir>",
this.rootDir
)) as Plugin;
)) as LoadedPlugin;
plugin.name = plugin.name || moduleName;
plugin.originalName = moduleName;
return plugin;
} catch (err) {
throw new ConfigError(
......@@ -330,7 +339,7 @@ export class Config {
});
}
private loadConfigurations(plugins: Plugin[]): Map<string, ConfigData> {
private loadConfigurations(plugins: LoadedPlugin[]): Map<string, ConfigData> {
const configs: Map<string, ConfigData> = new Map();
/* builtin presets */
......@@ -340,9 +349,16 @@ export class Config {
/* presets from plugins */
for (const plugin of plugins) {
for (const [name, config] of Object.entries(plugin.configs || {})) {
configs.set(`${plugin.name}:${name}`, new Config(config).config);
/* add configuration with name provided by plugin */
configs.set(`${plugin.name}:${name}`, config);
/* add configuration with name provided by user (in config file) */
if (plugin.name !== plugin.originalName) {
configs.set(`${plugin.originalName}:${name}`, config);
}
}
}
return configs;
}
......
......@@ -26,6 +26,7 @@ describe("Plugin", () => {
};
/* reset mock */
jest.resetModules();
mockPlugin = require("mock-plugin");
});
......@@ -86,6 +87,30 @@ describe("Plugin", () => {
transform: {},
});
});
it("should retain original name", () => {
expect.assertions(1);
mockPlugin.name = "my-plugin";
mockPlugin.configs = {
foo: {
rules: {
"my-rule": "error",
},
},
};
config = Config.fromObject({
extends: ["mock-plugin:foo"],
plugins: ["mock-plugin"],
});
expect(config.get()).toEqual({
extends: ["mock-plugin:foo"],
plugins: ["mock-plugin"],
rules: {
"my-rule": "error",
},
transform: {},
});
});
});
describe("configs", () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment