]> git.proxmox.com Git - pve-eslint.git/blame - src/app.js
bump version to 7.28.0-1
[pve-eslint.git] / src / app.js
CommitLineData
8e4082d9 1(function() {
eb39fafa
DC
2'use strict';
3
6d505c57 4const path = require('path');
eb39fafa
DC
5const color = require('colors');
6const program = require('commander');
7
8program
9 .usage('[options] [<file(s) ...>]')
10 .option('-c, --config <configfile>', 'uses <configfile> for eslint config instead.')
177b4d5e 11 .option('-e, --extend <configfile>', 'uses <configfile> ontop of default eslint config.')
4e679d18 12 .option('-f, --fix', 'if set, fixes will be applied.')
a4a25724 13 .option('-s, --strict', 'if set, also exit uncleanly on warnings')
4e679d18
TL
14 .option('--output-config', 'if set, only output the config as JSON and exit.')
15 ;
eb39fafa
DC
16
17program.on('--help', function() {
18 console.log('');
19 console.log(' Description:');
20 console.log('');
21 console.log(' lints javascript files');
eb39fafa
DC
22 console.log('');
23});
24
177b4d5e
TL
25program.parse(process.argv);
26
27if (program.config && program.extend) {
28 console.error('Cannot use both, --config and --extend, at the same time!');
29 process.exit(1);
be566999
TL
30}
31
4e679d18 32if (program.args.length < 1 && !program.outputConfig) {
177b4d5e
TL
33 program.help();
34}
eb39fafa
DC
35
36let paths = program.args;
37
38if (!paths.length) {
39 paths = [process.cwd()];
40}
41
42const defaultConfig = {
43 parserOptions: {
35543d16 44 ecmaVersion: 2020,
eb39fafa
DC
45 ecmaFeatures: {
46 impliedStrict: true,
8e4082d9 47 },
eb39fafa
DC
48 },
49 env: {
50 browser: true,
51 node: true,
35543d16 52 es2020: true, // automatically sets ecmaVersion to 2020 and allows es2020 globals
eb39fafa
DC
53 },
54 globals: {
eb39fafa 55 Ext: "writable",
30ca1a0a
TL
56 FormData: "writable",
57 PBS: "writable",
eb39fafa 58 PMG: "writable",
30ca1a0a 59 PVE: "writable",
eb39fafa 60 PVE_vnc_console_event: "writable",
eb39fafa
DC
61 Proxmox: "writable",
62 console: "writable",
30ca1a0a
TL
63 eslint: "writable",
64 gettext: "writable",
d4b999a4
TL
65 proxmoxOnlineHelpInfo: "writable",
66 pveOnlineHelpInfo: "writable",
eb39fafa
DC
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",
a4d939da 120 "no-unused-vars": ["error", { vars: "all", args: "none", varsIgnorePattern: "^(me|_.*)$" }],
eb39fafa
DC
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"],
ca06ae98 134 "dot-location": ["error", "property"],
eb39fafa
DC
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",
3958e250 144 //"no-else-return": "warn", // not a real style problem, both can be OK
eb39fafa
DC
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",
e89a2e92
TL
151 "no-extra-parens": ["error", "all", {
152 "conditionalAssign": false, // useful for if ((match = str.match(/.../))) lines
153 "enforceForArrowConditionals": false,
154 "nestedBinaryExpressions": false,
155 }],
eb39fafa 156 "no-floating-decimal": "error",
5fad4d81 157 "no-implicit-coercion": ["error", { allow: ["!!"] }],
eb39fafa
DC
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...
8e4082d9
DC
196 "array-bracket-spacing": ["warn", "never"],
197 "brace-style": ["warn", "1tbs", { allowSingleLine: true }],
198 "comma-dangle": ["warn", "always-multiline"], // maybe only-multiline?
eb39fafa
DC
199 "comma-spacing": "warn",
200 "comma-style": "warn",
201 "computed-property-spacing": "warn",
8e4082d9 202 "consistent-this": ["warn", "me"],
eb39fafa
DC
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",
8e4082d9 210 "max-len": ["warn", { code: 110, tabWidth: 8, ignoreComments: true, ignoreStrings: true, ignoreRegExpLiterals: true }],
eb39fafa
DC
211 "no-array-constructor": "warn",
212 "no-lonely-if": "warn",
6a98065e
TL
213 "no-mixed-operators": ["warn", {
214 "groups": [
215 //["+", "-", "*", "/", "%", "**"], // assume the devs can do basic math..
216 ["&", "|", "^", "~", "<<", ">>", ">>>"],
217 ["==", "!=", "===", "!==", ">", ">=", "<", "<="],
218 ["&&", "||"],
3a486d1d 219 ["in", "instanceof"],
6a98065e 220 ],
3a486d1d 221 "allowSamePrecedence": true,
6a98065e 222 }],
eb39fafa
DC
223 "no-multiple-empty-lines": "warn",
224 "no-trailing-spaces": "warn",
3fb7828e 225 // "no-underscore-dangle": ["warn", { allowAfterThis: true }], // we use it for unused variables, like in rust
eb39fafa
DC
226 "no-unneeded-ternary": "warn",
227 "no-whitespace-before-property": "warn",
067b576d
TL
228 "object-curly-newline": ["warn", {
229 "multiline": true,
230 //"minProperties": 2, // FIXME: enable this??
231 "consistent": true,
232 }],
8e4082d9 233 "object-curly-spacing": ["warn", "always"],
630511ae
TL
234 "operator-linebreak": ["warn", "after", {
235 "overrides": {
236 "?": "before",
237 ":": "before",
238 "+": "ignore",
239 },
240 }],
eb39fafa 241 "padded-blocks": ["warn", "never"], // not sure ...
8e4082d9 242 "quote-props": ["warn", "as-needed", { keywords: true, unnecessary: false }], // does nothing, maybe deactivate unnecessary
eb39fafa
DC
243 "semi": "warn",
244 "semi-spacing": "warn",
490226fa 245 // "semi-style": "warn", // could be useful, but for ?: we often want to allow the ; on the next line
eb39fafa
DC
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",
630511ae 254 // "no-confusing-arrow": "warn", // can be useful to do and isn't really confusing
eb39fafa
DC
255 "prefer-numeric-literals": "warn",
256 "template-curly-spacing": "warn",
257 },
258};
259
756e2b86
TL
260let pathExpand = (p) => {
261 if (p.match(/^[^/]/)) {
262 p = process.cwd() + "/" + p;
263 }
264 return p;
265};
266
eb39fafa
DC
267let config = defaultConfig;
268if (program.config) {
eb39fafa 269 config = {
756e2b86 270 "extends": pathExpand(program.config),
8e4082d9 271 };
177b4d5e
TL
272} else if (program.extend) {
273 config.extends = pathExpand(program.extend);
274 console.log(`Extend with path: ${config.extends}`);
eb39fafa
DC
275}
276
4e679d18
TL
277if (program.outputConfig) {
278 let cfg = JSON.stringify(config, null, 2);
279 console.log(cfg);
280 process.exit(0);
281}
282
eb39fafa
DC
283const cli = new eslint.CLIEngine({
284 baseConfig: config,
285 useEslintrc: true,
286 fix: !!program.fix,
0803a17d 287 cwd: process.cwd(),
eb39fafa
DC
288});
289
290const report = cli.executeOnFiles(paths);
35543d16 291
eb39fafa 292let exitcode = 0;
35543d16 293let files_err = [], files_warn = [], files_ok = [];
eb39fafa
DC
294let fixes = 0;
295console.log('------------------------------------------------------------');
296report.results.forEach(function(result) {
6d505c57 297 let filename = path.relative(process.cwd(), result.filePath);
eb39fafa
DC
298 let msgs = result.messages;
299 let max_sev = 0;
300 if (!!program.fix && result.output) {
301 fixes++;
302 }
7b71dd2c 303 if (msgs.length <= 0) {
eb39fafa
DC
304 files_ok.push(filename);
305 return;
306 }
7b71dd2c
TL
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);
a4a25724
TL
315 if (exitcode < 1 && !!program.strict) {
316 exitcode = 1;
317 }
7b71dd2c
TL
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 }
eb39fafa
DC
344
345 console.log('------------------------------------------------------------');
346});
347
348if (report.results.length > 1) {
8e4082d9 349 console.log(`${color.bold(files_ok.length + files_err.length)} files:`);
eb39fafa 350 if (files_err.length > 0) {
7b71dd2c 351 console.log(color.red(` ${color.bold(files_err.length)} files have Errors`));
eb39fafa
DC
352 }
353 if (files_warn.length > 0) {
7b71dd2c 354 console.log(color.yellow(` ${color.bold(files_warn.length)} files have Warnings`));
eb39fafa
DC
355 }
356 if (files_ok.length > 0) {
7b71dd2c 357 console.log(color.green(` ${color.bold(files_ok.length)} files are OK`));
eb39fafa 358 }
eb39fafa
DC
359} else if (files_ok.length > 0) {
360 console.log(color.green(`${files_ok[0]} OK`));
eb39fafa 361}
7b71dd2c 362console.log('------------------------------------------------------------');
eb39fafa
DC
363
364if (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 }
7b71dd2c
TL
372} else if (fixes > 0) {
373 console.log(`${color.bold(fixes)} issues marked with (*) could be auto-fixed using '--fix'.`);
eb39fafa
DC
374}
375
376process.exit(exitcode);
eb39fafa 377}());