Skip to content

Commit

Permalink
feat!: rework migrate package to support custom from from and to
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Sep 30, 2023
1 parent ca8429d commit f3a9013
Show file tree
Hide file tree
Showing 9 changed files with 369 additions and 139 deletions.
29 changes: 9 additions & 20 deletions docs/guide/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,6 @@ We provides an ESLint plugin for migrating built-in stylistic rules to the `@sty
npm i -D @stylistic/eslint-plugin-migrate
```

#### Preset Usage

Extend the `plugin:@stylistic/migrate/recommended` preset in your ESLint configuration. It will check the `.eslintrc.{js|json}` file and the `eslint-config-*/index.js` files in your workspace automatically.

```js
// .eslintrc.js
module.exports = {
extends: [
'plugin:@stylistic/migrate/recommended'
],
};
```

#### Manual Usage

You can install the plugin manually:

```js
// .eslintrc.js
module.exports = {
Expand All @@ -54,14 +37,20 @@ module.exports = {
}
```

And opt-in to your eslint configure file by adding `/* eslint @stylistic/migrate/rules: "error" */` to the top of your file:
And opt-in to your eslint configure file by adding eslint comments to the top of your file:

```js
/* eslint @stylistic/migrate/rules: "error" */
// Migrate built-in rules to @stylistic/js namespace
/* eslint @stylistic/migrate/migrate-js: "error" */

// Migrate `@typescript-eslint` rules to @stylistic/ts namespace
/* eslint @stylistic/migrate/migrate-ts: "error" */

module.exports = {
rules: {
indent: ['error', 2], // Error: Use @stylistic/indent instead
indent: ['error', 2], // Error: Use @stylistic/js/indent instead

'@typescript-eslint/indent': ['error', 2], // Error: Use @stylistic/ts/indent instead
}
}
```
Expand Down
39 changes: 6 additions & 33 deletions packages/eslint-plugin-migrate/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,11 @@
import migrate from './migrate'
import migrate from './rules/migrate'
import migrateTs from './rules/migrate-ts'
import migrateJs from './rules/migrate-js'

export default {
rules: {
rules: migrate,
},
configs: {
recommended: {
plugins: [
'@stylistic/migrate',
],
overrides: [
{
files: [
'.eslintrc.js',
'.eslintrc.cjs',
'.eslintrc.json',
'eslintrc.js',
'eslintrc.cjs',
'eslintrc.json',
'eslint.config.js',
'eslint.config.cjs',
'eslint.config.ts',
'**/eslint-config-*/index.js',
'**/eslint-config-*/src/index.js',
'**/eslint-config-*/src/index.ts',
'**/eslint-config/index.js',
'**/eslint-config/src/index.js',
'**/eslint-config/src/index.ts',
],
rules: {
'@stylistic/migrate/rules': 'error',
},
},
],
},
migrate,
'migrate-ts': migrateTs,
'migrate-js': migrateJs,
},
}
86 changes: 0 additions & 86 deletions packages/eslint-plugin-migrate/src/migrate.ts

This file was deleted.

34 changes: 34 additions & 0 deletions packages/eslint-plugin-migrate/src/rules/migrate-js.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { packages } from '@eslint-stylistic/metadata'
import { createEslintRule, createRuleListener } from '../shared'
import type { MigrationOption } from '../shared'

const js = packages.find(p => p.shortId === 'js')!

export default createEslintRule<[MigrationOption], 'migrate'>({
name: 'migrate-js',
meta: {
type: 'suggestion',
docs: {
description: 'Migrate built-in stylistic rules to @stylistic/js rules',
},
fixable: 'code',
messages: {
migrate: 'Should migrate stylistic rule \'{{from}}\' to \'{{to}}\'',
},
schema: undefined!,
},
defaultOptions: [{
namespaceFrom: '',
namespaceTo: '@stylistic/js',
}],
create(context, options) {
return createRuleListener(
context as any,
[{
namespaceFrom: options[0].namespaceFrom,
namespaceTo: options[0].namespaceTo,
rules: js.rules,
}],
)
},
})
42 changes: 42 additions & 0 deletions packages/eslint-plugin-migrate/src/rules/migrate-ts.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { RuleTester } from '@typescript-eslint/rule-tester'
import rule from './migrate-ts'

const valids = [
{
rules: {
'foo': 'error',
'@stylistic/indent': 'error',
},
},
{
rules: {
indent: 'error',
},
},
].map(json => `module.exports = ${JSON.stringify(json, null, 2)}`)

const invalids = [
[
{
rules: {
'@typescript-eslint/indent': 'error',
},
},
{
rules: {
'@stylistic/ts/indent': 'error',
},
},
],
].map(([from, to]) => ({
code: `module.exports = ${JSON.stringify(from, null, 2)}`,
output: `module.exports = ${JSON.stringify(to, null, 2)}`,
errors: [{ messageId: 'migrate' }],
}))

const ruleTester: RuleTester = new RuleTester()

ruleTester.run('migrate-ts', rule as any, {
valid: valids,
invalid: invalids,
})
34 changes: 34 additions & 0 deletions packages/eslint-plugin-migrate/src/rules/migrate-ts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { packages } from '@eslint-stylistic/metadata'
import type { MigrationOption } from '../shared'
import { createEslintRule, createRuleListener } from '../shared'

const ts = packages.find(p => p.shortId === 'ts')!

export default createEslintRule<[MigrationOption], 'migrate'>({
name: 'migrate-ts',
meta: {
type: 'suggestion',
docs: {
description: 'Migrate `@typescript-eslint` stylistic rules to `@stylistic/ts` rules',
},
fixable: 'code',
messages: {
migrate: 'Should migrate stylistic rule \'{{from}}\' to \'{{to}}\'',
},
schema: undefined!,
},
defaultOptions: [{
namespaceFrom: '@typescript-eslint',
namespaceTo: '@stylistic/ts',
}],
create(context, options) {
return createRuleListener(
context as any,
[{
namespaceFrom: options[0].namespaceFrom,
namespaceTo: options[0].namespaceTo,
rules: ts.rules,
}],
)
},
})
65 changes: 65 additions & 0 deletions packages/eslint-plugin-migrate/src/rules/migrate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { RuleTester } from '@typescript-eslint/rule-tester'
import rule from './migrate'

const valids = [
{
rules: {
'foo': 'error',
'@stylistic/indent': 'error',
},
},
].map(json => `module.exports = ${JSON.stringify(json, null, 2)}`)

const invalids = [
[
{
rules: {
indent: 'error',
},
},
{
rules: {
'@stylistic/indent': 'error',
},
},
],
[
{
rules: {
'@typescript-eslint/indent': 'error',
},
},
{
rules: {
'@stylistic/indent': 'error',
},
},
],
[
{
rules: {
'@typescript-eslint/indent': 'error',
},
},
{
rules: {
'style/indent': 'error',
},
},
{
namespaceTo: 'style',
},
],
].map(([from, to, options]) => ({
code: `module.exports = ${JSON.stringify(from, null, 2)}`,
output: `module.exports = ${JSON.stringify(to, null, 2)}`,
errors: [{ messageId: 'migrate' }],
options: [options] as any,
}))

const ruleTester: RuleTester = new RuleTester()

ruleTester.run('migrate', rule as any, {
valid: valids,
invalid: invalids,
})

0 comments on commit f3a9013

Please sign in to comment.