Commit 6554f72f authored by David Sveningsson's avatar David Sveningsson

feat(plugin): allow specifying name

helps when developing plugins as the plugin may reference itself by its proper name
parent 0bc166ab
Pipeline #98082560 passed with stages
in 8 minutes and 52 seconds
......@@ -12,7 +12,14 @@ export interface Plugin {
/**
* Name of the plugin.
*
* Read-only property set by config.
* If specified this is the name used when referring to the plugin. Default is
* to use the name/path the user used when loading the plugin. To be less
* confusing for users you should use the same name as your package.
*
* The name must be a valid package name according to NPM (basically lowercase
* characters, must not begin with dot, slash or non-url safe characters).
*
* Hint: import and use the name from `package.json`.
*/
name?: string;
......
......@@ -8,7 +8,7 @@ import { Severity } from "./severity";
let mockElements: any;
jest.mock("mock-elements", () => mockElements, { virtual: true });
jest.mock("mock-plugin", () => ({ name: "mock plugin" }), { virtual: true });
jest.mock("mock-plugin", () => ({}), { virtual: true });
/* a mocked file which throws an exception when loaded */
jest.mock(
......
......@@ -300,7 +300,7 @@ export class Config {
"<rootDir>",
this.rootDir
)) as Plugin;
plugin.name = moduleName;
plugin.name = plugin.name || moduleName;
return plugin;
});
}
......
......@@ -27,6 +27,56 @@ describe("Plugin", () => {
mockPlugin = require("mock-plugin");
});
describe("name", () => {
it("should use plugin name if set", () => {
expect.assertions(1);
mockPlugin.name = "my-plugin";
mockPlugin.configs = {
foo: {
rules: {
"my-rule": "error",
},
},
};
config = Config.fromObject({
extends: ["my-plugin:foo"],
plugins: ["mock-plugin"],
});
expect(config.get()).toEqual({
extends: ["my-plugin:foo"],
plugins: ["mock-plugin"],
rules: {
"my-rule": "error",
},
transform: {},
});
});
it("should default to package name", () => {
expect.assertions(1);
mockPlugin.name = null;
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", () => {
it("should add extendable predefined configurations", () => {
mockPlugin.configs = {
......
......@@ -13,7 +13,14 @@ export interface Plugin {
/**
* Name of the plugin.
*
* Read-only property set by config.
* If specified this is the name used when referring to the plugin. Default is
* to use the name/path the user used when loading the plugin. To be less
* confusing for users you should use the same name as your package.
*
* The name must be a valid package name according to NPM (basically lowercase
* characters, must not begin with dot, slash or non-url safe characters).
*
* Hint: import and use the name from `package.json`.
*/
name?: string;
......
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