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.

index.js 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // @ts-nocheck
  2. 'use strict';
  3. const declarationValueIndex = require('../../utils/declarationValueIndex');
  4. const getDeclarationValue = require('../../utils/getDeclarationValue');
  5. const isSingleLineString = require('../../utils/isSingleLineString');
  6. const isStandardSyntaxFunction = require('../../utils/isStandardSyntaxFunction');
  7. const report = require('../../utils/report');
  8. const ruleMessages = require('../../utils/ruleMessages');
  9. const setDeclarationValue = require('../../utils/setDeclarationValue');
  10. const validateOptions = require('../../utils/validateOptions');
  11. const valueParser = require('postcss-value-parser');
  12. const ruleName = 'function-parentheses-newline-inside';
  13. const messages = ruleMessages(ruleName, {
  14. expectedOpening: 'Expected newline after "("',
  15. expectedClosing: 'Expected newline before ")"',
  16. expectedOpeningMultiLine: 'Expected newline after "(" in a multi-line function',
  17. rejectedOpeningMultiLine: 'Unexpected whitespace after "(" in a multi-line function',
  18. expectedClosingMultiLine: 'Expected newline before ")" in a multi-line function',
  19. rejectedClosingMultiLine: 'Unexpected whitespace before ")" in a multi-line function',
  20. });
  21. function rule(expectation, options, context) {
  22. return (root, result) => {
  23. const validOptions = validateOptions(result, ruleName, {
  24. actual: expectation,
  25. possible: ['always', 'always-multi-line', 'never-multi-line'],
  26. });
  27. if (!validOptions) {
  28. return;
  29. }
  30. root.walkDecls((decl) => {
  31. if (!decl.value.includes('(')) {
  32. return;
  33. }
  34. let hasFixed = false;
  35. const declValue = getDeclarationValue(decl);
  36. const parsedValue = valueParser(declValue);
  37. parsedValue.walk((valueNode) => {
  38. if (valueNode.type !== 'function') {
  39. return;
  40. }
  41. if (!isStandardSyntaxFunction(valueNode)) {
  42. return;
  43. }
  44. const functionString = valueParser.stringify(valueNode);
  45. const isMultiLine = !isSingleLineString(functionString);
  46. function containsNewline(str) {
  47. return str.includes('\n');
  48. }
  49. // Check opening ...
  50. const openingIndex = valueNode.sourceIndex + valueNode.value.length + 1;
  51. const checkBefore = getCheckBefore(valueNode);
  52. if (expectation === 'always' && !containsNewline(checkBefore)) {
  53. if (context.fix) {
  54. hasFixed = true;
  55. fixBeforeForAlways(valueNode, context.newline);
  56. } else {
  57. complain(messages.expectedOpening, openingIndex);
  58. }
  59. }
  60. if (isMultiLine && expectation === 'always-multi-line' && !containsNewline(checkBefore)) {
  61. if (context.fix) {
  62. hasFixed = true;
  63. fixBeforeForAlways(valueNode, context.newline);
  64. } else {
  65. complain(messages.expectedOpeningMultiLine, openingIndex);
  66. }
  67. }
  68. if (isMultiLine && expectation === 'never-multi-line' && checkBefore !== '') {
  69. if (context.fix) {
  70. hasFixed = true;
  71. fixBeforeForNever(valueNode);
  72. } else {
  73. complain(messages.rejectedOpeningMultiLine, openingIndex);
  74. }
  75. }
  76. // Check closing ...
  77. const closingIndex = valueNode.sourceIndex + functionString.length - 2;
  78. const checkAfter = getCheckAfter(valueNode);
  79. if (expectation === 'always' && !containsNewline(checkAfter)) {
  80. if (context.fix) {
  81. hasFixed = true;
  82. fixAfterForAlways(valueNode, context.newline);
  83. } else {
  84. complain(messages.expectedClosing, closingIndex);
  85. }
  86. }
  87. if (isMultiLine && expectation === 'always-multi-line' && !containsNewline(checkAfter)) {
  88. if (context.fix) {
  89. hasFixed = true;
  90. fixAfterForAlways(valueNode, context.newline);
  91. } else {
  92. complain(messages.expectedClosingMultiLine, closingIndex);
  93. }
  94. }
  95. if (isMultiLine && expectation === 'never-multi-line' && checkAfter !== '') {
  96. if (context.fix) {
  97. hasFixed = true;
  98. fixAfterForNever(valueNode);
  99. } else {
  100. complain(messages.rejectedClosingMultiLine, closingIndex);
  101. }
  102. }
  103. });
  104. if (hasFixed) {
  105. setDeclarationValue(decl, parsedValue.toString());
  106. }
  107. function complain(message, offset) {
  108. report({
  109. ruleName,
  110. result,
  111. message,
  112. node: decl,
  113. index: declarationValueIndex(decl) + offset,
  114. });
  115. }
  116. });
  117. };
  118. }
  119. function getCheckBefore(valueNode) {
  120. let before = valueNode.before;
  121. for (const node of valueNode.nodes) {
  122. if (node.type === 'comment') {
  123. continue;
  124. }
  125. if (node.type === 'space') {
  126. before += node.value;
  127. continue;
  128. }
  129. break;
  130. }
  131. return before;
  132. }
  133. function getCheckAfter(valueNode) {
  134. let after = '';
  135. for (const node of valueNode.nodes.slice().reverse()) {
  136. if (node.type === 'comment') {
  137. continue;
  138. }
  139. if (node.type === 'space') {
  140. after = node.value + after;
  141. continue;
  142. }
  143. break;
  144. }
  145. after += valueNode.after;
  146. return after;
  147. }
  148. function fixBeforeForAlways(valueNode, newline) {
  149. let target;
  150. for (const node of valueNode.nodes) {
  151. if (node.type === 'comment') {
  152. continue;
  153. }
  154. if (node.type === 'space') {
  155. target = node;
  156. continue;
  157. }
  158. break;
  159. }
  160. if (target) {
  161. target.value = newline + target.value;
  162. } else {
  163. valueNode.before = newline + valueNode.before;
  164. }
  165. }
  166. function fixBeforeForNever(valueNode) {
  167. valueNode.before = '';
  168. for (const node of valueNode.nodes) {
  169. if (node.type === 'comment') {
  170. continue;
  171. }
  172. if (node.type === 'space') {
  173. node.value = '';
  174. continue;
  175. }
  176. break;
  177. }
  178. }
  179. function fixAfterForAlways(valueNode, newline) {
  180. valueNode.after = newline + valueNode.after;
  181. }
  182. function fixAfterForNever(valueNode) {
  183. valueNode.after = '';
  184. for (const node of valueNode.nodes.slice().reverse()) {
  185. if (node.type === 'comment') {
  186. continue;
  187. }
  188. if (node.type === 'space') {
  189. node.value = '';
  190. continue;
  191. }
  192. break;
  193. }
  194. }
  195. rule.ruleName = ruleName;
  196. rule.messages = messages;
  197. module.exports = rule;