Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@es-joy/jsdoccomment

Package Overview
Dependencies
Maintainers
1
Versions
101
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@es-joy/jsdoccomment - npm Package Compare versions

Comparing version 0.17.0 to 0.18.0

19

CHANGES.md
# CHANGES for `@es-joy/jsdoccomment`
## 0.18.0
### User-impacting
- feat: add non-visitable `endLine` property (so can detect line number
when no description present)
- feat: supply `indent` default for `parseComment`
- fix: ensure `postName` gets a space for `@template` with a description
- fix: converting JSDoc comment with tag on same line as end (e.g., single
line) to AST
- chore: update `jsdoc-type-pratt-parser`
### Dev-impacting
- docs: add jsdoc blocks internally
- chore: update devDeps.
- test: avoid need for `expect`
- test: complete coverage for `commentHandler`, `parseComment` tests
## 0.17.0

@@ -4,0 +23,0 @@

22

package.json
{
"name": "@es-joy/jsdoccomment",
"version": "0.17.0",
"version": "0.18.0",
"author": "Brett Zamir <brettz9@yahoo.com>",

@@ -42,8 +42,8 @@ "contributors": [],

"esquery": "^1.4.0",
"jsdoc-type-pratt-parser": "~2.2.1"
"jsdoc-type-pratt-parser": "~2.2.2"
},
"devDependencies": {
"@babel/core": "^7.16.7",
"@babel/core": "^7.16.12",
"@babel/plugin-syntax-class-properties": "^7.12.13",
"@babel/preset-env": "^7.16.7",
"@babel/preset-env": "^7.16.11",
"@brettz9/eslint-plugin": "^1.0.4",

@@ -53,3 +53,3 @@ "@rollup/plugin-babel": "^5.3.0",

"chai": "^4.3.4",
"eslint": "^8.6.0",
"eslint": "^8.7.0",
"eslint-config-ash-nazg": "32.3.0",

@@ -62,3 +62,3 @@ "eslint-config-standard": "^16.0.3",

"eslint-plugin-import": "^2.25.4",
"eslint-plugin-jsdoc": "^37.6.0",
"eslint-plugin-jsdoc": "^37.6.3",
"eslint-plugin-markdown": "^2.2.1",

@@ -70,5 +70,5 @@ "eslint-plugin-no-unsanitized": "^4.0.1",

"eslint-plugin-sonarjs": "^0.11.0",
"eslint-plugin-unicorn": "^40.0.0",
"mocha": "^9.1.3",
"rollup": "^2.63.0"
"eslint-plugin-unicorn": "^40.1.0",
"mocha": "^9.2.0",
"rollup": "^2.66.0"
},

