Skip to content
This repository was archived by the owner on Feb 18, 2024. It is now read-only.

Support Object literal plugin usage #86

Merged
merged 2 commits into from
Aug 12, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,14 @@ module.exports = class extends ChainedMap {
const args = stringify(value.__pluginArgs).slice(1, -1);
return `${prefix}new ${constructorName}(${args})`;
}
return prefix + stringify({ args: value.__pluginArgs || [] });
return (
prefix +
stringify(
value.__pluginArgs && value.__pluginArgs.length
? { args: value.__pluginArgs }
: {}
)
);
}

// improve rule/use output
Expand Down
7 changes: 6 additions & 1 deletion src/Plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ module.exports = Orderable(
this.name = name;
this.extend(['init']);

this.init((Plugin, args = []) => new Plugin(...args));
this.init((Plugin, args = []) => {
if (typeof Plugin === 'function') {
return new Plugin(...args);
}
return Plugin;
});
}

use(plugin, args = []) {
Expand Down
14 changes: 14 additions & 0 deletions test/Plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,17 @@ test('toConfig with custom expression', t => {

t.is(initialized.__pluginConstructorName, `(require('my-plugin'))`);
});

test('toConfig with object literal plugin', t => {
const plugin = new Plugin(null, 'gamma');

const TestPlugin = {
apply() {},
};

plugin.use(TestPlugin);

const initialized = plugin.toConfig();

t.is(initialized, TestPlugin);
});