Skip to content

Infra/no import rule multiple #1419

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 3 commits into from
Aug 3, 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
2 changes: 2 additions & 0 deletions eslint-rules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module.exports = {
'function-deprecation': require('./lib/rules/function-deprecation'),
'prop-value-shape-deprecation': require('./lib/rules/prop-value-shape-deprecation'),
// for duplicate rules usage
'no-direct-import_warn': require('./lib/rules/no-direct-import'),
'no-direct-import_error': require('./lib/rules/no-direct-import'),
'component-deprecation_warn': require('./lib/rules/component-deprecation'),
'component-prop-deprecation_warn': require('./lib/rules/component-prop-deprecation'),
'assets-deprecation_warn': require('./lib/rules/assets-deprecation'),
Expand Down
53 changes: 38 additions & 15 deletions eslint-rules/lib/rules/no-direct-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,49 @@ const MAP_SCHEMA = {
},
applyAutofix: {
type: 'boolean'
},
rules: {
type: 'array',
items: [
{
type: 'object',
properties: {
origin: {
type: 'string'
},
destination: {
type: 'string'
},
applyAutofix: {
type: 'boolean'
}
}
}
]
}
},
additionalProperties: false
additionalProperties: true
};

module.exports = {
meta: {
docs: {
description: 'Do not import directly from open source project',
category: 'Best Practices',
recommended: true,
recommended: true
},
messages: {
uiLib: 'Do not import directly from this source. Please use another import source (autofix may be available).',
uiLib: 'Do not import directly from this source. Please use another import source (autofix may be available).'
},
fixable: 'code',
schema: [
MAP_SCHEMA,
],
schema: [MAP_SCHEMA]
},
create(context) {
function reportDirectImport(node) {
function reportDirectImport(node, rule) {
try {
const origin = context.options[0].origin;
const destination = context.options[0].destination;
const applyAutofix = context.options[0].applyAutofix;
const origin = rule.origin;
const destination = rule.destination;
const applyAutofix = rule.applyAutofix;
const autofixMessage = applyAutofix ? ' (autofix available)' : '';
const message = `Do not import directly from '${origin}'. Please use '${destination}'${autofixMessage}.`;
context.report({
Expand All @@ -47,23 +64,29 @@ module.exports = {
if (node && applyAutofix && destination) {
return fixer.replaceText(node.source, `'${destination}'`);
}
},
}
});
} catch (err) {
handleError(RULE_ID, err, context.getFilename());
}
}

function getRules() {
// To support both structures; single rule or array of rules
return context.options[0].rules || [context.options[0]];
}

function checkImportDeclaration(node) {
const origin = context.options[0].origin;
const source = node.source.value;
if (source && origin && source === origin) {
reportDirectImport(node);
const rule = getRules().find((rule) => rule.origin === source);

if (rule) {
reportDirectImport(node, rule);
}
}

return {
ImportDeclaration: checkImportDeclaration
};
},
}
};
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": "2.0.9",
"version": "2.1.0",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we should bump from 2.0.9 to 2.1.0 or to 2.0.10.. do you know if there's any logic for that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Base on Semantic Versioning.
("MINOR version when you add functionality in a backwards compatible manner")

"description": "uilib set of eslint rules",
"keywords": [
"eslint",
Expand Down
53 changes: 47 additions & 6 deletions eslint-rules/tests/lib/rules/no-direct-import.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
const RuleTester = require('eslint').RuleTester;
const rule = require('../../../lib/rules/no-direct-import');

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

const ruleOptionsArray = [
{
rules: [
{origin: 'some-module', destination: 'another-module', applyAutofix: true},
{origin: 'old-module', destination: 'new-module', applyAutofix: true}
]
}
];

RuleTester.setDefaultConfig({
parser: 'babel-eslint',
Expand All @@ -10,23 +21,53 @@ RuleTester.setDefaultConfig({

const ruleTester = new RuleTester();

const validExample = `import {Component} from 'another-module';`;
const invalidExample = `import {Component} from 'some-module';`;
const validExample1 = `import {Component} from 'another-module';`;
const validExample2 = `import {Component} from 'new-module';`;

const invalidExample1 = `import {Component} from 'some-module';`;
const invalidExample2 = `import {Component} from 'old-module';`;

const error1 = `Do not import directly from 'some-module'. Please use 'another-module' (autofix available).`;
const error2 = `Do not import directly from 'old-module'. Please use 'new-module' (autofix available).`;

ruleTester.run('no-direct-import', rule, {
valid: [
{
options: ruleOptions,
code: validExample
code: validExample1
},
{
options: ruleOptionsArray,
code: validExample1
},
{
options: ruleOptionsArray,
code: validExample2
}
],
invalid: [
{
options: ruleOptions,
code: invalidExample,
code: invalidExample1,
output: `import {Component} from 'another-module';`,
errors: [
{message: error1}
]
},
{
options: ruleOptionsArray,
code: invalidExample1,
output: `import {Component} from 'another-module';`,
errors: [
{message: `Do not import directly from 'some-module'. Please use 'another-module' (autofix available).`}
{message: error1}
]
},
{
options: ruleOptionsArray,
code: invalidExample2,
output: `import {Component} from 'new-module';`,
errors: [
{message: error2}
]
}
]
Expand Down