@@ -79,7 +79,7 @@ "scripts": {

"lint": "npm run eslint",
"mocha": "mocha --parallel",
"mocha": "mocha --parallel --require chai/register-expect",
"c8": "c8 npm run mocha",
"test": "npm run lint && npm run rollup && npm run c8"
},
"readme": "# @es-joy/jsdoccomment\n\n[![Node.js CI status](https://github.com/brettz9/getJSDocComment/workflows/Node.js%20CI/badge.svg)](https://github.com/brettz9/getJSDocComment/actions)\n\nThis project aims to preserve and expand upon the\n`SourceCode#getJSDocComment` functionality of the deprecated ESLint method.\n\nIt also exports a number of functions currently for working with JSDoc:\n\n## API\n\n### `parseComment`\n\nFor parsing `comment-parser` in a JSDoc-specific manner.\nMight wish to have tags with or without tags, etc. derived from a split off\nJSON file.\n\n### `commentParserToESTree`\n\nConverts [comment-parser](https://github.com/syavorsky/comment-parser)\nAST to ESTree/ESLint/Babel friendly AST. See the \"ESLint AST...\" section below.\n\n### `jsdocVisitorKeys`\n\nThe [VisitorKeys](https://github.com/eslint/eslint-visitor-keys)\nfor `JsdocBlock`, `JsdocDescriptionLine`, and `JsdocTag`. More likely to be\nsubject to change or dropped in favor of another type parser.\n\n### `jsdocTypeVisitorKeys`\n\nJust a re-export of [VisitorKeys](https://github.com/eslint/eslint-visitor-keys)\nfrom [`jsdoc-type-pratt-parser`](https://github.com/simonseyock/jsdoc-type-pratt-parser/).\n\n### `getDefaultTagStructureForMode`\n\nProvides info on JSDoc tags:\n\n- `nameContents` ('namepath-referencing'|'namepath-defining'|\n 'dual-namepath-referencing'|false) - Whether and how a name is allowed\n following any type. Tags without a proper name (value `false`) may still\n have a description (which can appear like a name); `descriptionAllowed`\n in such cases would be `true`.\n The presence of a truthy `nameContents` value is therefore only intended\n to signify whether separate parsing should occur for a name vs. a\n description, and what its nature should be.\n- `nameRequired` (boolean) - Whether a name must be present following any type.\n- `descriptionAllowed` (boolean) - Whether a description (following any name)\n is allowed.\n- `typeAllowed` (boolean) - Whether the tag accepts a curly bracketed portion.\n Even without a type, a tag may still have a name and/or description.\n- `typeRequired` (boolean) - Whether a curly bracketed type must be present.\n- `typeOrNameRequired` (boolean) - Whether either a curly bracketed type is\n required or a name, but not necessarily both.\n\n### Miscellaneous\n\nAlso currently exports these utilities, though they might be removed in the\nfuture:\n\n- `getTokenizers` - Used with `parseComment` (its main core)\n- `toCamelCase` - Convert to CamelCase.\n- `hasSeeWithLink` - A utility to detect if a tag is `@see` and has a `@link`\n- `commentHandler` - Used by `eslint-plugin-jsdoc`. Might be removed in future.\n- `commentParserToESTree`- Converts [comment-parser](https://github.com/syavorsky/comment-parser)\n AST to ESTree/ESLint/Babel friendly AST\n- `jsdocVisitorKeys` - The [VisitorKeys](https://github.com/eslint/eslint-visitor-keys)\n for `JSDocBlock`, `JSDocDescriptionLine`, and `JSDocTag`. Might change.\n- `jsdocTypeVisitorKeys` - [VisitorKeys](https://github.com/eslint/eslint-visitor-keys)\n for `jsdoc-type-pratt-parser`.\n- `getTokenizers` - A utility. Might be removed in future.\n- `toCamelCase` - A utility. Might be removed in future.\n- `hasSeeWithLink` - A utility to detect if a tag is `@see` and has a `@link`\n- `defaultNoTypes` = The tags which allow no types by default:\n `default`, `defaultvalue`, `see`;\n- `defaultNoNames` - The tags which allow no names by default:\n `access`, `author`, `default`, `defaultvalue`, `description`, `example`,\n `exception`, `license`, `return`, `returns`, `since`, `summary`, `throws`,\n `version`, `variation`\n\n## ESLint AST produced for `comment-parser` nodes (`JsdocBlock`, `JsdocTag`, and `JsdocDescriptionLine`)\n\nNote: Although not added in this package, `@es-joy/jsdoc-eslint-parser` adds\na `jsdoc` property to other ES nodes (using this project's `getJSDocComment`\nto determine the specific comment-block that will be attached as AST).\n\n### `JsdocBlock`\n\nHas two visitable properties:\n\n1. `tags` (an array of `JsdocTag`; see below)\n2. `descriptionLines` (an array of `JsdocDescriptionLine` for multiline\n descriptions).\n\nHas the following custom non-visitable property:\n\n1. `lastDescriptionLine` - A number\n\nMay also have the following non-visitable properties from `comment-parser`:\n\n1. `description` - Same as `descriptionLines` but as a string with newlines.\n2. `delimiter`\n3. `postDelimiter`\n4. `end`\n\n### `JsdocTag`\n\nHas three visitable properties:\n\n1. `parsedType` (the `jsdoc-type-pratt-parser` AST representation of the tag's\n type (see the `jsdoc-type-pratt-parser` section below)).\n2. `descriptionLines`' (an array of `JsdocDescriptionLine` for multiline\n descriptions)\n3. `typeLines` (an array of `JsdocTypeLine` for multiline type strings)\n\nMay also have the following non-visitable properties from `comment-parser`\n(note that all are included from `comment-parser` except `end` as that is only\nfor JSDoc blocks and note that `type` is renamed to `rawType`):\n\n1. `description` - Same as `descriptionLines` but as a string with newlines.\n2. `rawType` - `comment-parser` has this named as `type`, but because of a\n conflict with ESTree using `type` for Node type, we renamed it to\n `rawType`. It is otherwise the same as in `comment-parser`, i.e., a string\n with newlines, though with the initial `{` and final `}` stripped out.\n See `typeLines` for the array version of this property.\n3. `start`\n4. `delimiter`\n5. `postDelimiter`\n6. `tag` (this does differ from `comment-parser` now in terms of our stripping\n the initial `@`)\n7. `postTag`\n8. `name`\n9. `postName`\n10. `type`\n11. `postType`\n\n### `JsdocDescriptionLine`\n\nNo visitable properties.\n\nMay also have the following non-visitable properties from `comment-parser`:\n\n1. `delimiter`\n2. `postDelimiter`\n3. `start`\n4. `description`\n\n### `JsdocTypeLine`\n\nNo visitable properties.\n\nMay also have the following non-visitable properties from `comment-parser`:\n\n1. `delimiter`\n2. `postDelimiter`\n3. `start`\n4. `rawType` - Renamed from `comment-parser` to avoid a conflict. See\n explanation under `JsdocTag`\n\n## ESLint AST produced for `jsdoc-type-pratt-parser`\n\nThe AST, including `type`, remains as is from [jsdoc-type-pratt-parser](https://github.com/simonseyock/jsdoc-type-pratt-parser/).\n\nThe type will always begin with a `JsdocType` prefix added, along with a\ncamel-cased type name, e.g., `JsdocTypeUnion`.\n\nThe `jsdoc-type-pratt-parser` visitor keys are also preserved without change.\n\n## Installation\n\n```shell\nnpm i @es-joy/jsdoccomment\n```\n\n## Changelog\n\nThe changelog can be found on the [CHANGES.md](./CHANGES.md).\n<!--## Contributing\n\nEveryone is welcome to contribute. Please take a moment to review the [contributing guidelines](CONTRIBUTING.md).\n-->\n## Authors and license\n\n[Brett Zamir](http://brett-zamir.me/) and\n[contributors](https://github.com/es-joy/jsdoc-eslint-parser/graphs/contributors).\n\nMIT License, see the included [LICENSE-MIT.txt](LICENSE-MIT.txt) file.\n\n## To-dos\n\n1. Get complete code coverage\n"
"readme": "# @es-joy/jsdoccomment\n\n[![Node.js CI status](https://github.com/brettz9/getJSDocComment/workflows/Node.js%20CI/badge.svg)](https://github.com/brettz9/getJSDocComment/actions)\n\nThis project aims to preserve and expand upon the\n`SourceCode#getJSDocComment` functionality of the deprecated ESLint method.\n\nIt also exports a number of functions currently for working with JSDoc:\n\n## API\n\n### `parseComment`\n\nFor parsing `comment-parser` in a JSDoc-specific manner.\nMight wish to have tags with or without tags, etc. derived from a split off\nJSON file.\n\n### `commentParserToESTree`\n\nConverts [comment-parser](https://github.com/syavorsky/comment-parser)\nAST to ESTree/ESLint/Babel friendly AST. See the \"ESLint AST...\" section below.\n\n### `jsdocVisitorKeys`\n\nThe [VisitorKeys](https://github.com/eslint/eslint-visitor-keys)\nfor `JsdocBlock`, `JsdocDescriptionLine`, and `JsdocTag`. More likely to be\nsubject to change or dropped in favor of another type parser.\n\n### `jsdocTypeVisitorKeys`\n\nJust a re-export of [VisitorKeys](https://github.com/eslint/eslint-visitor-keys)\nfrom [`jsdoc-type-pratt-parser`](https://github.com/simonseyock/jsdoc-type-pratt-parser/).\n\n### `getDefaultTagStructureForMode`\n\nProvides info on JSDoc tags:\n\n- `nameContents` ('namepath-referencing'|'namepath-defining'|\n 'dual-namepath-referencing'|false) - Whether and how a name is allowed\n following any type. Tags without a proper name (value `false`) may still\n have a description (which can appear like a name); `descriptionAllowed`\n in such cases would be `true`.\n The presence of a truthy `nameContents` value is therefore only intended\n to signify whether separate parsing should occur for a name vs. a\n description, and what its nature should be.\n- `nameRequired` (boolean) - Whether a name must be present following any type.\n- `descriptionAllowed` (boolean) - Whether a description (following any name)\n is allowed.\n- `typeAllowed` (boolean) - Whether the tag accepts a curly bracketed portion.\n Even without a type, a tag may still have a name and/or description.\n- `typeRequired` (boolean) - Whether a curly bracketed type must be present.\n- `typeOrNameRequired` (boolean) - Whether either a curly bracketed type is\n required or a name, but not necessarily both.\n\n### Miscellaneous\n\nAlso currently exports these utilities, though they might be removed in the\nfuture:\n\n- `getTokenizers` - Used with `parseComment` (its main core)\n- `toCamelCase` - Convert to CamelCase.\n- `hasSeeWithLink` - A utility to detect if a tag is `@see` and has a `@link`\n- `commentHandler` - Used by `eslint-plugin-jsdoc`. Might be removed in future.\n- `commentParserToESTree`- Converts [comment-parser](https://github.com/syavorsky/comment-parser)\n AST to ESTree/ESLint/Babel friendly AST\n- `jsdocVisitorKeys` - The [VisitorKeys](https://github.com/eslint/eslint-visitor-keys)\n for `JSDocBlock`, `JSDocDescriptionLine`, and `JSDocTag`. Might change.\n- `jsdocTypeVisitorKeys` - [VisitorKeys](https://github.com/eslint/eslint-visitor-keys)\n for `jsdoc-type-pratt-parser`.\n- `getTokenizers` - A utility. Might be removed in future.\n- `toCamelCase` - A utility. Might be removed in future.\n- `hasSeeWithLink` - A utility to detect if a tag is `@see` and has a `@link`\n- `defaultNoTypes` = The tags which allow no types by default:\n `default`, `defaultvalue`, `see`;\n- `defaultNoNames` - The tags which allow no names by default:\n `access`, `author`, `default`, `defaultvalue`, `description`, `example`,\n `exception`, `license`, `return`, `returns`, `since`, `summary`, `throws`,\n `version`, `variation`\n\n## ESLint AST produced for `comment-parser` nodes (`JsdocBlock`, `JsdocTag`, and `JsdocDescriptionLine`)\n\nNote: Although not added in this package, `@es-joy/jsdoc-eslint-parser` adds\na `jsdoc` property to other ES nodes (using this project's `getJSDocComment`\nto determine the specific comment-block that will be attached as AST).\n\n### `JsdocBlock`\n\nHas two visitable properties:\n\n1. `tags` (an array of `JsdocTag`; see below)\n2. `descriptionLines` (an array of `JsdocDescriptionLine` for multiline\n descriptions).\n\nHas the following custom non-visitable property:\n\n1. `lastDescriptionLine` - A number\n2. `endLine` - A number representing the line number with `end`\n\nMay also have the following non-visitable properties from `comment-parser`:\n\n1. `description` - Same as `descriptionLines` but as a string with newlines.\n2. `delimiter`\n3. `postDelimiter`\n4. `lineEnd`\n5. `end`\n\n### `JsdocTag`\n\nHas three visitable properties:\n\n1. `parsedType` (the `jsdoc-type-pratt-parser` AST representation of the tag's\n type (see the `jsdoc-type-pratt-parser` section below)).\n2. `descriptionLines` (an array of `JsdocDescriptionLine` for multiline\n descriptions)\n3. `typeLines` (an array of `JsdocTypeLine` for multiline type strings)\n\nMay also have the following non-visitable properties from `comment-parser`\n(note that all are included from `comment-parser` except `end` as that is only\nfor JSDoc blocks and note that `type` is renamed to `rawType`):\n\n1. `description` - Same as `descriptionLines` but as a string with newlines.\n2. `rawType` - `comment-parser` has this named as `type`, but because of a\n conflict with ESTree using `type` for Node type, we renamed it to\n `rawType`. It is otherwise the same as in `comment-parser`, i.e., a string\n with newlines, though with the initial `{` and final `}` stripped out.\n See `typeLines` for the array version of this property.\n3. `start`\n4. `delimiter`\n5. `postDelimiter`\n6. `tag` (this does differ from `comment-parser` now in terms of our stripping\n the initial `@`)\n7. `postTag`\n8. `name`\n9. `postName`\n10. `postType`\n\n### `JsdocDescriptionLine`\n\nNo visitable properties.\n\nMay also have the following non-visitable properties from `comment-parser`:\n\n1. `delimiter`\n2. `postDelimiter`\n3. `start`\n4. `description`\n\n### `JsdocTypeLine`\n\nNo visitable properties.\n\nMay also have the following non-visitable properties from `comment-parser`:\n\n1. `delimiter`\n2. `postDelimiter`\n3. `start`\n4. `rawType` - Renamed from `comment-parser` to avoid a conflict. See\n explanation under `JsdocTag`\n\n## ESLint AST produced for `jsdoc-type-pratt-parser`\n\nThe AST, including `type`, remains as is from [jsdoc-type-pratt-parser](https://github.com/simonseyock/jsdoc-type-pratt-parser/).\n\nThe type will always begin with a `JsdocType` prefix added, along with a\ncamel-cased type name, e.g., `JsdocTypeUnion`.\n\nThe `jsdoc-type-pratt-parser` visitor keys are also preserved without change.\n\n## Installation\n\n```shell\nnpm i @es-joy/jsdoccomment\n```\n\n## Changelog\n\nThe changelog can be found on the [CHANGES.md](./CHANGES.md).\n<!--## Contributing\n\nEveryone is welcome to contribute. Please take a moment to review the [contributing guidelines](CONTRIBUTING.md).\n-->\n## Authors and license\n\n[Brett Zamir](http://brett-zamir.me/) and\n[contributors](https://github.com/es-joy/jsdoc-eslint-parser/graphs/contributors).\n\nMIT License, see the included [LICENSE-MIT.txt](LICENSE-MIT.txt) file.\n\n## To-dos\n\n1. Get complete code coverage\n"
}

