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

fix: respect empty lines in example block #128

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 5 additions & 6 deletions src/loader/babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,20 +210,19 @@ const babelPluginUntyped: PluginItem = function (

export default babelPluginUntyped;

function containsIncompleteCodeblock(line = "") {
const codeDelimiters = line
.split("\n")
.filter((line) => line.startsWith("```")).length;
return !!(codeDelimiters % 2);
function isExampleBlock(line = "") {
return line.includes("@example");
pi0 marked this conversation as resolved.
Show resolved Hide resolved
}

function clumpLines(lines: string[], delimiters = [" "], separator = " ") {
const clumps: string[] = [];
while (lines.length > 0) {
const line = lines.shift();

if (
(line && !delimiters.includes(line[0]) && clumps.at(-1)) ||
containsIncompleteCodeblock(clumps.at(-1))
// Support for example blocks in mutliline comments (last line is an example and next line is not a tag)
Barbapapazes marked this conversation as resolved.
Show resolved Hide resolved
(isExampleBlock(clumps.at(-1)) && !line.startsWith("@"))
) {
clumps[clumps.length - 1] += separator + line;
} else {
Expand Down
30 changes: 30 additions & 0 deletions test/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,36 @@ describe("transform (jsdoc)", () => {
});
});

it("does not split example without codeblock", () => {
const result = transform(`
export default {
/**
* @note This is a note.
* @example
* export default secretNumber = 42
*
* export default nothing = null
* @type {'src' | 'root'}
*/
srcDir: 'src'
}
`);
expectCodeToMatch(result, /export default ([\S\s]*)$/, {
srcDir: {
$default: "src",
$schema: {
title: "",
tsType: "'src' | 'root'",
description: "",
tags: [
"@note This is a note.",
"@example\nexport default secretNumber = 42\n\nexport default nothing = null",
],
},
},
});
});

it("correctly parses type assertion", () => {
const result = transform(`
import type { InputObject } from 'untyped'
Expand Down
Loading