Skip to content

Commit

Permalink
fix: windows newline issues
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Mar 21, 2022
1 parent 6ad6bb9 commit 93901db
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
29 changes: 17 additions & 12 deletions src/__tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,17 @@ for (const entry of fs.readdirSync(FIXTURES)) {
pos: Position;
label: string;
}[][] = Array.from({ length: lines.length }, () => []);
const addRange = (label: string, range: Range) => {
// we'll remove windows newlines for the purposes of snapshots.
const read = (range: Range) => parser.read(range).replace(/\r/g, "");
const addRange = (label: string, inputRange: Range) => {
// we'll normalize windows newline positions for the snapshots.
const range =
src.charAt(inputRange.start) === "\r"
? {
start: inputRange.start + 1,
end: inputRange.end,
}
: inputRange;
const pos = parser.positionAt(range.start);
partsByLine[pos.line].push({
label,
Expand Down Expand Up @@ -100,13 +110,10 @@ for (const entry of fs.readdirSync(FIXTURES)) {
addValueRange("attrSpread", range);
},
onOpenTagEnd(range) {
addRange(
`openTagEnd(${parser.read(tagStack[tagStack.length - 1])})`,
range
);
addRange(`openTagEnd(${read(tagStack[tagStack.length - 1])})`, range);
},
onCloseTag(range) {
const label = `closeTag(${parser.read(tagStack.pop()!)})`;
const label = `closeTag(${read(tagStack.pop()!)})`;
if (range.value) {
addValueRange(label, range as Ranges.Value);
} else {
Expand Down Expand Up @@ -134,15 +141,15 @@ for (const entry of fs.readdirSync(FIXTURES)) {
if (line === 0) {
result += `${
linePrefix +
parser.read({
read({
start: 0,
end: lines[1],
})
}`;
} else {
result += `\n${
linePrefix +
parser.read({
read({
start: lines[line] + 1,
end: lines[line + 1],
})
Expand Down Expand Up @@ -177,11 +184,9 @@ for (const entry of fs.readdirSync(FIXTURES)) {
let label = `─ ${part.label}`;

if (range.end > range.start) {
const txt = parser.read(part.range);
const txt = read(part.range);
if (txt.length > 1 || /\s/.test(txt)) {
label += ` ${JSON.stringify(
parser.read(part.range).replace(/\r\n/g, "\n")
)}`;
label += ` ${JSON.stringify(txt)}`;
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/core/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ export class Parser {

while (this.pos < maxPos) {
const code = data.charCodeAt(this.pos);
let skip = 1;

if (code === CODE.NEWLINE) {
this.activeState.eol.call(this, 1, this.activeRange);
Expand All @@ -306,13 +307,13 @@ export class Parser {
data.charCodeAt(this.pos + 1) === CODE.NEWLINE
) {
this.activeState.eol.call(this, 2, this.activeRange);
this.pos++;
skip = 2;
} else {
this.activeState.char.call(this, code, this.activeRange);
}

if (this.forward) {
this.pos++;
this.pos += skip;
} else {
this.forward = true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/states/BEGIN_DELIMITED_HTML_BLOCK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ function handleDelimitedBlockEOL(
parser.startText();
parser.pos += indent.length;
// We stay in the same state since we are still parsing a multiline, delimited HTML block
} else if (indent && !parser.onlyWhitespaceRemainsOnLine()) {
} else if (indent && !parser.onlyWhitespaceRemainsOnLine(newLineLength)) {
// the next line does not have enough indentation
// so unless it is blank (whitespace only),
// we will end the block
Expand Down

0 comments on commit 93901db

Please sign in to comment.