]> git.proxmox.com Git - pve-eslint.git/blob - src/app.js
drop "lints cwd on no params" hint
[pve-eslint.git] / src / app.js
1 (function () {
2 'use strict';
3
4 const color = require('colors');
5 const program = require('commander');
6
7 program
8 .usage('[options] [<file(s) ...>]')
9 .option('-c, --config <configfile>', 'uses <configfile> for eslint config instead.')
10 .option('-f, --fix', 'if set, fixes will be applied.');
11
12 program.on('--help', function() {
13 console.log('');
14 console.log(' Description:');
15 console.log('');
16 console.log(' lints javascript files');
17 console.log('');
18 });
19
20 if (process.argv.length <= 2) {
21 program.help();
22 }
23
24 program.parse(process.argv);
25
26 let paths = program.args;
27
28 if (!paths.length) {
29 paths = [process.cwd()];
30 }
31
32 const defaultConfig = {
33 parserOptions: {
34 ecmaVersion: 2017,
35 ecmaFeatures: {
36 impliedStrict: true,
37 }
38 },
39 env: {
40 browser: true,
41 node: true,
42 es2017: true,
43 },
44 globals: {
45 Ext: "writable",
46 FormData: "writable",
47 PBS: "writable",
48 PMG: "writable",
49 PVE: "writable",
50 PVE_vnc_console_event: "writable",
51 Proxmox: "writable",
52 console: "writable",
53 eslint: "writable",
54 gettext: "writable",
55 },
56 rules: {
57 // from eslint:recommend, with tweaks for our source
58 "constructor-super": "error",
59 "for-direction": "error",
60 "getter-return": "error",
61 "no-async-promise-executor": "error",
62 "no-case-declarations": "error",
63 "no-class-assign": "error",
64 "no-compare-neg-zero": "error",
65 "no-cond-assign": "error",
66 "no-const-assign": "error",
67 "no-constant-condition": "error",
68 "no-control-regex": "error",
69 "no-debugger": "error",
70 "no-delete-var": "error",
71 "no-dupe-args": "error",
72 "no-dupe-class-members": "error",
73 "no-dupe-else-if": "error",
74 "no-dupe-keys": "error",
75 "no-duplicate-case": "error",
76 "no-empty": "error",
77 "no-empty-character-class": "error",
78 "no-empty-pattern": "error",
79 "no-ex-assign": "error",
80 "no-extra-boolean-cast": "error",
81 "no-extra-semi": "error",
82 "no-fallthrough": "error",
83 "no-func-assign": "error",
84 "no-global-assign": "error",
85 "no-import-assign": "error",
86 "no-inner-declarations": "error",
87 "no-invalid-regexp": "error",
88 "no-irregular-whitespace": "error",
89 "no-misleading-character-class": "error",
90 "no-mixed-spaces-and-tabs": ["error", "smart-tabs"],
91 "no-new-symbol": "error",
92 "no-obj-calls": "error",
93 "no-octal": "error",
94 "no-prototype-builtins": "error",
95 "no-redeclare": "error",
96 "no-regex-spaces": "error",
97 "no-self-assign": "error",
98 "no-setter-return": "error",
99 "no-shadow-restricted-names": "error",
100 "no-sparse-arrays": "error",
101 "no-this-before-super": "error",
102 "no-undef": "error",
103 "no-unexpected-multiline": "error",
104 "no-unreachable": "error",
105 "no-unsafe-finally": "error",
106 "no-unsafe-negation": "error",
107 "no-unused-labels": "error",
108 "no-unused-vars": ["error", { vars: "all", args: "none" } ],
109 "no-useless-catch": "error",
110 "no-useless-escape": "error",
111 "no-with": "error",
112 "require-yield": "error",
113 "use-isnan": "error",
114 "valid-typeof": "error",
115
116 // selection of best practices
117 "accessor-pairs": "error",
118 "array-callback-return": "error",
119 "block-scoped-var": "error",
120 "consistent-return": "error",
121 "curly": ["error", "multi-line"],
122 "dot-location": ["error", "property"],
123 "dot-notation": "error",
124 "eqeqeq": "error",
125 "grouped-accessor-pairs": "error",
126 "guard-for-in": "error",
127 "no-alert": "error",
128 "no-await-in-loop": "error",
129 "no-caller": "error",
130 "no-constructor-return": "error",
131 "no-div-regex": "error",
132 "no-else-return": "error",
133 "no-empty-function": "error",
134 "no-eq-null": "error",
135 "no-eval": "error",
136 "no-extend-native": "error",
137 "no-extra-bind": "error",
138 "no-extra-label": "error",
139 "no-extra-parens": "error",
140 "no-floating-decimal": "error",
141 "no-implicit-coercion": "error",
142 "no-implicit-globals": "error",
143 "no-implied-eval": "error",
144 "no-invalid-this": "error",
145 "no-lone-blocks": "error",
146 "no-loop-func": "error",
147 "no-multi-spaces": "error",
148 "no-multi-str": "error",
149 "no-new": "error",
150 "no-new-func": "error",
151 "no-new-wrappers": "error",
152 "no-octal-escape": "error",
153 "no-proto": "error",
154 "no-return-assign": "error",
155 "no-return-await": "error",
156 "no-script-url": "error",
157 "no-self-compare": "error",
158 "no-sequences": "error",
159 "no-template-curly-in-string": "error",
160 "no-unmodified-loop-condition": "error",
161 "no-unused-expressions": "error",
162 "no-useless-call": "error",
163 "no-useless-concat": "error",
164 "no-useless-return": "error",
165 "no-void": "error",
166 "prefer-regex-literals": "error",
167 "radix": "error",
168 "require-atomic-updates": "error",
169 "wrap-iife": "error",
170 "yoda": "error",
171
172 // variable issues
173 "no-label-var": "error",
174 "no-shadow": "error",
175 "no-undef-init": "error",
176 "no-use-before-define": "error",
177
178 // stylistic issues, only warn, most can be auto-fixed
179 // those are quite opinionated...
180 "array-bracket-spacing": [ "warn", "never" ],
181 "brace-style": [ "warn", "1tbs", { allowSingleLine: true }],
182 "comma-dangle": [ "warn", "always-multiline" ], // maybe only-multiline?
183 "comma-spacing": "warn",
184 "comma-style": "warn",
185 "computed-property-spacing": "warn",
186 "consistent-this": [ "warn", "me" ],
187 "eol-last": "warn",
188 "func-call-spacing": "warn",
189 "func-name-matching": "warn",
190 "func-style": "warn",
191 "key-spacing": "warn",
192 "keyword-spacing": "warn",
193 "linebreak-style": "warn",
194 "max-len": [ "warn", { code: 110, tabWidth: 8, ignoreComments: true, ignoreStrings: true, ignoreRegExpLiterals: true }],
195 "no-array-constructor": "warn",
196 "no-lonely-if": "warn",
197 "no-mixed-operators": "warn",
198 "no-multiple-empty-lines": "warn",
199 "no-trailing-spaces": "warn",
200 "no-underscore-dangle": [ "warn", { allowAfterThis: true, }],
201 "no-unneeded-ternary": "warn",
202 "no-whitespace-before-property": "warn",
203 "object-curly-newline": "warn",
204 "object-curly-spacing": [ "warn", "always" ],
205 "operator-linebreak" : [ "warn", "after", { overrides: { "?": "after" }}],
206 "padded-blocks": ["warn", "never"], // not sure ...
207 "quote-props": [ "warn", "as-needed", { keywords: true, unnecessary: false }], // does nothing, maybe deactivate unnecessary
208 "semi": "warn",
209 "semi-spacing": "warn",
210 "semi-style": "warn",
211 "space-before-blocks": "warn",
212 "space-before-function-paren": ["warn", "never"],
213 "space-in-parens": "warn",
214 "space-unary-ops": "warn",
215 "switch-colon-spacing": "warn",
216 "unicode-bom": "warn",
217 "arrow-body-style": "warn",
218 "arrow-spacing": "warn",
219 "no-confusing-arrow": "warn",
220 "prefer-numeric-literals": "warn",
221 "template-curly-spacing": "warn",
222 },
223 };
224
225 let config = defaultConfig;
226 if (program.config) {
227 let path = program.config;
228 if (program.config.match(/^[^/]/)) {
229 path = process.cwd() + "/" + path;
230 }
231 config = {
232 extends: path
233 }
234 }
235
236 const cli = new eslint.CLIEngine({
237 baseConfig: config,
238 useEslintrc: true,
239 fix: !!program.fix,
240 });
241
242 const report = cli.executeOnFiles(paths);
243 let exitcode = 0;
244 let files_err = [];
245 let files_warn = [];
246 let files_ok = [];
247 let fixes = 0;
248 console.log('------------------------------------------------------------');
249 report.results.forEach(function(result) {
250 let filename = result.filePath;
251 let msgs = result.messages;
252 let max_sev = 0;
253 if (!!program.fix && result.output) {
254 fixes++;
255 }
256 if (msgs.length > 0) {
257 console.error('[' + color.bold(filename) + ']:');
258 msgs.forEach(function(e) {
259 let msg = 'at line ' + color.bold(e.line) + ' column ' +
260 color.bold(e.column) + ': ' + e.ruleId;
261 if (e.severity === 1) {
262 console.error(color.yellow("Warning " + msg));
263 if (max_sev < 1) {
264 max_sev = 1;
265 }
266 } else if (e.severity === 2) {
267 console.error(color.red("Error " + msg));
268 if (exitcode < 1) {
269 exitcode = 1;
270 }
271 if (max_sev < 2) {
272 max_sev = 2;
273 }
274 } else {
275 console.error("Info " + msg);
276 }
277 if (e.message) {
278 console.error(e.message);
279 }
280 if (e.suggestion) {
281 console.error(e.suggestion);
282 }
283 if (!program.fix && e.fix) {
284 fixes++;
285 console.error("Could be auto-fixed");
286 }
287 });
288
289 if (max_sev === 1) {
290 files_warn.push(filename);
291 } else if (max_sev === 2) {
292 files_err.push(filename);
293 }
294 } else {
295 files_ok.push(filename);
296 return;
297 }
298
299 console.log('------------------------------------------------------------');
300 });
301
302 if (report.results.length > 1) {
303 console.log((`${color.bold(files_ok.length + files_err.length)} files:`));
304 if (files_err.length > 0) {
305 console.log(color.red(`${color.bold(files_err.length)} files have Errors`));
306 }
307 if (files_warn.length > 0) {
308 console.log(color.yellow(`${color.bold(files_warn.length)} files have Warnings`));
309 }
310 if (files_ok.length > 0) {
311 console.log(color.green(`${color.bold(files_ok.length)} files are OK`));
312 }
313 console.log('------------------------------------------------------------');
314 } else if (files_ok.length > 0) {
315 console.log(color.green(`${files_ok[0]} OK`));
316 console.log('------------------------------------------------------------');
317 }
318
319 if (program.fix) {
320 if (fixes > 0) {
321 console.log(`Writing ${color.bold(fixes)} fixed files...`);
322 eslint.CLIEngine.outputFixes(report);
323 console.log('Done');
324 } else {
325 console.log("No fixable Errors/Warnings found.");
326 }
327 } else {
328 console.log(`${color.bold(fixes)} Errors/Warnings could be auto-fixed.`);
329 }
330
331 process.exit(exitcode);
332
333 }());