]> git.proxmox.com Git - pve-eslint.git/blob - src/app.js
add --output-config CLI option
[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('--output-config', 'if set, only output the config as JSON and exit.')
14 ;
15
16 program.on('--help', function() {
17 console.log('');
18 console.log(' Description:');
19 console.log('');
20 console.log(' lints javascript files');
21 console.log('');
22 });
23
24 program.parse(process.argv);
25
26 if (program.config && program.extend) {
27 console.error('Cannot use both, --config and --extend, at the same time!');
28 process.exit(1);
29 }
30
31 if (program.args.length < 1 && !program.outputConfig) {
32 program.help();
33 }
34
35 let paths = program.args;
36
37 if (!paths.length) {
38 paths = [process.cwd()];
39 }
40
41 const defaultConfig = {
42 parserOptions: {
43 ecmaVersion: 2017,
44 ecmaFeatures: {
45 impliedStrict: true,
46 },
47 },
48 env: {
49 browser: true,
50 node: true,
51 es2017: true,
52 },
53 globals: {
54 Ext: "writable",
55 FormData: "writable",
56 PBS: "writable",
57 PMG: "writable",
58 PVE: "writable",
59 PVE_vnc_console_event: "writable",
60 Proxmox: "writable",
61 console: "writable",
62 eslint: "writable",
63 gettext: "writable",
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",
117 "no-unused-vars": ["error", { vars: "all", args: "none", varsIgnorePattern: "^(me|_.*)$" }],
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"],
131 "dot-location": ["error", "property"],
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",
150 "no-implicit-coercion": ["error", { allow: ["!!"] }],
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...
189 "array-bracket-spacing": ["warn", "never"],
190 "brace-style": ["warn", "1tbs", { allowSingleLine: true }],
191 "comma-dangle": ["warn", "always-multiline"], // maybe only-multiline?
192 "comma-spacing": "warn",
193 "comma-style": "warn",
194 "computed-property-spacing": "warn",
195 "consistent-this": ["warn", "me"],
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",
203 "max-len": ["warn", { code: 110, tabWidth: 8, ignoreComments: true, ignoreStrings: true, ignoreRegExpLiterals: true }],
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",
209 "no-underscore-dangle": ["warn", { allowAfterThis: true }],
210 "no-unneeded-ternary": "warn",
211 "no-whitespace-before-property": "warn",
212 "object-curly-newline": "warn",
213 "object-curly-spacing": ["warn", "always"],
214 "operator-linebreak": ["warn", "after", { overrides: { "?": "after" } }],
215 "padded-blocks": ["warn", "never"], // not sure ...
216 "quote-props": ["warn", "as-needed", { keywords: true, unnecessary: false }], // does nothing, maybe deactivate unnecessary
217 "semi": "warn",
218 "semi-spacing": "warn",
219 "semi-style": "warn",
220 "space-before-blocks": "warn",
221 "space-before-function-paren": ["warn", "never"],
222 "space-in-parens": "warn",
223 "space-unary-ops": "warn",
224 "switch-colon-spacing": "warn",
225 "unicode-bom": "warn",
226 "arrow-body-style": "warn",
227 "arrow-spacing": "warn",
228 "no-confusing-arrow": "warn",
229 "prefer-numeric-literals": "warn",
230 "template-curly-spacing": "warn",
231 },
232 };
233
234 let pathExpand = (p) => {
235 if (p.match(/^[^/]/)) {
236 p = process.cwd() + "/" + p;
237 }
238 return p;
239 };
240
241 let config = defaultConfig;
242 if (program.config) {
243 config = {
244 "extends": pathExpand(program.config),
245 };
246 } else if (program.extend) {
247 config.extends = pathExpand(program.extend);
248 console.log(`Extend with path: ${config.extends}`);
249 }
250
251 if (program.outputConfig) {
252 let cfg = JSON.stringify(config, null, 2);
253 console.log(cfg);
254 process.exit(0);
255 }
256
257 const cli = new eslint.CLIEngine({
258 baseConfig: config,
259 useEslintrc: true,
260 fix: !!program.fix,
261 });
262
263 const report = cli.executeOnFiles(paths);
264 let exitcode = 0;
265 let files_err = [];
266 let files_warn = [];
267 let files_ok = [];
268 let fixes = 0;
269 console.log('------------------------------------------------------------');
270 report.results.forEach(function(result) {
271 let filename = path.relative(process.cwd(), result.filePath);
272 let msgs = result.messages;
273 let max_sev = 0;
274 if (!!program.fix && result.output) {
275 fixes++;
276 }
277 if (msgs.length <= 0) {
278 files_ok.push(filename);
279 return;
280 }
281 console.error(`[./${color.bold(filename)}]:`);
282 msgs.forEach(function(e) {
283 if (max_sev < e.severity) {
284 max_sev = e.severity;
285 }
286 let msg = `: line ${color.bold(e.line)} col ${color.bold(e.column)}: ${e.ruleId}`;
287 if (e.severity === 1) {
288 msg = color.yellow("WARN" + msg);
289 } else if (e.severity === 2) {
290 msg = color.red("ERR " + msg);
291 if (exitcode < 1) {
292 exitcode = 1;
293 }
294 } else {
295 msg = "INFO" + msg;
296 }
297 if (e.message) {
298 msg += ` - ${e.message}`;
299 }
300 if (!program.fix && e.fix) {
301 fixes++;
302 msg += ' (*)';
303 }
304 console.error(msg);
305 if (e.suggestion) {
306 console.error(e.suggestion);
307 }
308 });
309
310 if (max_sev === 1) {
311 files_warn.push(filename);
312 } else if (max_sev === 2) {
313 files_err.push(filename);
314 }
315
316 console.log('------------------------------------------------------------');
317 });
318
319 if (report.results.length > 1) {
320 console.log(`${color.bold(files_ok.length + files_err.length)} files:`);
321 if (files_err.length > 0) {
322 console.log(color.red(` ${color.bold(files_err.length)} files have Errors`));
323 }
324 if (files_warn.length > 0) {
325 console.log(color.yellow(` ${color.bold(files_warn.length)} files have Warnings`));
326 }
327 if (files_ok.length > 0) {
328 console.log(color.green(` ${color.bold(files_ok.length)} files are OK`));
329 }
330 } else if (files_ok.length > 0) {
331 console.log(color.green(`${files_ok[0]} OK`));
332 }
333 console.log('------------------------------------------------------------');
334
335 if (program.fix) {
336 if (fixes > 0) {
337 console.log(`Writing ${color.bold(fixes)} fixed files...`);
338 eslint.CLIEngine.outputFixes(report);
339 console.log('Done');
340 } else {
341 console.log("No fixable Errors/Warnings found.");
342 }
343 } else if (fixes > 0) {
344 console.log(`${color.bold(fixes)} issues marked with (*) could be auto-fixed using '--fix'.`);
345 }
346
347 process.exit(exitcode);
348 }());