|
| 1 | +# What the Parser does |
| 2 | + |
| 3 | +The main thing this parser does is parsing the `*.svelte` file and return an AST that can be parsed by ESLint. |
| 4 | +However, this parser does a few other things for a better experience with ESLint integration. |
| 5 | + |
| 6 | +## Entry Point |
| 7 | + |
| 8 | +This parser parses `*.svelte` via `parseForESLint()` and returns the result to ESLint. This is a requirement for ESLint's custom parser. |
| 9 | + |
| 10 | +See https://eslint.org/docs/latest/developer-guide/working-with-custom-parsers. |
| 11 | + |
| 12 | +## Results |
| 13 | + |
| 14 | +### `ast` |
| 15 | + |
| 16 | +Returns the AST of the parses result. |
| 17 | + |
| 18 | +Script AST is [ESTree] compliant AST by default. However, if you used `@typescript-eslint/parser` as script parser, it may contain [TypeScript AST](https://github.com/typescript-eslint/typescript-eslint/tree/main/packages/ast-spec). |
| 19 | +However, the parser assigns a special node `SvelteReactiveStatement` to the parsed result of `$:`. |
| 20 | +`SvelteReactiveStatement` is a special node to avoid confusing ESLint check rules with `LabeledStatement`. |
| 21 | + |
| 22 | +[ESTree]: https://github.com/estree/estree |
| 23 | + |
| 24 | +The HTML template part returns a special AST. See [AST.md](./AST.md). |
| 25 | + |
| 26 | +The `Program` node contains `tokens` and `comments`. This is a requirement for ESLint's custom parser. |
| 27 | + |
| 28 | +See https://eslint.org/docs/latest/developer-guide/working-with-custom-parsers#the-ast-specification. |
| 29 | + |
| 30 | +### `services` |
| 31 | + |
| 32 | +This parser returns the `services` returned by the script parser. |
| 33 | +In particular, typescript-eslint contains important information such as type information in `services`. The parser does not edit the `services`, but there is a trick in parsing the script to get the correct result of the `services` returned by the script parser. |
| 34 | + |
| 35 | +When parsing the script, the parser does not pass only the `<script>` part, but generates virtual script code including the script that converted the HTML template into a script, and lets the script parser parse it. |
| 36 | + |
| 37 | +For example, if you enter `*.svelte` template to listen for input events: |
| 38 | + |
| 39 | +```svelte |
| 40 | +<script lang="ts"> |
| 41 | + function inputHandler () { |
| 42 | + // process |
| 43 | + } |
| 44 | +</script> |
| 45 | +<input on:input={inputHandler}> |
| 46 | +``` |
| 47 | + |
| 48 | +Parse the following virtual script code as a script: |
| 49 | + |
| 50 | +```ts |
| 51 | + |
| 52 | + function inputHandler () { |
| 53 | + // process |
| 54 | + } |
| 55 | +; |
| 56 | + |
| 57 | +(inputHandler)as ((e:'input' extends keyof HTMLElementEventMap?HTMLElementEventMap['input']:CustomEvent<any>)=>void); |
| 58 | +``` |
| 59 | + |
| 60 | +This gives the correct type information to the inputHandler when used with `on:input={inputHandler}`. |
| 61 | + |
| 62 | +The script AST for the HTML template is then remapped to the template AST. |
| 63 | + |
| 64 | +You can check what happens to virtual scripts in the Online Demo. |
| 65 | +https://ota-meshi.github.io/svelte-eslint-parser/virtual-script-code/ |
| 66 | + |
| 67 | +### `scopeManager` |
| 68 | + |
| 69 | +This parser returns a ScopeManager instance. |
| 70 | +ScopeManager is used in variable analysis such as [no-unused-vars](https://eslint.org/docs/latest/rules/no-unused-vars) and [no-undef](https://eslint.org/docs/latest/rules/no-undef) rules. |
| 71 | +See https://eslint.org/docs/latest/developer-guide/scope-manager-interface for details. |
| 72 | + |
| 73 | +The parser will generate a virtual script so that it can parse the correct scope. |
| 74 | +For example, when using `{#each}` and `{@const}`: |
| 75 | + |
| 76 | +```svelte |
| 77 | +<script lang="ts"> |
| 78 | + const array = [1, 2, 3] |
| 79 | +</script> |
| 80 | +{#each array as e} |
| 81 | + {@const ee = e * 2} |
| 82 | + {ee} |
| 83 | +{/each} |
| 84 | +``` |
| 85 | + |
| 86 | +Parse the following virtual script code as a script: |
| 87 | + |
| 88 | +```ts |
| 89 | + |
| 90 | + const array = [1, 2, 3] |
| 91 | +; |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | +Array.from(array).forEach((e)=>{const ee = e * 2;(ee);}); |
| 98 | +``` |
| 99 | + |
| 100 | +This ensures that the variable `e` defined by `{#each}` is correctly scoped only within `{#each}`. |
| 101 | + |
| 102 | +Also, this parser returns special results for variables used in `$: foo = expression` and `$count` for proper analysis. |
| 103 | + |
| 104 | +It also adds virtual references for variables that are marked specially used in `*.svelte` (e.g. `export let` and `$ref`). This is a hack that is also used in typescript-eslint. |
| 105 | +https://github.com/typescript-eslint/typescript-eslint/issues/4508#issuecomment-1030508403 |
| 106 | + |
| 107 | +You can also check the results [Online DEMO](https://ota-meshi.github.io/svelte-eslint-parser/). |
| 108 | + |
| 109 | +### `visitorKeys` |
| 110 | + |
| 111 | +ESLint custom parsers that provide their own AST require `visitorKeys` to properly traverse the node. |
| 112 | + |
| 113 | +See https://eslint.org/docs/latest/developer-guide/working-with-custom-parsers. |
0 commit comments