extracted-config.js 4.4 KB

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