File tree Expand file tree Collapse file tree 3 files changed +45
-12
lines changed Expand file tree Collapse file tree 3 files changed +45
-12
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import type { Optionals } from './utils';
3
3
import type { FileContentSpecifier } from './file-content' ;
4
4
import { filterFile , getFileContent } from './utils' ;
5
5
import { createFilter } from '@rollup/pluginutils' ;
6
+ import { checkOptions } from './type-check' ;
6
7
7
8
/** The plugin options */
8
9
export interface Options {
@@ -43,6 +44,7 @@ export function customImport(opts: Options): Plugin {
43
44
defaultOptions ,
44
45
opts
45
46
) as Required < Options > ;
47
+ checkOptions ( options ) ;
46
48
47
49
const filter = createFilter (
48
50
// make sure that the include and exclude options are arrays
Original file line number Diff line number Diff line change
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' && typeof target . code !== 'string' ) {
28
+ throw new TypeError (
29
+ `SourceDescription returned by ${ varName } must have a code property of type string`
30
+ ) ;
31
+ } else {
32
+ throw new TypeError ( `${ varName } must return a string or an object` ) ;
33
+ }
34
+ }
Original file line number Diff line number Diff line change @@ -2,6 +2,10 @@ import type { CreateFilter } from '@rollup/pluginutils';
2
2
import { SourceDescription } from 'rollup' ;
3
3
import { FileContentSpecifier } from './file-content' ;
4
4
import type { Options } from './plugin' ;
5
+ import {
6
+ checkFileContentSetterReturnValue ,
7
+ checkFileContentSpecifier ,
8
+ } from './type-check' ;
5
9
6
10
/**
7
11
* Extracts the keys of optional properties from a type T.
@@ -40,30 +44,23 @@ export function getFileContent<self>(
40
44
id : string ,
41
45
originalCode : string
42
46
) : SourceDescription {
47
+ checkFileContentSpecifier ( specifier ) ;
43
48
if ( typeof specifier === 'string' ) {
44
49
return {
45
50
code : specifier ,
46
51
map : null ,
47
52
} ;
48
53
} else if ( typeof specifier === 'function' ) {
49
54
const ret = specifier . call ( _this , id , originalCode ) ;
55
+ checkFileContentSetterReturnValue ( ret ) ;
50
56
if ( typeof ret === 'string' ) {
51
57
return {
52
58
code : ret ,
53
59
map : null ,
54
60
} ;
55
61
} 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
- ) ;
60
62
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' ) ;
69
66
}
You can’t perform that action at this time.
0 commit comments