Skip to content

Commit d5d9f26

Browse files
committed
feat: add type checking functions for plugin options
- Add checkOptions function to validate plugin options - Add checkFileContentSpecifier function to validate FileContentSpecifier - Add checkFileContentSetterReturnValue function to validate return value of FileContentSetter - Integrate type checking in plugin initialization and utils
1 parent 3b337a2 commit d5d9f26

File tree

3 files changed

+46
-12
lines changed

3 files changed

+46
-12
lines changed

src/plugin.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Optionals } from './utils';
33
import type { FileContentSpecifier } from './file-content';
44
import { filterFile, getFileContent } from './utils';
55
import { createFilter } from '@rollup/pluginutils';
6+
import { checkOptions } from './type-check';
67

78
/** The plugin options */
89
export interface Options {
@@ -43,6 +44,7 @@ export function customImport(opts: Options): Plugin {
4344
defaultOptions,
4445
opts
4546
) as Required<Options>;
47+
checkOptions(options);
4648

4749
const filter = createFilter(
4850
// make sure that the include and exclude options are arrays

src/type-check.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { FileContentSetter, FileContentSpecifier } from './file-content';
2+
import { Options } from './plugin';
3+
4+
export function checkOptions(target: Options, varName: string = 'Options') {
5+
if (typeof target.include !== 'function' && !Array.isArray(target.include)) {
6+
throw new TypeError(`${varName}.include must be a function or an array`);
7+
}
8+
if (typeof target.exclude === 'function' && !Array.isArray(target.exclude)) {
9+
throw new TypeError(`${varName}.exclude must be a function or an array`);
10+
}
11+
checkFileContentSpecifier(target.content, `${varName}.content`);
12+
}
13+
14+
export function checkFileContentSpecifier(
15+
target: FileContentSpecifier,
16+
varName: string = 'FileContentSpecifier'
17+
) {
18+
if (typeof target !== 'string' && typeof target !== 'function') {
19+
throw new TypeError(`${varName} must be an string or a function`);
20+
}
21+
}
22+
23+
export function checkFileContentSetterReturnValue(
24+
target: ReturnType<FileContentSetter>,
25+
varName: string = 'FileContentSetter'
26+
) {
27+
if (typeof target === 'object') {
28+
if (typeof target.code !== 'string')
29+
throw new TypeError(
30+
`SourceDescription returned by ${varName} must have a code property of type string`
31+
);
32+
} else {
33+
throw new TypeError(`${varName} must return a string or an object`);
34+
}
35+
}

src/utils.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import type { CreateFilter } from '@rollup/pluginutils';
22
import { SourceDescription } from 'rollup';
33
import { FileContentSpecifier } from './file-content';
44
import type { Options } from './plugin';
5+
import {
6+
checkFileContentSetterReturnValue,
7+
checkFileContentSpecifier,
8+
} from './type-check';
59

610
/**
711
* Extracts the keys of optional properties from a type T.
@@ -40,30 +44,23 @@ export function getFileContent<self>(
4044
id: string,
4145
originalCode: string
4246
): SourceDescription {
47+
checkFileContentSpecifier(specifier);
4348
if (typeof specifier === 'string') {
4449
return {
4550
code: specifier,
4651
map: null,
4752
};
4853
} else if (typeof specifier === 'function') {
4954
const ret = specifier.call(_this, id, originalCode);
55+
checkFileContentSetterReturnValue(ret);
5056
if (typeof ret === 'string') {
5157
return {
5258
code: ret,
5359
map: null,
5460
};
5561
} else if (typeof ret === 'object') {
56-
if (typeof ret.code !== 'string')
57-
throw new TypeError(
58-
'The returned SourceDescription of the content function must have a code property'
59-
);
6062
return ret;
61-
} else
62-
throw new TypeError(
63-
'The return value of the content function must be string | SourceDescription'
64-
);
65-
} else
66-
throw new TypeError(
67-
'Invalid content specifier. It should be a string or a function'
68-
);
63+
}
64+
}
65+
throw new Error('Invalid FileContentSpecifier');
6966
}

0 commit comments

Comments
 (0)