Détail du package

@es-joy/jsdoc-eslint-parser

es-joy826MIT0.21.1

Parser to support JavaScript with JSDoc comments and types available as AST

eslint-parser, jsdoc

readme

@es-joy/jsdoc-eslint-parser

This project has not been well-tested and should be considered experimental.

This project aims to allow full @babel/eslint-parser configuration, but with comment-parser-based AST for JSDoc blocks and jsdoctypeparser AST for JSDoc types within those blocks.

There is also a @es-joy/jsdoc-eslint-parser/typescript.js parser which builds on @typescript-eslint/parser.

It is hoped that this may spur development of official ESTree acceptance of such AST.

You can run our lint-test script for an example.

Use cases

  1. Using no-restricted-syntax to prohibit certain comment or comment type AST
  2. Supplying comment or comment-type aware query selectors to contexts in eslint-plugin-jsdoc

Approach for attaching AST

In using the Babel ESLint parser, AST is produced which has the tokens on Program expected by ESLint as well as the range array expected on each Node.

We add comment AST--or, rather, JSDocBlock AST--by adding a jsdoc property to each item of the @babel/eslint-parser-parsed code which does not have a suitable ancestor to hold the comment block. We correspondingly update the visitor keys to ensure that for every key's array of visitable properties, jsdoc is added, so it can be separately traversable. We also ensure the comment-parser and jsdoctypeparser visitor keys are added to our parser's visitor keys.

And we add a parent for each Node so that our getJSDocComment method can find its way around (see below).

The tricky part with the jsdoc property comes in with determining which JSDoc should be associated with a given Node. In jsdoccomment, we have a getJSDocComment method, originally begun by ESLint itself to determine where that should be expected.

This detection of the comment for a given structure is not a trivial matter. For example, with:

/* A */
const /* B */ aFunc = /* C */ function () {};

... for the function expression, we might look for the JSDoc Block at point C or B first, but then if not present, look for it at point A. The getJSDocComment method uses such an algorithm, and we call this in our parser currently on each node. If there is no JSDoc block at point B or C, the node for the aFunc VariableDeclaration node should get the JSDoc Block at point A added instead of the FunctionExpression getting it.

Although abstractly, it may appear a better fit with FunctionExpression, the practical purposes of finding JSDoc blocks relates more to position and code generation, which should be easiest to work with if in a position closest to its actual location.

Note that the AST produced is described in the jsdoccomment project. Additionally, this parser adds to Program nodes, a jsdocBlocks property (corresponding to comments though for unattached comment blocks only), along with a commentsIndex integer proprerty to reflect the index to which this JSDoc block corresponds with the comments array.

Installation

npm i -D @es-joy/jsdoc-eslint-parser

Parser options

mode (Defaults to "jsdoc")

The jsdoctypeparser mode, jsdoc, typescript, closure, or permissive. The latter is not generally recommended.

maxLines (default: 1) and minLines (default: 0)

Determines how far up in terms of lines from a structure, we should allow the finding of comment blocks.

indent (default: "")

This is currently exposed but should probably not be changed and may be removed in the future. It indicates the indent to be used when parsing comments. (That may need to be auto-detected in the future.)

