Skip to content

Handle intersection types #77

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,22 @@ To:
*/
```

### Intersection types

```js
/**
* @type {A & B}
*/
```

To:

```js
/**
* @type {A | B}
*/
```

## Module id resolution

For resolving module ids, this plugin mirrors the method used by JSDoc:
Expand Down
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,13 @@ exports.defineTags = function (dictionary) {
case '\\':
// Skip escaped character
++i;
break;
case '&':
// Intersection `&` to union `|`
if (!isWithinString && openCurly >= 1) {
replacements.push([i, i + 1, '|']);
}

break;
case '"':
case "'":
Expand Down
100 changes: 76 additions & 24 deletions test/dest/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -742,14 +742,66 @@
"params": []
},
{
"comment": "/**\n * {@link module:test/sub/NumberStore~NumberStore NumberStore}\n */",
"comment": "/** @type {'&' | \"|\" & '?'} */",
"meta": {
"range": [
1424,
1456
1462
],
"filename": "index.js",
"lineno": 67,
"lineno": 65,
"columnno": 0,
"code": {
"name": "exports.unionIntersections",
"type": "VariableDeclaration"
}
},
"type": {
"names": [
"'&'",
"\"|\"",
"'?'"
]
},
"name": "unionIntersections",
"longname": "module:test.unionIntersections",
"kind": "constant",
"memberof": "module:test",
"scope": "static"
},
{
"comment": "",
"meta": {
"range": [
1437,
1461
],
"filename": "index.js",
"lineno": 65,
"columnno": 13,
"code": {
"name": "unionIntersections",
"type": "Literal",
"value": "&"
}
},
"undocumented": true,
"name": "unionIntersections",
"longname": "module:test~unionIntersections",
"kind": "constant",
"scope": "inner",
"memberof": "module:test",
"params": []
},
{
"comment": "/**\n * {@link module:test/sub/NumberStore~NumberStore NumberStore}\n */",
"meta": {
"range": [
1495,
1527
],
"filename": "index.js",
"lineno": 70,
"columnno": 0,
"code": {
"name": "exports.Link",
Expand All @@ -767,11 +819,11 @@
"comment": "",
"meta": {
"range": [
1437,
1455
1508,
1526
],
"filename": "index.js",
"lineno": 67,
"lineno": 70,
"columnno": 13,
"code": {
"name": "Link",
Expand All @@ -791,11 +843,11 @@
"comment": "/**\n * {@link module:test/sub/NumberStore~NumberStore Num}\n */",
"meta": {
"range": [
1493,
1533
1564,
1604
],
"filename": "index.js",
"lineno": 72,
"lineno": 75,
"columnno": 0,
"code": {
"name": "exports.LinkWithText",
Expand All @@ -813,11 +865,11 @@
"comment": "",
"meta": {
"range": [
1506,
1532
1577,
1603
],
"filename": "index.js",
"lineno": 72,
"lineno": 75,
"columnno": 13,
"code": {
"name": "LinkWithText",
Expand All @@ -837,11 +889,11 @@
"comment": "/**\n * {@link module:test/sub/NumberStore~NumberStore NumberStore.getNumber}\n */",
"meta": {
"range": [
1576,
1626
1647,
1697
],
"filename": "index.js",
"lineno": 77,
"lineno": 80,
"columnno": 0,
"code": {
"name": "exports.LinkWithMemberAccessor",
Expand All @@ -859,11 +911,11 @@
"comment": "",
"meta": {
"range": [
1589,
1625
1660,
1696
],
"filename": "index.js",
"lineno": 77,
"lineno": 80,
"columnno": 13,
"code": {
"name": "LinkWithMemberAccessor",
Expand All @@ -883,11 +935,11 @@
"comment": "/**\n * {@link module:test/sub/NumberStore~NumberStore Num}\n */",
"meta": {
"range": [
1673,
1730
1744,
1801
],
"filename": "index.js",
"lineno": 82,
"lineno": 85,
"columnno": 0,
"code": {
"name": "exports.LinkWithMemberAccessorAndText",
Expand All @@ -905,11 +957,11 @@
"comment": "",
"meta": {
"range": [
1686,
1729
1757,
1800
],
"filename": "index.js",
"lineno": 82,
"lineno": 85,
"columnno": 13,
"code": {
"name": "LinkWithMemberAccessorAndText",
Expand Down
3 changes: 3 additions & 0 deletions test/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ export const bracketNotation = 1;
/** @type {[number, [number, string], "[number, boolean]"]} */
export const nesteedTuples = [1, [1, 'a'], '[number, boolean]'];

/** @type {'&' | "|" & '?'} */
export const unionIntersections = '&';

/**
* {@link NumberStore}
*/
Expand Down