Skip to content

Commit

Permalink
Update LKG
Browse files Browse the repository at this point in the history
  • Loading branch information
ivogabe committed Jun 1, 2015
1 parent 6d620e0 commit 0610692
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 34 deletions.
28 changes: 15 additions & 13 deletions release/compiler.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
var ts = require('typescript');
var path = require('path');
var tsApi = require('./tsapi');
var output = require('./output');
var host = require('./host');
var filter = require('./filter');
var output_1 = require('./output');
var host_1 = require('./host');
var filter_1 = require('./filter');
var reporter_1 = require('./reporter');
var utils = require('./utils');
/**
* Compiles a whole project, with full type checking
Expand All @@ -17,7 +18,7 @@ var ProjectCompiler = (function () {
ProjectCompiler.prototype.inputFile = function (file) { };
ProjectCompiler.prototype.inputDone = function () {
if (!this.project.input.firstSourceFile) {
this.project.output.finish();
this.project.output.finish(reporter_1.emptyCompilationResult());
return;
}
if (!this.project.input.isChanged(true)) {
Expand All @@ -30,22 +31,23 @@ var ProjectCompiler = (function () {
for (var _b = 0, _c = Object.keys(old.files); _b < _c.length; _b++) {
var fileName = _c[_b];
var file = old.files[fileName];
this.project.output.write(file.fileName + '.js', file.content[output.OutputFileKind.JavaScript]);
this.project.output.write(file.fileName + '.js.map', file.content[output.OutputFileKind.SourceMap]);
if (file.content[output.OutputFileKind.Definitions] !== undefined) {
this.project.output.write(file.fileName + '.d.ts', file.content[output.OutputFileKind.Definitions]);
this.project.output.write(file.fileName + '.js', file.content[output_1.OutputFileKind.JavaScript]);
this.project.output.write(file.fileName + '.js.map', file.content[output_1.OutputFileKind.SourceMap]);
if (file.content[output_1.OutputFileKind.Definitions] !== undefined) {
this.project.output.write(file.fileName + '.d.ts', file.content[output_1.OutputFileKind.Definitions]);
}
}
this.project.output.finish(old.results);
return;
}
var root = this.project.input.commonBasePath;
this.project.options.sourceRoot = root;
this.project.options.rootDir = root; // rootDir was added in 1.5 & not available in 1.4
this.host = new host.Host(this.project.typescript, this.project.currentDirectory, this.project.input, !this.project.noExternalResolve, this.project.options.target >= 2 /* ES6 */ ? 'lib.es6.d.ts' : 'lib.d.ts');
this.host = new host_1.Host(this.project.typescript, this.project.currentDirectory, this.project.input, !this.project.noExternalResolve, this.project.options.target >= 2 /* ES6 */ ? 'lib.es6.d.ts' : 'lib.d.ts');
var rootFilenames = this.project.input.getFileNames(true);
if (this.project.filterSettings !== undefined) {
var _filter = new filter.Filter(this.project, this.project.filterSettings);
rootFilenames = rootFilenames.filter(function (fileName) { return _filter.match(fileName); });
var filter = new filter_1.Filter(this.project, this.project.filterSettings);
rootFilenames = rootFilenames.filter(function (fileName) { return filter.match(fileName); });
}
if (tsApi.isTS14(this.project.typescript) && !this.project.singleOutput) {
// Add an empty file under the root, as the rootDir option is not supported in TS1.4.
Expand All @@ -55,7 +57,7 @@ var ProjectCompiler = (function () {
}
// Creating a program to compile the sources
this.program = this.project.typescript.createProgram(rootFilenames, this.project.options, this.host);
var errors = tsApi.getDiagnosticsAndEmit(this.program);
var _d = tsApi.getDiagnosticsAndEmit(this.program), errors = _d[0], result = _d[1];
for (var i = 0; i < errors.length; i++) {
this.project.output.diagnostic(errors[i]);
}
Expand All @@ -64,7 +66,7 @@ var ProjectCompiler = (function () {
continue;
this.project.output.write(fileName, this.host.output[fileName]);
}
this.project.output.finish();
this.project.output.finish(result);
};
Object.defineProperty(ProjectCompiler.prototype, "commonBaseDiff", {
/**
Expand Down
4 changes: 2 additions & 2 deletions release/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ var tsApi = require('./tsapi');
var path = require('path');
var utils = require('./utils');
var Filter = (function () {
function Filter(_project, filters) {
function Filter(project, filters) {
var _this = this;
this.referencedFrom = undefined;
this.referencedFromAll = undefined;
this.project = _project;
this.project = project;
if (filters.referencedFrom !== undefined) {
this.referencedFrom = this.mapFilenamesToFiles(filters.referencedFrom);
this.referencedFromAll = [];
Expand Down
4 changes: 3 additions & 1 deletion release/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ var compile;
if (fileNameOrSettings !== undefined) {
if (typeof fileNameOrSettings === 'string') {
tsConfigFileName = fileNameOrSettings;
tsConfigContent = JSON.parse(fs.readFileSync(fileNameOrSettings).toString());
// load file and strip BOM, since JSON.parse fails to parse if there's a BOM present
var tsConfigText = fs.readFileSync(fileNameOrSettings).toString();
tsConfigContent = JSON.parse(tsConfigText.replace(/^\uFEFF/, ''));
var newSettings = {};
if (tsConfigContent.compilerOptions) {
for (var _i = 0, _a = Object.keys(tsConfigContent.compilerOptions); _i < _a.length; _i++) {
Expand Down
5 changes: 4 additions & 1 deletion release/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ var Output = (function () {
this.streamDts.push(fileDts);
}
};
Output.prototype.finish = function () {
Output.prototype.finish = function (results) {
var _this = this;
if (this.project.sortOutput) {
var sortedEmit = function (fileName) {
Expand All @@ -179,6 +179,9 @@ var Output = (function () {
sortedEmit(fileName);
}
}
this.results = results;
if (this.project.reporter.finish)
this.project.reporter.finish(results);
this.streamJs.push(null);
this.streamDts.push(null);
};
Expand Down
41 changes: 34 additions & 7 deletions release/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
var ts = require('typescript');
var vfs = require('vinyl-fs');
var path = require('path');
var input = require('./input');
var output = require('./output');
var through2 = require('through2');
var utils = require('./utils');
var input_1 = require('./input');
var output_1 = require('./output');
var Project = (function () {
function Project(configFileName, config, options, noExternalResolve, sortOutput, typescript) {
if (typescript === void 0) { typescript = ts; }
Expand All @@ -14,7 +16,7 @@ var Project = (function () {
this.noExternalResolve = noExternalResolve;
this.sortOutput = sortOutput;
this.singleOutput = options.out !== undefined;
this.input = new input.FileCache(typescript, options);
this.input = new input_1.FileCache(typescript, options);
}
/**
* Resets the compiler.
Expand All @@ -23,17 +25,42 @@ var Project = (function () {
Project.prototype.reset = function (outputJs, outputDts) {
this.input.reset();
this.previousOutput = this.output;
this.output = new output.Output(this, outputJs, outputDts);
this.output = new output_1.Output(this, outputJs, outputDts);
};
Project.prototype.src = function () {
var _this = this;
if (!this.config.files) {
throw new Error('gulp-typescript: You can only use src() if the \'files\' property exists in your tsconfig.json. Use gulp.src(\'**/**.ts\') instead.');
}
var base = path.dirname(this.configFileName);
var configPath = path.dirname(this.configFileName);
var base;
if (this.config.compilerOptions && this.config.compilerOptions.rootDir) {
base = path.resolve(base, this.config.compilerOptions.rootDir);
base = path.resolve(configPath, this.config.compilerOptions.rootDir);
}
return vfs.src(this.config.files.map(function (file) { return path.resolve(base, file); }), { base: base });
else {
base = configPath;
}
var resolvedFiles = [];
var checkMissingFiles = through2.obj(function (file, enc, callback) {
resolvedFiles.push(utils.normalizePath(file.path));
callback();
});
checkMissingFiles.on('finish', function () {
for (var _i = 0, _a = _this.config.files; _i < _a.length; _i++) {
var fileName = _a[_i];
var fullPaths = [
utils.normalizePath(path.join(configPath, fileName)),
utils.normalizePath(path.join(process.cwd(), configPath, fileName))
];
if (resolvedFiles.indexOf(fullPaths[0]) === -1 && resolvedFiles.indexOf(fullPaths[1]) === -1) {

This comment has been minimized.

Copy link
@inakianduaga

inakianduaga Jun 29, 2015

@ivogabe This check is causing the compilation to fail because the file globs are not matched

tsconfig.json

{
    "version" : "1.5.0-beta",
    "compilerOptions": {
        "target" : "es5",
        "module": "commonjs",
        "noImplicitAny": false,
        "removeComments": false,
        "preserveConstEnums": true,
        "outDir": "dist",
        "sourceMap": true
    },
    "files": [
        "src/**/*.ts",
        "typings/**/*.ts",
        "gulp/**/*.ts"
    ]
}

This comment has been minimized.

Copy link
@ivogabe

ivogabe Jun 30, 2015

Author Owner

@inakianduaga Globs are not supported by TypeScript, see microsoft/TypeScript#1927, microsoft/TypeScript#3043

This comment has been minimized.

Copy link
@inakianduaga

inakianduaga Jun 30, 2015

@ivogabe Yeah, thanks for the response, found that out a bit after I posted the comment (sorry for not updating it). Funny thing is your gulp-typescript was actually working just fine with globs before this update (the check can be commented out and the compilation works). Seems atom's tsconfig implementation has a "filesGlob" field where globs are supported

https://github.com/TypeStrong/atom-typescript/blob/master/docs/tsconfig.md

At any rate, I just switched to gulp.src that obviously accepts globs, so all is fine.

Thanks for the help!

var error = new Error("error TS6053: File '" + fileName + "' not found.");
console.error(error.message);
checkMissingFiles.emit('error', error);
}
}
});
return vfs.src(this.config.files.map(function (file) { return path.resolve(base, file); }), { base: base })
.pipe(checkMissingFiles);
};
return Project;
})();
Expand Down
38 changes: 35 additions & 3 deletions release/reporter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
///<reference path='../typings/tsd.d.ts'/>
var gutil = require('gulp-util');
function emptyCompilationResult() {
return {
syntaxErrors: 0,
globalErrors: 0,
semanticErrors: 0,
emitErrors: 0,
emitSkipped: false
};
}
exports.emptyCompilationResult = emptyCompilationResult;
function defaultFinishHandler(results) {
var hasError = false;
var showErrorCount = function (count, type) {
if (count === 0)
return;
gutil.log('TypeScript:', gutil.colors.magenta(count.toString()), type + ' ' + (count === 1 ? 'error' : 'errors'));
hasError = true;
};
showErrorCount(results.syntaxErrors, 'syntax');
showErrorCount(results.globalErrors, 'global');
showErrorCount(results.semanticErrors, 'semantic');
showErrorCount(results.emitErrors, 'emit');
if (results.emitSkipped) {
gutil.log('TypeScript: emit', gutil.colors.red('failed'));
}
else if (hasError) {
gutil.log('TypeScript: emit', gutil.colors.cyan('succeeded'), '(with errors)');
}
}
function nullReporter() {
return {};
}
Expand All @@ -8,7 +37,8 @@ function defaultReporter() {
return {
error: function (error) {
console.error(error.message);
}
},
finish: defaultFinishHandler
};
}
exports.defaultReporter = defaultReporter;
Expand Down Expand Up @@ -40,7 +70,8 @@ function longReporter() {
else {
console.error(error.message);
}
}
},
finish: defaultFinishHandler
};
}
exports.longReporter = longReporter;
Expand All @@ -67,7 +98,8 @@ function fullReporter(fullFilename) {
logLine(i, i === error.startPosition.line ? error.startPosition.character - 1 : 0, i === error.endPosition.line ? error.endPosition.character - 1 : undefined);
}
}
}
},
finish: defaultFinishHandler
};
}
exports.fullReporter = fullReporter;
28 changes: 21 additions & 7 deletions release/tsapi.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var reporter_1 = require('./reporter');
function isTS14(typescript) {
return !('findConfigFile' in typescript);
}
Expand All @@ -9,28 +10,41 @@ function getFileName(thing) {
}
exports.getFileName = getFileName;
function getDiagnosticsAndEmit(program) {
var result = reporter_1.emptyCompilationResult();
if (program.getDiagnostics) {
var errors = program.getDiagnostics();
result.syntaxErrors = errors.length;
if (!errors.length) {
// If there are no syntax errors, check types
var checker = program.getTypeChecker(true);
var semanticErrors = checker.getDiagnostics();
var emitErrors = checker.emitFiles().diagnostics;
errors = semanticErrors.concat(emitErrors);
result.semanticErrors = errors.length;
}
return errors;
else {
result.emitSkipped = true;
}
return [errors, result];
}
else {
var errors = program.getSyntacticDiagnostics();
if (errors.length === 0)
result.syntaxErrors = errors.length;
if (errors.length === 0) {
errors = program.getGlobalDiagnostics();
// Remove error: "File '...' is not under 'rootDir' '...'. 'rootDir' is expected to contain all source files."
// This is handled by ICompiler#correctSourceMap, so this error can be muted.
errors = errors.filter(function (item) { return item.code !== 6059; });
if (errors.length === 0)
// Remove error: "File '...' is not under 'rootDir' '...'. 'rootDir' is expected to contain all source files."
// This is handled by ICompiler#correctSourceMap, so this error can be muted.
errors = errors.filter(function (item) { return item.code !== 6059; });
result.globalErrors = errors.length;
}
if (errors.length === 0) {
errors = program.getSemanticDiagnostics();
result.semanticErrors = errors.length;
}
var emitOutput = program.emit();
return errors.concat(emitOutput.diagnostics);
result.emitErrors = emitOutput.diagnostics.length;
result.emitSkipped = emitOutput.emitSkipped;
return [errors.concat(emitOutput.diagnostics), result];
}
}
exports.getDiagnosticsAndEmit = getDiagnosticsAndEmit;
Expand Down

0 comments on commit 0610692

Please sign in to comment.