|
| 1 | +## Advanced |
| 2 | + |
| 3 | +{"gitdown": "contents", "maxLevel": 6, "rootId": "advanced"} |
| 4 | + |
| 5 | +### AST and Selectors |
| 6 | + |
| 7 | +For various rules, one can add to the environments to which the rule applies |
| 8 | +by using the `contexts` option. |
| 9 | + |
| 10 | +This option works with [ESLint's selectors](https://eslint.org/docs/developer-guide/selectors) which are [esquery](https://github.com/estools/esquery/#readme) |
| 11 | +expressions one may use to target a specific node type or types, including |
| 12 | +subsets of the type(s) such as nodes with certain children or attributes. |
| 13 | + |
| 14 | +These expressions are used within ESLint plugins to find those parts of |
| 15 | +your files' code which are of interest to check. However, in |
| 16 | +`eslint-plugin-jsdoc`, we also allow you to use these selectors to define |
| 17 | +additional contexts where you wish our own rules to be applied. |
| 18 | + |
| 19 | +#### `contexts` format |
| 20 | + |
| 21 | +While at their simplest, these can be an array of string selectors, one can |
| 22 | +also supply an object with `context` (in place of the string) and one of two |
| 23 | +properties: |
| 24 | + |
| 25 | +1. For `require-jsdoc`, there are also `inlineCommentBlock` and |
| 26 | + `minLineCount` properties. See that rule for details. |
| 27 | +1. For `no-missing-syntax` and `no-restricted-syntax`, there is also a |
| 28 | + `message` property which allows customization of the message to be shown |
| 29 | + when the rule is triggered. |
| 30 | +1. For `no-missing-syntax`, there is also a `minimum` property. See that rule. |
| 31 | +1. For other rules, there is a `comment` property which adds to the `context` |
| 32 | + in requiring that the `comment` AST condition is also met, e.g., to |
| 33 | + require that certain tags are present and/or or types and type operators |
| 34 | + are in use. Note that this AST (either for `Jsdoc*` or `JsdocType*` AST) |
| 35 | + has not been standardized and should be considered experimental. |
| 36 | + Note that this property might also become obsolete if parsers begin to |
| 37 | + include JSDoc-structured AST. A |
| 38 | + [parser](https://github.com/brettz9/jsdoc-eslint-parser/) is available |
| 39 | + which aims to support comment AST as |
| 40 | + a first class citizen where comment/comment types can be used anywhere |
| 41 | + within a normal AST selector but this should only be considered |
| 42 | + experimental. When using such a parser, you need not use `comment` and |
| 43 | + can just use a plain string context. The determination of the node on |
| 44 | + which the comment is attached is based more on actual location than |
| 45 | + semantics (e.g., it will be attached to a `VariableDeclaration` if above |
| 46 | + that rather than to the `FunctionExpression` it is fundamentally |
| 47 | + describing). See |
| 48 | + [@es-joy/jsdoccomment](https://github.com/es-joy/jsdoccomment) |
| 49 | + for the precise structure of the comment (and comment type) nodes. |
| 50 | + |
| 51 | +#### Discovering available AST definitions |
| 52 | + |
| 53 | +To know all of the AST definitions one may target, it will depend on the |
| 54 | +[parser](https://eslint.org/docs/user-guide/configuring#specifying-parser) |
| 55 | +you are using with ESLint (e.g., `espree` is the default parser for ESLint, |
| 56 | +and this follows [EStree AST](https://github.com/estree/estree) but |
| 57 | +to support the the latest experimental features of JavaScript, one may use |
| 58 | +`@babel/eslint-parser` or to be able to have one's rules (including JSDoc rules) |
| 59 | +apply to TypeScript, one may use `@typescript-eslint/parser`, etc. |
| 60 | + |
| 61 | +So you can look up a particular parser to see its rules, e.g., browse through |
| 62 | +the [ESTree docs](https://github.com/estree/estree) as used by Espree or see |
| 63 | +ESLint's [overview of the structure of AST](https://eslint.org/docs/developer-guide/working-with-custom-parsers#the-ast-specification). |
| 64 | + |
| 65 | +However, it can sometimes be even more helpful to get an idea of AST by just |
| 66 | +providing some of your JavaScript to the wonderful |
| 67 | +[AST Explorer](https://astexplorer.net/) tool and see what AST is built out |
| 68 | +of your code. You can set the tool to the specific parser which you are using. |
| 69 | + |
| 70 | +#### Uses/Tips for AST |
| 71 | + |
| 72 | +And if you wish to introspect on the AST of code within your projects, you can |
| 73 | +use [eslint-plugin-query](https://github.com/brettz9/eslint-plugin-query). |
| 74 | +Though it also works as a plugin, you can use it with its own CLI, e.g., |
| 75 | +to search your files for matching esquery selectors, optionally showing |
| 76 | +it as AST JSON. |
| 77 | + |
| 78 | +Tip: If you want to more deeply understand not just the resulting AST tree |
| 79 | +structures for any given code but also the syntax for esquery selectors so |
| 80 | +that you can, for example, find only those nodes with a child of a certain |
| 81 | +type, you can set the "Transform" feature to ESLint and test out |
| 82 | +esquery selectors in place of the selector expression (e.g., replace |
| 83 | +`'VariableDeclaration > VariableDeclarator > Identifier[name="someVar"]'` as |
| 84 | +we have |
| 85 | +[here](https://astexplorer.net/#/gist/71a93130c19599d6f197bddb29c13a59/latest)) |
| 86 | +to the selector you wish so as to get messages reported in the bottom right |
| 87 | +pane which match your [esquery](https://github.com/estools/esquery/#readme) |
| 88 | +selector). |
0 commit comments