Skip to content

Commit 0ca0556

Browse files
authored
Infra/no import rule multiple (#1419)
* Adding support for no-direct warn and error rules * Add support for multiple rules * version bump
1 parent 9673f22 commit 0ca0556

File tree

4 files changed

+88
-22
lines changed

4 files changed

+88
-22
lines changed

eslint-rules/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ module.exports = {
99
'function-deprecation': require('./lib/rules/function-deprecation'),
1010
'prop-value-shape-deprecation': require('./lib/rules/prop-value-shape-deprecation'),
1111
// for duplicate rules usage
12+
'no-direct-import_warn': require('./lib/rules/no-direct-import'),
13+
'no-direct-import_error': require('./lib/rules/no-direct-import'),
1214
'component-deprecation_warn': require('./lib/rules/component-deprecation'),
1315
'component-prop-deprecation_warn': require('./lib/rules/component-prop-deprecation'),
1416
'assets-deprecation_warn': require('./lib/rules/assets-deprecation'),

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

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,49 @@ const MAP_SCHEMA = {
1212
},
1313
applyAutofix: {
1414
type: 'boolean'
15+
},
16+
rules: {
17+
type: 'array',
18+
items: [
19+
{
20+
type: 'object',
21+
properties: {
22+
origin: {
23+
type: 'string'
24+
},
25+
destination: {
26+
type: 'string'
27+
},
28+
applyAutofix: {
29+
type: 'boolean'
30+
}
31+
}
32+
}
33+
]
1534
}
1635
},
17-
additionalProperties: false
36+
additionalProperties: true
1837
};
1938

2039
module.exports = {
2140
meta: {
2241
docs: {
2342
description: 'Do not import directly from open source project',
2443
category: 'Best Practices',
25-
recommended: true,
44+
recommended: true
2645
},
2746
messages: {
28-
uiLib: 'Do not import directly from this source. Please use another import source (autofix may be available).',
47+
uiLib: 'Do not import directly from this source. Please use another import source (autofix may be available).'
2948
},
3049
fixable: 'code',
31-
schema: [
32-
MAP_SCHEMA,
33-
],
50+
schema: [MAP_SCHEMA]
3451
},
3552
create(context) {
36-
function reportDirectImport(node) {
53+
function reportDirectImport(node, rule) {
3754
try {
38-
const origin = context.options[0].origin;
39-
const destination = context.options[0].destination;
40-
const applyAutofix = context.options[0].applyAutofix;
55+
const origin = rule.origin;
56+
const destination = rule.destination;
57+
const applyAutofix = rule.applyAutofix;
4158
const autofixMessage = applyAutofix ? ' (autofix available)' : '';
4259
const message = `Do not import directly from '${origin}'. Please use '${destination}'${autofixMessage}.`;
4360
context.report({
@@ -47,23 +64,29 @@ module.exports = {
4764
if (node && applyAutofix && destination) {
4865
return fixer.replaceText(node.source, `'${destination}'`);
4966
}
50-
},
67+
}
5168
});
5269
} catch (err) {
5370
handleError(RULE_ID, err, context.getFilename());
5471
}
5572
}
5673

74+
function getRules() {
75+
// To support both structures; single rule or array of rules
76+
return context.options[0].rules || [context.options[0]];
77+
}
78+
5779
function checkImportDeclaration(node) {
58-
const origin = context.options[0].origin;
5980
const source = node.source.value;
60-
if (source && origin && source === origin) {
61-
reportDirectImport(node);
81+
const rule = getRules().find((rule) => rule.origin === source);
82+
83+
if (rule) {
84+
reportDirectImport(node, rule);
6285
}
6386
}
6487

6588
return {
6689
ImportDeclaration: checkImportDeclaration
6790
};
68-
},
91+
}
6992
};

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": "2.0.9",
3+
"version": "2.1.0",
44
"description": "uilib set of eslint rules",
55
"keywords": [
66
"eslint",

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

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
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', applyAutofix: true}];
4+
const ruleOptions = [
5+
{origin: 'some-module', destination: 'another-module', applyAutofix: true}
6+
];
7+
8+
const ruleOptionsArray = [
9+
{
10+
rules: [
11+
{origin: 'some-module', destination: 'another-module', applyAutofix: true},
12+
{origin: 'old-module', destination: 'new-module', applyAutofix: true}
13+
]
14+
}
15+
];
516

617
RuleTester.setDefaultConfig({
718
parser: 'babel-eslint',
@@ -10,23 +21,53 @@ RuleTester.setDefaultConfig({
1021

1122
const ruleTester = new RuleTester();
1223

13-
const validExample = `import {Component} from 'another-module';`;
14-
const invalidExample = `import {Component} from 'some-module';`;
24+
const validExample1 = `import {Component} from 'another-module';`;
25+
const validExample2 = `import {Component} from 'new-module';`;
26+
27+
const invalidExample1 = `import {Component} from 'some-module';`;
28+
const invalidExample2 = `import {Component} from 'old-module';`;
29+
30+
const error1 = `Do not import directly from 'some-module'. Please use 'another-module' (autofix available).`;
31+
const error2 = `Do not import directly from 'old-module'. Please use 'new-module' (autofix available).`;
1532

1633
ruleTester.run('no-direct-import', rule, {
1734
valid: [
1835
{
1936
options: ruleOptions,
20-
code: validExample
37+
code: validExample1
38+
},
39+
{
40+
options: ruleOptionsArray,
41+
code: validExample1
42+
},
43+
{
44+
options: ruleOptionsArray,
45+
code: validExample2
2146
}
2247
],
2348
invalid: [
2449
{
2550
options: ruleOptions,
26-
code: invalidExample,
51+
code: invalidExample1,
52+
output: `import {Component} from 'another-module';`,
53+
errors: [
54+
{message: error1}
55+
]
56+
},
57+
{
58+
options: ruleOptionsArray,
59+
code: invalidExample1,
2760
output: `import {Component} from 'another-module';`,
2861
errors: [
29-
{message: `Do not import directly from 'some-module'. Please use 'another-module' (autofix available).`}
62+
{message: error1}
63+
]
64+
},
65+
{
66+
options: ruleOptionsArray,
67+
code: invalidExample2,
68+
output: `import {Component} from 'new-module';`,
69+
errors: [
70+
{message: error2}
3071
]
3172
}
3273
]

0 commit comments

Comments
 (0)