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