refactor(csv): throw errors immediately (#5299)

This commit is contained in:
Tim Reichen 2024-07-04 09:08:57 +02:00 committed by GitHub
parent 29f5612eb1
commit 73f236d2f2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 64 deletions

View File

@ -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;
@ -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;
@ -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).
@ -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;
@ -179,13 +160,7 @@ 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;
@ -193,9 +168,6 @@ export async function parseRecord(
}
}
}
if (quoteError) {
throw quoteError;
}
const result = [] as string[];
let preIdx = 0;
for (const i of fieldIndexes) {

View File

@ -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;
@ -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;
@ -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).
@ -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;
@ -198,13 +179,7 @@ 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;
@ -212,9 +187,6 @@ class Parser {
}
}
}
if (quoteError) {
throw quoteError;
}
const result = [] as string[];
let preIdx = 0;
for (const i of fieldIndexes) {