Skip to content

Commit

Permalink
fix: respect multines example block
Browse files Browse the repository at this point in the history
  • Loading branch information
Barbapapazes committed Feb 25, 2024
1 parent 71cb7e3 commit 53b7713
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
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");
}

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)
(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

0 comments on commit 53b7713

Please sign in to comment.