]> git.proxmox.com Git - pve-eslint.git/blame - src/app.js
adapt some rules
[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",
eb39fafa
DC
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",
a4d939da 117 "no-unused-vars": ["error", { vars: "all", args: "none", varsIgnorePattern: "^(me|_.*)$" }],
eb39fafa
DC
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"],
ca06ae98 131 "dot-location": ["error", "property"],
eb39fafa
DC
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",
5fad4d81 150 "no-implicit-coercion": ["error", { allow: ["!!"] }],
eb39fafa
DC
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...
8e4082d9
DC
189 "array-bracket-spacing": ["warn", "never"],
190 "brace-style": ["warn", "1tbs", { allowSingleLine: true }],
191 "comma-dangle": ["warn", "always-multiline"], // maybe only-multiline?
eb39fafa
DC
192 "comma-spacing": "warn",
193 "comma-style": "warn",
194 "computed-property-spacing": "warn",
8e4082d9 195 "consistent-this": ["warn", "me"],
eb39fafa
DC
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",
8e4082d9 203 "max-len": ["warn", { code: 110, tabWidth: 8, ignoreComments: true, ignoreStrings: true, ignoreRegExpLiterals: true }],
eb39fafa
DC
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",
8e4082d9 209 "no-underscore-dangle": ["warn", { allowAfterThis: true }],
eb39fafa
DC
210 "no-unneeded-ternary": "warn",
211 "no-whitespace-before-property": "warn",
212 "object-curly-newline": "warn",
8e4082d9 213 "object-curly-spacing": ["warn", "always"],
630511ae
TL
214 "operator-linebreak": ["warn", "after", {
215 "overrides": {
216 "?": "before",
217 ":": "before",
218 "+": "ignore",
219 },
220 }],
eb39fafa 221 "padded-blocks": ["warn", "never"], // not sure ...
8e4082d9 222 "quote-props": ["warn", "as-needed", { keywords: true, unnecessary: false }], // does nothing, maybe deactivate unnecessary
eb39fafa
DC
223 "semi": "warn",
224 "semi-spacing": "warn",
225 "semi-style": "warn",
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",
630511ae 234 // "no-confusing-arrow": "warn", // can be useful to do and isn't really confusing
eb39fafa
DC
235 "prefer-numeric-literals": "warn",
236 "template-curly-spacing": "warn",
237 },
238};
239
756e2b86
TL
240let pathExpand = (p) => {
241 if (p.match(/^[^/]/)) {
242 p = process.cwd() + "/" + p;
243 }
244 return p;
245};
246
eb39fafa
DC
247let config = defaultConfig;
248if (program.config) {
eb39fafa 249 config = {
756e2b86 250 "extends": pathExpand(program.config),
8e4082d9 251 };
177b4d5e
TL
252} else if (program.extend) {
253 config.extends = pathExpand(program.extend);
254 console.log(`Extend with path: ${config.extends}`);
eb39fafa
DC
255}
256
4e679d18
TL
257if (program.outputConfig) {
258 let cfg = JSON.stringify(config, null, 2);
259 console.log(cfg);
260 process.exit(0);
261}
262
eb39fafa
DC
263const cli = new eslint.CLIEngine({
264 baseConfig: config,
265 useEslintrc: true,
266 fix: !!program.fix,
267});
268
269const report = cli.executeOnFiles(paths);
270let exitcode = 0;
271let files_err = [];
272let files_warn = [];
273let files_ok = [];
274let fixes = 0;
275console.log('------------------------------------------------------------');
276report.results.forEach(function(result) {
6d505c57 277 let filename = path.relative(process.cwd(), result.filePath);
eb39fafa
DC
278 let msgs = result.messages;
279 let max_sev = 0;
280 if (!!program.fix && result.output) {
281 fixes++;
282 }
7b71dd2c 283 if (msgs.length <= 0) {
eb39fafa
DC
284 files_ok.push(filename);
285 return;
286 }
7b71dd2c
TL
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 }
eb39fafa
DC
321
322 console.log('------------------------------------------------------------');
323});
324
325if (report.results.length > 1) {
8e4082d9 326 console.log(`${color.bold(files_ok.length + files_err.length)} files:`);
eb39fafa 327 if (files_err.length > 0) {
7b71dd2c 328 console.log(color.red(` ${color.bold(files_err.length)} files have Errors`));
eb39fafa
DC
329 }
330 if (files_warn.length > 0) {
7b71dd2c 331 console.log(color.yellow(` ${color.bold(files_warn.length)} files have Warnings`));
eb39fafa
DC
332 }
333 if (files_ok.length > 0) {
7b71dd2c 334 console.log(color.green(` ${color.bold(files_ok.length)} files are OK`));
eb39fafa 335 }
eb39fafa
DC
336} else if (files_ok.length > 0) {
337 console.log(color.green(`${files_ok[0]} OK`));
eb39fafa 338}
7b71dd2c 339console.log('------------------------------------------------------------');
eb39fafa
DC
340
341if (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 }
7b71dd2c
TL
349} else if (fixes > 0) {
350 console.log(`${color.bold(fixes)} issues marked with (*) could be auto-fixed using '--fix'.`);
eb39fafa
DC
351}
352
353process.exit(exitcode);
eb39fafa 354}());