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

feat: support eslint v9 flat config in plugin #20

Merged
merged 8 commits into from
Jul 1, 2024
Merged
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
5 changes: 5 additions & 0 deletions .changeset/rich-peaches-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@intlify/eslint-plugin-svelte": minor
---

feat: support eslint v9 flat config in plugin
4 changes: 4 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ module.exports = {
files: ['**/*.md/*.*'],
rules: {
'prettier/prettier': 'off'
},
parserOptions: {
ecmaVersion: 2021,
sourceType: 'module'
}
}
]
Expand Down
2 changes: 1 addition & 1 deletion docs/rules/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Available Rules

- :star: mark: the rule which is enabled by `plugin:@intlify/svelte/recommended` preset.
- :star: mark: the rule which is enabled by the `plugin:@intlify/svelte/recommended` or `*.configs.["flat/recommended"]` preset.
- :black_nib: mark: the rule which is fixable by `eslint --fix` command.

## Recommended
Expand Down
2 changes: 1 addition & 1 deletion docs/rules/no-raw-text.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ since: v0.0.1

> disallow to string literal in template

- :star: The `"extends": "plugin:@intlify/svelte/recommended"` property in a configuration file enables this rule.
- :star: The `"extends": "plugin:@intlify/svelte/recommended"` or `*.configs["flat/recommended"]` property in a configuration file enables this rule.

This rule warns the usage of string literal.

Expand Down
49 changes: 46 additions & 3 deletions docs/started.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,47 @@ npm install --save-dev eslint @intlify/eslint-plugin-svelte

## :rocket: Usage

### Configuration
### Configuration `eslint.config.[c|m]js`

Configure your `.eslintrc.*` file.
In ESLint v9, the the default way to configure files is using an `eslint.config.[c|m]js` file, but this can be used starting from ESLint v8.57.0.

For example:
See also: https://eslint.org/docs/latest/use/configure/configuration-files-new.

Example `eslint.config.js`:

```js
import intlifySvelte from '@intlify/eslint-plugin-svelte'

export default [
// add more generic rulesets here, such as:
//...eslintPluginSvelte.configs["flat/recommended"],

// Recommended
...intlifySvelte.configs['flat/recommended'],
{
rules: {
// override/add rules settings here, such as:
'@intlify/svelte/no-raw-text': 'error'
}
}
]
```

See [the rule list](./rules/README.md)

#### Bundle Configurations `eslint.config.[c|m]js`

This plugin provides some predefined configs. You can use the following configs by adding them to `eslint.config.[c|m]js`. (All flat configs in this plugin are provided as arrays, so spread syntax is required when combining them with other configs.)

- `*configs["flat/base"]`: Settings and rules to enable correct ESlint parsing.
- `*configs["flat/recommended"]`: Above, plus rules to enforce subjective community defaults to ensure consistency.

### Configuration `.eslintrc.*`

Use the `.eslintrc.*` file to configure rules in ESLint < v9. See also:
https://eslint.org/docs/latest/use/configure/.

Example `.eslintrc.js`:

```js
module.export = {
Expand All @@ -44,6 +80,13 @@ module.export = {

See [the rule list](./rules/README.md)

#### Bundle Configurations `eslintrc.*`

This plugin provides some predefined configs. You can use the following configs by adding them to `eslintrc.*`.

- `plugin:@intlify/svelte/base`: Settings and rules to enable correct ESlint parsing.
- `plugin:@intlify/svelte/recommended`: Above, plus rules to enforce subjective community defaults to ensure consistency.

::: warning ❗ Attention

The `@intlify/eslint-plugin-svelte` can not be used with the [eslint-plugin-svelte3].
Expand Down
8 changes: 0 additions & 8 deletions lib/configs.ts

This file was deleted.

18 changes: 18 additions & 0 deletions lib/configs/flat/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/** DON'T EDIT THIS FILE; was created by scripts. */
export = [
{
name: '@intlify/vue-i18n:base:setup',
plugins: {
get '@intlify/svelte'() {
return require('../../index')
}
}
},
{
name: '@intlify/svelte:base:svelte',
files: ['*.svelte'],
languageOptions: {
parser: require('svelte-eslint-parser')
}
}
]
23 changes: 23 additions & 0 deletions lib/configs/flat/recommended.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/** DON'T EDIT THIS FILE; was created by scripts. */
import config from './base'
export = [
...config,
{
name: '@intlify/svelte:recommended:setup',
languageOptions: {
ecmaVersion: 2018,
sourceType: 'module',
parserOptions: {
ecmaFeatures: {
jsx: true
}
}
}
},
{
name: '@intlify/svelte:recommended:rules',
rules: {
'@intlify/svelte/no-raw-text': 'warn'
}
}
]
15 changes: 13 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,23 @@
* @fileoverview ESLint plugin for internationalization with Svelte
* @author Yosuke Ota
*/
import configs from './configs'
import legacyBase from './configs/base'
import legacyRecommended from './configs/recommended'
import flatBase from './configs/flat/base'
import flatRecommended from './configs/flat/recommended'
import rules from './rules'
import * as meta from './meta'

export = {
meta,
configs,
// eslintrc configs
configs: {
base: legacyBase,
recommended: legacyRecommended,

// flat configs
'flat/base': flatBase,
'flat/recommended': flatRecommended
},
rules
}
2 changes: 1 addition & 1 deletion scripts/update-docs-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ writeFileSync(
resolve(__dirname, '../docs/rules/README.md'),
`# Available Rules

