From 3c27216bffd010c46d388376d2e18cd7bf93ba33 Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Mon, 3 Jun 2024 15:27:18 +1000 Subject: [PATCH 1/2] docs(csv): use assertions in example code snippets --- csv/_io.ts | 29 ++++++++++++++++++++--------- csv/csv_parse_stream.ts | 8 ++++---- csv/csv_stringify_stream.ts | 4 ++-- csv/mod.ts | 4 ++-- csv/stringify.ts | 2 +- 5 files changed, 29 insertions(+), 18 deletions(-) diff --git a/csv/_io.ts b/csv/_io.ts index a158113a5858..04f278ebfb54 100644 --- a/csv/_io.ts +++ b/csv/_io.ts @@ -219,12 +219,13 @@ function runeCount(s: string): number { * @example Usage * ```ts * import { parse, ParseError } from "@std/csv/parse"; + * import { assertEquals } from "@std/assert/assert-equals"; * * try { * parse(`a "word","b"`); * } catch (error) { * if (error instanceof ParseError) { - * console.error(error.message); + * assertEquals(error.message, `parse error on line 1, column 2: bare " in non-quoted-field`); * } * } * ``` @@ -234,14 +235,15 @@ export class ParseError extends SyntaxError { * Line where the record starts. * * @example Usage - * ```ts + * ```ts no-assert * import { parse, ParseError } from "@std/csv/parse"; + * import { assertEquals } from "@std/assert/assert-equals"; * * try { * parse(`a "word","b"`); * } catch (error) { * if (error instanceof ParseError) { - * console.error(error.startLine); + * assertEquals(error.startLine, 1); * } * } * ``` @@ -253,12 +255,13 @@ export class ParseError extends SyntaxError { * @example Usage * ```ts * import { parse, ParseError } from "@std/csv/parse"; + * import { assertEquals } from "@std/assert/assert-equals"; * * try { * parse(`a "word","b"`); * } catch (error) { * if (error instanceof ParseError) { - * console.error(error.line); + * assertEquals(error.line, 1); * } * } * ``` @@ -268,14 +271,15 @@ export class ParseError extends SyntaxError { * Column (rune index) where the error occurred. * * @example Usage - * ```ts + * ```ts no-assert * import { parse, ParseError } from "@std/csv/parse"; + * import { assertEquals } from "@std/assert/assert-equals"; * * try { * parse(`a "word","b"`); * } catch (error) { * if (error instanceof ParseError) { - * console.error(error.column); + * assertEquals(error.column, 2); * } * } * ``` @@ -286,10 +290,17 @@ export class ParseError extends SyntaxError { * Constructs a new instance. * * @example Usage - * ```ts no-eval - * import { ParseError } from "@std/csv/parse"; + * ```ts + * import { parse, ParseError } from "@std/csv/parse"; + * import { assertEquals } from "@std/assert/assert-equals"; * - * throw new ParseError(1, 2, 3, "error message"); + * try { + * parse(`a "word","b"`); + * } catch (error) { + * if (error instanceof ParseError) { + * assertEquals(error.message, `parse error on line 1, column 2: bare " in non-quoted-field`); + * } + * } * ``` * * @param start Line where the record starts diff --git a/csv/csv_parse_stream.ts b/csv/csv_parse_stream.ts index f52c30a5a285..fae4b5daab79 100644 --- a/csv/csv_parse_stream.ts +++ b/csv/csv_parse_stream.ts @@ -67,7 +67,7 @@ export type RowType = T extends undefined ? string[] * {@link https://www.rfc-editor.org/rfc/rfc4180.html | RFC 4180}. * * @example Usage - * ```ts + * ```ts no-assert * import { CsvParseStream } from "@std/csv/csv-parse-stream"; * * const source = ReadableStream.from([ @@ -98,7 +98,7 @@ export class CsvParseStream< /** Construct a new instance. * * @example Usage - * ```ts + * ```ts no-assert * import { CsvParseStream } from "@std/csv/csv-parse-stream"; * * const source = ReadableStream.from([ @@ -196,7 +196,7 @@ export class CsvParseStream< * The instance's {@linkcode ReadableStream}. * * @example Usage - * ```ts + * ```ts no-assert * import { CsvParseStream } from "@std/csv/csv-parse-stream"; * * const source = ReadableStream.from([ @@ -222,7 +222,7 @@ export class CsvParseStream< * The instance's {@linkcode WritableStream}. * * @example Usage - * ```ts + * ```ts no-assert * import { CsvParseStream } from "@std/csv/csv-parse-stream"; * * const source = ReadableStream.from([ diff --git a/csv/csv_stringify_stream.ts b/csv/csv_stringify_stream.ts index 7dc476976d42..d8b1ffea8134 100644 --- a/csv/csv_stringify_stream.ts +++ b/csv/csv_stringify_stream.ts @@ -23,7 +23,7 @@ export interface CsvStringifyStreamOptions { * Convert each chunk to a CSV record. * * @example Usage - * ```ts + * ```ts no-assert no-eval * import { CsvStringifyStream } from "@std/csv/csv-stringify-stream"; * * const path = await Deno.makeTempFile(); @@ -53,7 +53,7 @@ export class CsvStringifyStream * Construct a new instance. * * @example Usage - * ```ts + * ```ts no-eval no-assert * import { CsvStringifyStream } from "@std/csv/csv-stringify-stream"; * * const path = await Deno.makeTempFile(); diff --git a/csv/mod.ts b/csv/mod.ts index 1bbde541f0ba..0541b020ed25 100644 --- a/csv/mod.ts +++ b/csv/mod.ts @@ -32,7 +32,7 @@ * * results in the fields * - * ```ts + * ```ts no-assert * [`normal string`, `quoted-field`] * ``` * @@ -55,7 +55,7 @@ * * results in * - * ```ts + * ```ts no-assert * [`Multi-line * field`, `comma is ,`] * ``` diff --git a/csv/stringify.ts b/csv/stringify.ts index fc327e069280..10a9703e9e56 100644 --- a/csv/stringify.ts +++ b/csv/stringify.ts @@ -172,7 +172,7 @@ function normalizeColumn(column: Column): NormalizedColumn { * Error thrown in {@linkcode stringify}. * * @example Usage - * ```ts + * ```ts no-assert * import { stringify, StringifyError } from "@std/csv/stringify"; * * try { From fcf92d53641bbcb2401a84e3db55f1b1f8ba7972 Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Mon, 3 Jun 2024 15:37:54 +1000 Subject: [PATCH 2/2] fixes --- csv/_io.ts | 4 ++-- csv/csv_stringify_stream.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/csv/_io.ts b/csv/_io.ts index 04f278ebfb54..9347196ec18d 100644 --- a/csv/_io.ts +++ b/csv/_io.ts @@ -235,7 +235,7 @@ export class ParseError extends SyntaxError { * Line where the record starts. * * @example Usage - * ```ts no-assert + * ```ts * import { parse, ParseError } from "@std/csv/parse"; * import { assertEquals } from "@std/assert/assert-equals"; * @@ -271,7 +271,7 @@ export class ParseError extends SyntaxError { * Column (rune index) where the error occurred. * * @example Usage - * ```ts no-assert + * ```ts * import { parse, ParseError } from "@std/csv/parse"; * import { assertEquals } from "@std/assert/assert-equals"; * diff --git a/csv/csv_stringify_stream.ts b/csv/csv_stringify_stream.ts index d8b1ffea8134..c327ee8af8d5 100644 --- a/csv/csv_stringify_stream.ts +++ b/csv/csv_stringify_stream.ts @@ -23,7 +23,7 @@ export interface CsvStringifyStreamOptions { * Convert each chunk to a CSV record. * * @example Usage - * ```ts no-assert no-eval + * ```ts no-assert * import { CsvStringifyStream } from "@std/csv/csv-stringify-stream"; * * const path = await Deno.makeTempFile(); @@ -53,7 +53,7 @@ export class CsvStringifyStream * Construct a new instance. * * @example Usage - * ```ts no-eval no-assert + * ```ts no-assert * import { CsvStringifyStream } from "@std/csv/csv-stringify-stream"; * * const path = await Deno.makeTempFile();