Skip to content

Commit

Permalink
Fix line numbers computation in generated docs
Browse files Browse the repository at this point in the history
  • Loading branch information
nvie committed Dec 28, 2023
1 parent a16d4ea commit abdcee2
Show file tree
Hide file tree
Showing 11 changed files with 718 additions and 95 deletions.
10 changes: 0 additions & 10 deletions bin/linenos

This file was deleted.

119 changes: 119 additions & 0 deletions bin/linenos-Decoder-class.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import path from 'path';
import { Project } from 'ts-morph';

// const branch = 'main';
const branch = 'rewrite-in-ts';
let output = [];

const project = new Project({
tsConfigFilePath: './tsconfig.json',
});

for (const src of project.getSourceFiles('src/Decoder.ts')) {
const func = src.getFunction('define');
const members = func.getFunctions();

for (const member of members) {
// Grab the first JSDoc, which, technically, may live on an overload
// declaration
const comment =
[...member.getJsDocs()]
.map((x) => x.getCommentText()?.trim())
.filter(Boolean)?.[0] ?? null;

const start = Math.min(
member.getStartLineNumber(),
...member.getJsDocs().map((doc) => doc.getStartLineNumber()),
...member.getOverloads().map((ov) => ov.getStartLineNumber()),
...member
.getOverloads()
.flatMap((ov) => ov.getJsDocs().map((doc) => doc.getStartLineNumber())),
);
const end = Math.max(
member.getEndLineNumber(),
...member.getJsDocs().map((doc) => doc.getEndLineNumber()),
...member.getOverloads().map((ov) => ov.getEndLineNumber()),
...member
.getOverloads()
.flatMap((ov) => ov.getJsDocs().map((doc) => doc.getEndLineNumber())),
);

const localPath = path.relative('.', src.getFilePath());
const remotePath = `https://github.com/nvie/decoders/tree/${branch}/${localPath}`;
const remote = `${remotePath}#L${start}-L${end}`;

output.push({
name: member.getName(),
comment,
span: { start, end },
localPath,
remotePath,
remote,
});
}

// const start = Math.min(
// type.getStartLineNumber(),
// ...type.getJsDocs().map((doc) => doc.getStartLineNumber()),
// ...type.getOverloads().map((ov) => ov.getStartLineNumber()),
// ...type
// .getOverloads()
// .flatMap((ov) => ov.getJsDocs().map((doc) => doc.getStartLineNumber())),
// );
// const end = Math.max(
// type.getEndLineNumber(),
// ...type.getJsDocs().map((doc) => doc.getEndLineNumber()),
// ...type.getOverloads().map((ov) => ov.getEndLineNumber()),
// ...type
// .getOverloads()
// .flatMap((ov) => ov.getJsDocs().map((doc) => doc.getEndLineNumber())),
// );

// const localPath = path.relative('.', src.getFilePath());
// const remotePath = `https://github.com/nvie/decoders/tree/${branch}/${localPath}`;
// const remote = `${remotePath}#L${start}-L${end}`;

// output.push({
// name: type.getName(),
// comment,
// span: { start, end },
// localPath,
// remotePath,
// remote,
// });
// }

// for (const stm of src.getVariableStatements()) {
// if (!stm.hasExportKeyword()) continue;

// // Grab the first JSDoc, which, technically, may live on an overload
// // declaration
// const comment =
// [...stm.getJsDocs()].map((x) => x.getCommentText()?.trim()).filter(Boolean)?.[0] ??
// null;

// for (const variable of stm.getDeclarations()) {
// const start = Math.min(
// variable.getStartLineNumber(),
// ...stm.getJsDocs().map((doc) => doc.getStartLineNumber()),
// );
// const end = Math.max(
// variable.getEndLineNumber(),
// ...stm.getJsDocs().map((doc) => doc.getEndLineNumber()),
// );
// const localPath = path.relative('.', src.getFilePath());
// const remotePath = `https://github.com/nvie/decoders/tree/${branch}/${localPath}`;
// const remote = `${remotePath}#L${start}-L${end}`;

// output.push({
// name: variable.getName(),
// comment,
// span: { start, end },
// localPath,
// remotePath,
// remote,
// });
// }
}

