You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[implemented some rules]: https://sveltejs.github.io/eslint-plugin-svelte/rules/
24
+
The `svelte-eslint-parser` aims to make it easy to create your own ESLint rules for [Svelte](https://svelte.dev/).
25
+
26
+
[eslint-plugin-svelte](https://github.com/sveltejs/eslint-plugin-svelte) is an ESLint plugin built upon this parser, and it already [implements some rules](https://sveltejs.github.io/eslint-plugin-svelte/rules/).
1. Write `parser` option into your ESLint Config file.
51
-
2. Use glob patterns or `--ext .svelte` CLI option.
49
+
## Usage
52
50
53
51
### ESLint Config (`eslint.config.js`)
54
52
55
53
```js
56
54
importjsfrom"@eslint/js";
57
55
importsvelteParserfrom"svelte-eslint-parser";
56
+
58
57
exportdefault [
59
58
js.configs.recommended,
60
59
{
61
-
files: ["**/*.svelte", "*.svelte"],
60
+
files: [
61
+
"**/*.svelte",
62
+
"*.svelte",
63
+
// Need to specify the file extension for Svelte 5 with rune symbols
64
+
"**/*.svelte.js",
65
+
"*.svelte.js",
66
+
"**/*.svelte.ts",
67
+
"*.svelte.ts",
68
+
],
62
69
languageOptions: {
63
70
parser: svelteParser,
64
71
},
@@ -68,23 +75,27 @@ export default [
68
75
69
76
### CLI
70
77
71
-
```console
72
-
$ eslint "src/**/*.{js,svelte}"
73
-
# or
74
-
$ eslint src --ext .svelte
78
+
```bash
79
+
eslint "src/**/*.{js,svelte}"
75
80
```
76
81
77
-
## 🔧 Options
82
+
---
83
+
84
+
## Options
85
+
86
+
The [parserOptions](https://eslint.org/docs/latest/use/configure/parser#configure-parser-options) for this parser generally match what [espree](https://github.com/eslint/espree#usage)—ESLint's default parser—supports.
78
87
79
-
[`parserOptions`] has the same properties as what [espree](https://github.com/eslint/espree#usage), the default parser of ESLint, is supporting.
80
88
For example:
81
89
82
90
```js
83
91
importsvelteParserfrom"svelte-eslint-parser";
92
+
84
93
exportdefault [
85
94
// ...
86
95
{
87
-
files: ["**/*.svelte", "*.svelte"],
96
+
files: [
97
+
// Set .svelte/.js/.ts files. See above for more details.
You can use `parserOptions.parser` property to specify a custom parser to parse `<script>` tags.
109
-
Other properties than parser would be given to the specified parser.
110
-
111
-
For example in `eslint.config.js`:
117
+
Use the `parserOptions.parser` property to define a custom parser for `<script>` tags. Any additional parser options (besides the parser itself) are passed along to the specified parser.
112
118
113
119
```js
114
120
importtsParserfrom"@typescript-eslint/parser";
121
+
115
122
exportdefault [
116
123
{
117
-
files: ["**/*.svelte", "*.svelte"],
124
+
files: [
125
+
// Set .svelte/.js/.ts files. See above for more details.
126
+
],
118
127
languageOptions: {
119
128
parser: svelteParser,
120
129
parserOptions: {
@@ -125,51 +134,53 @@ export default [
125
134
];
126
135
```
127
136
128
-
If you are using the `"@typescript-eslint/parser"`, and if you want to use TypeScript in `<script>` of `.svelte`, you need to add more `parserOptions` configuration.
137
+
#### Using TypeScript in `<script>`
129
138
130
-
For example in `eslint.config.js`:
139
+
If you use `@typescript-eslint/parser` for TypeScript within `<script>` of `.svelte` files, additional configuration is needed. For example:
131
140
132
141
```js
133
142
importtsParserfrom"@typescript-eslint/parser";
143
+
134
144
exportdefault [
135
-
//...
145
+
//Other config for non-Svelte files
136
146
{
137
-
// ...
138
147
languageOptions: {
139
148
parser: tsParser,
140
149
parserOptions: {
141
-
// ...
142
150
project:"path/to/your/tsconfig.json",
143
-
extraFileExtensions: [".svelte"],// This is a required setting in `@typescript-eslint/parser` v4.24.0.
151
+
extraFileExtensions: [".svelte"],
144
152
},
145
153
},
146
154
},
155
+
// Svelte config
147
156
{
148
-
files: ["**/*.svelte", "*.svelte"],
157
+
files: [
158
+
// Set .svelte/.js/.ts files. See above for more details.
159
+
],
149
160
languageOptions: {
150
161
parser: svelteParser,
151
162
// Parse the `<script>` in `.svelte` as TypeScript by adding the following configuration.
152
163
parserOptions: {
153
164
parser: tsParser,
154
165
},
155
166
},
156
-
// ...
157
167
},
158
168
];
159
169
```
160
170
161
171
#### Multiple parsers
162
172
163
-
If you want to switch the parser for each lang, specify the object.
164
-
165
-
For example in `eslint.config.js`:
173
+
To switch parsers for each language, provide an object:
166
174
167
175
```js
168
176
importtsParserfrom"@typescript-eslint/parser";
169
177
importespreefrom"espree";
178
+
170
179
exportdefault [
171
180
{
172
-
files: ["**/*.svelte", "*.svelte"],
181
+
files: [
182
+
// Set .svelte/.js/.ts files. See above for more details.
183
+
],
173
184
languageOptions: {
174
185
parser: svelteParser,
175
186
parserOptions: {
@@ -186,15 +197,16 @@ export default [
186
197
187
198
### parserOptions.svelteConfig
188
199
189
-
If you are using `eslint.config.js`, you can provide a `svelte.config.js` in the `parserOptions.svelteConfig` property.
190
-
191
-
For example:
200
+
If you use `eslint.config.js`, you can specify a `svelte.config.js` file via `parserOptions.svelteConfig`.
192
201
193
202
```js
194
203
importsvelteConfigfrom"./svelte.config.js";
204
+
195
205
exportdefault [
196
206
{
197
-
files: ["**/*.svelte", "*.svelte"],
207
+
files: [
208
+
// Set .svelte/.js/.ts files. See above for more details.
209
+
],
198
210
languageOptions: {
199
211
parser: svelteParser,
200
212
parserOptions: {
@@ -205,28 +217,28 @@ export default [
205
217
];
206
218
```
207
219
208
-
If `parserOptions.svelteConfig` is not specified, some config will be statically parsed from the `svelte.config.js` file.
209
-
210
-
The `.eslintrc.*` style configuration cannot load `svelte.config.js` because it cannot use ESM. We recommend using the `eslint.config.js` style configuration.
220
+
If `parserOptions.svelteConfig` is not set, the parser attempts to statically read some config from `svelte.config.js`.
211
221
212
222
### parserOptions.svelteFeatures
213
223
214
-
You can use `parserOptions.svelteFeatures` property to specify how to parse related to Svelte features.
224
+
You can configure how Svelte-specific features are parsed via `parserOptions.svelteFeatures`.
215
225
216
-
For example in `eslint.config.js`:
226
+
For example:
217
227
218
228
```js
219
229
exportdefault [
220
230
{
221
-
files: ["**/*.svelte", "*.svelte"],
231
+
files: [
232
+
// Set .svelte/.js/.ts files. See above for more details.
233
+
],
222
234
languageOptions: {
223
235
parser: svelteParser,
224
236
parserOptions: {
225
237
svelteFeatures: {
226
-
// This option is for Svelte 5. The default value is `true`.
227
-
// If `false`, ESLint will not recognize rune symbols.
228
-
// If not configured this option, The parser will try to read the option from `compilerOptions.runes` from `svelte.config.js`.
229
-
// If `parserOptions.svelteConfig` is not specified and the file cannot be parsed by static analysis, it will behave as `true`.
238
+
// This is for Svelte 5. The default is true.
239
+
// If false, ESLint won't recognize rune symbols.
240
+
// If not specified, the parser tries to read compilerOptions.runes from `svelte.config.js`.
241
+
// If `parserOptions.svelteConfig` is not given and static analysis fails, it defaults to true.
230
242
runes:true,
231
243
},
232
244
},
@@ -235,88 +247,39 @@ export default [
235
247
];
236
248
```
237
249
238
-
### Runes support
239
-
240
-
If you install Svelte v5 the parser will be able to parse runes, and will also be able to parse `*.js` and `*.ts` files.
241
-
If you don't want to use Runes, you may need to configure. Please read [parserOptions.svelteFeatures](#parseroptionssveltefeatures) for more details.
242
-
243
-
When using this mode in an ESLint configuration, it is recommended to set it per file pattern as below.
244
-
245
-
For example in `eslint.config.js`:
246
-
247
-
```js
248
-
importsvelteConfigfrom"./svelte.config.js";
249
-
exportdefault [
250
-
{
251
-
files: ["**/*.svelte", "*.svelte"],
252
-
languageOptions: {
253
-
parser: svelteParser,
254
-
parserOptions: {
255
-
parser:"...",
256
-
svelteConfig,
257
-
/* ... */
258
-
},
259
-
},
260
-
},
261
-
{
262
-
files: ["**/*.svelte.js", "*.svelte.js"],
263
-
languageOptions: {
264
-
parser: svelteParser,
265
-
parserOptions: {
266
-
svelteConfig,
267
-
/* ... */
268
-
},
269
-
},
270
-
},
271
-
{
272
-
files: ["**/*.svelte.ts", "*.svelte.ts"],
273
-
languageOptions: {
274
-
parser: svelteParser,
275
-
parserOptions: {
276
-
parser:"...(ts parser)...",
277
-
svelteConfig,
278
-
/* ... */
279
-
},
280
-
},
281
-
},
282
-
];
283
-
```
250
+
---
284
251
285
-
## :computer:Editor Integrations
252
+
## Editor Integrations
286
253
287
254
### Visual Studio Code
288
255
289
-
Use the [dbaeumer.vscode-eslint](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint) extension that Microsoft provides officially.
256
+
Use the [dbaeumer.vscode-eslint](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint) extension provided by Microsoft.
290
257
291
-
You have to configure the `eslint.validate` option of the extension to check `.svelte` files, because the extension targets only `*.js` or `*.jsx` files by default.
292
-
293
-
Example **.vscode/settings.json**:
258
+
By default, it only targets `*.js` and `*.jsx`, so you need to configure `.svelte` file support. For example, in **.vscode/settings.json**:
-[AST.md](./docs/AST.md) is AST specification. You can check it on the [Online DEMO](https://sveltejs.github.io/svelte-eslint-parser/).
304
-
- The parser will generate its own [ScopeManager](https://eslint.org/docs/developer-guide/scope-manager-interface). You can check it on the [Online DEMO](https://sveltejs.github.io/svelte-eslint-parser/scope).
305
-
- I have already [implemented some rules] in the [`eslint-plugin-svelte`]. The source code for these rules will be helpful to you.
266
+
---
306
267
307
-
## :beers: Contributing
268
+
## Usage for Custom Rules / Plugins
308
269
309
-
Welcome contributing!
270
+
- See [AST.md](./docs/AST.md) for the AST specification. You can explore it on the [Live DEMO](https://sveltejs.github.io/svelte-eslint-parser/).
271
+
- This parser generates its own [ScopeManager](https://eslint.org/docs/developer-guide/scope-manager-interface). Check the [Live DEMO](https://sveltejs.github.io/svelte-eslint-parser/scope).
272
+
- Several rules are [already implemented] in [`eslint-plugin-svelte`], and their source code can be a helpful reference.
310
273
311
-
Please use GitHub's Issues/PRs.
274
+
---
312
275
313
-
See also the documentation for the internal mechanism.
0 commit comments