@@ -97,2 +97,3 @@ # @es-joy/jsdoccomment

1. `lastDescriptionLine` - A number
2. `endLine` - A number representing the line number with `end`

@@ -104,3 +105,4 @@ May also have the following non-visitable properties from `comment-parser`:

3. `postDelimiter`
4. `end`
4. `lineEnd`
5. `end`

@@ -113,3 +115,3 @@ ### `JsdocTag`

type (see the `jsdoc-type-pratt-parser` section below)).
2. `descriptionLines`' (an array of `JsdocDescriptionLine` for multiline
2. `descriptionLines` (an array of `JsdocDescriptionLine` for multiline
descriptions)

@@ -136,4 +138,3 @@ 3. `typeLines` (an array of `JsdocTypeLine` for multiline type strings)

9. `postName`
10. `type`
11. `postType`
10. `postType`

@@ -140,0 +141,0 @@ ### `JsdocDescriptionLine`

import {parse as jsdocTypePrattParse} from 'jsdoc-type-pratt-parser';
/**
* Removes initial and ending brackets from `rawType`
* @param {JsdocTypeLine[]|JsdocTag} container
* @param {boolean} isArr
* @returns {void}
*/
const stripEncapsulatingBrackets = (container, isArr) => {

@@ -20,3 +26,94 @@ if (isArr) {

/**
* Strips brackets from a tag's `rawType` values and adds `parsedType`
* @param {JsdocTag} lastTag
* @param {external:JsdocTypePrattParserMode} mode
* @returns {void}
*/
const cleanUpLastTag = (lastTag, mode) => {
// Strip out `}` that encapsulates and is not part of
// the type
stripEncapsulatingBrackets(lastTag);
if (lastTag.typeLines.length) {
stripEncapsulatingBrackets(lastTag.typeLines, true);
}
// With even a multiline type now in full, add parsing
let parsedType = null;
try {
parsedType = jsdocTypePrattParse(lastTag.rawType, mode);
} catch {
// Ignore
}
lastTag.parsedType = parsedType;
};
/**
* @external CommentParserJsdoc
*/
/**
* @external JsdocTypePrattParserMode
*/
/**
* @typedef {{
* delimiter: string,
* postDelimiter: string,
* rawType: string,
* start: string,
* type: "JsdocTypeLine"
* }} JsdocTypeLine
*/
/**
* @typedef {{
* delimiter: string,
* description: string,
* postDelimiter: string,
* start: string,
* type: "JsdocDescriptionLine"
* }} JsdocDescriptionLine
*/
/**
* @typedef {{
* delimiter: string,
* description: string,
* postDelimiter: string,
* start: string,
* tag: string,
* end: string,
* type: string,
* descriptionLines: JsdocDescriptionLine[],
* rawType: string,
* type: "JsdocTag",
* typeLines: JsdocTypeLine[]
* }} JsdocTag
*/
/**
* @typedef {{
* delimiter: string,
* description: string,
* descriptionLines: JsdocDescriptionLine[],
* end: string,
* postDelimiter: string,
* lineEnd: string,
* type: "JsdocBlock",
* lastDescriptionLine: Integer,
* tags: JsdocTag[]
* }} JsdocBlock
*/
/**
*
* @param {external:CommentParserJsdoc} jsdoc
* @param {external:JsdocTypePrattParserMode} mode
* @returns {JsdocBlock}
*/
const commentParserToESTree = (jsdoc, mode) => {
const {source} = jsdoc;
const {tokens: {

@@ -28,3 +125,4 @@ delimiter: delimiterRoot,

description: descriptionRoot
}} = jsdoc.source[0];
}} = source[0];
const ast = {

@@ -38,2 +136,3 @@ delimiter: delimiterRoot,

end: endRoot,
endLine: source.length - 1,
postDelimiter: postDelimiterRoot,

@@ -48,3 +147,4 @@ lineEnd: lineEndRoot,

let lastTag = null;
jsdoc.source.slice(1).forEach((info, idx) => {
source.forEach((info, idx) => {
const {tokens} = info;

@@ -68,21 +168,9 @@ const {

if (lastTag) {
// Strip out `}` that encapsulates and is not part of
// the type
stripEncapsulatingBrackets(lastTag);
if (lastTag.typeLines.length) {
stripEncapsulatingBrackets(lastTag.typeLines, true);
}
// With even a multiline type now in full, add parsing
let parsedType = null;
try {
parsedType = jsdocTypePrattParse(lastTag.rawType, mode);
} catch {
// Ignore
}
lastTag.parsedType = parsedType;
cleanUpLastTag(lastTag, mode);
}
if (end) {
// Stop the iteration when we reach the end
// but only when there is no tag earlier in the line
// to still process
if (end && !tag) {
ast.end = end;

@@ -139,2 +227,9 @@

}
// Clean-up where last line itself has tag content
if (end && tag) {
ast.end = end;
cleanUpLastTag(lastTag, mode);
}
});

@@ -141,0 +236,0 @@

@@ -65,3 +65,3 @@ /* eslint-disable prefer-named-capture-group -- Temporary */

const name = pos === -1 ? remainder : remainder.slice(0, pos);
const extra = remainder.slice(pos + 1);
const extra = remainder.slice(pos);
let postName = '', description = '', lineEnd = '';

@@ -100,6 +100,6 @@ if (pos > -1) {

* @param {PlainObject} commentNode
* @param {string} indent Whitespace
* @param {string} [indent=""] Whitespace
* @returns {PlainObject}
*/
const parseComment = (commentNode, indent) => {
const parseComment = (commentNode, indent = '') => {
// Preserve JSDoc block start/end indentation.

@@ -106,0 +106,0 @@ return commentParser(`/*${commentNode.value}*/`, {

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc