Skip to content

no-direct-import eslint rule - adding applyAutofix flag #1183

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

Merged
merged 6 commits into from
Feb 9, 2021
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
17 changes: 11 additions & 6 deletions eslint-rules/lib/rules/no-direct-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ const MAP_SCHEMA = {
type: 'object',
properties: {
origin: {
type: 'string',
type: 'string'
},
destination: {
type: 'string',
type: 'string'
},
applyAutofix: {
type: 'boolean'
}
},
additionalProperties: false,
additionalProperties: false
};

module.exports = {
Expand All @@ -19,7 +22,7 @@ module.exports = {
recommended: true,
},
messages: {
uiLib: 'Do not import directly from this source. Please use another import source (autofix available).',
uiLib: 'Do not import directly from this source. Please use another import source (autofix may be available).',
},
fixable: 'code',
schema: [
Expand All @@ -31,12 +34,14 @@ module.exports = {
try {
const origin = context.options[0].origin;
const destination = context.options[0].destination;
const message = `Do not import directly from '${origin}'. Please use '${destination}' (autofix available).`;
const applyAutofix = context.options[0].applyAutofix;
const autofixMessage = applyAutofix ? ' (autofix available)' : '';
const message = `Do not import directly from '${origin}'. Please use '${destination}'${autofixMessage}.`;
context.report({
node,
message,
fix(fixer) {
if (node && destination) {
if (node && applyAutofix && destination) {
return fixer.replaceText(node.source, `'${destination}'`);
}
},
Expand Down
2 changes: 1 addition & 1 deletion eslint-rules/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-uilib",
"version": "1.0.28",
"version": "1.0.29",
"description": "uilib set of eslint rules",
"keywords": [
"eslint",
Expand Down
16 changes: 8 additions & 8 deletions eslint-rules/tests/lib/rules/no-direct-import.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const RuleTester = require('eslint').RuleTester;
const rule = require('../../../lib/rules/no-direct-import');

const ruleOptions = [{ origin: 'some-module', destination: 'another-module' }];
const ruleOptions = [{origin: 'some-module', destination: 'another-module', applyAutofix: true}];

RuleTester.setDefaultConfig({
parser: 'babel-eslint',
parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } },
parserOptions: {ecmaVersion: 6, ecmaFeatures: {jsx: true}}
});

const ruleTester = new RuleTester();
Expand All @@ -17,17 +17,17 @@ ruleTester.run('no-direct-import', rule, {
valid: [
{
options: ruleOptions,
code: validExample,
},
code: validExample
}
],
invalid: [
{
options: ruleOptions,
code: invalidExample,
output: `import {Component} from 'another-module';`,
errors: [
{ message: `Do not import directly from 'some-module'. Please use 'another-module' (autofix available).` },
],
},
],
{message: `Do not import directly from 'some-module'. Please use 'another-module' (autofix available).`}
]
}
]
});