whitespace.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 baseConfig = require('.');
  8. const severities = ['off', 'warn', 'error'];
  9. function getSeverity(ruleConfig) {
  10. if (Array.isArray(ruleConfig)) {
  11. return getSeverity(ruleConfig[0]);
  12. }
  13. if (typeof ruleConfig === 'number') {
  14. return severities[ruleConfig];
  15. }
  16. return ruleConfig;
  17. }
  18. function onlyErrorOnRules(rulesToError, config) {
  19. const errorsOnly = assign({}, config);
  20. const cli = new CLIEngine({ baseConfig: config, useEslintrc: false });
  21. const baseRules = cli.getConfigForFile(require.resolve('./')).rules;
  22. entries(baseRules).forEach((rule) => {
  23. const ruleName = rule[0];
  24. const ruleConfig = rule[1];
  25. const severity = getSeverity(ruleConfig);
  26. if (rulesToError.indexOf(ruleName) === -1 && severity === 'error') {
  27. if (Array.isArray(ruleConfig)) {
  28. errorsOnly.rules[ruleName] = ['warn'].concat(ruleConfig.slice(1));
  29. } else if (typeof ruleConfig === 'number') {
  30. errorsOnly.rules[ruleName] = 1;
  31. } else {
  32. errorsOnly.rules[ruleName] = 'warn';
  33. }
  34. }
  35. });
  36. return errorsOnly;
  37. }
  38. module.exports = onlyErrorOnRules([
  39. 'array-bracket-newline',
  40. 'array-bracket-spacing',
  41. 'array-element-newline',
  42. 'arrow-spacing',
  43. 'block-spacing',
  44. 'comma-spacing',
  45. 'computed-property-spacing',
  46. 'dot-location',
  47. 'eol-last',
  48. 'func-call-spacing',
  49. 'function-paren-newline',
  50. 'generator-star-spacing',
  51. 'implicit-arrow-linebreak',
  52. 'indent',
  53. 'key-spacing',
  54. 'keyword-spacing',
  55. 'line-comment-position',
  56. 'linebreak-style',
  57. 'multiline-ternary',
  58. 'newline-per-chained-call',
  59. 'no-irregular-whitespace',
  60. 'no-mixed-spaces-and-tabs',
  61. 'no-multi-spaces',
  62. 'no-regex-spaces',
  63. 'no-spaced-func',
  64. 'no-trailing-spaces',
  65. 'no-whitespace-before-property',
  66. 'nonblock-statement-body-position',
  67. 'object-curly-newline',
  68. 'object-curly-spacing',
  69. 'object-property-newline',
  70. 'one-var-declaration-per-line',
  71. 'operator-linebreak',
  72. 'padded-blocks',
  73. 'padding-line-between-statements',
  74. 'rest-spread-spacing',
  75. 'semi-spacing',
  76. 'semi-style',
  77. 'space-before-blocks',
  78. 'space-before-function-paren',
  79. 'space-in-parens',
  80. 'space-infix-ops',
  81. 'space-unary-ops',
  82. 'spaced-comment',
  83. 'switch-colon-spacing',
  84. 'template-tag-spacing',
  85. 'import/newline-after-import',
  86. ], baseConfig);
  87. } else {
  88. const path = require('path');
  89. const { execSync } = require('child_process');
  90. module.exports = JSON.parse(String(execSync(path.join(__dirname, 'whitespace-async.js'))));
  91. }