Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link tag support #66

Merged
merged 5 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,39 @@ When assigned to a variable in the exporting module:

This syntax is also used when referring to types of `@typedef`s and `@enum`s.

### `@link` tags

```js
/**
* {@link Identifier}
*/

/**
* {@link Identifier Link text}
*/

/**
* {@link Identifier.member}
*/
```

To:

```js
/**
* {@link module:path/to/module.Identifier Identifier}
*/

/**
* {@link module:path/to/module.Identifier Link text}
*/

/**
* Member accessors are not currently linked to, just the root identifier:
* {@link module:path/to/module.Identifier Identifier.member}
*/
```

### `typeof type`

```js
Expand Down Expand Up @@ -165,6 +198,7 @@ To:
```

To:

```js
/**
* @type {Array}
Expand Down
26 changes: 26 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,32 @@ exports.astNodeVisitor = {
);
replace(typeRegex);

const linkWithoutTextRegex = new RegExp(
`@(link (?!https?:))${key}((?:\\.[^\\s}]*)*\\s*\\})`,
'g',
);
const linkMemberAccessorRegex = new RegExp(
`@(link (?!https?:)${key})(?:\\.[^\\s}]*)*([\\s|][^}]*\\})`,
'g',
);
const linkWithTextRegex = new RegExp(
`@(link (?!https?:))${key}([\\s|][^}]*\\})`,
'g',
);

// If link is without text, use key as text
comment.value = comment.value.replace(
linkWithoutTextRegex,
`@$1${key} ${key}$2`,
);
// Remove member accessors from link key
comment.value = comment.value.replace(
linkMemberAccessorRegex,
'@$1$2',
);

replace(linkWithTextRegex);

function replace(regex) {
if (regex.test(comment.value)) {
const identifier = identifiers[key];
Expand Down
184 changes: 184 additions & 0 deletions test/dest/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,190 @@
"memberof": "module:test",
"params": []
},
{
"comment": "/**\n * {@link module:test/sub/NumberStore~NumberStore NumberStore}\n */",
"meta": {
"range": [
1424,
1456
],
"filename": "index.js",
"lineno": 67,
"columnno": 0,
"code": {
"name": "exports.Link",
"type": "VariableDeclaration"
}
},
"description": "{@link module:test/sub/NumberStore~NumberStore NumberStore}",
"name": "Link",
"longname": "module:test.Link",
"kind": "constant",
"memberof": "module:test",
"scope": "static"
},
{
"comment": "",
"meta": {
"range": [
1437,
1455
],
"filename": "index.js",
"lineno": 67,
"columnno": 13,
"code": {
"name": "Link",
"type": "Identifier",
"value": "NumberStore"
}
},
"undocumented": true,
"name": "Link",
"longname": "module:test~Link",
"kind": "constant",
"scope": "inner",
"memberof": "module:test",
"params": []
},
{
"comment": "/**\n * {@link module:test/sub/NumberStore~NumberStore Num}\n */",
"meta": {
"range": [
1493,
1533
],
"filename": "index.js",
"lineno": 72,
"columnno": 0,
"code": {
"name": "exports.LinkWithText",
"type": "VariableDeclaration"
}
},
"description": "{@link module:test/sub/NumberStore~NumberStore Num}",
"name": "LinkWithText",
"longname": "module:test.LinkWithText",
"kind": "constant",
"memberof": "module:test",
"scope": "static"
},
{
"comment": "",
"meta": {
"range": [
1506,
1532
],
"filename": "index.js",
"lineno": 72,
"columnno": 13,
"code": {
"name": "LinkWithText",
"type": "Identifier",
"value": "NumberStore"
}
},
"undocumented": true,
"name": "LinkWithText",
"longname": "module:test~LinkWithText",
"kind": "constant",
"scope": "inner",
"memberof": "module:test",
"params": []
},
{
"comment": "/**\n * {@link module:test/sub/NumberStore~NumberStore NumberStore.getNumber}\n */",
"meta": {
"range": [
1576,
1626
],
"filename": "index.js",
"lineno": 77,
"columnno": 0,
"code": {
"name": "exports.LinkWithMemberAccessor",
"type": "VariableDeclaration"
}
},
"description": "{@link module:test/sub/NumberStore~NumberStore NumberStore.getNumber}",
"name": "LinkWithMemberAccessor",
"longname": "module:test.LinkWithMemberAccessor",
"kind": "constant",
"memberof": "module:test",
"scope": "static"
},
{
"comment": "",
"meta": {
"range": [
1589,
1625
],
"filename": "index.js",
"lineno": 77,
"columnno": 13,
"code": {
"name": "LinkWithMemberAccessor",
"type": "Identifier",
"value": "NumberStore"
}
},
"undocumented": true,
"name": "LinkWithMemberAccessor",
"longname": "module:test~LinkWithMemberAccessor",
"kind": "constant",
"scope": "inner",
"memberof": "module:test",
"params": []
},
{
"comment": "/**\n * {@link module:test/sub/NumberStore~NumberStore Num}\n */",
"meta": {
"range": [
1673,
1730
],
"filename": "index.js",
"lineno": 82,
"columnno": 0,
"code": {
"name": "exports.LinkWithMemberAccessorAndText",
"type": "VariableDeclaration"
}
},
"description": "{@link module:test/sub/NumberStore~NumberStore Num}",
"name": "LinkWithMemberAccessorAndText",
"longname": "module:test.LinkWithMemberAccessorAndText",
"kind": "constant",
"memberof": "module:test",
"scope": "static"
},
{
"comment": "",
"meta": {
"range": [
1686,
1729
],
"filename": "index.js",
"lineno": 82,
"columnno": 13,
"code": {
"name": "LinkWithMemberAccessorAndText",
"type": "Identifier",
"value": "NumberStore"
}
},
"undocumented": true,
"name": "LinkWithMemberAccessorAndText",
"longname": "module:test~LinkWithMemberAccessorAndText",
"kind": "constant",
"scope": "inner",
"memberof": "module:test",
"params": []
},
{
"comment": "/** @ignore */",
"meta": {
Expand Down
20 changes: 20 additions & 0 deletions test/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,23 @@ export const bracketNotation = 1;

/** @type {[number, [number, string], "[number, boolean]"]} */
export const nesteedTuples = [1, [1, 'a'], '[number, boolean]'];

/**
* {@link NumberStore}
*/
export const Link = NumberStore;

/**
* {@link NumberStore Num}
*/
export const LinkWithText = NumberStore;

/**
* {@link NumberStore.getNumber}
*/
export const LinkWithMemberAccessor = NumberStore;

/**
* {@link NumberStore.getNumber Num}
*/
export const LinkWithMemberAccessorAndText = NumberStore;