flat-config-schema.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /**
  2. * @fileoverview Flat config schema
  3. * @author Nicholas C. Zakas
  4. */
  5. "use strict";
  6. //-----------------------------------------------------------------------------
  7. // Requirements
  8. //-----------------------------------------------------------------------------
  9. const { normalizeSeverityToNumber } = require("../shared/severity");
  10. //-----------------------------------------------------------------------------
  11. // Type Definitions
  12. //-----------------------------------------------------------------------------
  13. /**
  14. * @typedef ObjectPropertySchema
  15. * @property {Function|string} merge The function or name of the function to call
  16. * to merge multiple objects with this property.
  17. * @property {Function|string} validate The function or name of the function to call
  18. * to validate the value of this property.
  19. */
  20. //-----------------------------------------------------------------------------
  21. // Helpers
  22. //-----------------------------------------------------------------------------
  23. const ruleSeverities = new Map([
  24. [0, 0],
  25. ["off", 0],
  26. [1, 1],
  27. ["warn", 1],
  28. [2, 2],
  29. ["error", 2],
  30. ]);
  31. /**
  32. * Check if a value is a non-null object.
  33. * @param {any} value The value to check.
  34. * @returns {boolean} `true` if the value is a non-null object.
  35. */
  36. function isNonNullObject(value) {
  37. return typeof value === "object" && value !== null;
  38. }
  39. /**
  40. * Check if a value is a non-null non-array object.
  41. * @param {any} value The value to check.
  42. * @returns {boolean} `true` if the value is a non-null non-array object.
  43. */
  44. function isNonArrayObject(value) {
  45. return isNonNullObject(value) && !Array.isArray(value);
  46. }
  47. /**
  48. * Check if a value is undefined.
  49. * @param {any} value The value to check.
  50. * @returns {boolean} `true` if the value is undefined.
  51. */
  52. function isUndefined(value) {
  53. return typeof value === "undefined";
  54. }
  55. /**
  56. * Deeply merges two non-array objects.
  57. * @param {Object} first The base object.
  58. * @param {Object} second The overrides object.
  59. * @param {Map<string, Map<string, Object>>} [mergeMap] Maps the combination of first and second arguments to a merged result.
  60. * @returns {Object} An object with properties from both first and second.
  61. */
  62. function deepMerge(first, second, mergeMap = new Map()) {
  63. let secondMergeMap = mergeMap.get(first);
  64. if (secondMergeMap) {
  65. const result = secondMergeMap.get(second);
  66. if (result) {
  67. // If this combination of first and second arguments has been already visited, return the previously created result.
  68. return result;
  69. }
  70. } else {
  71. secondMergeMap = new Map();
  72. mergeMap.set(first, secondMergeMap);
  73. }
  74. /*
  75. * First create a result object where properties from the second object
  76. * overwrite properties from the first. This sets up a baseline to use
  77. * later rather than needing to inspect and change every property
  78. * individually.
  79. */
  80. const result = {
  81. ...first,
  82. ...second,
  83. };
  84. delete result.__proto__; // eslint-disable-line no-proto -- don't merge own property "__proto__"
  85. // Store the pending result for this combination of first and second arguments.
  86. secondMergeMap.set(second, result);
  87. for (const key of Object.keys(second)) {
  88. // avoid hairy edge case
  89. if (
  90. key === "__proto__" ||
  91. !Object.prototype.propertyIsEnumerable.call(first, key)
  92. ) {
  93. continue;
  94. }
  95. const firstValue = first[key];
  96. const secondValue = second[key];
  97. if (isNonArrayObject(firstValue) && isNonArrayObject(secondValue)) {
  98. result[key] = deepMerge(firstValue, secondValue, mergeMap);
  99. } else if (isUndefined(secondValue)) {
  100. result[key] = firstValue;
  101. }
  102. }
  103. return result;
  104. }
  105. /**
  106. * Normalizes the rule options config for a given rule by ensuring that
  107. * it is an array and that the first item is 0, 1, or 2.
  108. * @param {Array|string|number} ruleOptions The rule options config.
  109. * @returns {Array} An array of rule options.
  110. */
  111. function normalizeRuleOptions(ruleOptions) {
  112. const finalOptions = Array.isArray(ruleOptions)
  113. ? ruleOptions.slice(0)
  114. : [ruleOptions];
  115. finalOptions[0] = ruleSeverities.get(finalOptions[0]);
  116. return structuredClone(finalOptions);
  117. }
  118. /**
  119. * Determines if an object has any methods.
  120. * @param {Object} object The object to check.
  121. * @returns {boolean} `true` if the object has any methods.
  122. */
  123. function hasMethod(object) {
  124. for (const key of Object.keys(object)) {
  125. if (typeof object[key] === "function") {
  126. return true;
  127. }
  128. }
  129. return false;
  130. }
  131. //-----------------------------------------------------------------------------
  132. // Assertions
  133. //-----------------------------------------------------------------------------
  134. /**
  135. * The error type when a rule's options are configured with an invalid type.
  136. */
  137. class InvalidRuleOptionsError extends Error {
  138. /**
  139. * @param {string} ruleId Rule name being configured.
  140. * @param {any} value The invalid value.
  141. */
  142. constructor(ruleId, value) {
  143. super(
  144. `Key "${ruleId}": Expected severity of "off", 0, "warn", 1, "error", or 2.`,
  145. );
  146. this.messageTemplate = "invalid-rule-options";
  147. this.messageData = { ruleId, value };
  148. }
  149. }
  150. /**
  151. * Validates that a value is a valid rule options entry.
  152. * @param {string} ruleId Rule name being configured.
  153. * @param {any} value The value to check.
  154. * @returns {void}
  155. * @throws {InvalidRuleOptionsError} If the value isn't a valid rule options.
  156. */
  157. function assertIsRuleOptions(ruleId, value) {
  158. if (
  159. typeof value !== "string" &&
  160. typeof value !== "number" &&
  161. !Array.isArray(value)
  162. ) {
  163. throw new InvalidRuleOptionsError(ruleId, value);
  164. }
  165. }
  166. /**
  167. * The error type when a rule's severity is invalid.
  168. */
  169. class InvalidRuleSeverityError extends Error {
  170. /**
  171. * @param {string} ruleId Rule name being configured.
  172. * @param {any} value The invalid value.
  173. */
  174. constructor(ruleId, value) {
  175. super(
  176. `Key "${ruleId}": Expected severity of "off", 0, "warn", 1, "error", or 2.`,
  177. );
  178. this.messageTemplate = "invalid-rule-severity";
  179. this.messageData = { ruleId, value };
  180. }
  181. }
  182. /**
  183. * Validates that a value is valid rule severity.
  184. * @param {string} ruleId Rule name being configured.
  185. * @param {any} value The value to check.
  186. * @returns {void}
  187. * @throws {InvalidRuleSeverityError} If the value isn't a valid rule severity.
  188. */
  189. function assertIsRuleSeverity(ruleId, value) {
  190. const severity = ruleSeverities.get(value);
  191. if (typeof severity === "undefined") {
  192. throw new InvalidRuleSeverityError(ruleId, value);
  193. }
  194. }
  195. /**
  196. * Validates that a given string is the form pluginName/objectName.
  197. * @param {string} value The string to check.
  198. * @returns {void}
  199. * @throws {TypeError} If the string isn't in the correct format.
  200. */
  201. function assertIsPluginMemberName(value) {
  202. if (!/[\w\-@$]+(?:\/[\w\-$]+)+$/iu.test(value)) {
  203. throw new TypeError(
  204. `Expected string in the form "pluginName/objectName" but found "${value}".`,
  205. );
  206. }
  207. }
  208. /**
  209. * Validates that a value is an object.
  210. * @param {any} value The value to check.
  211. * @returns {void}
  212. * @throws {TypeError} If the value isn't an object.
  213. */
  214. function assertIsObject(value) {
  215. if (!isNonNullObject(value)) {
  216. throw new TypeError("Expected an object.");
  217. }
  218. }
  219. /**
  220. * The error type when there's an eslintrc-style options in a flat config.
  221. */
  222. class IncompatibleKeyError extends Error {
  223. /**
  224. * @param {string} key The invalid key.
  225. */
  226. constructor(key) {
  227. super(
  228. "This appears to be in eslintrc format rather than flat config format.",
  229. );
  230. this.messageTemplate = "eslintrc-incompat";
  231. this.messageData = { key };
  232. }
  233. }
  234. /**
  235. * The error type when there's an eslintrc-style plugins array found.
  236. */
  237. class IncompatiblePluginsError extends Error {
  238. /**
  239. * Creates a new instance.
  240. * @param {Array<string>} plugins The plugins array.
  241. */
  242. constructor(plugins) {
  243. super(
  244. "This appears to be in eslintrc format (array of strings) rather than flat config format (object).",
  245. );
  246. this.messageTemplate = "eslintrc-plugins";
  247. this.messageData = { plugins };
  248. }
  249. }
  250. //-----------------------------------------------------------------------------
  251. // Low-Level Schemas
  252. //-----------------------------------------------------------------------------
  253. /** @type {ObjectPropertySchema} */
  254. const booleanSchema = {
  255. merge: "replace",
  256. validate: "boolean",
  257. };
  258. const ALLOWED_SEVERITIES = new Set(["error", "warn", "off", 2, 1, 0]);
  259. /** @type {ObjectPropertySchema} */
  260. const disableDirectiveSeveritySchema = {
  261. merge(first, second) {
  262. const value = second === void 0 ? first : second;
  263. if (typeof value === "boolean") {
  264. return value ? "warn" : "off";
  265. }
  266. return normalizeSeverityToNumber(value);
  267. },
  268. validate(value) {
  269. if (!(ALLOWED_SEVERITIES.has(value) || typeof value === "boolean")) {
  270. throw new TypeError(
  271. 'Expected one of: "error", "warn", "off", 0, 1, 2, or a boolean.',
  272. );
  273. }
  274. },
  275. };
  276. /** @type {ObjectPropertySchema} */
  277. const unusedInlineConfigsSeveritySchema = {
  278. merge(first, second) {
  279. const value = second === void 0 ? first : second;
  280. return normalizeSeverityToNumber(value);
  281. },
  282. validate(value) {
  283. if (!ALLOWED_SEVERITIES.has(value)) {
  284. throw new TypeError(
  285. 'Expected one of: "error", "warn", "off", 0, 1, or 2.',
  286. );
  287. }
  288. },
  289. };
  290. /** @type {ObjectPropertySchema} */
  291. const deepObjectAssignSchema = {
  292. merge(first = {}, second = {}) {
  293. return deepMerge(first, second);
  294. },
  295. validate: "object",
  296. };
  297. //-----------------------------------------------------------------------------
  298. // High-Level Schemas
  299. //-----------------------------------------------------------------------------
  300. /** @type {ObjectPropertySchema} */
  301. const languageOptionsSchema = {
  302. merge(first = {}, second = {}) {
  303. const result = deepMerge(first, second);
  304. for (const [key, value] of Object.entries(result)) {
  305. /*
  306. * Special case: Because the `parser` property is an object, it should
  307. * not be deep merged. Instead, it should be replaced if it exists in
  308. * the second object. To make this more generic, we just check for
  309. * objects with methods and replace them if they exist in the second
  310. * object.
  311. */
  312. if (isNonArrayObject(value)) {
  313. if (hasMethod(value)) {
  314. result[key] = second[key] ?? first[key];
  315. continue;
  316. }
  317. // for other objects, make sure we aren't reusing the same object
  318. result[key] = { ...result[key] };
  319. continue;
  320. }
  321. }
  322. return result;
  323. },
  324. validate: "object",
  325. };
  326. /** @type {ObjectPropertySchema} */
  327. const languageSchema = {
  328. merge: "replace",
  329. validate: assertIsPluginMemberName,
  330. };
  331. /** @type {ObjectPropertySchema} */
  332. const pluginsSchema = {
  333. merge(first = {}, second = {}) {
  334. const keys = new Set([...Object.keys(first), ...Object.keys(second)]);
  335. const result = {};
  336. // manually validate that plugins are not redefined
  337. for (const key of keys) {
  338. // avoid hairy edge case
  339. if (key === "__proto__") {
  340. continue;
  341. }
  342. if (key in first && key in second && first[key] !== second[key]) {
  343. throw new TypeError(`Cannot redefine plugin "${key}".`);
  344. }
  345. result[key] = second[key] || first[key];
  346. }
  347. return result;
  348. },
  349. validate(value) {
  350. // first check the value to be sure it's an object
  351. if (value === null || typeof value !== "object") {
  352. throw new TypeError("Expected an object.");
  353. }
  354. // make sure it's not an array, which would mean eslintrc-style is used
  355. if (Array.isArray(value)) {
  356. throw new IncompatiblePluginsError(value);
  357. }
  358. // second check the keys to make sure they are objects
  359. for (const key of Object.keys(value)) {
  360. // avoid hairy edge case
  361. if (key === "__proto__") {
  362. continue;
  363. }
  364. if (value[key] === null || typeof value[key] !== "object") {
  365. throw new TypeError(`Key "${key}": Expected an object.`);
  366. }
  367. }
  368. },
  369. };
  370. /** @type {ObjectPropertySchema} */
  371. const processorSchema = {
  372. merge: "replace",
  373. validate(value) {
  374. if (typeof value === "string") {
  375. assertIsPluginMemberName(value);
  376. } else if (value && typeof value === "object") {
  377. if (
  378. typeof value.preprocess !== "function" ||
  379. typeof value.postprocess !== "function"
  380. ) {
  381. throw new TypeError(
  382. "Object must have a preprocess() and a postprocess() method.",
  383. );
  384. }
  385. } else {
  386. throw new TypeError("Expected an object or a string.");
  387. }
  388. },
  389. };
  390. /** @type {ObjectPropertySchema} */
  391. const rulesSchema = {
  392. merge(first = {}, second = {}) {
  393. const result = {
  394. ...first,
  395. ...second,
  396. };
  397. for (const ruleId of Object.keys(result)) {
  398. try {
  399. // avoid hairy edge case
  400. if (ruleId === "__proto__") {
  401. /* eslint-disable-next-line no-proto -- Though deprecated, may still be present */
  402. delete result.__proto__;
  403. continue;
  404. }
  405. result[ruleId] = normalizeRuleOptions(result[ruleId]);
  406. /*
  407. * If either rule config is missing, then the correct
  408. * config is already present and we just need to normalize
  409. * the severity.
  410. */
  411. if (!(ruleId in first) || !(ruleId in second)) {
  412. continue;
  413. }
  414. const firstRuleOptions = normalizeRuleOptions(first[ruleId]);
  415. const secondRuleOptions = normalizeRuleOptions(second[ruleId]);
  416. /*
  417. * If the second rule config only has a severity (length of 1),
  418. * then use that severity and keep the rest of the options from
  419. * the first rule config.
  420. */
  421. if (secondRuleOptions.length === 1) {
  422. result[ruleId] = [
  423. secondRuleOptions[0],
  424. ...firstRuleOptions.slice(1),
  425. ];
  426. continue;
  427. }
  428. /*
  429. * In any other situation, then the second rule config takes
  430. * precedence. That means the value at `result[ruleId]` is
  431. * already correct and no further work is necessary.
  432. */
  433. } catch (ex) {
  434. throw new Error(`Key "${ruleId}": ${ex.message}`, {
  435. cause: ex,
  436. });
  437. }
  438. }
  439. return result;
  440. },
  441. validate(value) {
  442. assertIsObject(value);
  443. /*
  444. * We are not checking the rule schema here because there is no
  445. * guarantee that the rule definition is present at this point. Instead
  446. * we wait and check the rule schema during the finalization step
  447. * of calculating a config.
  448. */
  449. for (const ruleId of Object.keys(value)) {
  450. // avoid hairy edge case
  451. if (ruleId === "__proto__") {
  452. continue;
  453. }
  454. const ruleOptions = value[ruleId];
  455. assertIsRuleOptions(ruleId, ruleOptions);
  456. if (Array.isArray(ruleOptions)) {
  457. assertIsRuleSeverity(ruleId, ruleOptions[0]);
  458. } else {
  459. assertIsRuleSeverity(ruleId, ruleOptions);
  460. }
  461. }
  462. },
  463. };
  464. /**
  465. * Creates a schema that always throws an error. Useful for warning
  466. * about eslintrc-style keys.
  467. * @param {string} key The eslintrc key to create a schema for.
  468. * @returns {ObjectPropertySchema} The schema.
  469. */
  470. function createEslintrcErrorSchema(key) {
  471. return {
  472. merge: "replace",
  473. validate() {
  474. throw new IncompatibleKeyError(key);
  475. },
  476. };
  477. }
  478. const eslintrcKeys = [
  479. "env",
  480. "extends",
  481. "globals",
  482. "ignorePatterns",
  483. "noInlineConfig",
  484. "overrides",
  485. "parser",
  486. "parserOptions",
  487. "reportUnusedDisableDirectives",
  488. "root",
  489. ];
  490. //-----------------------------------------------------------------------------
  491. // Full schema
  492. //-----------------------------------------------------------------------------
  493. const flatConfigSchema = {
  494. // eslintrc-style keys that should always error
  495. ...Object.fromEntries(
  496. eslintrcKeys.map(key => [key, createEslintrcErrorSchema(key)]),
  497. ),
  498. // flat config keys
  499. settings: deepObjectAssignSchema,
  500. linterOptions: {
  501. schema: {
  502. noInlineConfig: booleanSchema,
  503. reportUnusedDisableDirectives: disableDirectiveSeveritySchema,
  504. reportUnusedInlineConfigs: unusedInlineConfigsSeveritySchema,
  505. },
  506. },
  507. language: languageSchema,
  508. languageOptions: languageOptionsSchema,
  509. processor: processorSchema,
  510. plugins: pluginsSchema,
  511. rules: rulesSchema,
  512. };
  513. //-----------------------------------------------------------------------------
  514. // Exports
  515. //-----------------------------------------------------------------------------
  516. module.exports = {
  517. flatConfigSchema,
  518. hasMethod,
  519. assertIsRuleSeverity,
  520. };