Skip to content

Commit c7d599c

Browse files
committed
feat: make Base.plugin() accept an array instead of
BREAKING CHANGE: `Base.plugin()` now only accepts a single argument which has to be an array of plugin functions
1 parent d780bff commit c7d599c

File tree

4 files changed

+60
-52
lines changed

4 files changed

+60
-52
lines changed

index.d.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ type ClassWithPlugins = Constructor<any> & {
3535
plugins: any[];
3636
};
3737

38-
type ConstructorRequiringVersion<Class extends ClassWithPlugins, PredefinedOptions> = {
38+
type ConstructorRequiringVersion<
39+
Class extends ClassWithPlugins,
40+
PredefinedOptions
41+
> = {
3942
defaultOptions: PredefinedOptions;
4043
} & (PredefinedOptions extends { version: string }
4144
? {
@@ -73,12 +76,12 @@ export declare class Base<TOptions extends Base.Options = Base.Options> {
7376
*/
7477
static plugin<
7578
Class extends ClassWithPlugins,
76-
Plugins extends [Plugin, ...Plugin[]],
79+
Plugins extends [Plugin, ...Plugin[]]
7780
>(
7881
this: Class,
79-
...plugins: Plugins,
82+
plugins: Plugins
8083
): Class & {
81-
plugins: [...Class['plugins'], ...Plugins];
84+
plugins: [...Class["plugins"], ...Plugins];
8285
} & Constructor<UnionToIntersection<ReturnTypeOf<Plugins>>>;
8386

8487
/**
@@ -99,7 +102,8 @@ export declare class Base<TOptions extends Base.Options = Base.Options> {
99102
*/
100103
static defaults<
101104
PredefinedOptionsOne,
102-
ClassOne extends Constructor<Base<Base.Options & PredefinedOptionsOne>> & ClassWithPlugins
105+
ClassOne extends Constructor<Base<Base.Options & PredefinedOptionsOne>> &
106+
ClassWithPlugins
103107
>(
104108
this: ClassOne,
105109
defaults: PredefinedOptionsOne
@@ -117,8 +121,12 @@ export declare class Base<TOptions extends Base.Options = Base.Options> {
117121
): ConstructorRequiringVersion<
118122
ClassOne & ClassTwo & ClassThree,
119123
PredefinedOptionsOne & PredefinedOptionsTwo & PredefinedOptionsThree
120-
> & ClassOne & ClassTwo & ClassThree;
121-
} & ClassOne & ClassTwo;
124+
> &
125+
ClassOne &
126+
ClassTwo &
127+
ClassThree;
128+
} & ClassOne &
129+
ClassTwo;
122130
} & ClassOne;
123131

124132
static defaultOptions: {};

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export class Base {
66
});
77
}
88

