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