From 5f76bef372783f3018e1d5022d5cab7261617b0e Mon Sep 17 00:00:00 2001 From: Saito Nakamura Date: Tue, 10 Sep 2019 13:26:38 +0900 Subject: [PATCH] Combined transformers and programTransformer into union Thanks @blakeembrey for suggestion! --- README.md | 3 +-- src/index.ts | 47 +++++++++++++++++------------------------------ 2 files changed, 18 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 602a9e578..a866f73b3 100644 --- a/README.md +++ b/README.md @@ -140,8 +140,7 @@ _Environment variable denoted in parentheses._ ### Programmatic Only Options -* `transformers` An object with transformers to pass to TypeScript -* `programTransformers` A function that accepts a program and return and object with transformers to pass to TypeScript. Not available with `transpileOnly` flag +* `transformers` `_ts.CustomTransformers | ((p: _ts.Program) => _ts.CustomTransformers)` An object with transformers or a function that accepts a program and returns an transformers object to pass to TypeScript. Function isn't available with `transpileOnly` flag * `readFile` Custom TypeScript-compatible file reading function * `fileExists` Custom TypeScript-compatible file existence function diff --git a/src/index.ts b/src/index.ts index 887e812d1..1a6138823 100644 --- a/src/index.ts +++ b/src/index.ts @@ -71,8 +71,7 @@ export interface Options { ignoreDiagnostics?: Array readFile?: (path: string) => string | undefined fileExists?: (path: string) => boolean - transformers?: _ts.CustomTransformers - programTransformers?: (p: _ts.Program) => _ts.CustomTransformers + transformers?: _ts.CustomTransformers | ((p: _ts.Program) => _ts.CustomTransformers) } /** @@ -215,7 +214,6 @@ export function register (opts: Options = {}): Register { const compiler = require.resolve(options.compiler || 'typescript', { paths: [cwd, __dirname] }) const ts: typeof _ts = require(compiler) const transformers = options.transformers || undefined - const programTransformers = options.programTransformers || undefined const readFile = options.readFile || ts.sys.readFile const fileExists = options.fileExists || ts.sys.fileExists const config = readConfig(cwd, ts, fileExists, readFile, options) @@ -279,7 +277,7 @@ export function register (opts: Options = {}): Register { let getOutput = function (code: string, fileName: string, lineOffset = 0): SourceOutput { const result = ts.transpileModule(code, { fileName, - transformers, + transformers: !isTransformersFunction(transformers) ? transformers : undefined, compilerOptions: config.options, reportDiagnostics: true }) @@ -297,8 +295,8 @@ export function register (opts: Options = {}): Register { throw new TypeError(`Type information is unavailable without "--type-check"`) } - if (!typeCheck && programTransformers) { - throw new TypeError(`Program transformers is unavailable without "--type-check"`) + if (!typeCheck && isTransformersFunction(transformers)) { + throw new TypeError(`Transformers function is unavailable without "--type-check"`) } // Use full language services when the fast option is disabled. @@ -311,35 +309,15 @@ export function register (opts: Options = {}): Register { let program: _ts.Program | undefined = undefined const getCustomTransformers = (): _ts.CustomTransformers | undefined => { - if (!programTransformers || !program) { + if (!isTransformersFunction(transformers)) { return transformers } - const programTransformersData = programTransformers(program) - - if (!transformers) { - return programTransformersData + if (!program) { + return undefined } - const before = [ - ...(programTransformersData.before || []), - ...(transformers.before || []) - ] - - const after = [ - ...(programTransformersData.after || []), - ...(transformers.after || []) - ] - - const afterDeclarations = [ - ...(programTransformersData.afterDeclarations || []), - ...(transformers.afterDeclarations || []) - ] - return { - before, - after, - afterDeclarations - } + return transformers(program) } // Create the compiler host for type checking. @@ -625,3 +603,12 @@ function updateSourceMap (sourceMapText: string, fileName: string) { function filterDiagnostics (diagnostics: _ts.Diagnostic[], ignore: number[]) { return diagnostics.filter(x => ignore.indexOf(x.code) === -1) } + +/** + * Check if transformers is a function + */ +function isTransformersFunction ( + transformers: _ts.CustomTransformers | ((p: _ts.Program) => _ts.CustomTransformers) | undefined +): transformers is (p: _ts.Program) => _ts.CustomTransformers { + return typeof transformers === 'function' +}