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