Skip to content

Commit 0263c85

Browse files
committed
Added prototype support to custom parser.
Updated tests snapshots.
1 parent e40a9ce commit 0263c85

File tree

4 files changed

+280
-2
lines changed

4 files changed

+280
-2
lines changed

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

Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,32 @@ exports[`custom parser parse amd-module 1`] = `
7171
"returns": [],
7272
"slug": "test-mocks-amd-module-js-jacket",
7373
},
74+
{
75+
"description": "Zip up the jacket.",
76+
"fullName": "Jacket.zip()",
77+
"name": "Jacket.zip",
78+
"params": [],
79+
"private": false,
80+
"returns": [],
81+
"slug": "test-mocks-amd-module-js-jacket-prototype-zip",
82+
},
83+
{
84+
"description": "Zip up a jacket.",
85+
"fullName": "module.zip(jacket)",
86+
"name": "module.zip",
87+
"params": [
88+
{
89+
"description": "- The jacket to zip up.",
90+
"name": "jacket",
91+
"types": [
92+
"Jacket",
93+
],
94+
},
95+
],
96+
"private": false,
97+
"returns": [],
98+
"slug": "test-mocks-amd-module-js-module-zip",
99+
},
74100
{
75101
"description": "A module representing a shirt.",
76102
"fullName": "shirt()",
@@ -150,6 +176,31 @@ exports[`custom parser parse amd-module 1`] = `
150176
"returns": [],
151177
"slug": "test-mocks-amd-module-js-utils",
152178
},
179+
{
180+
"description": "Determine if an element is in the document head.",
181+
"fullName": "utils.isInHead(element)",
182+
"name": "utils.isInHead",
183+
"params": [
184+
{
185+
"description": "- The element.",
186+
"name": "element",
187+
"types": [
188+
"HTMLElement",
189+
],
190+
},
191+
],
192+
"private": false,
193+
"returns": [
194+
{
195+
"description": "Set to \`true\` if the element is in the document head, \`false\` otherwise.",
196+
"name": null,
197+
"types": [
198+
"boolean",
199+
],
200+
},
201+
],
202+
"slug": "test-mocks-amd-module-js-utils-isinhead",
203+
},
153204
],
154205
"path": "./test/mocks/amd-module.js",
155206
}
@@ -329,6 +380,60 @@ exports[`custom parser parse commonjs-module 1`] = `
329380
"returns": [],
330381
"slug": "test-mocks-commonjs-module-js-button",
331382
},
383+
{
384+
"description": "Color mixer.",
385+
"fullName": "module.exports()",
386+
"name": "module.exports",
387+
"params": [],
388+
"private": false,
389+
"returns": [],
390+
"slug": "test-mocks-commonjs-module-js-module-exports",
391+
},
392+
{
393+
"description": "Color mixer.",
394+
"fullName": "module.exports()",
395+
"name": "module.exports",
396+
"params": [],
397+
"private": false,
398+
"returns": [],
399+
"slug": "test-mocks-commonjs-module-js-module-exports",
400+
},
401+
{
402+
"description": "Color mixer.",
403+
"fullName": "module.exports()",
404+
"name": "module.exports",
405+
"params": [],
406+
"private": false,
407+
"returns": [],
408+
"slug": "test-mocks-commonjs-module-js-module-exports",
409+
},
410+
{
411+
"description": "Color mixer.",
412+
"fullName": "module.exports()",
413+
"name": "module.exports",
414+
"params": [],
415+
"private": false,
416+
"returns": [],
417+
"slug": "test-mocks-commonjs-module-js-module-exports",
418+
},
419+
{
420+
"description": "Create a color mixer.",
421+
"fullName": "module.exports()",
422+
"name": "module.exports",
423+
"params": [],
424+
"private": false,
425+
"returns": [],
426+
"slug": "test-mocks-commonjs-module-js-module-exports",
427+
},
428+
{
429+
"description": "Module representing the word of the day.",
430+
"fullName": "module.exports()",
431+
"name": "module.exports",
432+
"params": [],
433+
"private": false,
434+
"returns": [],
435+
"slug": "test-mocks-commonjs-module-js-module-exports",
436+
},
332437
{
333438
"description": "The title of the book.",
334439
"fullName": "title()",
@@ -725,6 +830,68 @@ exports[`custom parser parse no-comment 1`] = `
725830
exports[`custom parser parse params 1`] = `
726831
{
727832
"methods": [
833+
{
834+
"description": "Assign the project to an employee.",
835+
"fullName": "Project.assign(employee, employee.name, employee.department)",
836+
"name": "Project.assign",
837+
"params": [
838+
{
839+
"description": "- The employee who is responsible for the project.",
840+
"name": "employee",
841+
"types": [
842+
"Object",
843+
],
844+
},
845+
{
846+
"description": "- The name of the employee.",
847+
"name": "employee.name",
848+
"types": [
849+
"string",
850+
],
851+
},
852+
{
853+
"description": "- The employee's department.",
854+
"name": "employee.department",
855+
"types": [
856+
"string",
857+
],
858+
},
859+
],
860+
"private": false,
861+
"returns": [],
862+
"slug": "test-mocks-params-js-project-prototype-assign",
863+
},
864+
{
865+
"description": "Assign the project to a list of employees.",
866+
"fullName": "Project.assign(employees, employees[].name, employees[].department)",
867+
"name": "Project.assign",
868+
"params": [
869+
{
870+
"description": "- The employees who are responsible for the project.",
871+
"name": "employees",
872+
"types": [
873+
"Object[]",
874+
],
875+
},
876+
{
877+
"description": "- The name of an employee.",
878+
"name": "employees[].name",
879+
"types": [
880+
"string",
881+
],
882+
},
883+
{
884+
"description": "- The employee's department.",
885+
"name": "employees[].department",
886+
"types": [
887+
"string",
888+
],
889+
},
890+
],
891+
"private": false,
892+
"returns": [],
893+
"slug": "test-mocks-params-js-project-prototype-assign",
894+
},
728895
{
729896
"description": "",
730897
"fullName": "sayHello(somebody)",
@@ -899,3 +1066,78 @@ exports[`custom parser parse params 1`] = `
8991066
"path": "./test/mocks/params.js",
9001067
}
9011068
`;
1069+
1070+
exports[`custom parser prototype methods 1`] = `
1071+
{
1072+
"methods": [
1073+
{
1074+
"description": "Create a point.",
1075+
"fullName": "constructor(x, y)",
1076+
"name": "constructor",
1077+
"params": [
1078+
{
1079+
"description": "- The x value.",
1080+
"name": "x",
1081+
"types": [
1082+
"number",
1083+
],
1084+
},
1085+
{
1086+
"description": "- The y value.",
1087+
"name": "y",
1088+
"types": [
1089+
"number",
1090+
],
1091+
},
1092+
],
1093+
"private": false,
1094+
"returns": [],
1095+
"slug": "test-mocks-prototype-js-constructor",
1096+
},
1097+
{
1098+
"description": "Class representing a point.",
1099+
"fullName": "Point()",
1100+
"name": "Point",
1101+
"params": [],
1102+
"private": false,
1103+
"returns": [],
1104+
"slug": "test-mocks-prototype-js-point",
1105+
},
1106+
{
1107+
"description": "Get the x value.",
1108+
"fullName": "Point.getX()",
1109+
"name": "Point.getX",
1110+
"params": [],
1111+
"private": false,
1112+
"returns": [
1113+
{
1114+
"description": "The x value.",
1115+
"name": null,
1116+
"types": [
1117+
"number",
1118+
],
1119+
},
1120+
],
1121+
"slug": "test-mocks-prototype-js-point-prototype-getx",
1122+
},
1123+
{
1124+
"description": "Get the y value.",
1125+
"fullName": "Point.getY()",
1126+
"name": "Point.getY",
1127+
"params": [],
1128+
"private": false,
1129+
"returns": [
1130+
{
1131+
"description": "The y value.",
1132+
"name": null,
1133+
"types": [
1134+
"number",
1135+
],
1136+
},
1137+
],
1138+
"slug": "test-mocks-prototype-js-point-prototype-gety",
1139+
},
1140+
],
1141+
"path": "./test/mocks/prototype.js",
1142+
}
1143+
`;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ describe('custom parser', () => {
1717
).resolves.toMatchSnapshot();
1818
});
1919

20+
it('prototype methods', async () => {
21+
await expect(
22+
parse(process.cwd(), './test/mocks/prototype.js')
23+
).resolves.toMatchSnapshot();
24+
});
25+
2026
// JSDoc Example from https://jsdoc.app/
2127
it('parse amd-module', async () => {
2228
await expect(

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const IDENTIFIER_PATTERNS = [
1919
/^(?:this|exports)\.([a-z0-9]+)\s*=/i,
2020
/^(?:export\s+)?function\s+([a-z0-9]+)\s*\(/i,
2121
/^class\s+([a-z0-9]+)\s*{/i,
22+
/^((?:[a-z0-9]+)(\.prototype)?\.(?:[a-z0-9]+))/i,
2223
/^[a-z0-9]+\s*as\s*([a-z0-9]+)/i
2324
];
2425

@@ -95,6 +96,8 @@ export const parseString = async (
9596
methods: methods
9697
.filter(method => method.name)
9798
.map(method => {
99+
const methodName = method.name.replace(/\.prototype/i, '');
100+
98101
const paramTags = method.comment.tags.filter(({ tag }) =>
99102
/param$/.test(tag)
100103
);
@@ -120,8 +123,8 @@ export const parseString = async (
120123
);
121124
return {
122125
slug: `${slugify(path)}-${slugify(method.name)}`,
123-
name: method.name,
124-
fullName: `${method.name}(${params
126+
name: methodName,
127+
fullName: `${methodName}(${params
125128
.map(param => param.name)
126129
.join(', ')})`,
127130
description: method.comment.description,
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/** Class representing a point. */
2+
class Point {
3+
/**
4+
* Create a point.
5+
* @param {number} x - The x value.
6+
* @param {number} y - The y value.
7+
*/
8+
constructor(x, y) {
9+
// ...
10+
}
11+
}
12+
13+
/**
14+
* Get the x value.
15+
* @return {number} The x value.
16+
*/
17+
Point.prototype.getX = function () {
18+
// ...
19+
};
20+
21+
/**
22+
* Get the y value.
23+
* @return {number} The y value.
24+
*/
25+
Point.prototype.getY = function () {
26+
// ...
27+
};

0 commit comments

Comments
 (0)