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