mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
refactor(csv): throw errors immediately (#5299)
This commit is contained in:
parent
29f5612eb1
commit
73f236d2f2
36
csv/_io.ts
36
csv/_io.ts
@ -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) {
|
||||
|
36
csv/parse.ts
36
csv/parse.ts
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user