Skip to content

Commit

Permalink
Merge pull request #213 from docsbydoxdox/hotfix/multiline-pattern-of…
Browse files Browse the repository at this point in the history
…fset

[hotfix] Fixed parsing that failed due to comment/code pattern matching.
  • Loading branch information
neogeek authored Oct 15, 2022
2 parents 3b089b0 + 7b396b4 commit 37f20a9
Show file tree
Hide file tree
Showing 3 changed files with 225 additions and 17 deletions.
5 changes: 3 additions & 2 deletions packages/doxdox-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ export const isFile = async (path: string): Promise<boolean> => {

export const multiLinePatternMatch = (
content: string,
pattern: string
pattern: string,
offset = 0
): { start?: number; end?: number; matched: boolean } => {
const matched = content.includes(pattern);

Expand All @@ -174,7 +175,7 @@ export const multiLinePatternMatch = (
const contentLines = content.split(/\r?\n/);
const patternLines = pattern.split(/\r?\n/);

for (let i = 0; i < contentLines.length; i += 1) {
for (let i = offset; i < contentLines.length; i += 1) {
if (contentLines[i] === patternLines[0]) {
const contentGroup = contentLines
.slice(i, i + patternLines.length)
Expand Down
193 changes: 186 additions & 7 deletions packages/doxdox-parser-custom/src/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,38 @@ exports[`custom parser parse example from JSDoc documentation https://jsdoc.app/
"returns": [],
"slug": "test-mocks-commonjs-module-js-colormixer",
},
{
"description": "Darken a color by the given percentage.",
"fullName": "darken(color, percent)",
"name": "darken",
"params": [
{
"description": "- The color, in hexadecimal format.",
"name": "color",
"types": [
"string",
],
},
{
"description": "- The percentage, ranging from 0 to 100.",
"name": "percent",
"types": [
"number",
],
},
],
"private": false,
"returns": [
{
"description": "The darkened color.",
"name": null,
"types": [
"string",
],
},
],
"slug": "test-mocks-commonjs-module-js-darken",
},
{
"description": "Button the shirt.",
"fullName": "exports.button()",
Expand All @@ -447,6 +479,15 @@ exports[`custom parser parse example from JSDoc documentation https://jsdoc.app/
"returns": [],
"slug": "test-mocks-commonjs-module-js-exports-button",
},
{
"description": "Unbutton the shirt.",
"fullName": "exports.unbutton()",
"name": "exports.unbutton",
"params": [],
"private": false,
"returns": [],
"slug": "test-mocks-commonjs-module-js-exports-unbutton",
},
{
"description": "Wash the shirt.",
"fullName": "exports.wash()",
Expand All @@ -458,12 +499,12 @@ exports[`custom parser parse example from JSDoc documentation https://jsdoc.app/
},
{
"description": "Color mixer.",
"fullName": "module.exports()",
"name": "module.exports",
"fullName": "mixer()",
"name": "mixer",
"params": [],
"private": false,
"returns": [],
"slug": "test-mocks-commonjs-module-js-module-exports",
"slug": "test-mocks-commonjs-module-js-mixer",
},
{
"description": "Color mixer.",
Expand All @@ -484,12 +525,35 @@ exports[`custom parser parse example from JSDoc documentation https://jsdoc.app/
"slug": "test-mocks-commonjs-module-js-module-exports",
},
{
"description": "Color mixer.",
"fullName": "module.exports()",
"description": "Blend two colors together.",
"fullName": "module.exports(color1, color2)",
"name": "module.exports",
"params": [],
"params": [
{
"description": "- The first color, in hexadecimal format.",
"name": "color1",
"types": [
"string",
],
},
{
"description": "- The second color, in hexadecimal format.",
"name": "color2",
"types": [
"string",
],
},
],
"private": false,
"returns": [],
"returns": [
{
"description": "The blended color.",
"name": null,
"types": [
"string",
],
},
],
"slug": "test-mocks-commonjs-module-js-module-exports",
},
{
Expand All @@ -501,6 +565,38 @@ exports[`custom parser parse example from JSDoc documentation https://jsdoc.app/
"returns": [],
"slug": "test-mocks-commonjs-module-js-module-exports",
},
{
"description": "Darken a color by the given percentage.",
"fullName": "module.exports.darken(color, percent)",
"name": "module.exports.darken",
"params": [
{
"description": "- The color, in hexadecimal format.",
"name": "color",
"types": [
"string",
],
},
{
"description": "- The percentage, ranging from 0 to 100.",
"name": "percent",
"types": [
"number",
],
},
],
"private": false,
"returns": [
{
"description": "The darkened color.",
"name": null,
"types": [
"string",
],
},
],
"slug": "test-mocks-commonjs-module-js-module-exports-darken",
},
{
"description": "Create a new Book.",
"fullName": "this.Book(title)",
Expand All @@ -527,6 +623,33 @@ exports[`custom parser parse example from JSDoc documentation https://jsdoc.app/
"returns": [],
"slug": "test-mocks-commonjs-module-js-this-title",
},
{
"description": "Wash the shirt.",
"fullName": "wash()",
"name": "wash",
"params": [],
"private": false,
"returns": [],
"slug": "test-mocks-commonjs-module-js-wash",
},
{
"description": "Wash the shirt.",
"fullName": "wash()",
"name": "wash",
"params": [],
"private": false,
"returns": [],
"slug": "test-mocks-commonjs-module-js-wash",
},
{
"description": "Shirt module.",
"fullName": "wash()",
"name": "wash",
"params": [],
"private": false,
"returns": [],
"slug": "test-mocks-commonjs-module-js-wash",
},
],
"path": "./test/mocks/commonjs-module.js",
}
Expand Down Expand Up @@ -599,6 +722,31 @@ exports[`custom parser parse example from JSDoc documentation https://jsdoc.app/
"returns": [],
"slug": "test-mocks-es-classes-js-dot",
},
{
"description": "Convert a string containing two comma-separated numbers into a point.",
"fullName": "fromString(str)",
"name": "fromString",
"params": [
{
"description": "- The string containing two comma-separated numbers.",
"name": "str",
"types": [
"string",
],
},
],
"private": false,
"returns": [
{
"description": "A Point object.",
"name": null,
"types": [
"Point",
],
},
],
"slug": "test-mocks-es-classes-js-fromstring",
},
{
"description": "Get the dot's width.",
"fullName": "getWidth()",
Expand Down Expand Up @@ -775,6 +923,37 @@ exports[`custom parser parse example from JSDoc documentation https://jsdoc.app/
exports[`custom parser parse example from JSDoc documentation https://jsdoc.app/ parse params 1`] = `
{
"methods": [
{
"description": "Assign the project to an employee.",
"fullName": "Project.assign(employee, employee.name, employee.department)",
"name": "Project.assign",
"params": [
{
"description": "- The employee who is responsible for the project.",
"name": "employee",
"types": [
"Object",
],
},
{
"description": "- The name of the employee.",
"name": "employee.name",
"types": [
"string",
],
},
{
"description": "- The employee's department.",
"name": "employee.department",
"types": [
"string",
],
},
],
"private": false,
"returns": [],
"slug": "test-mocks-params-js-project-prototype-assign",
},
{
"description": "Assign the project to an employee.",
"fullName": "Project.assign(employee, employee.name, employee.department)",
Expand Down
44 changes: 36 additions & 8 deletions packages/doxdox-parser-custom/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { join } from 'path';

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

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

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

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

const methods = comments.map(comment => ({
rawComment: comment,
comment: commentParse(comment)[0],
name: '',
code: '',
lines: multiLinePatternMatch(content, comment)
}));
const methods: {
rawComment: string;
comment: Block;
name: string;
code: string;
lines: {
start?: number | undefined;
end?: number | undefined;
matched: boolean;
};
}[] = [];

for (let i = 0; i < comments.length; i += 1) {
const previousComment = methods[i - 1];

const comment = comments[i];

const linePatternMatchOffset =
previousComment && previousComment.lines
? previousComment.lines.end
: 0;

methods.push({
rawComment: comment,
comment: commentParse(comment)[0],
name: '',
code: '',
lines: multiLinePatternMatch(
content,
comment,
linePatternMatchOffset
)
});
}

for (let i = 0; i < methods.length; i += 1) {
if (methods[i].lines.matched && i === methods.length - 1) {
Expand Down Expand Up @@ -97,6 +124,7 @@ export const parseString = async (
const paramTags = method.comment.tags.filter(({ tag }) =>
/param$/.test(tag)
);

const returnTags = method.comment.tags.filter(({ tag }) =>
/return$/.test(tag)
);
Expand Down

0 comments on commit 37f20a9

Please sign in to comment.