Skip to content

Commit

Permalink
Tidy up conditional logic
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Sep 15, 2019
1 parent 5f76bef commit 616c481
Showing 1 changed file with 36 additions and 50 deletions.
86 changes: 36 additions & 50 deletions src/index.ts
Expand Up @@ -274,50 +274,21 @@ export function register (opts: Options = {}): Register {
/**
* Create the basic required function using transpile mode.
*/
let getOutput = function (code: string, fileName: string, lineOffset = 0): SourceOutput {
const result = ts.transpileModule(code, {
fileName,
transformers: !isTransformersFunction(transformers) ? transformers : undefined,
compilerOptions: config.options,
reportDiagnostics: true
})

const diagnosticList = result.diagnostics ?
filterDiagnostics(result.diagnostics, ignoreDiagnostics) :
[]

if (diagnosticList.length) reportTSError(configDiagnosticList)

return [result.outputText, result.sourceMapText as string]
}

let getTypeInfo = function (_code: string, _fileName: string, _position: number): TypeInfo {
throw new TypeError(`Type information is unavailable without "--type-check"`)
}

if (!typeCheck && isTransformersFunction(transformers)) {
throw new TypeError(`Transformers function is unavailable without "--type-check"`)
}
let getOutput: (code: string, fileName: string, lineOffset: number) => SourceOutput
let getTypeInfo: (_code: string, _fileName: string, _position: number) => TypeInfo

// Use full language services when the fast option is disabled.
if (typeCheck) {
const memoryCache = new MemoryCache(config.fileNames)
const cachedReadFile = cachedLookup(debugFn('readFile', readFile))

// The program isn't available right now
// but it will be when we'll create a language service
let program: _ts.Program | undefined = undefined

const getCustomTransformers = (): _ts.CustomTransformers | undefined => {
if (!isTransformersFunction(transformers)) {
return transformers
const getCustomTransformers = () => {
if (typeof transformers === 'function') {
const program = service.getProgram()
return program ? transformers(program) : undefined
}

if (!program) {
return undefined
}

return transformers(program)
return transformers
}

// Create the compiler host for type checking.
Expand Down Expand Up @@ -356,10 +327,9 @@ export function register (opts: Options = {}): Register {

const registry = ts.createDocumentRegistry(ts.sys.useCaseSensitiveFileNames, cwd)
const service = ts.createLanguageService(serviceHost, registry)
program = service.getProgram()

// Set the file contents into cache manually.
const updateMemoryCache = function (contents: string, fileName: string) {
const updateMemoryCache = (contents: string, fileName: string) => {
const fileVersion = memoryCache.fileVersions.get(fileName) || 0

// Avoid incrementing cache when nothing has changed.
Expand All @@ -369,7 +339,7 @@ export function register (opts: Options = {}): Register {
memoryCache.fileContents.set(fileName, contents)
}

getOutput = function (code: string, fileName: string, lineOffset: number = 0) {
getOutput = (code: string, fileName: string) => {
updateMemoryCache(code, fileName)

const output = service.getEmitOutput(fileName)
Expand Down Expand Up @@ -400,7 +370,7 @@ export function register (opts: Options = {}): Register {
return [output.outputFiles[1].text, output.outputFiles[0].text]
}

getTypeInfo = function (code: string, fileName: string, position: number) {
getTypeInfo = (code: string, fileName: string, position: number) => {
updateMemoryCache(code, fileName)

const info = service.getQuickInfoAtPosition(fileName, position)
Expand All @@ -409,10 +379,35 @@ export function register (opts: Options = {}): Register {

return { name, comment }
}
} else {
if (typeof transformers === 'function') {
throw new TypeError('Transformers function is unavailable in "--transpile-only"')
}

getOutput = (code: string, fileName: string): SourceOutput => {
const result = ts.transpileModule(code, {
fileName,
transformers,
compilerOptions: config.options,
reportDiagnostics: true
})

const diagnosticList = result.diagnostics ?
filterDiagnostics(result.diagnostics, ignoreDiagnostics) :
[]

if (diagnosticList.length) reportTSError(configDiagnosticList)

return [result.outputText, result.sourceMapText as string]
}

getTypeInfo = () => {
throw new TypeError('Type information is unavailable in "--transpile-only"')
}
}

// Create a simple TypeScript compiler proxy.
function compile (code: string, fileName: string, lineOffset?: number) {
function compile (code: string, fileName: string, lineOffset = 0) {
const [value, sourceMap] = getOutput(code, fileName, lineOffset)
const output = updateOutput(value, fileName, sourceMap, getExtension)
outputCache.set(fileName, output)
Expand Down Expand Up @@ -603,12 +598,3 @@ 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'
}

0 comments on commit 616c481

Please sign in to comment.