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