Skip to content

Commit 93901db

Browse files
committed
fix: windows newline issues
1 parent 6ad6bb9 commit 93901db

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

src/__tests__/main.test.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,17 @@ for (const entry of fs.readdirSync(FIXTURES)) {
1717
pos: Position;
1818
label: string;
1919
}[][] = Array.from({ length: lines.length }, () => []);
20-
const addRange = (label: string, range: Range) => {
20+
// we'll remove windows newlines for the purposes of snapshots.
21+
const read = (range: Range) => parser.read(range).replace(/\r/g, "");
22+
const addRange = (label: string, inputRange: Range) => {
23+
// we'll normalize windows newline positions for the snapshots.
24+
const range =
25+
src.charAt(inputRange.start) === "\r"
26+
? {
27+
start: inputRange.start + 1,
28+
end: inputRange.end,
29+
}
30+
: inputRange;
2131
const pos = parser.positionAt(range.start);
2232
partsByLine[pos.line].push({
2333
label,
@@ -100,13 +110,10 @@ for (const entry of fs.readdirSync(FIXTURES)) {
100110
addValueRange("attrSpread", range);
101111
},
102112
onOpenTagEnd(range) {
103-
addRange(
104-
`openTagEnd(${parser.read(tagStack[tagStack.length - 1])})`,
105-
range
106-
);
113+
addRange(`openTagEnd(${read(tagStack[tagStack.length - 1])})`, range);
107114
},
108115
onCloseTag(range) {
109-
const label = `closeTag(${parser.read(tagStack.pop()!)})`;
116+
const label = `closeTag(${read(tagStack.pop()!)})`;
110117
if (range.value) {
111118
addValueRange(label, range as Ranges.Value);
112119
} else {
@@ -134,15 +141,15 @@ for (const entry of fs.readdirSync(FIXTURES)) {
134141
if (line === 0) {
135142
result += `${
136143
linePrefix +
137-
parser.read({
144+
read({
138145
start: 0,
139146
end: lines[1],
140147
})
141148
}`;
142149
} else {
143150
result += `\n${
144151
linePrefix +
145-
parser.read({
152+
read({
146153
start: lines[line] + 1,
147154
end: lines[line + 1],
148155
})
@@ -177,11 +184,9 @@ for (const entry of fs.readdirSync(FIXTURES)) {
177184
let label = `─ ${part.label}`;
178185

179186
if (range.end > range.start) {
180-
const txt = parser.read(part.range);
187+
const txt = read(part.range);
181188
if (txt.length > 1 || /\s/.test(txt)) {
182-
label += ` ${JSON.stringify(
183-
parser.read(part.range).replace(/\r\n/g, "\n")
184-
)}`;
189+
label += ` ${JSON.stringify(txt)}`;
185190
}
186191
}
187192

src/core/Parser.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ export class Parser {
298298

299299
while (this.pos < maxPos) {
300300
const code = data.charCodeAt(this.pos);
301+
let skip = 1;
301302

302303
if (code === CODE.NEWLINE) {
303304
this.activeState.eol.call(this, 1, this.activeRange);
@@ -306,13 +307,13 @@ export class Parser {
306307
data.charCodeAt(this.pos + 1) === CODE.NEWLINE
307308
) {
308309
this.activeState.eol.call(this, 2, this.activeRange);
309-
this.pos++;
310+
skip = 2;
310311
} else {
311312
this.activeState.char.call(this, code, this.activeRange);
312313
}
313314

314315
if (this.forward) {
315-
this.pos++;
316+
this.pos += skip;
316317
} else {
317318
this.forward = true;
318319
}

src/states/BEGIN_DELIMITED_HTML_BLOCK.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ function handleDelimitedBlockEOL(
118118
parser.startText();
119119
parser.pos += indent.length;
120120
// We stay in the same state since we are still parsing a multiline, delimited HTML block
121-
} else if (indent && !parser.onlyWhitespaceRemainsOnLine()) {
121+
} else if (indent && !parser.onlyWhitespaceRemainsOnLine(newLineLength)) {
122122
// the next line does not have enough indentation
123123
// so unless it is blank (whitespace only),
124124
// we will end the block

0 commit comments

Comments
 (0)