console.log(JSON.stringify(output, null, 2));
88 changes: 88 additions & 0 deletions bin/linenos-decoders.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import path from 'path';
import { Project } from 'ts-morph';

// const branch = 'main';
const branch = 'rewrite-in-ts';
let output = [];

const project = new Project({
tsConfigFilePath: './tsconfig.json',
});

for (const src of project.getSourceFiles('src/**')) {
for (const func of src.getFunctions()) {
if (!func.hasExportKeyword()) continue;

// Grab the first JSDoc, which, technically, may live on an overload
// declaration
const comment =
[...func.getJsDocs(), ...func.getOverloads().flatMap((ov) => ov.getJsDocs())]
.map((x) => x.getCommentText()?.trim())
.filter(Boolean)?.[0] ?? null;

const start = Math.min(
func.getStartLineNumber(),
...func.getJsDocs().map((doc) => doc.getStartLineNumber()),
...func.getOverloads().map((ov) => ov.getStartLineNumber()),
...func
.getOverloads()
.flatMap((ov) => ov.getJsDocs().map((doc) => doc.getStartLineNumber())),
);
const end = Math.max(
func.getEndLineNumber(),
...func.getJsDocs().map((doc) => doc.getEndLineNumber()),
...func.getOverloads().map((ov) => ov.getEndLineNumber()),
...func
.getOverloads()
.flatMap((ov) => ov.getJsDocs().map((doc) => doc.getEndLineNumber())),
);

const localPath = path.relative('.', src.getFilePath());
const remotePath = `https://github.com/nvie/decoders/tree/${branch}/${localPath}`;
const remote = `${remotePath}#L${start}-L${end}`;

output.push({
name: func.getName(),
comment,
span: { start, end },
localPath,
remotePath,
remote,
});
}

for (const stm of src.getVariableStatements()) {
if (!stm.hasExportKeyword()) continue;

// Grab the first JSDoc, which, technically, may live on an overload
// declaration
const comment =
[...stm.getJsDocs()].map((x) => x.getCommentText()?.trim()).filter(Boolean)?.[0] ??
null;

for (const variable of stm.getDeclarations()) {
const start = Math.min(
variable.getStartLineNumber(),
...stm.getJsDocs().map((doc) => doc.getStartLineNumber()),
);
const end = Math.max(
variable.getEndLineNumber(),
...stm.getJsDocs().map((doc) => doc.getEndLineNumber()),
);
const localPath = path.relative('.', src.getFilePath());
const remotePath = `https://github.com/nvie/decoders/tree/${branch}/${localPath}`;
const remote = `${remotePath}#L${start}-L${end}`;

output.push({
name: variable.getName(),
comment,
span: { start, end },
localPath,
remotePath,
remote,
});
}
}
}

console.log(JSON.stringify(output, null, 2));
Binary file removed bin/linenos-linux
Binary file not shown.
Binary file removed bin/linenos-macos
Binary file not shown.
9 changes: 0 additions & 9 deletions bin/linenos.README.md

This file was deleted.

18 changes: 9 additions & 9 deletions docs/Decoder.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ for name in DECODER_METHODS:
]]]-->
---

