Skip to content

Commit

Permalink
refactor(csv): throw errors immediately (#5299)
Browse files Browse the repository at this point in the history
  • Loading branch information
timreichen authored Jul 4, 2024
1 parent 29f5612 commit 73f236d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 64 deletions.
36 changes: 4 additions & 32 deletions csv/_io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ export async function parseRecord(
if (opt.separator === undefined) throw new TypeError("Separator is required");

let fullLine = line;
let quoteError: ParseError | null = null;
const quote = '"';
const quoteLen = quote.length;
const separatorLen = opt.separator.length;
Expand All @@ -98,13 +97,7 @@ export async function parseRecord(
const col = runeCount(
fullLine.slice(0, fullLine.length - line.slice(j).length),
);
quoteError = new ParseError(
startLine + 1,
lineIndex,
col,
ERR_BARE_QUOTE,
);
break parseField;
throw new ParseError(startLine + 1, lineIndex, col, ERR_BARE_QUOTE);
}
}
recordBuffer += field;
Expand Down Expand Up @@ -144,13 +137,7 @@ export async function parseRecord(
const col = runeCount(
fullLine.slice(0, fullLine.length - line.length - quoteLen),
);
quoteError = new ParseError(
startLine + 1,
lineIndex,
col,
ERR_QUOTE,
);
break parseField;
throw new ParseError(startLine + 1, lineIndex, col, ERR_QUOTE);
}
} else if (line.length > 0 || !reader.isEOF()) {
// Hit end of line (copy all data so far).
Expand All @@ -163,13 +150,7 @@ export async function parseRecord(
// Abrupt end of file (EOF or error).
if (!opt.lazyQuotes) {
const col = runeCount(fullLine);
quoteError = new ParseError(
startLine + 1,
lineIndex,
col,
ERR_QUOTE,
);
break parseField;
throw new ParseError(startLine + 1, lineIndex, col, ERR_QUOTE);
}
fieldIndexes.push(recordBuffer.length);
break parseField;
Expand All @@ -179,23 +160,14 @@ export async function parseRecord(
// Abrupt end of file (EOF on error).
if (!opt.lazyQuotes) {
const col = runeCount(fullLine);
quoteError = new ParseError(
startLine + 1,
lineIndex,
col,
ERR_QUOTE,
);
break parseField;
throw new ParseError(startLine + 1, lineIndex, col, ERR_QUOTE);
}
fieldIndexes.push(recordBuffer.length);
break parseField;
}
}
}
}
if (quoteError) {
throw quoteError;
}
const result = [] as string[];
let preIdx = 0;
for (const i of fieldIndexes) {
Expand Down
36 changes: 4 additions & 32 deletions csv/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ class Parser {
}

let fullLine = line;
let quoteError: ParseError | null = null;
const quote = '"';
const quoteLen = quote.length;
const separatorLen = this.#options.separator.length;
Expand All @@ -117,13 +116,7 @@ class Parser {
const col = runeCount(
fullLine.slice(0, fullLine.length - line.slice(j).length),
);
quoteError = new ParseError(
startLine + 1,
lineIndex,
col,
ERR_BARE_QUOTE,
);
break parseField;
throw new ParseError(startLine + 1, lineIndex, col, ERR_BARE_QUOTE);
}
}
recordBuffer += field;
Expand Down Expand Up @@ -163,13 +156,7 @@ class Parser {
const col = runeCount(
fullLine.slice(0, fullLine.length - line.length - quoteLen),
);
quoteError = new ParseError(
startLine + 1,
lineIndex,
col,
ERR_QUOTE,
);
break parseField;
throw new ParseError(startLine + 1, lineIndex, col, ERR_QUOTE);
}
} else if (line.length > 0 || !(this.#isEOF())) {
// Hit end of line (copy all data so far).
Expand All @@ -182,13 +169,7 @@ class Parser {
// Abrupt end of file (EOF or error).
if (!this.#options.lazyQuotes) {
const col = runeCount(fullLine);
quoteError = new ParseError(
startLine + 1,
lineIndex,
col,
ERR_QUOTE,
);
break parseField;
throw new ParseError(startLine + 1, lineIndex, col, ERR_QUOTE);
}
fieldIndexes.push(recordBuffer.length);
break parseField;
Expand All @@ -198,23 +179,14 @@ class Parser {
// Abrupt end of file (EOF on error).
if (!this.#options.lazyQuotes) {
const col = runeCount(fullLine);
quoteError = new ParseError(
startLine + 1,
lineIndex,
col,
ERR_QUOTE,
);
break parseField;
throw new ParseError(startLine + 1, lineIndex, col, ERR_QUOTE);
}
fieldIndexes.push(recordBuffer.length);
break parseField;
}
}
}
}
if (quoteError) {
throw quoteError;
}
const result = [] as string[];
let preIdx = 0;
for (const i of fieldIndexes) {
Expand Down

0 comments on commit 73f236d

Please sign in to comment.