- :star: mark: the rule which is enabled by \`plugin:@intlify/svelte/recommended\` preset.
- :star: mark: the rule which is enabled by the \`plugin:@intlify/svelte/recommended\` or \`*.configs.["flat/recommended"]\` preset.
- :black_nib: mark: the rule which is fixable by \`eslint --fix\` command.

${withCategories
Expand Down
25 changes: 25 additions & 0 deletions scripts/update-flat-base-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { resolve } from 'path'
import { writeAndFormat } from './lib/write'

writeAndFormat(
//base.ts
resolve(__dirname, '../lib/configs/flat/base.ts'),
`/** DON'T EDIT THIS FILE; was created by scripts. */
export = [
{
name: @intlify/svelte:base:setup',
plugins: {
get '@intlify/svelte'() {
return require('../../index')
}
}
},
{
name: "@intlify/svelte:base:svelte",
files: ['*.svelte'],
languageOptions: {
parser: require('svelte-eslint-parser'),
}
},
]`
)
34 changes: 34 additions & 0 deletions scripts/update-flat-recommended-rules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { resolve } from 'path'
import rules from './lib/rules'
import { writeAndFormat } from './lib/write'

// flat/recommended.ts
writeAndFormat(
resolve(__dirname, '../lib/configs/flat/recommended.ts'),
`/** DON'T EDIT THIS FILE; was created by scripts. */
import config from './base';
export = [
...config,
{
name: '@intlify/svelte:recommended:setup',
languageOptions: {
ecmaVersion: 2018,
sourceType: 'module',
parserOptions: {
ecmaFeatures: {
jsx: true
}
}
}
},
{
name: '@intlify/svelte:recommended:rules',
rules: {
${rules
.filter(rule => rule.recommended)
.map(rule => `'${rule.id}': 'warn',`)
.join('\n ')}
},
}
]`
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ import { resolve } from 'path'
import rules from './lib/rules'
import { writeAndFormat } from './lib/write'

// recommended.ts
// legacy/recommended.ts
writeAndFormat(
resolve(__dirname, '../lib/configs/recommended.ts'),
`/** DON'T EDIT THIS FILE; was created by scripts. */
export = {
extends: [require.resolve('./base')],
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
ecmaFeatures: {
jsx: true
}
},
env: {
browser: true,
es6: true
},
rules: {
${rules
.filter(rule => rule.recommended)
.map(rule => `'${rule.id}': 'warn',`)
.join('\n ')}
},
}`
export = {
extends: [require.resolve('./base')],
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
ecmaFeatures: {
jsx: true
}
},
env: {
browser: true,
es6: true
},
rules: {
${rules
.filter(rule => rule.recommended)
.map(rule => `'${rule.id}': 'warn',`)
.join('\n ')}
},
}`
)
2 changes: 1 addition & 1 deletion scripts/update-rule-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ async function updateRuleDocs(): Promise<void> {
}
} else if (rule.recommended) {
headerLines.push(
'- :star: The `"extends": "plugin:@intlify/svelte/recommended"` property in a configuration file enables this rule.'
'- :star: The `"extends": "plugin:@intlify/svelte/recommended"` or `*.configs["flat/recommended"]` property in a configuration file enables this rule.'
)
}

Expand Down
5 changes: 3 additions & 2 deletions scripts/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import './update-rule-docs'
import './update-docs-index'

// recommended rules.
import './update-recommended-rules'
import './update-legacy-recommended-rules'
import './update-flat-recommended-rules'
import './update-flat-base-config'
import './update-meta'
import { writeAndFormat } from './lib/write'

// indices.
for (const pairs of [
[resolve(__dirname, '../lib/configs')],
[resolve(__dirname, '../lib/rules')]
// [resolve(__dirname, '../lib/utils'), '', true]
] as const) {
Expand Down
Loading