<a href="#verify">#</a> **.verify**(blob: <i style="color: #267f99">mixed</i>): <i style="color: #267f99">T</i> [<small>(source)</small>](https://github.com/nvie/decoders/tree/main/src/Decoder.js#L138-L150 'Source')
<a href="#verify">#</a> **.verify**(blob: <i style="color: #267f99">mixed</i>): <i style="color: #267f99">T</i> [<small>(source)</small>](https://github.com/nvie/decoders/tree/rewrite-in-ts/src/Decoder.ts#L137-L149 'Source')
{: #verify .signature}

Verifies the untrusted/unknown input and either accepts or rejects it.
Expand All @@ -69,7 +69,7 @@ number.verify('hello'); // throws

---

<a href="#value">#</a> **.value**(blob: <i style="color: #267f99">mixed</i>): <i style="color: #267f99">T | undefined</i> [<small>(source)</small>](https://github.com/nvie/decoders/tree/main/src/Decoder.js#L152-L162 'Source')
<a href="#value">#</a> **.value**(blob: <i style="color: #267f99">mixed</i>): <i style="color: #267f99">T | undefined</i> [<small>(source)</small>](https://github.com/nvie/decoders/tree/rewrite-in-ts/src/Decoder.ts#L151-L161 'Source')
{: #value .signature}

Verifies the untrusted/unknown input and either accepts or rejects it.
Expand All @@ -95,7 +95,7 @@ string.value(42); // undefined
---

<a href="#decode">#</a> **.decode**(blob: <i style="color: #267f99">mixed</i>): <i style="color: #267f99">DecodeResult&lt;T&gt;</i> [<small>(source)</small>](https://github.com/nvie/decoders/tree/main/src/Decoder.js#L126-L136 'Source')
<a href="#decode">#</a> **.decode**(blob: <i style="color: #267f99">mixed</i>): <i style="color: #267f99">DecodeResult&lt;T&gt;</i> [<small>(source)</small>](https://github.com/nvie/decoders/tree/rewrite-in-ts/src/Decoder.ts#L125-L135 'Source')
{: #decode .signature}

Verifies the untrusted/unknown input and either accepts or rejects it.
Expand All @@ -117,7 +117,7 @@ number.decode('hi'); // { ok: false, error: { type: 'scalar', value: 'hi', text

---

<a href="#transform">#</a> **.transform**&lt;<i style="color: #267f99">V</i>&gt;(transformFn: <i style="color: #267f99">(T) =&gt; V</i>): <i style="color: #267f99"><a href="/Decoder.html" style="color: inherit">Decoder</a>&lt;V&gt;</i> [<small>(source)</small>](https://github.com/nvie/decoders/tree/main/src/Decoder.js#L164-L172 'Source')
<a href="#transform">#</a> **.transform**&lt;<i style="color: #267f99">V</i>&gt;(transformFn: <i style="color: #267f99">(T) =&gt; V</i>): <i style="color: #267f99"><a href="/Decoder.html" style="color: inherit">Decoder</a>&lt;V&gt;</i> [<small>(source)</small>](https://github.com/nvie/decoders/tree/rewrite-in-ts/src/Decoder.ts#L163-L171 'Source')
{: #transform .signature}

Accepts any value the given decoder accepts, and on success, will call
Expand All @@ -137,7 +137,7 @@ upper.verify(4); // throws

---

<a href="#refine">#</a> **.refine**(predicate: <i style="color: #267f99">T =&gt; boolean</i>, message: <i style="color: #267f99">string</i>): <i style="color: #267f99"><a href="/Decoder.html" style="color: inherit">Decoder</a>&lt;T&gt;</i> [<small>(source)</small>](https://github.com/nvie/decoders/tree/main/src/Decoder.js#L174-L187 'Source')
<a href="#refine">#</a> **.refine**(predicate: <i style="color: #267f99">T =&gt; boolean</i>, message: <i style="color: #267f99">string</i>): <i style="color: #267f99"><a href="/Decoder.html" style="color: inherit">Decoder</a>&lt;T&gt;</i> [<small>(source)</small>](https://github.com/nvie/decoders/tree/rewrite-in-ts/src/Decoder.ts#L173-L186 'Source')
{: #refine .signature}

Adds an extra predicate to a decoder. The new decoder is like the
Expand All @@ -162,7 +162,7 @@ In TypeScript, if you provide a predicate that also is a [type predicate](https:

---

<a href="#reject">#</a> **.reject**(rejectFn: <i style="color: #267f99">T =&gt; string | null</i>): <i style="color: #267f99"><a href="/Decoder.html" style="color: inherit">Decoder</a>&lt;T&gt;</i> [<small>(source)</small>](https://github.com/nvie/decoders/tree/main/src/Decoder.js#L215-L233 'Source')
<a href="#reject">#</a> **.reject**(rejectFn: <i style="color: #267f99">T =&gt; string | null</i>): <i style="color: #267f99"><a href="/Decoder.html" style="color: inherit">Decoder</a>&lt;T&gt;</i> [<small>(source)</small>](https://github.com/nvie/decoders/tree/rewrite-in-ts/src/Decoder.ts#L214-L232 'Source')
{: #reject .signature}

Adds an extra predicate to a decoder. The new decoder is like the
Expand Down Expand Up @@ -195,7 +195,7 @@ decoder.verify({ id: 123, _name: 'Vincent' }) // throws: "Disallowed keys: _n

---

<a href="#describe">#</a> **.describe**(message: <i style="color: #267f99">string</i>): <i style="color: #267f99"><a href="/Decoder.html" style="color: inherit">Decoder</a>&lt;T&gt;</i> [<small>(source)</small>](https://github.com/nvie/decoders/tree/main/src/Decoder.js#L235-L252 'Source')
<a href="#describe">#</a> **.describe**(message: <i style="color: #267f99">string</i>): <i style="color: #267f99"><a href="/Decoder.html" style="color: inherit">Decoder</a>&lt;T&gt;</i> [<small>(source)</small>](https://github.com/nvie/decoders/tree/rewrite-in-ts/src/Decoder.ts#L234-L251 'Source')
{: #describe .signature}

Uses the given decoder, but will use an alternative error message in case it rejects. This can be used to simplify or shorten otherwise long or low-level/technical errors.
Expand All @@ -213,7 +213,7 @@ const vowel = decoder.describe('Must be vowel');

---

<a href="#then">#</a> **.then**&lt;<i style="color: #267f99">V</i>&gt;(next: <i style="color: #267f99">(blob: T, ok, err) =&gt; DecodeResult&lt;V&gt;</i>): <i style="color: #267f99"><a href="/Decoder.html" style="color: inherit">Decoder</a>&lt;V&gt;</i> [<small>(source)</small>](https://github.com/nvie/decoders/tree/main/src/Decoder.js#L189-L213 'Source')
<a href="#then">#</a> **.then**&lt;<i style="color: #267f99">V</i>&gt;(next: <i style="color: #267f99">(blob: T, ok, err) =&gt; DecodeResult&lt;V&gt;</i>): <i style="color: #267f99"><a href="/Decoder.html" style="color: inherit">Decoder</a>&lt;V&gt;</i> [<small>(source)</small>](https://github.com/nvie/decoders/tree/rewrite-in-ts/src/Decoder.ts#L188-L212 'Source')
{: #then .signature}

Chain together the current decoder with another.
Expand All @@ -234,5 +234,5 @@ about its input and avoid re-refining inputs.
If it helps, you can think of `define(...)` as equivalent to
`unknown.then(...)`.

<!--[[[end]]] (checksum: 840c751f199ea1d09d20fc35b608a605) -->
<!--[[[end]]] (checksum: d299550dc63a0cc8f9074670098ca3c8) -->
<!-- prettier-ignore-end -->
8 changes: 2 additions & 6 deletions docs/_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1260,12 +1260,8 @@ def run_json(cmd):


def mine_source_code_data():
locinfo1 = run_json(
"./bin/linenos src/Decoder.ts --remote-url --branch main --object-keys --object-methods --json",
)
locinfo2 = run_json(
"./bin/linenos src/*.ts src/**/*.ts --remote-url --branch main --functions --global-variables --json",
)
locinfo1 = run_json("node bin/linenos-Decoder-class.mjs")
locinfo2 = run_json("node bin/linenos-decoders.mjs")

# Check the definitions against the found sources
for method in DECODER_METHODS:
Expand Down
Loading

0 comments on commit abdcee2

Please sign in to comment.