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.

assignDisabledRanges.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. 'use strict';
  2. const _ = require('lodash');
  3. const COMMAND_PREFIX = 'stylelint-';
  4. const disableCommand = `${COMMAND_PREFIX}disable`;
  5. const enableCommand = `${COMMAND_PREFIX}enable`;
  6. const disableLineCommand = `${COMMAND_PREFIX}disable-line`;
  7. const disableNextLineCommand = `${COMMAND_PREFIX}disable-next-line`;
  8. const ALL_RULES = 'all';
  9. /** @typedef {import('postcss/lib/comment')} PostcssComment */
  10. /** @typedef {import('postcss').Root} PostcssRoot */
  11. /** @typedef {import('stylelint').PostcssResult} PostcssResult */
  12. /** @typedef {import('stylelint').DisabledRangeObject} DisabledRangeObject */
  13. /** @typedef {import('stylelint').DisabledRange} DisabledRange */
  14. /**
  15. * @param {PostcssComment} comment
  16. * @param {number} start
  17. * @param {boolean} strictStart
  18. * @param {string|undefined} description
  19. * @param {number} [end]
  20. * @param {boolean} [strictEnd]
  21. * @returns {DisabledRange}
  22. */
  23. function createDisableRange(comment, start, strictStart, description, end, strictEnd) {
  24. return {
  25. comment,
  26. start,
  27. end: end || undefined,
  28. strictStart,
  29. strictEnd: typeof strictEnd === 'boolean' ? strictEnd : undefined,
  30. description,
  31. };
  32. }
  33. /**
  34. * Run it like a plugin ...
  35. * @param {PostcssRoot} root
  36. * @param {PostcssResult} result
  37. * @returns {PostcssResult}
  38. */
  39. module.exports = function (root, result) {
  40. result.stylelint = result.stylelint || {
  41. disabledRanges: {},
  42. ruleSeverities: {},
  43. customMessages: {},
  44. };
  45. /**
  46. * Most of the functions below work via side effects mutating this object
  47. * @type {DisabledRangeObject}
  48. */
  49. const disabledRanges = {
  50. all: [],
  51. };
  52. result.stylelint.disabledRanges = disabledRanges;
  53. // Work around postcss/postcss-scss#109 by merging adjacent `//` comments
  54. // into a single node before passing to `checkComment`.
  55. /** @type {PostcssComment?} */
  56. let inlineEnd;
  57. root.walkComments((/** @type {PostcssComment} */ comment) => {
  58. if (inlineEnd) {
  59. // Ignore comments already processed by grouping with a previous one.
  60. if (inlineEnd === comment) inlineEnd = null;
  61. return;
  62. }
  63. const nextComment = comment.next();
  64. // If any of these conditions are not met, do not merge comments.
  65. if (
  66. !(
  67. isInlineComment(comment) &&
  68. isStylelintCommand(comment) &&
  69. nextComment &&
  70. nextComment.type === 'comment' &&
  71. (comment.text.includes('--') || nextComment.text.startsWith('--'))
  72. )
  73. ) {
  74. checkComment(comment);
  75. return;
  76. }
  77. let lastLine = (comment.source && comment.source.end && comment.source.end.line) || 0;
  78. const fullComment = comment.clone();
  79. let current = nextComment;
  80. while (isInlineComment(current) && !isStylelintCommand(current)) {
  81. const currentLine = (current.source && current.source.end && current.source.end.line) || 0;
  82. if (lastLine + 1 !== currentLine) break;
  83. fullComment.text += `\n${current.text}`;
  84. if (fullComment.source && current.source) {
  85. fullComment.source.end = current.source.end;
  86. }
  87. inlineEnd = current;
  88. const next = current.next();
  89. if (!next || next.type !== 'comment') break;
  90. current = next;
  91. lastLine = currentLine;
  92. }
  93. checkComment(fullComment);
  94. });
  95. return result;
  96. /**
  97. * @param {PostcssComment} comment
  98. */
  99. function isInlineComment(comment) {
  100. // We check both here because the Sass parser uses `raws.inline` to indicate
  101. // inline comments, while the Less parser uses `inline`.
  102. return comment.inline || comment.raws.inline;
  103. }
  104. /**
  105. * @param {PostcssComment} comment
  106. */
  107. function isStylelintCommand(comment) {
  108. return comment.text.startsWith(disableCommand) || comment.text.startsWith(enableCommand);
  109. }
  110. /**
  111. * @param {PostcssComment} comment
  112. */
  113. function processDisableLineCommand(comment) {
  114. if (comment.source && comment.source.start) {
  115. const line = comment.source.start.line;
  116. const description = getDescription(comment.text);
  117. getCommandRules(disableLineCommand, comment.text).forEach((ruleName) => {
  118. disableLine(comment, line, ruleName, description);
  119. });
  120. }
  121. }
  122. /**
  123. * @param {PostcssComment} comment
  124. */
  125. function processDisableNextLineCommand(comment) {
  126. if (comment.source && comment.source.end) {
  127. const line = comment.source.end.line;
  128. const description = getDescription(comment.text);
  129. getCommandRules(disableNextLineCommand, comment.text).forEach((ruleName) => {
  130. disableLine(comment, line + 1, ruleName, description);
  131. });
  132. }
  133. }
  134. /**
  135. * @param {PostcssComment} comment
  136. * @param {number} line
  137. * @param {string} ruleName
  138. * @param {string|undefined} description
  139. */
  140. function disableLine(comment, line, ruleName, description) {
  141. if (ruleIsDisabled(ALL_RULES)) {
  142. throw comment.error('All rules have already been disabled', {
  143. plugin: 'stylelint',
  144. });
  145. }
  146. if (ruleName === ALL_RULES) {
  147. Object.keys(disabledRanges).forEach((disabledRuleName) => {
  148. if (ruleIsDisabled(disabledRuleName)) return;
  149. const strict = disabledRuleName === ALL_RULES;
  150. startDisabledRange(comment, line, disabledRuleName, strict, description);
  151. endDisabledRange(line, disabledRuleName, strict);
  152. });
  153. } else {
  154. if (ruleIsDisabled(ruleName)) {
  155. throw comment.error(`"${ruleName}" has already been disabled`, {
  156. plugin: 'stylelint',
  157. });
  158. }
  159. startDisabledRange(comment, line, ruleName, true, description);
  160. endDisabledRange(line, ruleName, true);
  161. }
  162. }
  163. /**
  164. * @param {PostcssComment} comment
  165. */
  166. function processDisableCommand(comment) {
  167. const description = getDescription(comment.text);
  168. getCommandRules(disableCommand, comment.text).forEach((ruleToDisable) => {
  169. const isAllRules = ruleToDisable === ALL_RULES;
  170. if (ruleIsDisabled(ruleToDisable)) {
  171. throw comment.error(
  172. isAllRules
  173. ? 'All rules have already been disabled'
  174. : `"${ruleToDisable}" has already been disabled`,
  175. {
  176. plugin: 'stylelint',
  177. },
  178. );
  179. }
  180. if (comment.source && comment.source.start) {
  181. const line = comment.source.start.line;
  182. if (isAllRules) {
  183. Object.keys(disabledRanges).forEach((ruleName) => {
  184. startDisabledRange(comment, line, ruleName, ruleName === ALL_RULES, description);
  185. });
  186. } else {
  187. startDisabledRange(comment, line, ruleToDisable, true, description);
  188. }
  189. }
  190. });
  191. }
  192. /**
  193. * @param {PostcssComment} comment
  194. */
  195. function processEnableCommand(comment) {
  196. getCommandRules(enableCommand, comment.text).forEach((ruleToEnable) => {
  197. // TODO TYPES
  198. // need fallback if endLine will be undefined
  199. const endLine = /** @type {number} */ (comment.source &&
  200. comment.source.end &&
  201. comment.source.end.line);
  202. if (ruleToEnable === ALL_RULES) {
  203. if (
  204. Object.values(disabledRanges).every(
  205. (ranges) => ranges.length === 0 || typeof ranges[ranges.length - 1].end === 'number',
  206. )
  207. ) {
  208. throw comment.error('No rules have been disabled', {
  209. plugin: 'stylelint',
  210. });
  211. }
  212. Object.keys(disabledRanges).forEach((ruleName) => {
  213. if (!_.get(_.last(disabledRanges[ruleName]), 'end')) {
  214. endDisabledRange(endLine, ruleName, ruleName === ALL_RULES);
  215. }
  216. });
  217. return;
  218. }
  219. if (ruleIsDisabled(ALL_RULES) && disabledRanges[ruleToEnable] === undefined) {
  220. // Get a starting point from the where all rules were disabled
  221. if (!disabledRanges[ruleToEnable]) {
  222. disabledRanges[ruleToEnable] = disabledRanges.all.map(({ start, end, description }) =>
  223. createDisableRange(comment, start, false, description, end, false),
  224. );
  225. } else {
  226. const range = _.last(disabledRanges[ALL_RULES]);
  227. if (range) {
  228. disabledRanges[ruleToEnable].push({ ...range });
  229. }
  230. }
  231. endDisabledRange(endLine, ruleToEnable, true);
  232. return;
  233. }
  234. if (ruleIsDisabled(ruleToEnable)) {
  235. endDisabledRange(endLine, ruleToEnable, true);
  236. return;
  237. }
  238. throw comment.error(`"${ruleToEnable}" has not been disabled`, {
  239. plugin: 'stylelint',
  240. });
  241. });
  242. }
  243. /**
  244. * @param {PostcssComment} comment
  245. */
  246. function checkComment(comment) {
  247. const text = comment.text;
  248. // Ignore comments that are not relevant commands
  249. if (text.indexOf(COMMAND_PREFIX) !== 0) {
  250. return result;
  251. }
  252. if (text.startsWith(disableLineCommand)) {
  253. processDisableLineCommand(comment);
  254. } else if (text.startsWith(disableNextLineCommand)) {
  255. processDisableNextLineCommand(comment);
  256. } else if (text.startsWith(disableCommand)) {
  257. processDisableCommand(comment);
  258. } else if (text.startsWith(enableCommand)) {
  259. processEnableCommand(comment);
  260. }
  261. }
  262. /**
  263. * @param {string} command
  264. * @param {string} fullText
  265. * @returns {string[]}
  266. */
  267. function getCommandRules(command, fullText) {
  268. const rules = fullText
  269. .slice(command.length)
  270. .split(/\s-{2,}\s/u)[0] // Allow for description (f.e. /* stylelint-disable a, b -- Description */).
  271. .trim()
  272. .split(',')
  273. .filter(Boolean)
  274. .map((r) => r.trim());
  275. if (_.isEmpty(rules)) {
  276. return [ALL_RULES];
  277. }
  278. return rules;
  279. }
  280. /**
  281. * @param {string} fullText
  282. * @returns {string|undefined}
  283. */
  284. function getDescription(fullText) {
  285. const descriptionStart = fullText.indexOf('--');
  286. if (descriptionStart === -1) return;
  287. return fullText.slice(descriptionStart + 2).trim();
  288. }
  289. /**
  290. * @param {PostcssComment} comment
  291. * @param {number} line
  292. * @param {string} ruleName
  293. * @param {boolean} strict
  294. * @param {string|undefined} description
  295. */
  296. function startDisabledRange(comment, line, ruleName, strict, description) {
  297. const rangeObj = createDisableRange(comment, line, strict, description);
  298. ensureRuleRanges(ruleName);
  299. disabledRanges[ruleName].push(rangeObj);
  300. }
  301. /**
  302. * @param {number} line
  303. * @param {string} ruleName
  304. * @param {boolean} strict
  305. */
  306. function endDisabledRange(line, ruleName, strict) {
  307. const lastRangeForRule = _.last(disabledRanges[ruleName]);
  308. if (!lastRangeForRule) {
  309. return;
  310. }
  311. // Add an `end` prop to the last range of that rule
  312. lastRangeForRule.end = line;
  313. lastRangeForRule.strictEnd = strict;
  314. }
  315. /**
  316. * @param {string} ruleName
  317. */
  318. function ensureRuleRanges(ruleName) {
  319. if (!disabledRanges[ruleName]) {
  320. disabledRanges[ruleName] = disabledRanges.all.map(({ comment, start, end, description }) =>
  321. createDisableRange(comment, start, false, description, end, false),
  322. );
  323. }
  324. }
  325. /**
  326. * @param {string} ruleName
  327. * @returns {boolean}
  328. */
  329. function ruleIsDisabled(ruleName) {
  330. if (disabledRanges[ruleName] === undefined) return false;
  331. if (_.last(disabledRanges[ruleName]) === undefined) return false;
  332. if (_.get(_.last(disabledRanges[ruleName]), 'end') === undefined) return true;
  333. return false;
  334. }
  335. };