Skip to content

Commit

Permalink
Merge pull request #150 from Shopify/eslint-plugin/strict-component-b…
Browse files Browse the repository at this point in the history
…oundaries-options

[eslint-plugin] adding new options to strict-component-boundaries
  • Loading branch information
Pat Sissons authored May 5, 2020
2 parents 09a5b1a + c07a2fc commit f567cc9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
31 changes: 29 additions & 2 deletions packages/eslint-plugin/lib/rules/strict-component-boundaries.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,30 @@ module.exports = {
uri: docsUrl('strict-component-boundaries'),
},
fixable: null,
schema: [
{
type: 'object',
properties: {
allow: {
type: 'array',
items: {type: 'string'},
},
maxDepth: {
type: 'integer',
},
},
additionalProperties: false,
},
],
},
create(context) {
const {
options: [{allow = [], maxDepth = 1} = {}],
} = context;
const allowRegexps = (allow || []).map(
(pattern) => new RegExp(pattern, 'i'),
);

function report(node) {
context.report({
node,
Expand All @@ -29,6 +51,7 @@ module.exports = {
const resolvedSource = resolve(importSource, context);

if (
isPathAllowed(allowRegexps, importSource) ||
isCoreModule(resolvedSource) ||
isNotFound(resolvedSource) ||
inNodeModules(pathSegmantsFromSource(resolvedSource))
Expand All @@ -41,7 +64,7 @@ module.exports = {

if (
hasAnotherComponentInPath(pathDifferenceParts) &&
pathDifferenceParts.length > 1 &&
pathDifferenceParts.length > maxDepth &&
!indexFile(pathDifference) &&
!validFixtureImport(pathDifferenceParts)
) {
Expand All @@ -51,7 +74,7 @@ module.exports = {

if (
hasDirectoryInPath(pathDifferenceParts, 'components') &&
pathDifferenceParts.length > 2 &&
pathDifferenceParts.length > maxDepth + 1 &&
!validFixtureImport(pathDifferenceParts)
) {
report(node);
Expand All @@ -61,6 +84,10 @@ module.exports = {
},
};

function isPathAllowed(allowRegexps, importSource) {
return allowRegexps.some((re) => re.test(importSource));
}

function isNotFound(resolvedSource) {
return resolvedSource === undefined;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ ruleTester.run('strict-component-boundaries', rule, {
'basic-app/app/components/Foo/components/Bar/index.js',
),
},
{
code: `import someThing from './components/Foo';`,
parserOptions,
options: [{allow: ['components/\\w+$']}],
filename: fixtureFile('basic-app/app/index.js'),
},
{
code: `import someThing from './components/Foo';`,
parserOptions,
options: [{maxDepth: 2}],
filename: fixtureFile('basic-app/app/index.js'),
},
],
invalid: [
{
Expand All @@ -88,5 +100,19 @@ ruleTester.run('strict-component-boundaries', rule, {
errors,
filename: fixtureFile('basic-app/app/components/Foo/index.js'),
},
{
code: `import someThing from './components/Foo/Foo';`,
parserOptions,
options: [{allow: ['components/\\w+$']}],
errors,
filename: fixtureFile('basic-app/app/index.js'),
},
{
code: `import someThing from './components/Foo/Foo';`,
parserOptions,
options: [{maxDepth: 2}],
errors,
filename: fixtureFile('basic-app/app/index.js'),
},
],
});

0 comments on commit f567cc9

Please sign in to comment.