Skip to content

Commit 91868b1

Browse files
committed
Added a parseContents method for parsing strings.
Added tests.
1 parent e12b2ec commit 91868b1

File tree

2 files changed

+118
-1
lines changed

2 files changed

+118
-1
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { parseContents } from './index';
2+
3+
describe('jsdoc parser', () => {
4+
it('parse example jsdoc header', async () => {
5+
await expect(
6+
parseContents(
7+
'cache/',
8+
'utils.js',
9+
`/**
10+
* Finds file in path.
11+
*
12+
* console.log(await findFileInPath('./', 'package.json'));
13+
* console.log(await findFileInPath('../', 'package.json'));
14+
* console.log(await findFileInPath('~/git/github/doxdox/', '.package.json'));
15+
*
16+
* @param {string} [input] Directory to check for file.
17+
* @param {string?} [fileName = 'package.json'] File name to check for.
18+
* @return {Promise<string | null>} Path to package.json file.
19+
* @public
20+
*/
21+
22+
const findFileInPath = async (input, fileName = 'package.json') => {};
23+
24+
/**
25+
* Get the root directory of the package, supplied path or URL.
26+
*
27+
* @param {string?} [url] Optional path or URL.
28+
* @return {string} Directory path.
29+
* @public
30+
*/
31+
32+
const getRootDirPath = (url) => {}`
33+
)
34+
).resolves.toEqual(
35+
expect.objectContaining({
36+
path: 'utils.js',
37+
methods: expect.arrayContaining([
38+
expect.objectContaining({
39+
fullName: 'findFileInPath(input, fileName)',
40+
name: 'findFileInPath',
41+
params: expect.arrayContaining([
42+
expect.objectContaining({
43+
name: 'input',
44+
types: ['string']
45+
}),
46+
expect.objectContaining({
47+
name: 'fileName',
48+
types: ['string']
49+
})
50+
]),
51+
private: false,
52+
returns: expect.arrayContaining([
53+
expect.objectContaining({
54+
name: undefined,
55+
types: ['Promise.<(string|null)>']
56+
})
57+
]),
58+
slug: 'utils-js-findfileinpath'
59+
}),
60+
expect.objectContaining({
61+
fullName: 'getRootDirPath(url)',
62+
name: 'getRootDirPath',
63+
params: expect.arrayContaining([
64+
expect.objectContaining({
65+
name: 'url',
66+
types: ['string']
67+
})
68+
]),
69+
private: false,
70+
returns: expect.arrayContaining([
71+
expect.objectContaining({
72+
name: undefined,
73+
types: ['string']
74+
})
75+
]),
76+
slug: 'utils-js-getrootdirpath'
77+
})
78+
])
79+
})
80+
);
81+
});
82+
it('parse empty string', async () => {
83+
await expect(parseContents('cache/', 'test.js', '')).resolves.toEqual(
84+
expect.objectContaining({
85+
path: 'test.js',
86+
methods: []
87+
})
88+
);
89+
});
90+
});

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { promises as fs } from 'fs';
2+
13
import { platform } from 'os';
24

35
import { dirname, join } from 'path';
@@ -10,7 +12,7 @@ import { File } from 'doxdox-core';
1012

1113
import { Jsdoc } from './types';
1214

13-
export default async (cwd: string, path: string): Promise<File> => {
15+
const parser = async (cwd: string, path: string): Promise<File> => {
1416
try {
1517
const nodeModulesDir = await findParentNodeModules(
1618
dirname(sanitizePath(import.meta.url))
@@ -93,3 +95,28 @@ export default async (cwd: string, path: string): Promise<File> => {
9395

9496
return { path, methods: [] };
9597
};
98+
99+
export const parseContents = async (
100+
tempDir: string,
101+
path: string,
102+
content: string
103+
): Promise<File> => {
104+
const tempPath = `${tempDir}/${path}`;
105+
106+
await fs.mkdir(dirname(tempPath), { recursive: true });
107+
108+
await fs.writeFile(tempPath, content);
109+
110+
const file = await parser(tempDir, path);
111+
112+
try {
113+
await fs.unlink(tempPath);
114+
await fs.rm(tempDir, { recursive: true });
115+
} catch (error) {
116+
console.log(error);
117+
}
118+
119+
return file;
120+
};
121+
122+
export default parser;

0 commit comments

Comments
 (0)