extracted-config.js 4.1 KB

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