eslint-plugin: add auto fix for missing imports
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -18,6 +18,10 @@ import { RuleTester } from 'eslint';
|
||||
import { join as joinPath } from 'path';
|
||||
import rule from '../rules/no-undeclared-imports';
|
||||
|
||||
jest.mock('child_process', () => ({
|
||||
execFileSync: jest.fn(),
|
||||
}));
|
||||
|
||||
const RULE = 'no-undeclared-imports';
|
||||
const FIXTURE = joinPath(__dirname, '__fixtures__/monorepo');
|
||||
|
||||
@@ -45,6 +49,9 @@ const ERR_SWITCHED = (
|
||||
'package.json',
|
||||
)}.`,
|
||||
});
|
||||
const ERR_SWITCH_BACK = () => ({
|
||||
message: 'Switch back to import declaration',
|
||||
});
|
||||
|
||||
process.chdir(FIXTURE);
|
||||
|
||||
@@ -130,6 +137,7 @@ ruleTester.run(RULE, rule, {
|
||||
},
|
||||
{
|
||||
code: `import 'lodash'`,
|
||||
output: `import 'directive:add-import:dependencies:lodash'`,
|
||||
filename: joinPath(FIXTURE, 'packages/bar/src/index.ts'),
|
||||
errors: [
|
||||
ERR_UNDECLARED('lodash', 'dependencies', joinPath('packages', 'bar')),
|
||||
@@ -137,6 +145,7 @@ ruleTester.run(RULE, rule, {
|
||||
},
|
||||
{
|
||||
code: `import { debounce } from 'lodash'`,
|
||||
output: `import { debounce } from 'directive:add-import:dependencies:lodash'`,
|
||||
filename: joinPath(FIXTURE, 'packages/bar/src/index.ts'),
|
||||
errors: [
|
||||
ERR_UNDECLARED('lodash', 'dependencies', joinPath('packages', 'bar')),
|
||||
@@ -144,6 +153,7 @@ ruleTester.run(RULE, rule, {
|
||||
},
|
||||
{
|
||||
code: `import * as _ from 'lodash'`,
|
||||
output: `import * as _ from 'directive:add-import:dependencies:lodash'`,
|
||||
filename: joinPath(FIXTURE, 'packages/bar/src/index.ts'),
|
||||
errors: [
|
||||
ERR_UNDECLARED('lodash', 'dependencies', joinPath('packages', 'bar')),
|
||||
@@ -151,6 +161,7 @@ ruleTester.run(RULE, rule, {
|
||||
},
|
||||
{
|
||||
code: `import _ from 'lodash'`,
|
||||
output: `import _ from 'directive:add-import:dependencies:lodash'`,
|
||||
filename: joinPath(FIXTURE, 'packages/bar/src/index.ts'),
|
||||
errors: [
|
||||
ERR_UNDECLARED('lodash', 'dependencies', joinPath('packages', 'bar')),
|
||||
@@ -158,6 +169,7 @@ ruleTester.run(RULE, rule, {
|
||||
},
|
||||
{
|
||||
code: `import('lodash')`,
|
||||
output: `import('directive:add-import:dependencies:lodash')`,
|
||||
filename: joinPath(FIXTURE, 'packages/bar/src/index.ts'),
|
||||
errors: [
|
||||
ERR_UNDECLARED('lodash', 'dependencies', joinPath('packages', 'bar')),
|
||||
@@ -165,6 +177,7 @@ ruleTester.run(RULE, rule, {
|
||||
},
|
||||
{
|
||||
code: `require('lodash')`,
|
||||
output: `require('directive:add-import:dependencies:lodash')`,
|
||||
filename: joinPath(FIXTURE, 'packages/bar/src/index.ts'),
|
||||
errors: [
|
||||
ERR_UNDECLARED('lodash', 'dependencies', joinPath('packages', 'bar')),
|
||||
@@ -172,13 +185,7 @@ ruleTester.run(RULE, rule, {
|
||||
},
|
||||
{
|
||||
code: `import 'lodash'`,
|
||||
filename: joinPath(FIXTURE, 'packages/bar/src/index.ts'),
|
||||
errors: [
|
||||
ERR_UNDECLARED('lodash', 'dependencies', joinPath('packages', 'bar')),
|
||||
],
|
||||
},
|
||||
{
|
||||
code: `import 'lodash'`,
|
||||
output: `import 'directive:add-import:devDependencies:lodash'`,
|
||||
filename: joinPath(FIXTURE, 'packages/bar/src/index.test.ts'),
|
||||
errors: [
|
||||
ERR_UNDECLARED(
|
||||
@@ -191,6 +198,7 @@ ruleTester.run(RULE, rule, {
|
||||
},
|
||||
{
|
||||
code: `import 'react'`,
|
||||
output: `import 'directive:add-import:peerDependencies:react'`,
|
||||
filename: joinPath(FIXTURE, 'packages/bar/src/index.ts'),
|
||||
errors: [
|
||||
ERR_UNDECLARED(
|
||||
@@ -203,6 +211,7 @@ ruleTester.run(RULE, rule, {
|
||||
},
|
||||
{
|
||||
code: `import 'react'`,
|
||||
output: `import 'directive:add-import:peerDependencies:react'`,
|
||||
filename: joinPath(FIXTURE, 'packages/bar/src/index.test.ts'),
|
||||
errors: [
|
||||
ERR_UNDECLARED(
|
||||
@@ -215,6 +224,7 @@ ruleTester.run(RULE, rule, {
|
||||
},
|
||||
{
|
||||
code: `import 'react-dom'`,
|
||||
output: `import 'directive:add-import:dependencies:react-dom'`,
|
||||
filename: joinPath(FIXTURE, 'packages/foo/src/index.ts'),
|
||||
errors: [
|
||||
ERR_UNDECLARED(
|
||||
@@ -226,6 +236,7 @@ ruleTester.run(RULE, rule, {
|
||||
},
|
||||
{
|
||||
code: `import 'react-dom'`,
|
||||
output: `import 'directive:add-import:devDependencies:react-dom'`,
|
||||
filename: joinPath(FIXTURE, 'packages/foo/src/index.test.ts'),
|
||||
errors: [
|
||||
ERR_UNDECLARED(
|
||||
@@ -238,6 +249,7 @@ ruleTester.run(RULE, rule, {
|
||||
},
|
||||
{
|
||||
code: `import '@internal/foo'`,
|
||||
output: `import 'directive:add-import:dependencies:@internal/foo'`,
|
||||
filename: joinPath(FIXTURE, 'packages/bar/src/index.ts'),
|
||||
errors: [
|
||||
ERR_UNDECLARED(
|
||||
@@ -247,5 +259,43 @@ ruleTester.run(RULE, rule, {
|
||||
),
|
||||
],
|
||||
},
|
||||
|
||||
// Switching back to original import declarations
|
||||
{
|
||||
code: `import 'directive:add-import:dependencies:lodash'`,
|
||||
output: `import 'lodash'`,
|
||||
filename: joinPath(FIXTURE, 'packages/bar/src/index.ts'),
|
||||
errors: [ERR_SWITCH_BACK()],
|
||||
},
|
||||
{
|
||||
code: `import { debounce } from 'directive:add-import:dependencies:lodash'`,
|
||||
output: `import { debounce } from 'lodash'`,
|
||||
filename: joinPath(FIXTURE, 'packages/bar/src/index.ts'),
|
||||
errors: [ERR_SWITCH_BACK()],
|
||||
},
|
||||
{
|
||||
code: `import * as _ from 'directive:add-import:dependencies:lodash'`,
|
||||
output: `import * as _ from 'lodash'`,
|
||||
filename: joinPath(FIXTURE, 'packages/bar/src/index.ts'),
|
||||
errors: [ERR_SWITCH_BACK()],
|
||||
},
|
||||
{
|
||||
code: `import _ from 'directive:add-import:dependencies:lodash'`,
|
||||
output: `import _ from 'lodash'`,
|
||||
filename: joinPath(FIXTURE, 'packages/bar/src/index.ts'),
|
||||
errors: [ERR_SWITCH_BACK()],
|
||||
},
|
||||
{
|
||||
code: `import('directive:add-import:dependencies:lodash')`,
|
||||
output: `import('lodash')`,
|
||||
filename: joinPath(FIXTURE, 'packages/bar/src/index.ts'),
|
||||
errors: [ERR_SWITCH_BACK()],
|
||||
},
|
||||
{
|
||||
code: `require('directive:add-import:dependencies:lodash')`,
|
||||
output: `require('lodash')`,
|
||||
filename: joinPath(FIXTURE, 'packages/bar/src/index.ts'),
|
||||
errors: [ERR_SWITCH_BACK()],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user