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/
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.
51
+
1.In your ESLint config, set the `parser` option to `svelte-eslint-parser`.
52
+
2. Use glob patterns or the `--ext .svelte` CLI option to include `.svelte` files.
52
53
53
54
### ESLint Config (`eslint.config.js`)
54
55
55
56
```js
56
57
importjsfrom"@eslint/js";
57
58
importsvelteParserfrom"svelte-eslint-parser";
59
+
58
60
exportdefault [
59
61
js.configs.recommended,
60
62
{
61
-
files: ["**/*.svelte", "*.svelte"],
63
+
files: [
64
+
"**/*.svelte",
65
+
"*.svelte",
66
+
// Need to specify the file extension for Svelte 5 with rune symbols
67
+
"**/*.svelte.js",
68
+
"*.svelte.js",
69
+
"**/*.svelte.ts",
70
+
"*.svelte.ts",
71
+
],
62
72
languageOptions: {
63
73
parser: svelteParser,
64
74
},
@@ -68,23 +78,29 @@ export default [
68
78
69
79
### CLI
70
80
71
-
```console
72
-
$ eslint "src/**/*.{js,svelte}"
81
+
```bash
82
+
eslint "src/**/*.{js,svelte}"
73
83
# or
74
-
$ eslint src --ext .svelte
84
+
eslint src --ext .svelte
75
85
```
76
86
77
-
## 🔧 Options
87
+
---
88
+
89
+
## Options
90
+
91
+
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
92
79
-
[`parserOptions`] has the same properties as what [espree](https://github.com/eslint/espree#usage), the default parser of ESLint, is supporting.
80
93
For example:
81
94
82
95
```js
83
96
importsvelteParserfrom"svelte-eslint-parser";
97
+
84
98
exportdefault [
85
99
// ...
86
100
{
87
-
files: ["**/*.svelte", "*.svelte"],
101
+
files: [
102
+
// 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`:
122
+
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
123
113
124
```js
114
125
importtsParserfrom"@typescript-eslint/parser";
126
+
115
127
exportdefault [
116
128
{
117
-
files: ["**/*.svelte", "*.svelte"],
129
+
files: [
130
+
// Set .svelte/.js/.ts files. See above for more details.
131
+
],
118
132
languageOptions: {
119
133
parser: svelteParser,
120
134
parserOptions: {
@@ -125,51 +139,53 @@ export default [
125
139
];
126
140
```
127
141
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.
142
+
#### Using TypeScript in `<script>`
129
143
130
-
For example in `eslint.config.js`:
144
+
If you use `@typescript-eslint/parser` for TypeScript within `<script>` of `.svelte` files, additional configuration is needed. For example:
131
145
132
146
```js
133
147
importtsParserfrom"@typescript-eslint/parser";
148
+
134
149
exportdefault [
135
-
//...
150
+
//Other config for non-Svelte files
136
151
{
137
-
// ...
138
152
languageOptions: {
139
153
parser: tsParser,
140
154
parserOptions: {
141
-
// ...
142
155
project:"path/to/your/tsconfig.json",
143
-
extraFileExtensions: [".svelte"],// This is a required setting in `@typescript-eslint/parser` v4.24.0.
156
+
extraFileExtensions: [".svelte"],
144
157
},
145
158
},
146
159
},
160
+
// Svelte config
147
161
{
148
-
files: ["**/*.svelte", "*.svelte"],
162
+
files: [
163
+
// Set .svelte/.js/.ts files. See above for more details.
164
+
],
149
165
languageOptions: {
150
166
parser: svelteParser,
151
167
// Parse the `<script>` in `.svelte` as TypeScript by adding the following configuration.
152
168
parserOptions: {
153
169
parser: tsParser,
154
170
},
155
171
},
156
-
// ...
157
172
},
158
173
];
159
174
```
160
175
161
176
#### Multiple parsers
162
177
163
-
If you want to switch the parser for each lang, specify the object.
164
-
165
-
For example in `eslint.config.js`:
178
+
To switch parsers for each language, provide an object:
166
179
167
180
```js
168
181
importtsParserfrom"@typescript-eslint/parser";
169
182
importespreefrom"espree";
183
+
170
184
exportdefault [
171
185
{
172
-
files: ["**/*.svelte", "*.svelte"],
186
+
files: [
187
+
// Set .svelte/.js/.ts files. See above for more details.
188
+
],
173
189
languageOptions: {
174
190
parser: svelteParser,
175
191
parserOptions: {
@@ -186,15 +202,16 @@ export default [
186
202
187
203
### parserOptions.svelteConfig
188
204
189
-
If you are using `eslint.config.js`, you can provide a `svelte.config.js` in the `parserOptions.svelteConfig` property.
190
-
191
-
For example:
205
+
If you use `eslint.config.js`, you can specify a `svelte.config.js` file via `parserOptions.svelteConfig`.
192
206
193
207
```js
194
208
importsvelteConfigfrom"./svelte.config.js";
209
+
195
210
exportdefault [
196
211
{
197
-
files: ["**/*.svelte", "*.svelte"],
212
+
files: [
213
+
// Set .svelte/.js/.ts files. See above for more details.
214
+
],
198
215
languageOptions: {
199
216
parser: svelteParser,
200
217
parserOptions: {
@@ -205,28 +222,28 @@ export default [
205
222
];
206
223
```
207
224
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.
225
+
If `parserOptions.svelteConfig` is not set, the parser attempts to statically read some config from `svelte.config.js`.
211
226
212
227
### parserOptions.svelteFeatures
213
228
214
-
You can use `parserOptions.svelteFeatures` property to specify how to parse related to Svelte features.
229
+
You can configure how Svelte-specific features are parsed via `parserOptions.svelteFeatures`.
215
230
216
-
For example in `eslint.config.js`:
231
+
For example:
217
232
218
233
```js
219
234
exportdefault [
220
235
{
221
-
files: ["**/*.svelte", "*.svelte"],
236
+
files: [
237
+
// Set .svelte/.js/.ts files. See above for more details.
238
+
],
222
239
languageOptions: {
223
240
parser: svelteParser,
224
241
parserOptions: {
225
242
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`.
243
+
// This is for Svelte 5. The default is true.
244
+
// If false, ESLint won't recognize rune symbols.
245
+
// If not specified, the parser tries to read compilerOptions.runes from `svelte.config.js`.
246
+
// If `parserOptions.svelteConfig` is not given and static analysis fails, it defaults to true.
230
247
runes:true,
231
248
},
232
249
},
@@ -235,88 +252,39 @@ export default [
235
252
];
236
253
```
237
254
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.
255
+
---
242
256
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
-
```
284
-
285
-
## :computer: Editor Integrations
257
+
## Editor Integrations
286
258
287
259
### Visual Studio Code
288
260
289
-
Use the [dbaeumer.vscode-eslint](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint) extension that Microsoft provides officially.
261
+
Use the [dbaeumer.vscode-eslint](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint) extension provided by Microsoft.
290
262
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**:
263
+
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.
271
+
---
306
272
307
-
## :beers: Contributing
273
+
## Usage for Custom Rules / Plugins
308
274
309
-
Welcome contributing!
275
+
- 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/).
276
+
- 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).
277
+
- Several rules are [already implemented] in [`eslint-plugin-svelte`], and their source code can be a helpful reference.
310
278
311
-
Please use GitHub's Issues/PRs.
279
+
---
312
280
313
-
See also the documentation for the internal mechanism.
0 commit comments