Skip to content

Commit 7b396b4

Browse files
committed
Fixed parsing that failed due to comment/code pattern matching.
Updated snapshots.
1 parent 1da416d commit 7b396b4

File tree

2 files changed

+222
-15
lines changed

2 files changed

+222
-15
lines changed

packages/doxdox-parser-custom/src/__snapshots__/index.test.ts.snap

Lines changed: 186 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,38 @@ exports[`custom parser parse example from JSDoc documentation https://jsdoc.app/
438438
"returns": [],
439439
"slug": "test-mocks-commonjs-module-js-colormixer",
440440
},
441+
{
442+
"description": "Darken a color by the given percentage.",
443+
"fullName": "darken(color, percent)",
444+
"name": "darken",
445+
"params": [
446+
{
447+
"description": "- The color, in hexadecimal format.",
448+
"name": "color",
449+
"types": [
450+
"string",
451+
],
452+
},
453+
{
454+
"description": "- The percentage, ranging from 0 to 100.",
455+
"name": "percent",
456+
"types": [
457+
"number",
458+
],
459+
},
460+
],
461+
"private": false,
462+
"returns": [
463+
{
464+
"description": "The darkened color.",
465+
"name": null,
466+
"types": [
467+
"string",
468+
],
469+
},
470+
],
471+
"slug": "test-mocks-commonjs-module-js-darken",
472+
},
441473
{
442474
"description": "Button the shirt.",
443475
"fullName": "exports.button()",
@@ -447,6 +479,15 @@ exports[`custom parser parse example from JSDoc documentation https://jsdoc.app/
447479
"returns": [],
448480
"slug": "test-mocks-commonjs-module-js-exports-button",
449481
},
482+
{
483+
"description": "Unbutton the shirt.",
484+
"fullName": "exports.unbutton()",
485+
"name": "exports.unbutton",
486+
"params": [],
487+
"private": false,
488+
"returns": [],
489+
"slug": "test-mocks-commonjs-module-js-exports-unbutton",
490+
},
450491
{
451492
"description": "Wash the shirt.",
452493
"fullName": "exports.wash()",
@@ -458,12 +499,12 @@ exports[`custom parser parse example from JSDoc documentation https://jsdoc.app/
458499
},
459500
{
460501
"description": "Color mixer.",
461-
"fullName": "module.exports()",
462-
"name": "module.exports",
502+
"fullName": "mixer()",
503+
"name": "mixer",
463504
"params": [],
464505
"private": false,
465506
"returns": [],
466-
"slug": "test-mocks-commonjs-module-js-module-exports",
507+
"slug": "test-mocks-commonjs-module-js-mixer",
467508
},
468509
{
469510
"description": "Color mixer.",
@@ -484,12 +525,35 @@ exports[`custom parser parse example from JSDoc documentation https://jsdoc.app/
484525
"slug": "test-mocks-commonjs-module-js-module-exports",
485526
},
486527
{
487-
"description": "Color mixer.",
488-
"fullName": "module.exports()",
528+
"description": "Blend two colors together.",
529+
"fullName": "module.exports(color1, color2)",
489530
"name": "module.exports",
490-
"params": [],
531+
"params": [
532+
{
533+
"description": "- The first color, in hexadecimal format.",
534+
"name": "color1",
535+
"types": [
536+
"string",
537+
],
538+
},
539+
{
540+
"description": "- The second color, in hexadecimal format.",
541+
"name": "color2",
542+
"types": [
543+
"string",
544+
],
545+
},
546+
],
491547
"private": false,
492-
"returns": [],
548+
"returns": [
549+
{
550+
"description": "The blended color.",
551+
"name": null,
552+
"types": [
553+
"string",
554+
],
555+
},
556+
],
493557
"slug": "test-mocks-commonjs-module-js-module-exports",
494558
},
495559
{
@@ -501,6 +565,38 @@ exports[`custom parser parse example from JSDoc documentation https://jsdoc.app/
501565
"returns": [],
502566
"slug": "test-mocks-commonjs-module-js-module-exports",
503567
},
568+
{
569+
"description": "Darken a color by the given percentage.",
570+
"fullName": "module.exports.darken(color, percent)",
571+
"name": "module.exports.darken",
572+
"params": [
573+
{
574+
"description": "- The color, in hexadecimal format.",
575+
"name": "color",
576+
"types": [
577+
"string",
578+
],
579+
},
580+
{
581+
"description": "- The percentage, ranging from 0 to 100.",
582+
"name": "percent",
583+
"types": [
584+
"number",
585+
],
586+
},
587+
],
588+
"private": false,
589+
"returns": [
590+
{
591+
"description": "The darkened color.",
592+
"name": null,
593+
"types": [
594+
"string",
595+
],
596+
},
597+
],
598+
"slug": "test-mocks-commonjs-module-js-module-exports-darken",
599+
},
504600
{
505601
"description": "Create a new Book.",
506602
"fullName": "this.Book(title)",
@@ -527,6 +623,33 @@ exports[`custom parser parse example from JSDoc documentation https://jsdoc.app/
527623
"returns": [],
528624
"slug": "test-mocks-commonjs-module-js-this-title",
529625
},
626+
{
627+
"description": "Wash the shirt.",
628+
"fullName": "wash()",
629+
"name": "wash",
630+
"params": [],
631+
"private": false,
632+
"returns": [],
633+
"slug": "test-mocks-commonjs-module-js-wash",
634+
},
635+
{
636+
"description": "Wash the shirt.",
637+
"fullName": "wash()",
638+
"name": "wash",
639+
"params": [],
640+
"private": false,
641+
"returns": [],
642+
"slug": "test-mocks-commonjs-module-js-wash",
643+
},
644+
{
645+
"description": "Shirt module.",
646+
"fullName": "wash()",
647+
"name": "wash",
648+
"params": [],
649+
"private": false,
650+
"returns": [],
651+
"slug": "test-mocks-commonjs-module-js-wash",
652+
},
530653
],
531654
"path": "./test/mocks/commonjs-module.js",
532655
}
@@ -599,6 +722,31 @@ exports[`custom parser parse example from JSDoc documentation https://jsdoc.app/
599722
"returns": [],
600723
"slug": "test-mocks-es-classes-js-dot",
601724
},
725+
{
726+
"description": "Convert a string containing two comma-separated numbers into a point.",
727+
"fullName": "fromString(str)",
728+
"name": "fromString",
729+
"params": [
730+
{
731+
"description": "- The string containing two comma-separated numbers.",
732+
"name": "str",
733+
"types": [
734+
"string",
735+
],
736+
},
737+
],
738+
"private": false,
739+
"returns": [
740+
{
741+
"description": "A Point object.",
742+
"name": null,
743+
"types": [
744+
"Point",
745+
],
746+
},
747+
],
748+
"slug": "test-mocks-es-classes-js-fromstring",
749+
},
602750
{
603751
"description": "Get the dot's width.",
604752
"fullName": "getWidth()",
@@ -775,6 +923,37 @@ exports[`custom parser parse example from JSDoc documentation https://jsdoc.app/
775923
exports[`custom parser parse example from JSDoc documentation https://jsdoc.app/ parse params 1`] = `
776924
{
777925
"methods": [
926+
{
927+
"description": "Assign the project to an employee.",
928+
"fullName": "Project.assign(employee, employee.name, employee.department)",
929+
"name": "Project.assign",
930+
"params": [
931+
{
932+
"description": "- The employee who is responsible for the project.",
933+
"name": "employee",
934+
"types": [
935+
"Object",
936+
],
937+
},
938+
{
939+
"description": "- The name of the employee.",
940+
"name": "employee.name",
941+
"types": [
942+
"string",
943+
],
944+
},
945+
{
946+
"description": "- The employee's department.",
947+
"name": "employee.department",
948+
"types": [
949+
"string",
950+
],
951+
},
952+
],
953+
"private": false,
954+
"returns": [],
955+
"slug": "test-mocks-params-js-project-prototype-assign",
956+
},
778957
{
779958
"description": "Assign the project to an employee.",
780959
"fullName": "Project.assign(employee, employee.name, employee.department)",

packages/doxdox-parser-custom/src/index.ts

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { join } from 'path';
44

55
import { File, Method, multiLinePatternMatch, slugify } from 'doxdox-core';
66

7-
import { parse as commentParse } from 'comment-parser';
7+
import { Block, parse as commentParse } from 'comment-parser';
88

99
import { firstMatch, matches } from 'super-regex';
1010

@@ -41,13 +41,40 @@ export const parseString = async (
4141
matches(JSDOC_PATTERN, content, { timeout: REGEX_TIMEOUT })
4242
).map(({ match }) => match);
4343

44-
const methods = comments.map(comment => ({
45-
rawComment: comment,
46-
comment: commentParse(comment)[0],
47-
name: '',
48-
code: '',
49-
lines: multiLinePatternMatch(content, comment)
50-
}));
44+
const methods: {
45+
rawComment: string;
46+
comment: Block;
47+
name: string;
48+
code: string;
49+
lines: {
50+
start?: number | undefined;
51+
end?: number | undefined;
52+
matched: boolean;
53+
};
54+
}[] = [];
55+
56+
for (let i = 0; i < comments.length; i += 1) {
57+
const previousComment = methods[i - 1];
58+
59+
const comment = comments[i];
60+
61+
const linePatternMatchOffset =
62+
previousComment && previousComment.lines
63+
? previousComment.lines.end
64+
: 0;
65+
66+
methods.push({
67+
rawComment: comment,
68+
comment: commentParse(comment)[0],
69+
name: '',
70+
code: '',
71+
lines: multiLinePatternMatch(
72+
content,
73+
comment,
74+
linePatternMatchOffset
75+
)
76+
});
77+
}
5178

5279
for (let i = 0; i < methods.length; i += 1) {
5380
if (methods[i].lines.matched && i === methods.length - 1) {
@@ -97,6 +124,7 @@ export const parseString = async (
97124
const paramTags = method.comment.tags.filter(({ tag }) =>
98125
/param$/.test(tag)
99126
);
127+
100128
const returnTags = method.comment.tags.filter(({ tag }) =>
101129
/return$/.test(tag)
102130
);

0 commit comments

Comments
 (0)