]> git.proxmox.com Git - pve-eslint.git/blame - src/app.js
ignore unused 'me'
[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.')
11 .option('-f, --fix', 'if set, fixes will be applied.');
12
13program.on('--help', function() {
14 console.log('');
15 console.log(' Description:');
16 console.log('');
17 console.log(' lints javascript files');
eb39fafa
DC
18 console.log('');
19});
20
be566999
TL
21if (process.argv.length <= 2) {
22 program.help();
23}
24
eb39fafa
DC
25program.parse(process.argv);
26
27let paths = program.args;
28
29if (!paths.length) {
30 paths = [process.cwd()];
31}
32
33const defaultConfig = {
34 parserOptions: {
35 ecmaVersion: 2017,
36 ecmaFeatures: {
37 impliedStrict: true,
8e4082d9 38 },
eb39fafa
DC
39 },
40 env: {
41 browser: true,
42 node: true,
43 es2017: true,
44 },
45 globals: {
eb39fafa 46 Ext: "writable",
30ca1a0a
TL
47 FormData: "writable",
48 PBS: "writable",
eb39fafa 49 PMG: "writable",
30ca1a0a 50 PVE: "writable",
eb39fafa 51 PVE_vnc_console_event: "writable",
eb39fafa
DC
52 Proxmox: "writable",
53 console: "writable",
30ca1a0a
TL
54 eslint: "writable",
55 gettext: "writable",
eb39fafa
DC
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",
74e9b543 109 "no-unused-vars": ["error", { vars: "all", args: "none", varsIgnorePattern: "^me$" }],
eb39fafa
DC
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"],
ca06ae98 123 "dot-location": ["error", "property"],
eb39fafa
DC
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",
5fad4d81 142 "no-implicit-coercion": ["error", { allow: ["!!"] }],
eb39fafa
DC
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...
8e4082d9
DC
181 "array-bracket-spacing": ["warn", "never"],
182 "brace-style": ["warn", "1tbs", { allowSingleLine: true }],
183 "comma-dangle": ["warn", "always-multiline"], // maybe only-multiline?
eb39fafa
DC
184 "comma-spacing": "warn",
185 "comma-style": "warn",
186 "computed-property-spacing": "warn",
8e4082d9 187 "consistent-this": ["warn", "me"],
eb39fafa
DC
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",
8e4082d9 195 "max-len": ["warn", { code: 110, tabWidth: 8, ignoreComments: true, ignoreStrings: true, ignoreRegExpLiterals: true }],
eb39fafa
DC
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",
8e4082d9 201 "no-underscore-dangle": ["warn", { allowAfterThis: true }],
eb39fafa
DC
202 "no-unneeded-ternary": "warn",
203 "no-whitespace-before-property": "warn",
204 "object-curly-newline": "warn",
8e4082d9
DC
205 "object-curly-spacing": ["warn", "always"],
206 "operator-linebreak": ["warn", "after", { overrides: { "?": "after" } }],
eb39fafa 207 "padded-blocks": ["warn", "never"], // not sure ...
8e4082d9 208 "quote-props": ["warn", "as-needed", { keywords: true, unnecessary: false }], // does nothing, maybe deactivate unnecessary
eb39fafa
DC
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
226let config = defaultConfig;
227if (program.config) {
6d505c57
TL
228 let cfgpath = program.config;
229 if (cfgpath.match(/^[^/]/)) {
230 cfgpath = process.cwd() + "/" + cfgpath;
eb39fafa
DC
231 }
232 config = {
6d505c57 233 "extends": cfgpath,
8e4082d9 234 };
eb39fafa
DC
235}
236
237const cli = new eslint.CLIEngine({
238 baseConfig: config,
239 useEslintrc: true,
240 fix: !!program.fix,
241});
242
243const report = cli.executeOnFiles(paths);
244let exitcode = 0;
245let files_err = [];
246let files_warn = [];
247let files_ok = [];
248let fixes = 0;
249console.log('------------------------------------------------------------');
250report.results.forEach(function(result) {
6d505c57 251 let filename = path.relative(process.cwd(), result.filePath);
eb39fafa
DC
252 let msgs = result.messages;
253 let max_sev = 0;
254 if (!!program.fix && result.output) {
255 fixes++;
256 }
7b71dd2c 257 if (msgs.length <= 0) {
eb39fafa
DC
258 files_ok.push(filename);
259 return;
260 }
7b71dd2c
TL
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 }
eb39fafa
DC
295
296 console.log('------------------------------------------------------------');
297});
298
299if (report.results.length > 1) {
8e4082d9 300 console.log(`${color.bold(files_ok.length + files_err.length)} files:`);
eb39fafa 301 if (files_err.length > 0) {
7b71dd2c 302 console.log(color.red(` ${color.bold(files_err.length)} files have Errors`));
eb39fafa
DC
303 }
304 if (files_warn.length > 0) {
7b71dd2c 305 console.log(color.yellow(` ${color.bold(files_warn.length)} files have Warnings`));
eb39fafa
DC
306 }
307 if (files_ok.length > 0) {
7b71dd2c 308 console.log(color.green(` ${color.bold(files_ok.length)} files are OK`));
eb39fafa 309 }
eb39fafa
DC
310} else if (files_ok.length > 0) {
311 console.log(color.green(`${files_ok[0]} OK`));
eb39fafa 312}
7b71dd2c 313console.log('------------------------------------------------------------');
eb39fafa
DC
314
315if (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 }
7b71dd2c
TL
323} else if (fixes > 0) {
324 console.log(`${color.bold(fixes)} issues marked with (*) could be auto-fixed using '--fix'.`);
eb39fafa
DC
325}
326
327process.exit(exitcode);
eb39fafa 328}());