9-
static plugin(...newPlugins) {
9+
static plugin(newPlugins) {
1010
const currentPlugins = this.plugins;
1111
return class extends this {
1212
static plugins = currentPlugins.concat(

index.test-d.ts

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ const BaseWithEmptyDefaults = Base.defaults({
1919

2020
// 'version' is missing and should still be required
2121
// @ts-expect-error
22-
new BaseWithEmptyDefaults()
22+
new BaseWithEmptyDefaults();
2323

2424
// 'version' is missing and should still be required
2525
// @ts-expect-error
26-
new BaseWithEmptyDefaults({})
26+
new BaseWithEmptyDefaults({});
2727

28-
const BaseLevelOne = Base.plugin(fooPlugin).defaults({
28+
const BaseLevelOne = Base.plugin([fooPlugin]).defaults({
2929
defaultOne: "value",
3030
version: "1.2.3",
3131
});
@@ -35,8 +35,8 @@ new BaseLevelOne();
3535
new BaseLevelOne({});
3636

3737
expectType<{
38-
defaultOne: string,
39-
version: string,
38+
defaultOne: string;
39+
version: string;
4040
}>(BaseLevelOne.defaultOptions);
4141

4242
const baseLevelOne = new BaseLevelOne({
@@ -54,9 +54,9 @@ const BaseLevelTwo = BaseLevelOne.defaults({
5454
});
5555

5656
expectType<{
57-
defaultOne: string,
58-
defaultTwo: number,
59-
version: string,
57+
defaultOne: string;
58+
defaultTwo: number;
59+
version: string;
6060
}>({ ...BaseLevelTwo.defaultOptions });
6161

6262
// Because 'version' is already provided, this needs no argument
@@ -65,11 +65,11 @@ new BaseLevelTwo({});
6565

6666
// 'version' may be overriden, though it's not necessary
6767
new BaseLevelTwo({
68-
version: 'new version',
68+
version: "new version",
6969
});
7070

7171
const baseLevelTwo = new BaseLevelTwo({
72-
optionTwo: true
72+
optionTwo: true,
7373
});
7474

7575
expectType<number>(baseLevelTwo.options.defaultTwo);
@@ -80,14 +80,14 @@ expectType<string>(baseLevelTwo.options.version);
8080
baseLevelTwo.unknown;
8181

8282
const BaseLevelThree = BaseLevelTwo.defaults({
83-
defaultThree: ['a', 'b', 'c'],
83+
defaultThree: ["a", "b", "c"],
8484
});
8585

8686
expectType<{
87-
defaultOne: string,
88-
defaultTwo: number,
89-
defaultThree: string[],
90-
version: string,
87+
defaultOne: string;
88+
defaultTwo: number;
89+
defaultThree: string[];
90+
version: string;
9191
}>({ ...BaseLevelThree.defaultOptions });
9292

9393
// Because 'version' is already provided, this needs no argument
@@ -96,13 +96,13 @@ new BaseLevelThree({});
9696

9797
// Previous settings may be overriden, though it's not necessary
9898
new BaseLevelThree({
99-
optionOne: '',
99+
optionOne: "",
100100
optionTwo: false,
101-
version: 'new version',
101+
version: "new version",
102102
});
103103

104104
const baseLevelThree = new BaseLevelThree({
105-
optionThree: [0, 1, 2]
105+
optionThree: [0, 1, 2],
106106
});
107107

108108
expectType<string>(baseLevelThree.options.defaultOne);
@@ -113,15 +113,15 @@ expectType<string>(baseLevelThree.options.version);
113113
// @ts-expect-error unknown properties cannot be used, see #31
114114
baseLevelThree.unknown;
115115

116-
const BaseWithVoidPlugin = Base.plugin(voidPlugin);
116+
const BaseWithVoidPlugin = Base.plugin([voidPlugin]);
117117
const baseWithVoidPlugin = new BaseWithVoidPlugin({
118118
version: "1.2.3",
119119
});
120120

121121
// @ts-expect-error unknown properties cannot be used, see #31
122122
baseWithVoidPlugin.unknown;
123123

124-
const BaseWithFooAndBarPlugins = Base.plugin(barPlugin, fooPlugin);
124+
const BaseWithFooAndBarPlugins = Base.plugin([barPlugin, fooPlugin]);
125125
const baseWithFooAndBarPlugins = new BaseWithFooAndBarPlugins({
126126
version: "1.2.3",
127127
});
@@ -132,11 +132,11 @@ expectType<string>(baseWithFooAndBarPlugins.bar);
132132
// @ts-expect-error unknown properties cannot be used, see #31
133133
baseWithFooAndBarPlugins.unknown;
134134

135-
const BaseWithVoidAndNonVoidPlugins = Base.plugin(
135+
const BaseWithVoidAndNonVoidPlugins = Base.plugin([
136136
barPlugin,
137137
voidPlugin,
138-
fooPlugin
139-
);
138+
fooPlugin,
139+
]);
140140
const baseWithVoidAndNonVoidPlugins = new BaseWithVoidAndNonVoidPlugins({
141141
version: "1.2.3",
142142
});
@@ -147,7 +147,7 @@ expectType<string>(baseWithVoidAndNonVoidPlugins.bar);
147147
// @ts-expect-error unknown properties cannot be used, see #31
148148
baseWithVoidAndNonVoidPlugins.unknown;
149149

150-
const BaseWithOptionsPlugin = Base.plugin(withOptionsPlugin);
150+
const BaseWithOptionsPlugin = Base.plugin([withOptionsPlugin]);
151151
const baseWithOptionsPlugin = new BaseWithOptionsPlugin({
152152
version: "1.2.3",
153153
});
@@ -185,30 +185,30 @@ expectType<{
185185
// @ts-expect-error - .options from .defaults() is only supported until a depth of 4
186186
}>({ ...baseLevelFour.options });
187187

188-
const BaseWithChainedDefaultsAndPlugins = Base
189-
.defaults({
190-
defaultOne: "value",
191-
})
192-
.plugin(fooPlugin)
188+
const BaseWithChainedDefaultsAndPlugins = Base.defaults({
189+
defaultOne: "value",
190+
})
191+
.plugin([fooPlugin])
193192
.defaults({
194193
defaultTwo: 0,
195194
});
196195

197-
const baseWithChainedDefaultsAndPlugins =
198-
new BaseWithChainedDefaultsAndPlugins({
196+
const baseWithChainedDefaultsAndPlugins = new BaseWithChainedDefaultsAndPlugins(
197+
{
199198
version: "1.2.3",
200-
});
199+
}
200+
);
201201

202202
expectType<string>(baseWithChainedDefaultsAndPlugins.foo);
203203

204204
const BaseWithManyChainedDefaultsAndPlugins = Base.defaults({
205205
defaultOne: "value",
206206
})
207-
.plugin(fooPlugin, barPlugin, voidPlugin)
207+
.plugin([fooPlugin, barPlugin, voidPlugin])
208208
.defaults({
209209
defaultTwo: 0,
210210
})
211-
.plugin(withOptionsPlugin)
211+
.plugin([withOptionsPlugin])
212212
.defaults({
213213
defaultThree: ["a", "b", "c"],
214214
});

test.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,25 @@ import { fooPlugin } from "./plugins/foo/index.js";
66
import { barPlugin } from "./plugins/bar/index.js";
77
import { voidPlugin } from "./plugins/void/index.js";
88

9-
test(".plugin(fooPlugin)", () => {
10-
const FooTest = Base.plugin(fooPlugin);
9+
test(".plugin([fooPlugin])", () => {
10+
const FooTest = Base.plugin([fooPlugin]);
1111
const fooTest = new FooTest();
1212
assert.equal(fooTest.foo, "foo");
1313
});
14-
test(".plugin(fooPlugin, barPlugin)", () => {
15-
const FooBarTest = Base.plugin(fooPlugin, barPlugin);
14+
test(".plugin([fooPlugin, barPlugin])", () => {
15+
const FooBarTest = Base.plugin([fooPlugin, barPlugin]);
1616
const fooBarTest = new FooBarTest();
1717
assert.equal(fooBarTest.foo, "foo");
1818
assert.equal(fooBarTest.bar, "bar");
1919
});
20-
test(".plugin(fooPlugin, barPlugin, voidPlugin)", () => {
21-
const FooBarTest = Base.plugin(fooPlugin, barPlugin, voidPlugin);
20+
test(".plugin([fooPlugin, barPlugin, voidPlugin])", () => {
21+
const FooBarTest = Base.plugin([fooPlugin, barPlugin, voidPlugin]);
2222
const fooBarTest = new FooBarTest();
2323
assert.equal(fooBarTest.foo, "foo");
2424
assert.equal(fooBarTest.bar, "bar");
2525
});
26-
test(".plugin(fooPlugin).plugin(barPlugin)", () => {
27-
const FooBarTest = Base.plugin(fooPlugin).plugin(barPlugin);
26+
test(".plugin([fooPlugin]).plugin(barPlugin)", () => {
27+
const FooBarTest = Base.plugin([fooPlugin]).plugin([barPlugin]);
2828
const fooBarTest = new FooBarTest();
2929
assert.equal(fooBarTest.foo, "foo");
3030
assert.equal(fooBarTest.bar, "bar");
@@ -61,12 +61,12 @@ test(".defaults({foo: 'bar', baz: 'daz' })", () => {
6161
});
6262

6363
test(".plugin().defaults()", () => {
64-
const BaseWithPluginAndDefaults = Base.plugin(fooPlugin).defaults({
64+
const BaseWithPluginAndDefaults = Base.plugin([fooPlugin]).defaults({
6565
baz: "daz",
6666
});
6767
const BaseWithDefaultsAndPlugin = Base.defaults({
6868
baz: "daz",
69-
}).plugin(fooPlugin);
69+
}).plugin([fooPlugin]);
7070

7171
const instance1 = new BaseWithPluginAndDefaults();
7272
const instance2 = new BaseWithDefaultsAndPlugin();

0 commit comments

Comments
 (0)