Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lintPostcssResult.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. 'use strict';
  2. const assignDisabledRanges = require('./assignDisabledRanges');
  3. const get = require('lodash/get');
  4. const getOsEol = require('./utils/getOsEol');
  5. const reportUnknownRuleNames = require('./reportUnknownRuleNames');
  6. const rulesOrder = require('./rules');
  7. /** @typedef {import('stylelint').StylelintStandaloneOptions} StylelintStandaloneOptions */
  8. /** @typedef {import('stylelint').PostcssResult} PostcssResult */
  9. /** @typedef {import('stylelint').StylelintConfig} StylelintConfig */
  10. /**
  11. * @param {StylelintStandaloneOptions} stylelintOptions
  12. * @param {PostcssResult} postcssResult
  13. * @param {StylelintConfig} config
  14. * @returns {Promise<any>}
  15. */
  16. function lintPostcssResult(stylelintOptions, postcssResult, config) {
  17. postcssResult.stylelint.ruleSeverities = {};
  18. postcssResult.stylelint.customMessages = {};
  19. postcssResult.stylelint.stylelintError = false;
  20. postcssResult.stylelint.quiet = config.quiet;
  21. postcssResult.stylelint.config = config;
  22. /** @type {string} */
  23. let newline;
  24. const postcssDoc = postcssResult.root;
  25. if (postcssDoc) {
  26. if (!('type' in postcssDoc)) {
  27. throw new Error('Unexpected Postcss root object!');
  28. }
  29. // @ts-ignore TODO TYPES property css does not exists
  30. const newlineMatch = postcssDoc.source && postcssDoc.source.input.css.match(/\r?\n/);
  31. newline = newlineMatch ? newlineMatch[0] : getOsEol();
  32. assignDisabledRanges(postcssDoc, postcssResult);
  33. }
  34. const isFileFixCompatible = isFixCompatible(postcssResult);
  35. if (!isFileFixCompatible) {
  36. postcssResult.stylelint.disableWritingFix = true;
  37. }
  38. const postcssRoots = /** @type {import('postcss').Root[]} */ (postcssDoc &&
  39. postcssDoc.constructor.name === 'Document'
  40. ? postcssDoc.nodes
  41. : [postcssDoc]);
  42. // Promises for the rules. Although the rule code runs synchronously now,
  43. // the use of Promises makes it compatible with the possibility of async
  44. // rules down the line.
  45. /** @type {Array<Promise<any>>} */
  46. const performRules = [];
  47. const rules = config.rules
  48. ? Object.keys(config.rules).sort(
  49. (a, b) => Object.keys(rulesOrder).indexOf(a) - Object.keys(rulesOrder).indexOf(b),
  50. )
  51. : [];
  52. rules.forEach((ruleName) => {
  53. const ruleFunction = rulesOrder[ruleName] || get(config, ['pluginFunctions', ruleName]);
  54. if (ruleFunction === undefined) {
  55. performRules.push(
  56. Promise.all(
  57. postcssRoots.map((postcssRoot) =>
  58. reportUnknownRuleNames(ruleName, postcssRoot, postcssResult),
  59. ),
  60. ),
  61. );
  62. return;
  63. }
  64. const ruleSettings = get(config, ['rules', ruleName]);
  65. if (ruleSettings === null || ruleSettings[0] === null) {
  66. return;
  67. }
  68. const primaryOption = ruleSettings[0];
  69. const secondaryOptions = ruleSettings[1];
  70. // Log the rule's severity in the PostCSS result
  71. const defaultSeverity = config.defaultSeverity || 'error';
  72. postcssResult.stylelint.ruleSeverities[ruleName] = get(
  73. secondaryOptions,
  74. 'severity',
  75. defaultSeverity,
  76. );
  77. postcssResult.stylelint.customMessages[ruleName] = get(secondaryOptions, 'message');
  78. performRules.push(
  79. Promise.all(
  80. postcssRoots.map((postcssRoot) =>
  81. ruleFunction(primaryOption, secondaryOptions, {
  82. fix:
  83. stylelintOptions.fix &&
  84. // Next two conditionals are temporary measures until #2643 is resolved
  85. isFileFixCompatible &&
  86. !postcssResult.stylelint.disabledRanges[ruleName],
  87. newline,
  88. })(postcssRoot, postcssResult),
  89. ),
  90. ),
  91. );
  92. });
  93. return Promise.all(performRules);
  94. }
  95. /**
  96. * There are currently some bugs in the autofixer of Stylelint.
  97. * The autofixer does not yet adhere to stylelint-disable comments, so if there are disabled
  98. * ranges we can not autofix this document. More info in issue #2643.
  99. *
  100. * @param {PostcssResult} postcssResult
  101. * @returns {boolean}
  102. */
  103. function isFixCompatible({ stylelint }) {
  104. // Check for issue #2643
  105. if (stylelint.disabledRanges.all.length) return false;
  106. return true;
  107. }
  108. module.exports = lintPostcssResult;