Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(bytes,collections): fix doc checker and documentation #4691

Merged
merged 2 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions _tools/check_docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const ENTRY_POINTS = [

const MD_SNIPPET = /(?<=```ts\n)(\n|.)*(?=\n```)/g;

class ValidationError extends Error {
class DocumentError extends Error {
constructor(message: string, document: DocNodeBase) {
super(message, {
cause: `${document.location.filename}:${document.location.line}`,
Expand All @@ -40,14 +40,17 @@ function assert(
document: DocNodeBase,
): asserts condition {
if (!condition) {
throw new ValidationError(message, document);
throw new DocumentError(message, document);
}
}

/**
* We only check functions that have JSDocs. We know that exported functions
* have JSDocs thanks to `deno doc --lint`, which is used in the `lint:docs`
* task.
*/
function isFunctionDoc(document: DocNodeBase): document is DocNodeFunction {
return document.kind === "function" &&
// Ignores implementation signatures when overload signatures exist
(document as DocNodeFunction).functionDef.hasBody !== true;
return document.kind === "function" && document.jsDoc !== undefined;
}

function isExported(document: DocNodeBase) {
Expand Down Expand Up @@ -87,7 +90,7 @@ function assertHasParamTag(
function assertHasExampleTag(tags: JsDocTag[], document: DocNodeBase) {
tags = tags.filter((tag) => tag.kind === "example");
if (tags.length === 0) {
throw new ValidationError("Symbol must have an @example tag", document);
throw new DocumentError("Symbol must have an @example tag", document);
}
for (const tag of (tags as JsDocTagDocRequired[])) {
assert(
Expand All @@ -97,7 +100,7 @@ function assertHasExampleTag(tags: JsDocTag[], document: DocNodeBase) {
);
const snippets = tag.doc.match(MD_SNIPPET);
if (snippets === null) {
throw new ValidationError(
throw new DocumentError(
"@example tag must have a code snippet",
document,
);
Expand All @@ -106,14 +109,18 @@ function assertHasExampleTag(tags: JsDocTag[], document: DocNodeBase) {
const command = new Deno.Command(Deno.execPath(), {
args: [
"eval",
"--ext=ts",
snippet,
],
stderr: "piped",
});
// TODO(iuioiua): Use `await command.output()`
const { success } = command.outputSync();
const { success, stderr } = command.outputSync();
assert(
success,
`Example code snippet failed to execute: \n${snippet}\n`,
`Example code snippet failed to execute: \n${snippet}\n${
new TextDecoder().decode(stderr)
}`,
document,
);
}
Expand Down
3 changes: 2 additions & 1 deletion bytes/concat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
* @example Basic usage
* ```ts
* import { concat } from "@std/bytes/concat";
* import { assertEquals } from "@std/assert/assert-equals"
*
* const a = new Uint8Array([0, 1, 2]);
* const b = new Uint8Array([3, 4, 5]);
*
* concat([a, b]); // Uint8Array(6) [ 0, 1, 2, 3, 4, 5 ]
* assertEquals(concat([a, b]), new Uint8Array([0, 1, 2, 3, 4, 5]));
* ```
*/
export function concat(buffers: Uint8Array[]): Uint8Array {
Expand Down
10 changes: 6 additions & 4 deletions bytes/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,25 @@
* @example Basic usage
* ```ts
* import { copy } from "@std/bytes/copy";
* import { assertEquals } from "@std/assert/assert-equals";
*
* const src = new Uint8Array([9, 8, 7]);
* const dst = new Uint8Array([0, 1, 2, 3, 4, 5]);
*
* copy(src, dst); // 3
* dst; // Uint8Array(6) [9, 8, 7, 3, 4, 5]
* assertEquals(copy(src, dst), 3);
* assertEquals(dst, new Uint8Array([9, 8, 7, 3, 4, 5]));
* ```
*
* @example Copy with offset
* ```ts
* import { copy } from "@std/bytes/copy";
* import { assertEquals } from "@std/assert/assert-equals";
*
* const src = new Uint8Array([1, 1, 1, 1]);
* const dst = new Uint8Array([0, 0, 0, 0]);
*
* copy(src, dst, 1); // 3
* dst; // Uint8Array(4) [0, 1, 1, 1]
* assertEquals(copy(src, dst, 1), 3);
* assertEquals(dst, new Uint8Array([0, 1, 1, 1]));
* ```
* Defining an offset will start copying at the specified index in the
* destination array.
Expand Down
3 changes: 2 additions & 1 deletion bytes/ends_with.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
* @example Basic usage
* ```ts
* import { endsWith } from "@std/bytes/ends-with";
* import { assertEquals } from "@std/assert/assert-equals";
*
* const source = new Uint8Array([0, 1, 2, 1, 2, 1, 2, 3]);
* const suffix = new Uint8Array([1, 2, 3]);
*
* endsWith(source, suffix); // true
* assertEquals(endsWith(source, suffix), true);
* ```
*/
export function endsWith(source: Uint8Array, suffix: Uint8Array): boolean {
Expand Down
5 changes: 3 additions & 2 deletions bytes/equals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@ const THRESHOLD_32_BIT = 160;
* @example Basic usage
* ```ts
* import { equals } from "@std/bytes/equals";
* import { assertEquals } from "@std/assert/assert-equals";
*
* const a = new Uint8Array([1, 2, 3]);
* const b = new Uint8Array([1, 2, 3]);
* const c = new Uint8Array([4, 5, 6]);
*
* equals(a, b); // true
* equals(b, c); // false
* assertEquals(equals(a, b), true);
* assertEquals(equals(a, c), false);
* ```
*/
export function equals(a: Uint8Array, b: Uint8Array): boolean {
Expand Down
7 changes: 5 additions & 2 deletions bytes/includes_needle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,24 @@ import { indexOfNeedle } from "./index_of_needle.ts";
* @example Basic usage
* ```ts
* import { includesNeedle } from "@std/bytes/includes-needle";
* import { assertEquals } from "@std/assert/assert-equals";
*
* const source = new Uint8Array([0, 1, 2, 1, 2, 1, 2, 3]);
* const needle = new Uint8Array([1, 2]);
*
* includesNeedle(source, needle); // true
* assertEquals(includesNeedle(source, needle), true);
* ```
*
* @example Start index
* ```ts
* import { includesNeedle } from "@std/bytes/includes-needle";
* import { assertEquals } from "@std/assert/assert-equals";
*
* const source = new Uint8Array([0, 1, 2, 1, 2, 1, 2, 3]);
* const needle = new Uint8Array([1, 2]);
*
* includesNeedle(source, needle, 6); // false
* assertEquals(includesNeedle(source, needle, 3), true);
* assertEquals(includesNeedle(source, needle, 6), false);
* ```
* The search will start at the specified index in the source array.
*/
Expand Down
10 changes: 6 additions & 4 deletions bytes/index_of_needle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,26 @@
* @example Basic usage
* ```ts
* import { indexOfNeedle } from "@std/bytes/index-of-needle";
* import { assertEquals } from "@std/assert/assert-equals";
*
* const source = new Uint8Array([0, 1, 2, 1, 2, 1, 2, 3]);
* const needle = new Uint8Array([1, 2]);
* const notNeedle = new Uint8Array([5, 0]);
*
* indexOfNeedle(source, needle); // 1
* indexOfNeedle(source, notNeedle); // -1
* assertEquals(indexOfNeedle(source, needle), 1);
* assertEquals(indexOfNeedle(source, notNeedle), -1);
* ```
*
* @example Start index
* ```ts
* import { indexOfNeedle } from "@std/bytes/index-of-needle";
* import { assertEquals } from "@std/assert/assert-equals";
*
* const source = new Uint8Array([0, 1, 2, 1, 2, 1, 2, 3]);
* const needle = new Uint8Array([1, 2]);
*
* indexOfNeedle(source, needle, 2); // 3
* indexOfNeedle(source, needle, 6); // -1
* assertEquals(indexOfNeedle(source, needle, 2), 3);
* assertEquals(indexOfNeedle(source, needle, 6), -1);
* ```
* Defining a start index will begin the search at the specified index in the
* source array.
Expand Down
10 changes: 6 additions & 4 deletions bytes/last_index_of_needle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,26 @@
* @example Basic usage
* ```ts
* import { lastIndexOfNeedle } from "@std/bytes/last-index-of-needle";
* import { assertEquals } from "@std/assert/assert-equals";
*
* const source = new Uint8Array([0, 1, 2, 1, 2, 1, 2, 3]);
* const needle = new Uint8Array([1, 2]);
* const notNeedle = new Uint8Array([5, 0]);
*
* lastIndexOfNeedle(source, needle); // 5
* lastIndexOfNeedle(source, notNeedle); // -1
* assertEquals(lastIndexOfNeedle(source, needle), 5);
* assertEquals(lastIndexOfNeedle(source, notNeedle), -1);
* ```
*
* @example Start index
* ```ts
* import { lastIndexOfNeedle } from "@std/bytes/last-index-of-needle";
* import { assertEquals } from "@std/assert/assert-equals";
*
* const source = new Uint8Array([0, 1, 2, 1, 2, 1, 2, 3]);
* const needle = new Uint8Array([1, 2]);
*
* lastIndexOfNeedle(source, needle, 2); // 1
* lastIndexOfNeedle(source, needle, 6); // 5
* assertEquals(lastIndexOfNeedle(source, needle, 2), 1);
* assertEquals(lastIndexOfNeedle(source, needle, 6), 5);
* ```
* Defining a start index will begin the search at the specified index in the
* source array.
Expand Down
8 changes: 2 additions & 6 deletions bytes/repeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ import { copy } from "./copy.ts";
*
* const source = new Uint8Array([0, 1, 2]);
*
* const result = repeat(source, 3);
*
* assertEquals(result, new Uint8Array([0, 1, 2, 0, 1, 2, 0, 1, 2]));
* assertEquals(repeat(source, 3), new Uint8Array([0, 1, 2, 0, 1, 2, 0, 1, 2]));
* ```
*
* @example Zero count
Expand All @@ -30,9 +28,7 @@ import { copy } from "./copy.ts";
*
* const source = new Uint8Array([0, 1, 2]);
*
* const result = repeat(source, 0);
*
* assertEquals(result, new Uint8Array());
* assertEquals(repeat(source, 0), new Uint8Array());
* ```
*/
export function repeat(source: Uint8Array, count: number): Uint8Array {
Expand Down
3 changes: 2 additions & 1 deletion bytes/starts_with.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
* @example Basic usage
* ```ts
* import { startsWith } from "@std/bytes/starts-with";
* import { assertEquals } from "@std/assert/assert-equals";
*
* const source = new Uint8Array([0, 1, 2, 1, 2, 1, 2, 3]);
* const prefix = new Uint8Array([0, 1, 2]);
*
* startsWith(source, prefix); // true
* assertEquals(startsWith(source, prefix), true);
* ```
*/
export function startsWith(source: Uint8Array, prefix: Uint8Array): boolean {
Expand Down
6 changes: 3 additions & 3 deletions collections/drop_last_while.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
* import { dropLastWhile } from "@std/collections/drop-last-while";
* import { assertEquals } from "@std/assert/assert-equals";
*
* const numbers = [22, 30, 44];
* const numbers = [20, 33, 44];
*
* const notFortyFour = dropLastWhile(numbers, (number) => number !== 44);
* const notFortyFour = dropLastWhile(numbers, (number) => number > 30);
*
* assertEquals(notFortyFour, [22, 30]);
* assertEquals(notFortyFour, [20]);
* ```
*/
export function dropLastWhile<T>(
Expand Down
3 changes: 3 additions & 0 deletions collections/reduce_groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import { mapValues } from "./map_values.ts";
* @param reducer The reducer function to apply to each group.
* @param initialValue The initial value of the accumulator.
*
* @returns A record with the same keys as the input grouping, where each value
* is the result of applying the reducer to the respective group.
*
* @example Basic usage
* ```ts
* import { reduceGroups } from "@std/collections/reduce-groups";
Expand Down
2 changes: 2 additions & 0 deletions collections/running_reduce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* @param reducer The reducer function to apply to each element.
* @param initialValue The initial value of the accumulator.
*
* @returns An array of all intermediate accumulator results.
*
* @example Basic usage
* ```ts
* import { runningReduce } from "@std/collections/running-reduce";
Expand Down
4 changes: 2 additions & 2 deletions collections/sample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import { randomInteger } from "./_utils.ts";
* @example Basic usage
* ```ts
* import { sample } from "@std/collections/sample";
* import { assert } from "@std/assert/assert";
* import { assertArrayIncludes } from "@std/assert/assert-array-includes";
*
* const numbers = [1, 2, 3, 4];
* const random = sample(numbers);
*
* assert(numbers.includes(random!));
* assertArrayIncludes(numbers, [random]);
* ```
*/
export function sample<T>(array: readonly T[]): T | undefined {
Expand Down