Skip to content

Commit 225f98f

Browse files
no-direct-import eslint rule - adding applyAutofix flag (#1183)
* no-direct-import eslint rule - adding `applyAutofix` flag to control autofix option * bumping version to 1.1.00 * version number fix * Update eslint-rules/package.json Co-authored-by: Ethan Sharabi <[email protected]> Co-authored-by: Ethan Sharabi <[email protected]>
1 parent 45e07e9 commit 225f98f

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

eslint-rules/lib/rules/no-direct-import.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ const MAP_SCHEMA = {
22
type: 'object',
33
properties: {
44
origin: {
5-
type: 'string',
5+
type: 'string'
66
},
77
destination: {
8-
type: 'string',
8+
type: 'string'
99
},
10+
applyAutofix: {
11+
type: 'boolean'
12+
}
1013
},
11-
additionalProperties: false,
14+
additionalProperties: false
1215
};
1316

1417
module.exports = {
@@ -19,7 +22,7 @@ module.exports = {
1922
recommended: true,
2023
},
2124
messages: {
22-
uiLib: 'Do not import directly from this source. Please use another import source (autofix available).',
25+
uiLib: 'Do not import directly from this source. Please use another import source (autofix may be available).',
2326
},
2427
fixable: 'code',
2528
schema: [
@@ -31,12 +34,14 @@ module.exports = {
3134
try {
3235
const origin = context.options[0].origin;
3336
const destination = context.options[0].destination;
34-
const message = `Do not import directly from '${origin}'. Please use '${destination}' (autofix available).`;
37+
const applyAutofix = context.options[0].applyAutofix;
38+
const autofixMessage = applyAutofix ? ' (autofix available)' : '';
39+
const message = `Do not import directly from '${origin}'. Please use '${destination}'${autofixMessage}.`;
3540
context.report({
3641
node,
3742
message,
3843
fix(fixer) {
39-
if (node && destination) {
44+
if (node && applyAutofix && destination) {
4045
return fixer.replaceText(node.source, `'${destination}'`);
4146
}
4247
},

eslint-rules/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "eslint-plugin-uilib",
3-
"version": "1.0.28",
3+
"version": "1.0.29",
44
"description": "uilib set of eslint rules",
55
"keywords": [
66
"eslint",
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
const RuleTester = require('eslint').RuleTester;
22
const rule = require('../../../lib/rules/no-direct-import');
33

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

66
RuleTester.setDefaultConfig({
77
parser: 'babel-eslint',
8-
parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } },
8+
parserOptions: {ecmaVersion: 6, ecmaFeatures: {jsx: true}}
99
});
1010

1111
const ruleTester = new RuleTester();
@@ -17,17 +17,17 @@ ruleTester.run('no-direct-import', rule, {
1717
valid: [
1818
{
1919
options: ruleOptions,
20-
code: validExample,
21-
},
20+
code: validExample
21+
}
2222
],
2323
invalid: [
2424
{
2525
options: ruleOptions,
2626
code: invalidExample,
2727
output: `import {Component} from 'another-module';`,
2828
errors: [
29-
{ message: `Do not import directly from 'some-module'. Please use 'another-module' (autofix available).` },
30-
],
31-
},
32-
],
29+
{message: `Do not import directly from 'some-module'. Please use 'another-module' (autofix available).`}
30+
]
31+
}
32+
]
3333
});

0 commit comments

Comments
 (0)