sourceType (defaults to @babel/eslint-parser's default)

For indicating script or module

babelOptions (default: none)

Extra options to pass on to Babel (if not using the typescript parser).

...parserOptions (default: none)

Any other options are passed onto the parser.

Changelog

The changelog can be found on the CHANGES.md. <!--## Contributing

Everyone is welcome to contribute. Please take a moment to review the contributing guidelines. -->

Authors and license

Brett Zamir and contributors.

MIT License, see the included LICENSE-MIT.md file.

changelog

CHANGES for @es-joy/jsdoc-eslint-parser

0.21.1

  • fix: update build files

0.21.0

  • feat: add types
  • chore: update deps. and devDeps.

0.20.0

  • chore: update deps. and devDeps. and lint

0.19.0

  • chore: update es-joy/jsdoccomment (inlineTags support) and devDeps.

0.18.0

  • chore: update babel/core, babel/eslint-parser, es-joy/jsdoccomment, typescript-eslint/parser, eslint, esquery, typescript and devDeps.

0.17.0

  • chore: update babel/core, es-joy/jsdoccomment, typescript-eslint/parser, eslint, typescript and devDeps.

0.16.0

  • chore: bump jsdoccomment

0.15.0

  • chore: bump jsdoccomment

0.14.1

  • chore: bump jsdoccomment

0.14.0

  • chore: bump jsdoccomment
  • chore: update devDeps.
  • test: checks for unattached comment (now that one example is attached)

0.13.0

  • feat: set indent for attached comments based on source context
  • chore: bump jsdoccomment

0.12.0

  • feat: allow for empty options
  • fix: bake in typescript mode to typescript parser
  • fix: avoid jsdoc on Program (had only dropped as a visitor key)
  • chore: bump jsdoccomment

0.11.3

  • chore: update dep. jsdoccomment

0.11.2

  • fix: hide already attached jsdoc from global jsdocBlocks

0.11.1

  • fix: avoid adding jsdocBlocks to any node except Program and drop jsdoc from Program

0.11.0

  • BREAKING CHANGE: chore: update jsdoccomment, typescript-eslint/parser; must now use initial and terminal in place of start and end
  • chore: updates devDeps.

0.10.1

  • fix: ensure TS parser passing in tokens, comment, loc, range, sourceType: module info

0.10.0

  • BREAKING feat: ESM
  • BREAKING fix: avoid setting jsdoc on Program or Jsdoc nodes themselves
  • BREAKING fix: avoid setting jsdoc on any descendants where the ancestor can match
  • fix: avoid attaching jsdoc to jsdoc nodes
  • fix: clone range and SourceLocation rather than reusing
  • chore: fix linting
  • test: Get 100% coverage

0.9.0

  • feat: add commentsIndex integer property
  • chore: update babel/core, typescript-eslint/parser, and eslint; update devDeps.

0.8.2

  • fix: avoid erring out on non-JSDoc result (since comment-parser simplification)
  • chore: update lock file

0.8.1

  • chore: update @es-joy/jsdoccomment (minor), @babel/core (patch), eslint (minor) and devDeps.

0.8.0

  • BREAKING CHANGE: require Node 12
  • feat: support @es-joy/jsdoc-eslint-parser/typescript parser
  • fix: pass on comment visitor keys upon traversal for parent attachment
  • fix: add loc and range (used by ESLint in error reporting)

0.7.2

  • chore: update jsdoccomment

0.7.1

  • chore: update jsdoccomment

0.7.0

  • feat: throwOnTypeParsingErrors option
  • chore: update jsdoccomment and devDeps.

0.6.3

  • fix: attach parent to JSDoc nodes

0.6.2

  • fix: ensure tags, etc. get attached to jsdocBlocks blocks

0.6.1

  • fix: avoid chance for Line comments beginning with asterisk being parsed as JSDoc blocks

0.6.0

  • feat: add loc and range to individual jsdocBlocks

0.5.0

  • feat: support jsdocBlocks property (on Program)
  • fix: Replace any non-array key values with array
  • chore: add .idea to .gitignore
  • chore: Update devDeps.

0.4.1

  • chore: bump @es-joy/jsdoccomment, @babel/core, eslint
  • chore: Update devDeps.

0.4.0

  • npm: Bump dep. @es-joy/jsdoccomment, @babel/core, @babel/eslint-parser, eslint
  • npm: Update devDeps.
  • chore: allow lint script to be passed --fix

0.3.0

  • npm: Bump dep. @es-joy/jsdoccomment, @babel/core, @babel/eslint-parser, eslint
  • npm: Update devDeps.

0.2.0

  • npm: Bump dep. @es-joy/jsdoccomment, @babel/core, @babel/eslint-parser, eslint
  • npm: Update devDeps.

0.1.4

  • npm: Bump dep. @es-joy/jsdoccomment

0.1.3

  • Docs: Refer to jsdoccomment for AST structure

0.1.2

  • Docs: Indicate options and AST approach
  • Refactoring: Comment out unnecessary arguments
  • npm: Update @es-joy/jsdoccomment dep. (just doc changes)

0.1.1

  • Docs: Update

0.1.0

  • Initial version