whitespace.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* eslint global-require: 0 */
  2. const { CLIEngine } = require('eslint');
  3. if (CLIEngine) {
  4. /* eslint no-inner-declarations: 0 */
  5. const assign = require('object.assign');
  6. const entries = require('object.entries');
  7. const whitespaceRules = require('./whitespaceRules');
  8. const baseConfig = require('.');
  9. const severities = ['off', 'warn', 'error'];
  10. function getSeverity(ruleConfig) {
  11. if (Array.isArray(ruleConfig)) {
  12. return getSeverity(ruleConfig[0]);
  13. }
  14. if (typeof ruleConfig === 'number') {
  15. return severities[ruleConfig];
  16. }
  17. return ruleConfig;
  18. }
  19. function onlyErrorOnRules(rulesToError, config) {
  20. const errorsOnly = assign({}, config);
  21. const cli = new CLIEngine({ baseConfig: config, useEslintrc: false });
  22. const baseRules = cli.getConfigForFile(require.resolve('./')).rules;
  23. entries(baseRules).forEach((rule) => {
  24. const ruleName = rule[0];
  25. const ruleConfig = rule[1];
  26. const severity = getSeverity(ruleConfig);
  27. if (rulesToError.indexOf(ruleName) === -1 && severity === 'error') {
  28. if (Array.isArray(ruleConfig)) {
  29. errorsOnly.rules[ruleName] = ['warn'].concat(ruleConfig.slice(1));
  30. } else if (typeof ruleConfig === 'number') {
  31. errorsOnly.rules[ruleName] = 1;
  32. } else {
  33. errorsOnly.rules[ruleName] = 'warn';
  34. }
  35. }
  36. });
  37. return errorsOnly;
  38. }
  39. module.exports = onlyErrorOnRules(whitespaceRules, baseConfig);
  40. } else {
  41. const path = require('path');
  42. const { execSync } = require('child_process');
  43. module.exports = JSON.parse(String(execSync(path.join(__dirname, 'whitespace-async.js'))));
  44. }