Skip to content

Commit e4570cd

Browse files
committed
jsdoc supporting typescript import style (#1)
* Supporting typescript import style `catharsis.parse` does not accept the typescript import expression `@param p { import("./a").Pet }` - it raises `Invalid type expression` . This patch does a simple regexp to remove this pattern before the string is parsed. So in this example, `@param p { import("./a").Pet }` in jsdoc parse time is interpreted like it was `@param p { Pet }` * Adding README.md
1 parent 843d707 commit e4570cd

File tree

4 files changed

+36
-72
lines changed

4 files changed

+36
-72
lines changed

README.md

+16-68
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,32 @@
1-
# JSDoc
1+
# jsdoc-import-typedef
22

3-
[![Build Status](https://travis-ci.org/jsdoc/jsdoc.svg?branch=master)](http://travis-ci.org/jsdoc/jsdoc)
3+
This is a fork of [jsdoc](https://github.com/jsdoc/jsdoc) to deal with typescript type check import way - https://www.typescriptlang.org/docs/handbook/type-checking-javascript-files.html#import-types
44

5-
An API documentation generator for JavaScript.
5+
## Motivation
66

7-
Want to contribute to JSDoc? Please read `CONTRIBUTING.md`.
7+
There are some issues in jsdoc related to this problem and a feature is open here: https://github.com/jsdoc/jsdoc/issues/1645. But the feature will be very complex and maybe can take some time to be done. And I need this fix asap.
88

9-
Installation and Usage
10-
----------------------
9+
## Cause
1110

12-
JSDoc supports stable versions of Node.js 8.15.0 and later. You can install
13-
JSDoc globally or in your project's `node_modules` folder.
11+
jsdoc uses [catharsis](https://github.com/hegemonic/catharsis) to parse the expressions and `catharsis.parse` doesn't accept the typescript `import` expression (ex: `@param p { import("./a").Pet }`).
1412

15-
To install the latest version on npm globally (might require `sudo`;
16-
[learn how to fix this](https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally)):
13+
This raises `Invalid type expression` like seem here https://github.com/hegemonic/catharsis/blob/master/bin/parse.js#L48
1714

18-
npm install -g jsdoc
15+
Instead of patch catharsis, I apply the fix in jsdoc, using a regexp to remove this pattern before the string is parsed by `catharsis.parse`.
1916

20-
To install the latest version on npm locally and save it in your package's
21-
`package.json` file:
17+
In this case, `@param p { import("./a").Pet }` will be interpreted like `@param p { Pet }` in jsdoc.
2218

23-
npm install --save-dev jsdoc
19+
Works great if you are using Typescript to type check your jsdoc - (https://www.typescriptlang.org/docs/handbook/type-checking-javascript-files.html) and is compatible with all jsdoc templates.
2420

25-
**Note**: By default, npm adds your package using the caret operator in front of
26-
the version number (for example, `^3.6.3`). We recommend using the tilde
27-
operator instead (for example, `~3.6.3`), which limits updates to the most
28-
recent patch-level version. See
29-
[this Stack Overflow answer](https://stackoverflow.com/questions/22343224) for
30-
more information about the caret and tilde operators.
21+
## Installation
22+
```
23+
npm install jsdoc-import-typedef
24+
```
3125

32-
To install the latest development version locally, without updating your
33-
project's `package.json` file:
26+
## Usage
3427

35-
npm install git+https://github.com/jsdoc/jsdoc.git
28+
This is a patched `jsdoc` and all its usage remains the same (https://github.com/jsdoc/jsdoc#installation-and-usage).
3629

37-
If you installed JSDoc locally, the JSDoc command-line tool is available in
38-
`./node_modules/.bin`. To generate documentation for the file
39-
`yourJavaScriptFile.js`:
40-
41-
./node_modules/.bin/jsdoc yourJavaScriptFile.js
42-
43-
If you installed JSDoc globally, run the `jsdoc` command:
44-
45-
jsdoc yourJavaScriptFile.js
46-
47-
By default, the generated documentation is saved in a directory named `out`. You
48-
can use the `--destination` (`-d`) option to specify another directory.
49-
50-
Run `jsdoc --help` for a complete list of command-line options.
51-
52-
## Templates and tools
53-
54-
The JSDoc community has created templates and other tools to help you generate
55-
and customize your documentation. Here are a few of them:
56-
57-
### Templates
58-
59-
+ [jaguarjs-jsdoc](https://github.com/davidshimjs/jaguarjs-jsdoc)
60-
+ [DocStrap](https://github.com/docstrap/docstrap)
61-
([example](https://docstrap.github.io/docstrap))
62-
+ [jsdoc3Template](https://github.com/DBCDK/jsdoc3Template)
63-
([example](https://github.com/danyg/jsdoc3Template/wiki#wiki-screenshots))
64-
+ [minami](https://github.com/Nijikokun/minami)
65-
+ [docdash](https://github.com/clenemt/docdash)
66-
([example](http://clenemt.github.io/docdash/))
67-
+ [tui-jsdoc-template](https://github.com/nhnent/tui.jsdoc-template)
68-
([example](https://nhnent.github.io/tui.jsdoc-template/latest/))
69-
+ [better-docs](https://github.com/SoftwareBrothers/better-docs)
70-
([example](https://softwarebrothers.github.io/admin-bro-dev/index.html))
71-
72-
### Build tools
73-
74-
+ [JSDoc Grunt plugin](https://github.com/krampstudio/grunt-jsdoc)
75-
+ [JSDoc Gulp plugin](https://github.com/mlucool/gulp-jsdoc3)
76-
77-
### Other tools
78-
79-
+ [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown)
80-
+ [Integrating GitBook with
81-
JSDoc](https://medium.com/@kevinast/integrate-gitbook-jsdoc-974be8df6fb3)
8230

8331
## For more information
8432

lib/jsdoc/tag/type.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -234,18 +234,24 @@ function getTypeStrings(parsedType, isOutermostType) {
234234
*/
235235
function parseTypeExpression(tagInfo) {
236236
let parsedType;
237+
let typeExpression = tagInfo.typeExpression;
237238

238239
// don't try to parse empty type expressions
239-
if (!tagInfo.typeExpression) {
240+
if (!typeExpression) {
240241
return tagInfo;
241242
}
242243

244+
// if expression has typescript import types. Ex: @typedef { import("./").Foo }
245+
if (typeExpression.match(/import\(.*\)\./)) {
246+
typeExpression = typeExpression.replace(/import\(.*\)\./, '');
247+
}
248+
243249
try {
244-
parsedType = catharsis.parse(tagInfo.typeExpression, {jsdoc: true});
250+
parsedType = catharsis.parse(typeExpression, {jsdoc: true});
245251
}
246252
catch (e) {
247253
// always re-throw so the caller has a chance to report which file was bad
248-
throw new Error(`Invalid type expression "${tagInfo.typeExpression}": ${e.message}`);
254+
throw new Error(`Invalid type expression "${typeExpression}": ${e.message}`);
249255
}
250256

251257
tagInfo.type = tagInfo.type.concat( getTypeStrings(parsedType, true) );

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "jsdoc",
2+
"name": "jsdoc-import-typedef",
33
"version": "4.0.0-dev",
44
"revision": "1563126982480",
55
"description": "An API documentation generator for JavaScript.",

test/specs/jsdoc/tag/type.js

+10
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,16 @@ describe('jsdoc/tag/type', () => {
245245
expect(info.optional).toBe(true);
246246
expect(info.defaultvalue).toBe('hooray');
247247
});
248+
249+
it('should parse Typescript JSDoc-style import files', () => {
250+
const name = 'pet';
251+
const desc = '{import("./a").Pet} SomePet'
252+
const info = type.parse( buildText(null, name, desc), true, true);
253+
254+
expect(info.type[0]).toEqual('Pet');
255+
expect(info.text).toBe('SomePet');
256+
expect(info.name).toBe('pet');
257+
});
248258
});
249259

250260
// TODO: add more tests related to how JSDoc mangles the Catharsis parse results

0 commit comments

Comments
 (0)