2015-11-24 18:09:31 +00:00
|
|
|
/*---------------------------------------------------------------------------------------------
|
|
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
* Licensed under the MIT License. See License.txt in the project root for license information.
|
|
|
|
*--------------------------------------------------------------------------------------------*/
|
|
|
|
|
2016-07-28 08:28:01 +00:00
|
|
|
'use strict';
|
2015-11-24 18:09:31 +00:00
|
|
|
|
2016-07-28 08:28:01 +00:00
|
|
|
const gulp = require('gulp');
|
|
|
|
const filter = require('gulp-filter');
|
|
|
|
const es = require('event-stream');
|
|
|
|
const gulptslint = require('gulp-tslint');
|
2017-06-20 14:21:44 +00:00
|
|
|
const gulpeslint = require('gulp-eslint');
|
2016-10-07 15:25:32 +00:00
|
|
|
const tsfmt = require('typescript-formatter');
|
2016-07-28 08:28:01 +00:00
|
|
|
const tslint = require('tslint');
|
2017-10-10 15:53:33 +00:00
|
|
|
const vfs = require('vinyl-fs');
|
2016-07-28 08:28:01 +00:00
|
|
|
|
2016-11-11 08:12:25 +00:00
|
|
|
/**
|
|
|
|
* Hygiene works by creating cascading subsets of all our files and
|
2016-11-11 08:58:50 +00:00
|
|
|
* passing them through a sequence of checks. Here are the current subsets,
|
|
|
|
* named according to the checks performed on them. Each subset contains
|
|
|
|
* the following one, as described in mathematical notation:
|
2016-11-11 08:12:25 +00:00
|
|
|
*
|
|
|
|
* all ⊃ eol ⊇ indentation ⊃ copyright ⊃ typescript
|
|
|
|
*/
|
|
|
|
|
2016-07-28 08:28:01 +00:00
|
|
|
const all = [
|
2015-12-17 15:04:06 +00:00
|
|
|
'*',
|
2015-11-24 18:09:31 +00:00
|
|
|
'build/**/*',
|
|
|
|
'extensions/**/*',
|
|
|
|
'scripts/**/*',
|
|
|
|
'src/**/*',
|
2015-11-25 21:01:45 +00:00
|
|
|
'test/**/*'
|
|
|
|
];
|
|
|
|
|
2016-07-28 08:28:01 +00:00
|
|
|
const eolFilter = [
|
2015-11-25 21:01:45 +00:00
|
|
|
'**',
|
2015-12-17 15:04:06 +00:00
|
|
|
'!ThirdPartyNotices.txt',
|
|
|
|
'!LICENSE.txt',
|
2015-11-24 18:09:31 +00:00
|
|
|
'!extensions/**/out/**',
|
2017-09-04 12:41:46 +00:00
|
|
|
'!test/smoke/out/**',
|
2015-11-24 18:09:31 +00:00
|
|
|
'!**/node_modules/**',
|
|
|
|
'!**/fixtures/**',
|
2016-07-20 14:58:02 +00:00
|
|
|
'!**/*.{svg,exe,png,bmp,scpt,bat,cmd,cur,ttf,woff,eot}',
|
2016-05-31 14:29:52 +00:00
|
|
|
'!build/{lib,tslintRules}/**/*.js',
|
2016-07-20 15:14:42 +00:00
|
|
|
'!build/monaco/**',
|
2017-05-12 14:38:39 +00:00
|
|
|
'!build/win32/**',
|
2017-05-15 10:21:14 +00:00
|
|
|
'!build/**/*.sh',
|
2017-05-17 07:43:42 +00:00
|
|
|
'!build/tfs/**/*.js',
|
2017-05-15 10:21:14 +00:00
|
|
|
'!**/Dockerfile'
|
2015-11-24 18:09:31 +00:00
|
|
|
];
|
|
|
|
|
2016-07-28 08:28:01 +00:00
|
|
|
const indentationFilter = [
|
2015-11-24 18:09:31 +00:00
|
|
|
'**',
|
2015-12-17 15:04:06 +00:00
|
|
|
'!ThirdPartyNotices.txt',
|
|
|
|
'!**/*.md',
|
2017-05-09 09:35:22 +00:00
|
|
|
'!**/*.ps1',
|
2016-02-10 22:55:38 +00:00
|
|
|
'!**/*.template',
|
2017-10-04 11:54:22 +00:00
|
|
|
'!**/*.yaml',
|
2015-12-17 15:04:06 +00:00
|
|
|
'!**/*.yml',
|
2017-11-13 21:36:57 +00:00
|
|
|
'!**/yarn.lock',
|
2015-12-10 17:38:31 +00:00
|
|
|
'!**/lib/**',
|
2017-04-26 16:04:48 +00:00
|
|
|
'!extensions/**/*.d.ts',
|
|
|
|
'!src/typings/**/*.d.ts',
|
|
|
|
'!src/vs/*/**/*.d.ts',
|
2016-05-31 10:38:54 +00:00
|
|
|
'!**/*.d.ts.recipe',
|
2015-11-24 18:09:31 +00:00
|
|
|
'!test/assert.js',
|
|
|
|
'!**/package.json',
|
|
|
|
'!**/octicons/**',
|
2016-01-21 15:15:42 +00:00
|
|
|
'!**/vs/base/common/marked/raw.marked.js',
|
2015-11-24 18:09:31 +00:00
|
|
|
'!**/vs/base/common/winjs.base.raw.js',
|
|
|
|
'!**/vs/base/node/terminateProcess.sh',
|
2017-12-04 16:21:44 +00:00
|
|
|
'!**/vs/base/node/ps-win.ps1',
|
2015-11-24 18:09:31 +00:00
|
|
|
'!**/vs/nls.js',
|
|
|
|
'!**/vs/css.js',
|
|
|
|
'!**/vs/loader.js',
|
|
|
|
'!extensions/**/snippets/**',
|
|
|
|
'!extensions/**/syntaxes/**',
|
2016-04-11 13:54:58 +00:00
|
|
|
'!extensions/**/themes/**',
|
2016-04-28 15:14:58 +00:00
|
|
|
'!extensions/**/colorize-fixtures/**',
|
2018-01-26 15:16:36 +00:00
|
|
|
'!extensions/vscode-api-tests/testWorkspace/**',
|
|
|
|
'!extensions/vscode-api-tests/testWorkspace2/**'
|
2015-11-24 18:09:31 +00:00
|
|
|
];
|
|
|
|
|
2016-07-28 08:28:01 +00:00
|
|
|
const copyrightFilter = [
|
2015-11-24 18:09:31 +00:00
|
|
|
'**',
|
2016-02-10 22:55:38 +00:00
|
|
|
'!**/*.desktop',
|
2015-11-24 18:09:31 +00:00
|
|
|
'!**/*.json',
|
|
|
|
'!**/*.html',
|
2016-02-10 22:55:38 +00:00
|
|
|
'!**/*.template',
|
2015-11-24 18:09:31 +00:00
|
|
|
'!**/*.md',
|
2015-12-18 08:21:11 +00:00
|
|
|
'!**/*.bat',
|
|
|
|
'!**/*.cmd',
|
2016-06-12 15:54:03 +00:00
|
|
|
'!**/*.xml',
|
2015-11-24 18:09:31 +00:00
|
|
|
'!**/*.sh',
|
|
|
|
'!**/*.txt',
|
2016-03-24 21:01:51 +00:00
|
|
|
'!**/*.xpm',
|
2016-11-11 08:58:50 +00:00
|
|
|
'!**/*.opts',
|
|
|
|
'!**/*.disabled',
|
2018-01-26 15:16:36 +00:00
|
|
|
'!**/*.code-workspace',
|
2017-07-03 09:07:57 +00:00
|
|
|
'!build/**/*.init',
|
2017-10-04 16:53:27 +00:00
|
|
|
'!resources/linux/snap/snapcraft.yaml',
|
2016-11-11 08:58:50 +00:00
|
|
|
'!resources/win32/bin/code.js',
|
2016-11-16 09:58:34 +00:00
|
|
|
'!extensions/markdown/media/tomorrow.css',
|
|
|
|
'!extensions/html/server/src/modes/typescript/*'
|
2015-11-24 18:09:31 +00:00
|
|
|
];
|
|
|
|
|
2017-06-20 14:21:44 +00:00
|
|
|
const eslintFilter = [
|
|
|
|
'src/**/*.js',
|
2017-07-12 07:57:12 +00:00
|
|
|
'build/gulpfile.*.js',
|
2017-06-20 14:21:44 +00:00
|
|
|
'!src/vs/loader.js',
|
2017-06-23 14:31:32 +00:00
|
|
|
'!src/vs/css.js',
|
2017-06-20 14:21:44 +00:00
|
|
|
'!src/vs/nls.js',
|
2017-06-23 14:31:32 +00:00
|
|
|
'!src/vs/css.build.js',
|
|
|
|
'!src/vs/nls.build.js',
|
2017-06-20 14:21:44 +00:00
|
|
|
'!src/**/winjs.base.raw.js',
|
|
|
|
'!src/**/raw.marked.js',
|
|
|
|
'!**/test/**'
|
|
|
|
];
|
|
|
|
|
2016-07-28 08:28:01 +00:00
|
|
|
const tslintFilter = [
|
2016-01-18 08:50:41 +00:00
|
|
|
'src/**/*.ts',
|
2018-01-30 09:35:31 +00:00
|
|
|
//'test/**/*.ts',
|
|
|
|
//'extensions/**/*.ts',
|
2016-11-11 08:58:50 +00:00
|
|
|
'!**/fixtures/**',
|
2016-01-18 08:50:41 +00:00
|
|
|
'!**/typings/**',
|
2016-11-10 09:03:57 +00:00
|
|
|
'!**/node_modules/**',
|
2016-09-15 09:17:56 +00:00
|
|
|
'!extensions/typescript/test/colorize-fixtures/**',
|
|
|
|
'!extensions/vscode-api-tests/testWorkspace/**',
|
2018-01-26 15:16:36 +00:00
|
|
|
'!extensions/vscode-api-tests/testWorkspace2/**',
|
2017-11-07 10:53:59 +00:00
|
|
|
'!extensions/**/*.test.ts',
|
|
|
|
'!extensions/html/server/lib/jquery.d.ts'
|
2016-01-18 08:50:41 +00:00
|
|
|
];
|
|
|
|
|
2016-07-28 08:28:01 +00:00
|
|
|
const copyrightHeader = [
|
2015-11-24 18:09:31 +00:00
|
|
|
'/*---------------------------------------------------------------------------------------------',
|
|
|
|
' * Copyright (c) Microsoft Corporation. All rights reserved.',
|
|
|
|
' * Licensed under the MIT License. See License.txt in the project root for license information.',
|
|
|
|
' *--------------------------------------------------------------------------------------------*/'
|
|
|
|
].join('\n');
|
|
|
|
|
2017-06-20 14:21:44 +00:00
|
|
|
gulp.task('eslint', () => {
|
2017-10-31 15:11:10 +00:00
|
|
|
return vfs.src(all, { base: '.', follow: true, allowEmpty: true })
|
2017-06-20 14:21:44 +00:00
|
|
|
.pipe(filter(eslintFilter))
|
|
|
|
.pipe(gulpeslint('src/.eslintrc'))
|
|
|
|
.pipe(gulpeslint.formatEach('compact'))
|
|
|
|
.pipe(gulpeslint.failAfterError());
|
|
|
|
});
|
|
|
|
|
2016-07-28 08:28:01 +00:00
|
|
|
gulp.task('tslint', () => {
|
2017-11-13 10:12:56 +00:00
|
|
|
const options = { emitError: true };
|
2016-02-18 15:02:16 +00:00
|
|
|
|
2017-10-31 15:11:10 +00:00
|
|
|
return vfs.src(all, { base: '.', follow: true, allowEmpty: true })
|
2016-01-18 08:50:41 +00:00
|
|
|
.pipe(filter(tslintFilter))
|
2016-03-07 12:48:06 +00:00
|
|
|
.pipe(gulptslint({ rulesDirectory: 'build/lib/tslint' }))
|
2017-11-07 10:53:59 +00:00
|
|
|
.pipe(gulptslint.report(options));
|
2016-01-18 08:50:41 +00:00
|
|
|
});
|
|
|
|
|
2016-07-28 08:28:01 +00:00
|
|
|
const hygiene = exports.hygiene = (some, options) => {
|
2016-03-04 15:26:05 +00:00
|
|
|
options = options || {};
|
2016-07-28 08:28:01 +00:00
|
|
|
let errorCount = 0;
|
2015-11-24 18:09:31 +00:00
|
|
|
|
2016-07-28 08:28:01 +00:00
|
|
|
const eol = es.through(function (file) {
|
2015-11-24 18:09:31 +00:00
|
|
|
if (/\r\n?/g.test(file.contents.toString('utf8'))) {
|
2015-11-25 21:01:45 +00:00
|
|
|
console.error(file.relative + ': Bad EOL found');
|
2015-11-24 18:09:31 +00:00
|
|
|
errorCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.emit('data', file);
|
|
|
|
});
|
|
|
|
|
2016-07-28 08:28:01 +00:00
|
|
|
const indentation = es.through(function (file) {
|
2017-10-04 11:54:22 +00:00
|
|
|
file.contents
|
|
|
|
.toString('utf8')
|
|
|
|
.split(/\r\n|\r|\n/)
|
|
|
|
.forEach((line, i) => {
|
|
|
|
if (/^\s*$/.test(line)) {
|
|
|
|
// empty or whitespace lines are OK
|
|
|
|
} else if (/^[\t]*[^\s]/.test(line)) {
|
|
|
|
// good indent
|
|
|
|
} else if (/^[\t]* \*/.test(line)) {
|
|
|
|
// block comment using an extra space
|
|
|
|
} else {
|
|
|
|
console.error(file.relative + '(' + (i + 1) + ',1): Bad whitespace indentation');
|
|
|
|
errorCount++;
|
|
|
|
}
|
|
|
|
});
|
2015-11-24 18:09:31 +00:00
|
|
|
|
|
|
|
this.emit('data', file);
|
|
|
|
});
|
|
|
|
|
2016-07-28 08:28:01 +00:00
|
|
|
const copyrights = es.through(function (file) {
|
2015-11-24 18:09:31 +00:00
|
|
|
if (file.contents.toString('utf8').indexOf(copyrightHeader) !== 0) {
|
2015-11-25 21:01:45 +00:00
|
|
|
console.error(file.relative + ': Missing or bad copyright statement');
|
2015-11-24 18:09:31 +00:00
|
|
|
errorCount++;
|
|
|
|
}
|
2015-11-25 08:47:15 +00:00
|
|
|
|
2015-11-25 08:29:48 +00:00
|
|
|
this.emit('data', file);
|
2015-11-24 18:09:31 +00:00
|
|
|
});
|
|
|
|
|
2016-10-07 15:39:26 +00:00
|
|
|
const formatting = es.map(function (file, cb) {
|
2016-10-07 15:25:32 +00:00
|
|
|
tsfmt.processString(file.path, file.contents.toString('utf8'), {
|
|
|
|
verify: true,
|
|
|
|
tsfmt: true,
|
2018-01-25 16:11:34 +00:00
|
|
|
// verbose: true
|
2016-10-07 15:25:32 +00:00
|
|
|
}).then(result => {
|
|
|
|
if (result.error) {
|
2016-10-07 15:42:22 +00:00
|
|
|
console.error(result.message);
|
2016-10-07 15:25:32 +00:00
|
|
|
errorCount++;
|
|
|
|
}
|
2016-10-07 15:39:26 +00:00
|
|
|
cb(null, file);
|
2016-10-07 15:25:32 +00:00
|
|
|
|
|
|
|
}, err => {
|
2016-10-07 15:39:26 +00:00
|
|
|
cb(err);
|
2016-10-07 15:25:32 +00:00
|
|
|
});
|
|
|
|
});
|
2017-11-13 10:12:56 +00:00
|
|
|
|
2017-11-07 10:53:59 +00:00
|
|
|
function reportFailures(failures) {
|
|
|
|
failures.forEach(failure => {
|
|
|
|
const name = failure.name || failure.fileName;
|
|
|
|
const position = failure.startPosition;
|
|
|
|
const line = position.lineAndCharacter ? position.lineAndCharacter.line : position.line;
|
|
|
|
const character = position.lineAndCharacter ? position.lineAndCharacter.character : position.character;
|
|
|
|
|
|
|
|
console.error(`${name}:${line + 1}:${character + 1}:${failure.failure}`);
|
|
|
|
});
|
|
|
|
}
|
2016-10-07 15:25:32 +00:00
|
|
|
|
2018-02-07 21:50:02 +00:00
|
|
|
const program = tslint.Linter.createProgram('src/tsconfig.json');
|
2018-02-03 16:47:14 +00:00
|
|
|
const configuration = tslint.Configuration.findConfiguration('tslint-hygiene.json', '.');
|
|
|
|
const tslintOptions = { fix: false, formatter: 'json' };
|
2018-02-02 15:11:16 +00:00
|
|
|
const linter = new tslint.Linter(tslintOptions, program);
|
|
|
|
|
2016-11-11 08:58:50 +00:00
|
|
|
const tsl = es.through(function (file) {
|
2016-07-28 08:28:01 +00:00
|
|
|
const contents = file.contents.toString('utf8');
|
2018-02-07 21:50:02 +00:00
|
|
|
if (file.relative.startsWith('src/')) { // only lint files in src program
|
|
|
|
linter.lint(file.relative, contents, configuration.results);
|
|
|
|
}
|
2016-02-18 14:24:52 +00:00
|
|
|
this.emit('data', file);
|
|
|
|
});
|
2016-02-18 14:01:53 +00:00
|
|
|
|
2017-10-24 08:55:40 +00:00
|
|
|
const result = vfs.src(some || all, { base: '.', follow: true, allowEmpty: true })
|
2016-07-28 08:28:01 +00:00
|
|
|
.pipe(filter(f => !f.stat.isDirectory()))
|
2015-11-25 21:01:45 +00:00
|
|
|
.pipe(filter(eolFilter))
|
2016-03-04 09:44:20 +00:00
|
|
|
.pipe(options.skipEOL ? es.through() : eol)
|
2015-11-24 18:09:31 +00:00
|
|
|
.pipe(filter(indentationFilter))
|
|
|
|
.pipe(indentation)
|
2016-01-18 08:50:41 +00:00
|
|
|
.pipe(filter(copyrightFilter))
|
2017-06-20 14:21:44 +00:00
|
|
|
.pipe(copyrights);
|
|
|
|
|
|
|
|
const typescript = result
|
2016-02-18 14:01:53 +00:00
|
|
|
.pipe(filter(tslintFilter))
|
2016-10-07 15:25:32 +00:00
|
|
|
.pipe(formatting)
|
2017-06-20 14:21:44 +00:00
|
|
|
.pipe(tsl);
|
|
|
|
|
|
|
|
const javascript = result
|
|
|
|
.pipe(filter(eslintFilter))
|
|
|
|
.pipe(gulpeslint('src/.eslintrc'))
|
|
|
|
.pipe(gulpeslint.formatEach('compact'))
|
|
|
|
.pipe(gulpeslint.failAfterError());
|
|
|
|
|
2017-11-02 21:23:55 +00:00
|
|
|
let count = 0;
|
2017-06-20 14:21:44 +00:00
|
|
|
return es.merge(typescript, javascript)
|
2017-11-02 21:23:55 +00:00
|
|
|
.pipe(es.through(function (data) {
|
|
|
|
count++;
|
2017-11-13 10:32:38 +00:00
|
|
|
if (process.env['TRAVIS'] && count % 10 === 0) {
|
2017-11-02 21:23:55 +00:00
|
|
|
process.stdout.write('.');
|
|
|
|
}
|
|
|
|
this.emit('data', data);
|
|
|
|
}, function () {
|
|
|
|
process.stdout.write('\n');
|
2018-02-03 16:47:14 +00:00
|
|
|
|
2018-02-02 15:11:16 +00:00
|
|
|
const tslintResult = linter.getResult();
|
|
|
|
if (tslintResult.failures.length > 0) {
|
|
|
|
reportFailures(tslintResult.failures);
|
|
|
|
errorCount += tslintResult.failures.length;
|
|
|
|
}
|
|
|
|
|
2015-11-24 18:09:31 +00:00
|
|
|
if (errorCount > 0) {
|
2015-11-26 08:57:29 +00:00
|
|
|
this.emit('error', 'Hygiene failed with ' + errorCount + ' errors. Check \'build/gulpfile.hygiene.js\'.');
|
2015-11-24 18:09:31 +00:00
|
|
|
} else {
|
|
|
|
this.emit('end');
|
|
|
|
}
|
|
|
|
}));
|
2015-11-25 21:01:45 +00:00
|
|
|
};
|
|
|
|
|
2017-10-10 15:53:33 +00:00
|
|
|
gulp.task('hygiene', () => hygiene(''));
|
2015-11-25 21:01:45 +00:00
|
|
|
|
2016-07-28 08:28:01 +00:00
|
|
|
// this allows us to run hygiene as a git pre-commit hook
|
2015-11-25 21:01:45 +00:00
|
|
|
if (require.main === module) {
|
2016-07-28 08:28:01 +00:00
|
|
|
const cp = require('child_process');
|
|
|
|
|
2017-01-13 08:12:06 +00:00
|
|
|
process.on('unhandledRejection', (reason, p) => {
|
|
|
|
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
|
|
|
|
process.exit(1);
|
|
|
|
});
|
|
|
|
|
2016-07-28 08:28:01 +00:00
|
|
|
cp.exec('git config core.autocrlf', (err, out) => {
|
|
|
|
const skipEOL = out.trim() === 'true';
|
2016-03-04 09:44:20 +00:00
|
|
|
|
2017-03-21 10:28:26 +00:00
|
|
|
if (process.argv.length > 2) {
|
|
|
|
return hygiene(process.argv.slice(2), { skipEOL: skipEOL }).on('error', err => {
|
|
|
|
console.error();
|
|
|
|
console.error(err);
|
|
|
|
process.exit(1);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-07-28 08:28:01 +00:00
|
|
|
cp.exec('git diff --cached --name-only', { maxBuffer: 2000 * 1024 }, (err, out) => {
|
2016-03-04 09:44:20 +00:00
|
|
|
if (err) {
|
|
|
|
console.error();
|
|
|
|
console.error(err);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2015-11-25 21:01:45 +00:00
|
|
|
|
2016-07-28 08:28:01 +00:00
|
|
|
const some = out
|
2016-03-04 09:44:20 +00:00
|
|
|
.split(/\r?\n/)
|
2016-07-28 08:28:01 +00:00
|
|
|
.filter(l => !!l);
|
2015-11-25 21:01:45 +00:00
|
|
|
|
2017-11-06 14:39:46 +00:00
|
|
|
if (some.length > 0) {
|
|
|
|
hygiene(some, { skipEOL: skipEOL }).on('error', err => {
|
|
|
|
console.error();
|
|
|
|
console.error(err);
|
|
|
|
process.exit(1);
|
|
|
|
});
|
|
|
|
}
|
2015-11-25 21:01:45 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|