whitespace-async.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env node
  2. const assign = require('object.assign');
  3. const entries = require('object.entries');
  4. const { ESLint } = require('eslint');
  5. const baseConfig = require('.');
  6. const whitespaceRules = require('./whitespaceRules');
  7. const severities = ['off', 'warn', 'error'];
  8. function getSeverity(ruleConfig) {
  9. if (Array.isArray(ruleConfig)) {
  10. return getSeverity(ruleConfig[0]);
  11. }
  12. if (typeof ruleConfig === 'number') {
  13. return severities[ruleConfig];
  14. }
  15. return ruleConfig;
  16. }
  17. async function onlyErrorOnRules(rulesToError, config) {
  18. const errorsOnly = assign({}, config);
  19. const cli = new ESLint({
  20. useEslintrc: false,
  21. baseConfig: config
  22. });
  23. const baseRules = (await cli.calculateConfigForFile(require.resolve('./'))).rules;
  24. entries(baseRules).forEach((rule) => {
  25. const ruleName = rule[0];
  26. const ruleConfig = rule[1];
  27. const severity = getSeverity(ruleConfig);
  28. if (rulesToError.indexOf(ruleName) === -1 && severity === 'error') {
  29. if (Array.isArray(ruleConfig)) {
  30. errorsOnly.rules[ruleName] = ['warn'].concat(ruleConfig.slice(1));
  31. } else if (typeof ruleConfig === 'number') {
  32. errorsOnly.rules[ruleName] = 1;
  33. } else {
  34. errorsOnly.rules[ruleName] = 'warn';
  35. }
  36. }
  37. });
  38. return errorsOnly;
  39. }
  40. onlyErrorOnRules(whitespaceRules, baseConfig).then((config) => console.log(JSON.stringify(config)));