extracted-config.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * @fileoverview `ExtractedConfig` class.
  3. *
  4. * `ExtractedConfig` class expresses a final configuration for a specific file.
  5. *
  6. * It provides one method.
  7. *
  8. * - `toCompatibleObjectAsConfigFileContent()`
  9. * Convert this configuration to the compatible object as the content of
  10. * config files. It converts the loaded parser and plugins to strings.
  11. * `CLIEngine#getConfigForFile(filePath)` method uses this method.
  12. *
  13. * `ConfigArray#extractConfig(filePath)` creates a `ExtractedConfig` instance.
  14. *
  15. * @author Toru Nagashima <https://github.com/mysticatea>
  16. */
  17. import { IgnorePattern } from "./ignore-pattern.js";
  18. // For VSCode intellisense
  19. /** @typedef {import("../../shared/types").ConfigData} ConfigData */
  20. /** @typedef {import("../../shared/types").GlobalConf} GlobalConf */
  21. /** @typedef {import("../../shared/types").SeverityConf} SeverityConf */
  22. /** @typedef {import("./config-dependency").DependentParser} DependentParser */
  23. /** @typedef {import("./config-dependency").DependentPlugin} DependentPlugin */
  24. /**
  25. * Check if `xs` starts with `ys`.
  26. * @template T
  27. * @param {T[]} xs The array to check.
  28. * @param {T[]} ys The array that may be the first part of `xs`.
  29. * @returns {boolean} `true` if `xs` starts with `ys`.
  30. */
  31. function startsWith(xs, ys) {
  32. return xs.length >= ys.length && ys.every((y, i) => y === xs[i]);
  33. }
  34. /**
  35. * The class for extracted config data.
  36. */
  37. class ExtractedConfig {
  38. constructor() {
  39. /**
  40. * The config name what `noInlineConfig` setting came from.
  41. * @type {string}
  42. */
  43. this.configNameOfNoInlineConfig = "";
  44. /**
  45. * Environments.
  46. * @type {Record<string, boolean>}
  47. */
  48. this.env = {};
  49. /**
  50. * Global variables.
  51. * @type {Record<string, GlobalConf>}
  52. */
  53. this.globals = {};
  54. /**
  55. * The glob patterns that ignore to lint.
  56. * @type {(((filePath:string, dot?:boolean) => boolean) & { basePath:string; patterns:string[] }) | undefined}
  57. */
  58. this.ignores = void 0;
  59. /**
  60. * The flag that disables directive comments.
  61. * @type {boolean|undefined}
  62. */
  63. this.noInlineConfig = void 0;
  64. /**
  65. * Parser definition.
  66. * @type {DependentParser|null}
  67. */
  68. this.parser = null;
  69. /**
  70. * Options for the parser.
  71. * @type {Object}
  72. */
  73. this.parserOptions = {};
  74. /**
  75. * Plugin definitions.
  76. * @type {Record<string, DependentPlugin>}
  77. */
  78. this.plugins = {};
  79. /**
  80. * Processor ID.
  81. * @type {string|null}
  82. */
  83. this.processor = null;
  84. /**
  85. * The flag that reports unused `eslint-disable` directive comments.
  86. * @type {boolean|undefined}
  87. */
  88. this.reportUnusedDisableDirectives = void 0;
  89. /**
  90. * Rule settings.
  91. * @type {Record<string, [SeverityConf, ...any[]]>}
  92. */
  93. this.rules = {};
  94. /**
  95. * Shared settings.
  96. * @type {Object}
  97. */
  98. this.settings = {};
  99. }
  100. /**
  101. * Convert this config to the compatible object as a config file content.
  102. * @returns {ConfigData} The converted object.
  103. */
  104. toCompatibleObjectAsConfigFileContent() {
  105. const {
  106. /* eslint-disable no-unused-vars */
  107. configNameOfNoInlineConfig: _ignore1,
  108. processor: _ignore2,
  109. /* eslint-enable no-unused-vars */
  110. ignores,
  111. ...config
  112. } = this;
  113. config.parser = config.parser && config.parser.filePath;
  114. config.plugins = Object.keys(config.plugins).filter(Boolean).reverse();
  115. config.ignorePatterns = ignores ? ignores.patterns : [];
  116. // Strip the default patterns from `ignorePatterns`.
  117. if (startsWith(config.ignorePatterns, IgnorePattern.DefaultPatterns)) {
  118. config.ignorePatterns =
  119. config.ignorePatterns.slice(IgnorePattern.DefaultPatterns.length);
  120. }
  121. return config;
  122. }
  123. }
  124. export { ExtractedConfig };