-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
lint.js
220 lines (210 loc) · 6.63 KB
/
lint.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
* Copyright 2015 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const argv = require('minimist')(process.argv.slice(2));
const colors = require('ansi-colors');
const config = require('../test-configs/config');
const deglob = require('globs-to-files');
const eslint = require('gulp-eslint');
const eslintIfFixed = require('gulp-eslint-if-fixed');
const gulp = require('gulp');
const lazypipe = require('lazypipe');
const log = require('fancy-log');
const path = require('path');
const watch = require('gulp-watch');
const {getFilesChanged, logOnSameLine} = require('../common/utils');
const {gitDiffNameOnlyMaster} = require('../common/git');
const {isTravisBuild} = require('../common/travis');
const {maybeUpdatePackages} = require('./update-packages');
const isWatching = argv.watch || argv.w || false;
const options = {
fix: false,
quiet: argv.quiet || false,
};
const rootDir = path.dirname(path.dirname(__dirname));
/**
* Initializes the linter stream based on globs
* @param {!Object} globs
* @param {!Object} streamOptions
* @return {!ReadableStream}
*/
function initializeStream(globs, streamOptions) {
let stream = gulp.src(globs, streamOptions);
if (isWatching) {
const watcher = lazypipe().pipe(
watch,
globs
);
stream = stream.pipe(watcher());
}
return stream;
}
/**
* Runs the linter on the given stream using the given options.
* @param {!ReadableStream} stream
* @param {!Object} options
* @return {boolean}
*/
function runLinter(stream, options) {
if (!isTravisBuild()) {
log(colors.green('Starting linter...'));
}
const fixedFiles = {};
return stream
.pipe(eslint(options))
.pipe(
eslint.formatEach('stylish', function(msg) {
logOnSameLine(msg.replace(`${rootDir}/`, '').trim() + '\n');
})
)
.pipe(eslintIfFixed(rootDir))
.pipe(
eslint.result(function(result) {
const relativePath = path.relative(rootDir, result.filePath);
if (!isTravisBuild()) {
logOnSameLine(colors.green('Linted: ') + relativePath);
}
if (options.fix && result.fixed) {
const status =
result.errorCount == 0
? colors.green('Fixed: ')
: colors.yellow('Partially fixed: ');
logOnSameLine(status + colors.cyan(relativePath));
fixedFiles[relativePath] = status;
}
})
)
.pipe(
eslint.results(function(results) {
if (results.errorCount == 0 && results.warningCount == 0) {
if (!isTravisBuild()) {
logOnSameLine(
colors.green('SUCCESS: ') + 'No linter warnings or errors.'
);
}
} else {
const prefix =
results.errorCount == 0
? colors.yellow('WARNING: ')
: colors.red('ERROR: ');
logOnSameLine(
prefix +
'Found ' +
results.errorCount +
' error(s) and ' +
results.warningCount +
' warning(s).'
);
if (!options.fix) {
log(
colors.yellow('NOTE 1:'),
'You may be able to automatically fix some of these warnings ' +
'/ errors by running',
colors.cyan('gulp lint --local_changes --fix'),
'from your local branch.'
);
log(
colors.yellow('NOTE 2:'),
'Since this is a destructive operation (that edits your files',
'in-place), make sure you commit before running the command.'
);
log(
colors.yellow('NOTE 3:'),
'If you see any',
colors.cyan('prettier/prettier'),
'errors, read',
colors.cyan(
'https://github.com/ampproject/amphtml/blob/master/contributing/getting-started-e2e.md#code-quality-and-style'
)
);
}
}
if (options.fix && Object.keys(fixedFiles).length > 0) {
log(colors.green('INFO: ') + 'Summary of fixes:');
Object.keys(fixedFiles).forEach(file => {
log(fixedFiles[file] + colors.cyan(file));
});
}
})
)
.pipe(eslint.failAfterError());
}
/**
* Checks if there are eslint rule changes, in which case we must lint all
* files.
*
* @return {boolean}
*/
function eslintRulesChanged() {
return (
gitDiffNameOnlyMaster().filter(function(file) {
return (
path.basename(file).includes('.eslintrc') ||
path.dirname(file) === 'build-system/eslint-rules'
);
}).length > 0
);
}
/**
* Gets the list of files to be linted.
*
* @param {!Array<string>} files
* @return {!Array<string>}
*/
function getFilesToLint(files) {
const filesToLint = deglob.sync(files);
if (!isTravisBuild()) {
log(colors.green('INFO: ') + 'Running lint on the following files:');
filesToLint.forEach(file => {
log(colors.cyan(path.relative(rootDir, file)));
});
}
return filesToLint;
}
/**
* Run the eslinter on the src javascript and log the output
* @return {!Stream} Readable stream
*/
function lint() {
maybeUpdatePackages();
if (argv.fix) {
options.fix = true;
}
let filesToLint = config.lintGlobs;
if (argv.files) {
filesToLint = getFilesToLint(argv.files.split(','));
} else if (!eslintRulesChanged() && argv.local_changes) {
const lintableFiles = getFilesChanged(config.lintGlobs);
if (lintableFiles.length == 0) {
log(colors.green('INFO: ') + 'No JS files in this PR');
return Promise.resolve();
}
filesToLint = getFilesToLint(lintableFiles);
}
const stream = initializeStream(filesToLint, {base: rootDir});
return runLinter(stream, options);
}
module.exports = {
lint,
};
lint.description = 'Validates against Google Closure Linter';
lint.flags = {
'watch': ' Watches for changes in files, validates against the linter',
'fix': ' Fixes simple lint errors (spacing etc)',
'files': ' Lints just the specified files',
'local_changes': ' Lints just the files changed in the local branch',
'quiet': ' Suppress warnings from outputting',
};