Created
March 3, 2017 16:45
-
-
Save vegarringdal/416c9541f621c1dcae36299c1d788700 to your computer and use it in GitHub Desktop.
Fusebox typechecker plugin
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
| //to use: | |
| // let TypeCheckPlugin = require('./typeChecker') | |
| /* | |
| plugins: [ | |
| fb.CSSPlugin(), | |
| fb.HTMLPlugin({ | |
| useDefault: true | |
| }), | |
| fb.TypeScriptHelpers(), | |
| fb.SourceMapPlainJsPlugin(), | |
| TypeCheckPlugin() | |
| ]*/ | |
| // mostly a little from compiler API, gulp-typescript, tslint and a lot of failing :-) | |
| const fs = require("fs"); | |
| const path = require("path"); | |
| const ts = require("typescript"); | |
| const configFile = "tsconfig.json"; | |
| class TypeCheckPluginClass { | |
| constructor(opts) { | |
| this.opts | |
| } | |
| init(context) { | |
| this.firstRun = true; | |
| this.tsConfig = context.getTypeScriptConfig(); | |
| } | |
| executefirstRun() { | |
| this.firstRun = false; | |
| const parseConfigHost = { | |
| fileExists: ts.sys.fileExists, | |
| readDirectory: ts.sys.readDirectory, | |
| readFile: (file) => ts.sys.readFile, | |
| useCaseSensitiveFileNames: true, | |
| }; | |
| const parsed = ts.parseJsonConfigFileContent(this.tsConfig, parseConfigHost, ".", null, configFile); | |
| this.program = ts.createProgram(parsed.fileNames, parsed.options); | |
| } | |
| writeNewLine() { | |
| ts.sys.write(ts.sys.newLine) | |
| } | |
| writeText(text) { | |
| ts.sys.write(text) | |
| } | |
| resetTextColor(){ | |
| ts.sys.write("\x1b[0m") | |
| } | |
| setRedTextColor(){ | |
| ts.sys.write("\x1b[91m") | |
| } | |
| setBlueTextColor(){ | |
| ts.sys.write("\x1b[34m") | |
| } | |
| bundleEnd(context) { | |
| // shortcuts | |
| let newLine = this.writeNewLine; | |
| let write = this.writeText; | |
| let resetTextColor = this.resetTextColor; | |
| let setRedTextColor = this.setRedTextColor; | |
| let setBlueTextColor = this.setBlueTextColor; | |
| // tell use we are starting type checking | |
| setBlueTextColor(); | |
| write("\nStarting type checking, please wait...\n\n") | |
| // start timing | |
| console.time("Typechecking time") | |
| // if first time we need to create ts program | |
| if (this.firstRun) { | |
| this.executefirstRun(context); | |
| } | |
| // get diagnostics | |
| const diagnostics = ts.getPreEmitDiagnostics(this.program); | |
| setRedTextColor() | |
| // loop diagnostics | |
| if (diagnostics.length > 0) { | |
| const messages = diagnostics.map((diag) => { | |
| // get message type error, warn, info | |
| let message = ts.DiagnosticCategory[diag.category]; | |
| // if file | |
| if (diag.file) { | |
| const { | |
| line, | |
| character | |
| } = diag.file.getLineAndCharacterOfPosition(diag.start); | |
| message += ` : TS${diag.code} : (${line + 1}:${character + 1}):`; | |
| message += `\t-> ${diag.file.fileName}:` | |
| } | |
| //flatten error message | |
| message += " " + ts.flattenDiagnosticMessageText(diag.messageText, "\n"); | |
| //return message | |
| return message; | |
| }); | |
| //write errors | |
| write(messages.join("\n")); | |
| } | |
| setBlueTextColor(); | |
| write("\n\n"); | |
| write(`Options Errors ${this.program.getOptionsDiagnostics().length}\n`); | |
| write(`Global Errors ${this.program.getGlobalDiagnostics().length}\n`); | |
| write(`Syntactic Errors ${this.program.getSyntacticDiagnostics().length}\n`); | |
| write(`Semantic Errors ${this.program.getSemanticDiagnostics().length}\n`); | |
| console.timeEnd("Typechecking time") | |
| write("\nType checking done\n\n"); | |
| } | |
| } | |
| const TypeCheckPlugin = (opts) => { | |
| return new TypeCheckPluginClass(opts); | |
| }; | |
| module.exports = TypeCheckPlugin; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment