Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[doc] Update README and user guide to use eslint flat format for TS configuration #842

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 32 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,56 +164,57 @@ module.exports = {
};
```

For example, 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.
For example, 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.

```js
module.exports = {
// ...
extends: ['plugin:svelte/recommended'],
// ...
parser: '@typescript-eslint/parser',
parserOptions: {
// ...
project: 'path/to/your/tsconfig.json',
extraFileExtensions: ['.svelte'] // This is a required setting in `@typescript-eslint/parser` v4.24.0.
import svelteParser from 'svelte-eslint-parser';
import tseslint from 'typescript-eslint';

export default [
//...
{
files: ['**/*.ts', '**/*.js', '**/*.svelte', '**/*.cjs'],
languageOptions: {
parser: tseslint.parser,
parserOptions: {
//...
project: 'path/to/your/tsconfig.json',
extraFileExtensions: ['.svelte']
}
}
},
overrides: [
{
files: ['*.svelte'],
parser: 'svelte-eslint-parser',
// Parse the `<script>` in `.svelte` as TypeScript by adding the following configuration.
{
files: ['**/*.svelte'],
languageOptions: {
parser: svelteParser,
parserOptions: {
parser: '@typescript-eslint/parser'
parser: tseslint.parser
}
}
// ...
]
// ...
};
}
];
```

If you have a mix of TypeScript and JavaScript in your project, use a multiple parser configuration.

```js
module.exports = {
export default [
// ...
overrides: [
{
files: ['*.svelte'],
parser: 'svelte-eslint-parser',
{
files: ['**/*.svelte'],
languageOptions: {
parser: svelteParser,
parserOptions: {
parser: {
// Specify a parser for each lang.
ts: '@typescript-eslint/parser',
ts: tseslint.parser,
js: 'espree',
typescript: '@typescript-eslint/parser'
typescript: tseslint.parser
}
}
}
// ...
]
// ...
};
}
];
```

See also <https://github.com/sveltejs/svelte-eslint-parser#readme>.
Expand Down
63 changes: 32 additions & 31 deletions docs/user-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,56 +115,57 @@ module.exports = {
};
```

For example, 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.
For example, 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.

```js
module.exports = {
// ...
extends: ['plugin:svelte/recommended'],
// ...
parser: '@typescript-eslint/parser',
parserOptions: {
// ...
project: 'path/to/your/tsconfig.json',
extraFileExtensions: ['.svelte'] // This is a required setting in `@typescript-eslint/parser` v4.24.0.
import svelteParser from 'svelte-eslint-parser';
import tseslint from 'typescript-eslint';

export default [
//...
{
files: ['**/*.ts', '**/*.js', '**/*.svelte', '**/*.cjs'],
languageOptions: {
parser: tseslint.parser,
Copy link
Member

@ota-meshi ota-meshi Sep 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this PR!

I don't think we need to use tseslint.parser for *.js and *.svelte, I think it makes sense to specify only parserOptions here.

Suggested change
parser: tseslint.parser,

Maybe we need another section that only specifies parser: tseslint.parser. What do you think?

parserOptions: {
//...
project: 'path/to/your/tsconfig.json',
extraFileExtensions: ['.svelte']
}
}
},
overrides: [
{
files: ['*.svelte'],
parser: 'svelte-eslint-parser',
// Parse the `<script>` in `.svelte` as TypeScript by adding the following configuration.
{
files: ['**/*.svelte'],
languageOptions: {
parser: svelteParser,
parserOptions: {
parser: '@typescript-eslint/parser'
parser: tseslint.parser
}
}
// ...
]
// ...
};
}
];
```

If you have a mix of TypeScript and JavaScript in your project, use a multiple parser configuration.

```js
module.exports = {
export default [
// ...
overrides: [
{
files: ['*.svelte'],
parser: 'svelte-eslint-parser',
{
files: ['**/*.svelte'],
languageOptions: {
parser: svelteParser,
parserOptions: {
parser: {
// Specify a parser for each lang.
ts: '@typescript-eslint/parser',
ts: tseslint.parser,
js: 'espree',
typescript: '@typescript-eslint/parser'
typescript: tseslint.parser
}
}
}
// ...
]
// ...
};
}
];
```

See also <https://github.com/sveltejs/svelte-eslint-parser#readme>.
Expand Down