( ts: TypeScript, opts: Opts )
| 957 | } |
| 958 | |
| 959 | export function transformWithTs( |
| 960 | ts: TypeScript, |
| 961 | opts: Opts |
| 962 | ): typescript.TransformerFactory<typescript.SourceFile> { |
| 963 | opts = {...DEFAULT_OPTS, ...opts} |
| 964 | debug('Transforming options', opts) |
| 965 | const transformFn: typescript.TransformerFactory< |
| 966 | typescript.SourceFile |
| 967 | > = ctx => { |
| 968 | return sf => { |
| 969 | const pragmaResult = PRAGMA_REGEX.exec(sf.text) |
| 970 | if (pragmaResult) { |
| 971 | debug('Pragma found', pragmaResult) |
| 972 | const [, pragma, kvString] = pragmaResult |
| 973 | if (pragma === opts.pragma) { |
| 974 | const kvs = kvString.split(' ') |
| 975 | const result: Record<string, string> = {} |
| 976 | for (const kv of kvs) { |
| 977 | const [k, v] = kv.split(':') |
| 978 | result[k] = v |
| 979 | } |
| 980 | debug('Pragma extracted', result) |
| 981 | if (typeof opts.onMetaExtracted === 'function') { |
| 982 | opts.onMetaExtracted(sf.fileName, result) |
| 983 | } |
| 984 | } |
| 985 | } |
| 986 | return ts.visitEachChild(sf, getVisitor(ts, ctx, sf, opts), ctx) |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | return transformFn |
| 991 | } |
| 992 | |
| 993 | export function transform( |
| 994 | opts: Opts |
no test coverage detected