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