Skip to content

Commit

Permalink
Convert tuples to simple Array
Browse files Browse the repository at this point in the history
  • Loading branch information
ahocevar committed Oct 12, 2024
1 parent 187b3ab commit 1f59187
Show file tree
Hide file tree
Showing 4 changed files with 281 additions and 30 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,21 @@ To:
*/
```

### Tuples

```js
/**
* @type {[string, number]}
*/
```

To:
```js
/**
* @type {Array}
*/
```

## Module id resolution

For resolving module ids, this plugin mirrors the method used by JSDoc:
Expand Down
21 changes: 20 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const noClassdescRegEx = /@(typedef|module|type)/;
const extensionReplaceRegEx = /\.m?js$/;
const extensionEnsureRegEx = /(\.js)?$/;
const slashRegEx = /\\/g;

const variableNameRegEx = /^[a-zA-Z_$][0-9a-zA-Z_$]*$/;
const moduleInfos = {};
const fileNodes = {};
const resolvedPathCache = new Set();
Expand Down Expand Up @@ -228,6 +228,7 @@ exports.defineTags = function (dictionary) {
/** @type {Array<[number, number, string]>} */
let replacements = [];
let openCurly = 0;
let openSquare = 0;
let openRound = 0;
let isWithinString = false;
let quoteChar = '';
Expand Down Expand Up @@ -295,6 +296,24 @@ exports.defineTags = function (dictionary) {
functionStartIndex = null;
}

break;
case '[':
if (
isWithinString ||
variableNameRegEx.test(tagText.charAt(i - 1))
) {
break;
}
++openSquare;
break;
case ']':
if (isWithinString) {
break;
}
if (!--openSquare) {
// Replace [type1, type2] tuples with Array
replacements.push([startIndex + 1, i + 1, 'Array']);
}
break;
case '{':
++openCurly;
Expand Down
Loading

0 comments on commit 1f59187

Please sign in to comment.