cleanup hygiene

This commit is contained in:
Joao Moreno 2016-07-28 10:28:01 +02:00
parent dcfce392b4
commit e623def3ed

View file

@ -3,13 +3,15 @@
* Licensed under the MIT License. See License.txt in the project root for license information. * Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/ *--------------------------------------------------------------------------------------------*/
var gulp = require('gulp'); 'use strict';
var filter = require('gulp-filter');
var es = require('event-stream');
var gulptslint = require('gulp-tslint');
var tslint = require('tslint');
var all = [ const gulp = require('gulp');
const filter = require('gulp-filter');
const es = require('event-stream');
const gulptslint = require('gulp-tslint');
const tslint = require('tslint');
const all = [
'*', '*',
'build/**/*', 'build/**/*',
'extensions/**/*', 'extensions/**/*',
@ -18,7 +20,7 @@ var all = [
'test/**/*' 'test/**/*'
]; ];
var eolFilter = [ const eolFilter = [
'**', '**',
'!ThirdPartyNotices.txt', '!ThirdPartyNotices.txt',
'!LICENSE.txt', '!LICENSE.txt',
@ -31,7 +33,7 @@ var eolFilter = [
'!build/win32/**' '!build/win32/**'
]; ];
var indentationFilter = [ const indentationFilter = [
'**', '**',
'!ThirdPartyNotices.txt', '!ThirdPartyNotices.txt',
'!**/*.md', '!**/*.md',
@ -61,7 +63,7 @@ var indentationFilter = [
'!extensions/vscode-api-tests/testWorkspace/**' '!extensions/vscode-api-tests/testWorkspace/**'
]; ];
var copyrightFilter = [ const copyrightFilter = [
'**', '**',
'!**/*.desktop', '!**/*.desktop',
'!**/*.json', '!**/*.json',
@ -79,7 +81,7 @@ var copyrightFilter = [
'!extensions/markdown/media/tomorrow.css' '!extensions/markdown/media/tomorrow.css'
]; ];
var tslintFilter = [ const tslintFilter = [
'src/**/*.ts', 'src/**/*.ts',
'extensions/**/*.ts', 'extensions/**/*.ts',
'!**/*.d.ts', '!**/*.d.ts',
@ -90,45 +92,38 @@ var tslintFilter = [
'!extensions/**/*.test.ts' '!extensions/**/*.test.ts'
]; ];
var copyrightHeader = [ const copyrightHeader = [
'/*---------------------------------------------------------------------------------------------', '/*---------------------------------------------------------------------------------------------',
' * Copyright (c) Microsoft Corporation. All rights reserved.', ' * Copyright (c) Microsoft Corporation. All rights reserved.',
' * Licensed under the MIT License. See License.txt in the project root for license information.', ' * Licensed under the MIT License. See License.txt in the project root for license information.',
' *--------------------------------------------------------------------------------------------*/' ' *--------------------------------------------------------------------------------------------*/'
].join('\n'); ].join('\n');
function failureReporter(failure) { function reportFailures(failures) {
var name = failure.name || failure.fileName; failures.forEach(failure => {
var position = failure.startPosition; const name = failure.name || failure.fileName;
var line = position.lineAndCharacter ? position.lineAndCharacter.line : position.line; const position = failure.startPosition;
var character = position.lineAndCharacter ? position.lineAndCharacter.character : position.character; const line = position.lineAndCharacter ? position.lineAndCharacter.line : position.line;
const character = position.lineAndCharacter ? position.lineAndCharacter.character : position.character;
console.error( console.error(`${ name }:${ line + 1}:${ character + 1 }:${ failure.failure }`);
name });
+ ':' + (line + 1)
+ ':' + (character + 1)
+ ': ' + failure.failure
);
} }
gulp.task('tslint', function () { gulp.task('tslint', () => {
var options = { summarizeFailureOutput: true }; const options = { summarizeFailureOutput: true };
function reporter(failures) {
failures.forEach(failureReporter);
}
return gulp.src(all, { base: '.' }) return gulp.src(all, { base: '.' })
.pipe(filter(tslintFilter)) .pipe(filter(tslintFilter))
.pipe(gulptslint({ rulesDirectory: 'build/lib/tslint' })) .pipe(gulptslint({ rulesDirectory: 'build/lib/tslint' }))
.pipe(gulptslint.report(reporter, options)); .pipe(gulptslint.report(reportFailures, options));
}); });
var hygiene = exports.hygiene = function (some, options) { const hygiene = exports.hygiene = (some, options) => {
options = options || {}; options = options || {};
var errorCount = 0; let errorCount = 0;
var eol = es.through(function (file) { const eol = es.through(function (file) {
if (/\r\n?/g.test(file.contents.toString('utf8'))) { if (/\r\n?/g.test(file.contents.toString('utf8'))) {
console.error(file.relative + ': Bad EOL found'); console.error(file.relative + ': Bad EOL found');
errorCount++; errorCount++;
@ -137,11 +132,11 @@ var hygiene = exports.hygiene = function (some, options) {
this.emit('data', file); this.emit('data', file);
}); });
var indentation = es.through(function (file) { const indentation = es.through(function (file) {
file.contents file.contents
.toString('utf8') .toString('utf8')
.split(/\r\n|\r|\n/) .split(/\r\n|\r|\n/)
.forEach(function (line, i) { .forEach((line, i) => {
if (/^\s*$/.test(line)) { if (/^\s*$/.test(line)) {
// empty or whitespace lines are OK // empty or whitespace lines are OK
} else if (/^[\t]*[^\s]/.test(line)) { } else if (/^[\t]*[^\s]/.test(line)) {
@ -157,7 +152,7 @@ var hygiene = exports.hygiene = function (some, options) {
this.emit('data', file); this.emit('data', file);
}); });
var copyrights = es.through(function (file) { const copyrights = es.through(function (file) {
if (file.contents.toString('utf8').indexOf(copyrightHeader) !== 0) { if (file.contents.toString('utf8').indexOf(copyrightHeader) !== 0) {
console.error(file.relative + ': Missing or bad copyright statement'); console.error(file.relative + ': Missing or bad copyright statement');
errorCount++; errorCount++;
@ -166,25 +161,23 @@ var hygiene = exports.hygiene = function (some, options) {
this.emit('data', file); this.emit('data', file);
}); });
var tsl = es.through(function(file) { const tsl = es.through(function(file) {
var configuration = tslint.findConfiguration(null, '.'); const configuration = tslint.findConfiguration(null, '.');
var options = { const options = { configuration, formatter: 'json', rulesDirectory: 'build/lib/tslint' };
formatter: 'json', const contents = file.contents.toString('utf8');
configuration: configuration, const linter = new tslint(file.relative, contents, options);
rulesDirectory: 'build/lib/tslint' const result = linter.lint();
};
var contents = file.contents.toString('utf8');
var linter = new tslint(file.relative, contents, options);
var result = linter.lint();
if (result.failureCount > 0) { if (result.failureCount > 0) {
result.failures.forEach(failureReporter); reportFailures(result.failures);
errorCount += result.failureCount; errorCount += result.failureCount;
} }
this.emit('data', file); this.emit('data', file);
}); });
return gulp.src(some || all, { base: '.' }) return gulp.src(some || all, { base: '.' })
.pipe(filter(function (f) { return !f.stat.isDirectory(); })) .pipe(filter(f => !f.stat.isDirectory()))
.pipe(filter(eolFilter)) .pipe(filter(eolFilter))
.pipe(options.skipEOL ? es.through() : eol) .pipe(options.skipEOL ? es.through() : eol)
.pipe(filter(indentationFilter)) .pipe(filter(indentationFilter))
@ -202,28 +195,27 @@ var hygiene = exports.hygiene = function (some, options) {
})); }));
}; };
gulp.task('hygiene', function () { gulp.task('hygiene', () => hygiene());
return hygiene();
});
// this allows us to run this as a git pre-commit hook // this allows us to run hygiene as a git pre-commit hook
if (require.main === module) { if (require.main === module) {
var cp = require('child_process'); const cp = require('child_process');
cp.exec('git config core.autocrlf', function (err, out) {
var skipEOL = out.trim() === 'true';
cp.exec('git diff --cached --name-only', { maxBuffer: 2000 * 1024 }, function (err, out) { cp.exec('git config core.autocrlf', (err, out) => {
const skipEOL = out.trim() === 'true';
cp.exec('git diff --cached --name-only', { maxBuffer: 2000 * 1024 }, (err, out) => {
if (err) { if (err) {
console.error(); console.error();
console.error(err); console.error(err);
process.exit(1); process.exit(1);
} }
var some = out const some = out
.split(/\r?\n/) .split(/\r?\n/)
.filter(function (l) { return !!l; }); .filter(l => !!l);
hygiene(some, { skipEOL: skipEOL }).on('error', function (err) { hygiene(some, { skipEOL: skipEOL }).on('error', err => {
console.error(); console.error();
console.error(err); console.error(err);
process.exit(1); process.exit(1);