2 const reNewLine = /(?:\r?\n|\r)/gm;
3 const getSyntax = require("./get-syntax");
5 function DocumentFixer (source) {
8 reNewLine.lastIndex = 0;
9 while ((match = reNewLine.exec(source))) {
10 lines.push(match.index);
15 DocumentFixer.prototype = {
16 block: function (style) {
17 return new LocalFixer(this.lines, style);
21 function LocalFixer (lines, style) {
23 let column = style.startIndex;
24 lines.some((lineEndIndex, lineNumber) => {
25 if (lineEndIndex >= style.startIndex) {
27 if (lineNumber in lines) {
28 column = style.startIndex - lines[lineNumber] - 1;
39 LocalFixer.prototype = {
40 object: function (object) {
42 if (object.line === 1) {
43 object.column += this.column;
45 object.line += this.line;
48 node: function (node) {
49 this.object(node.source.start);
50 this.object(node.source.end);
52 root: function (root) {
53 this.object(root.source.start);
58 error: function (error) {
59 if (error && error.name === "CssSyntaxError") {
61 this.object(error.input);
62 error.message = error.message.replace(/:\d+:\d+:/, ":" + error.line + ":" + error.column + ":");
66 parse: function (opts) {
67 const style = this.style;
68 const syntax = getSyntax(opts, style.lang);
71 root = syntax.parse(style.content, Object.assign({}, opts, {
75 throw this.error(error);
78 Object.assign(root.source, {
79 isHTMLAttribute: !!style.isHTMLAttribute,
80 isMarkdown: !!style.isMarkdown,
81 isHTMLTag: !!style.isHTMLTag,
82 lang: style.lang || "css",
90 module.exports = DocumentFixer;