Commit c9ad0808 authored by David Sveningsson's avatar David Sveningsson

fix(config): fix loading non-plugin transformer with plugin present

fixes #54
parent 90b6fee7
......@@ -2,6 +2,7 @@ import fs from "fs";
import path from "path";
import { Source } from "../context";
import { UserError } from "../error/user-error";
import { Transformer, TRANSFORMER_API } from "../transform";
import { Config } from "./config";
import { Severity } from "./severity";
......@@ -349,6 +350,92 @@ describe("config", () => {
`);
});
describe("should handle transformers from plugins", () => {
let config: Config;
beforeEach(() => {
function transform(source: Source): Source[] {
return [
Object.assign(source, {
data: `transformed from ${source.filename}`,
}),
];
}
transform.api = TRANSFORMER_API.VERSION;
jest.mock(
"mock-plugin-unnamed",
() => ({
transformer: transform as Transformer,
}),
{ virtual: true }
);
jest.mock(
"mock-plugin-named",
() => ({
transformer: {
foobar: transform as Transformer,
},
}),
{ virtual: true }
);
config = Config.fromObject({
plugins: ["mock-plugin-unnamed", "mock-plugin-named"],
transform: {
"\\.unnamed$": "mock-plugin-unnamed",
"\\.named$": "mock-plugin-named:foobar",
"\\.nonplugin$": "mock-transform",
},
});
config.init();
});
it("unnamed", () => {
expect.assertions(1);
source.filename = "foo.unnamed";
expect(config.transformSource(source)).toMatchInlineSnapshot(`
Array [
Object {
"column": 3,
"data": "transformed from foo.unnamed",
"filename": "foo.unnamed",
"line": 2,
},
]
`);
});
it("named", () => {
expect.assertions(1);
source.filename = "bar.named";
expect(config.transformSource(source)).toMatchInlineSnapshot(`
Array [
Object {
"column": 3,
"data": "transformed from bar.named",
"filename": "bar.named",
"line": 2,
},
]
`);
});
it("non-plugin (regression test issue 54)", () => {
expect.assertions(1);
source.filename = "bar.nonplugin";
expect(config.transformSource(source)).toMatchInlineSnapshot(`
Array [
Object {
"column": 1,
"data": "transformed source (was: original data)",
"filename": "bar.nonplugin",
"line": 1,
"originalData": "original data",
},
]
`);
});
});
it("should throw error if transformer uses obsolete API", () => {
const config = Config.fromObject({
transform: {
......
......@@ -439,7 +439,7 @@ export class Config {
}
/* try to match an unnamed transformer from plugin */
const plugin = this.plugins.find(cur => (cur.name = name));
const plugin = this.plugins.find(cur => cur.name === name);
if (plugin) {
if (typeof plugin.transformer !== "function") {
throw new ConfigError(
......
......@@ -73,7 +73,7 @@ export interface Plugin {
* "^.*\\.foo$": "my-plugin:foobar"
* }
*/
transformer: Transformer | Record<string, Transformer>;
transformer?: Transformer | Record<string, Transformer>;
/**
* Extend metadata validation schema.
......
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