parse-styles.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. "use strict";
  2. const Input = require("postcss/lib/input");
  3. const Document = require("./document");
  4. const reNewLine = /\r?\n|\r/g;
  5. class Locations {
  6. constructor(source) {
  7. let match;
  8. const lines = [];
  9. reNewLine.lastIndex = 0;
  10. while ((match = reNewLine.exec(source))) {
  11. lines.push(match.index);
  12. }
  13. lines.push(source.length);
  14. this.lines = lines;
  15. this.source = source;
  16. }
  17. getOffsetFromLoc(loc) {
  18. const lineIndex = loc.line - 2;
  19. return (lineIndex >= 0 ? this.lines[lineIndex] : -1) + loc.column;
  20. }
  21. getLocFromOffset(offset) {
  22. const lines = this.lines;
  23. for (let index = 0; index < lines.length; index++) {
  24. const lineEndIndex = lines[index];
  25. if (lineEndIndex >= offset) {
  26. const before = this.lines[index - 1];
  27. return {
  28. line: index + 1,
  29. column: offset - (before != null ? before : -1),
  30. };
  31. }
  32. }
  33. const before = this.lines[this.lines.length - 2];
  34. return {
  35. line: lines.length,
  36. column: offset - (before != null ? before : -1),
  37. };
  38. }
  39. }
  40. class LocalFixer {
  41. constructor(locations, style) {
  42. const { line, column } = locations.getLocFromOffset(style.startIndex);
  43. this.line = line - 1;
  44. this.column = column - 1;
  45. this.style = style;
  46. }
  47. fixLocation(object) {
  48. if (object) {
  49. if (object.line === 1) {
  50. object.column += this.column;
  51. }
  52. object.line += this.line;
  53. if (typeof object.offset === "number") {
  54. object.offset += this.style.startIndex;
  55. }
  56. if (typeof object.endLine === "number") {
  57. if (object.endLine === 1) {
  58. object.endColumn += this.column;
  59. }
  60. object.endLine += this.line;
  61. }
  62. }
  63. }
  64. node(node) {
  65. this.fixLocation(node.source.start);
  66. this.fixLocation(node.source.end);
  67. }
  68. root(root) {
  69. this.node(root);
  70. root.walk((node) => {
  71. this.node(node);
  72. });
  73. }
  74. error(error) {
  75. if (error && error.name === "CssSyntaxError") {
  76. this.fixLocation(error);
  77. this.fixLocation(error.input);
  78. error.message = error.message.replace(
  79. /:\d+:\d+:/,
  80. `:${error.line}:${error.column}:`
  81. );
  82. }
  83. return error;
  84. }
  85. parse(opts) {
  86. const style = this.style;
  87. const syntax = style.syntax;
  88. let root;
  89. try {
  90. root = syntax.parse(
  91. style.content,
  92. Object.assign(
  93. {},
  94. opts,
  95. {
  96. map: false,
  97. },
  98. style.opts
  99. )
  100. );
  101. } catch (error) {
  102. this.error(error);
  103. throw error;
  104. }
  105. this.root(root);
  106. root.source.inline = Boolean(style.inline);
  107. root.source.lang = style.lang;
  108. root.source.syntax = syntax;
  109. patchRoot(root, syntax);
  110. return root;
  111. }
  112. }
  113. function docFixer(source, opts) {
  114. const locations = new Locations(source);
  115. return function parseStyle(style) {
  116. return new LocalFixer(locations, style).parse(opts);
  117. };
  118. }
  119. function parseStyles(source, opts, styles) {
  120. const document = new Document();
  121. let index = 0;
  122. if (styles.length) {
  123. const parseStyle = docFixer(source, opts);
  124. styles
  125. .sort((a, b) => a.startIndex - b.startIndex)
  126. .forEach((style) => {
  127. const root = parseStyle(style);
  128. if (root) {
  129. root.raws.codeBefore = source.slice(index, style.startIndex);
  130. // Note: Stylelint is still using this property.
  131. try {
  132. Object.defineProperty(root.raws, "beforeStart", {
  133. configurable: true,
  134. get() {
  135. return root.raws.codeBefore;
  136. },
  137. set(value) {
  138. root.raws.codeBefore = value;
  139. },
  140. });
  141. } catch {
  142. // ignore
  143. }
  144. index =
  145. style.startIndex + (style.content || root.source.input.css).length;
  146. root.document = document;
  147. document.nodes.push(root);
  148. }
  149. });
  150. }
  151. const last = document.nodes[document.nodes.length - 1];
  152. if (last) {
  153. last.raws.codeAfter = index ? source.slice(index) : source;
  154. }
  155. document.source = {
  156. input: new Input(source, opts),
  157. start: {
  158. line: 1,
  159. column: 1,
  160. },
  161. opts,
  162. };
  163. return document;
  164. }
  165. module.exports = parseStyles;
  166. function patchRoot(root, syntax) {
  167. const originalToString = root.toString;
  168. try {
  169. Object.defineProperty(root, "toString", {
  170. configurable: true,
  171. enumerable: false,
  172. value(stringifier) {
  173. return originalToString.call(this, stringifier || syntax);
  174. },
  175. });
  176. } catch {
  177. // ignore
  178. }
  179. }