Created
June 15, 2016 08:49
-
-
Save iamssen/6c61cf56c412cb9938bd30d55c122dee to your computer and use it in GitHub Desktop.
Test case helper for Typescript declaration work
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ts = require('typescript'); | |
const rimraf = require('rimraf'); | |
const fs = require('fs'); | |
const chalk = require('chalk'); | |
const defaultOptions = { | |
target: ts.ScriptTarget.ES5, | |
module: ts.ModuleKind.CommonJS, | |
outDir: __dirname + '/.output', | |
tempFile: __dirname + "/.test.ts" | |
} | |
module.exports = function (options) { | |
options = Object.assign({}, defaultOptions, options); | |
function compile(source) { | |
fs.writeFileSync(options.tempFile, source, {encoding: 'utf8'}); | |
const program = ts.createProgram([options.tempFile], options); | |
const emitResult = program.emit(); | |
const diagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics); | |
return diagnostics.map(diagnostic => { | |
const {line, character} = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start); | |
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'); | |
return { | |
fileName: diagnostic.file.fileName, | |
line: line + 1, | |
character: character + 1, | |
message | |
}; | |
}); | |
} | |
function accept(source) { | |
const diagnostics = compile(source); | |
if (diagnostics.length > 0) { | |
let message = [chalk.red(source)]; | |
diagnostics.forEach(diagnostic => { | |
if (diagnostic.fileName !== options.tempFile) message.push(`${diagnostic.fileName} (${diagnostic.line},${diagnostic.character})`); | |
message.push(diagnostic.message); | |
}); | |
return new Error(message); | |
} | |
return null; | |
} | |
function reject(source) { | |
const diagnostics = compile(source); | |
if (diagnostics.length === 0) { | |
return new Error(chalk.red(source)); | |
} | |
return null; | |
} | |
return {accept, reject}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment