This repository has been archived by the owner on Aug 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
cartogram
committed
Oct 15, 2018
1 parent
7d4d64e
commit e257b2d
Showing
9 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Prefer that imports from within a directory extend to the file from where they are importing without relying on an index file. (no-ancestor-directory-import) | ||
|
||
Imports inside the same directory should extend directly to the file from where they are importing without relying on an index file. This means the source of these imports should not end with a directory (`/`), but the path should terminate at an individual filename. This preserves the index file inside a directory as a mechanism for exposing pieces of the module to the outside application. | ||
|
||
## Rule Details | ||
|
||
This rule disallows any full directory imports from within that same directory. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```ts | ||
import Thing from '../'; | ||
import OtherThing from './'; | ||
import Module from '../../index.ts' | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```ts | ||
import Thing from '../Thing'; | ||
import OtherThing from './OtherThing'; | ||
import Module from '../Module' | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you do not wish to prevent directory imports from within that directory, you can safely disable this rule. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const {extname, basename, relative, sep} = require('path'); | ||
const resolve = require('eslint-module-utils/resolve').default; | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: | ||
'Prefer that imports from within a directory extend to the file from where they are importing without relying on an index file.', | ||
category: 'Best Practices', | ||
recommended: false, | ||
uri: | ||
'https://github.com/Shopify/eslint-plugin-shopify/blob/master/docs/rules/no-ancestor-directory-import.md', | ||
}, | ||
fixable: null, | ||
}, | ||
create(context) { | ||
function isAncestorDirectoryImport(resolvedSource) { | ||
const relativeDifference = relative( | ||
context.getFilename(), | ||
resolvedSource, | ||
); | ||
|
||
const parts = relativeDifference | ||
.split(sep) | ||
.filter((part) => part[0] !== '.'); | ||
|
||
if (parts.length > 1) { | ||
return false; | ||
} | ||
|
||
return basename(parts[0], extname(parts[0])) === 'index'; | ||
} | ||
|
||
return { | ||
ImportDeclaration(node) { | ||
const importSource = node.source.value; | ||
const resolvedSource = resolve(importSource, context); | ||
|
||
if (resolvedSource && isAncestorDirectoryImport(resolvedSource)) { | ||
context.report({ | ||
node, | ||
message: | ||
'Relative local imports should extend to the file from where they are importing without relying on an index file.', | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
const {RuleTester} = require('eslint'); | ||
const {fixtureFile} = require('../../utilities'); | ||
const rule = require('../../../lib/rules/no-ancestor-directory-import'); | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 6, | ||
sourceType: 'module', | ||
}, | ||
}); | ||
|
||
const errors = [ | ||
{ | ||
type: 'ImportDeclaration', | ||
message: | ||
'Relative local imports should extend to the file from where they are importing without relying on an index file.', | ||
}, | ||
]; | ||
|
||
ruleTester.run('no-ancestor-directory-import', rule, { | ||
valid: [ | ||
{ | ||
code: "import Foo from '../Foo'", | ||
filename: fixtureFile('basic-app/app/components/Foo/tests/Foo.test.js'), | ||
}, | ||
{ | ||
code: "import {Foo, Bar} from '../../components'", | ||
filename: fixtureFile('basic-app/app/sections/MySection/MySection.js'), | ||
}, | ||
{ | ||
code: "import Bar from '../Bar'", | ||
filename: fixtureFile('basic-app/app/components/Foo/Foo.js'), | ||
}, | ||
{ | ||
code: "import Bar from '../Bar'", | ||
filename: fixtureFile( | ||
'basic-app/app/components/Foo/components/Baz/Baz.js', | ||
), | ||
}, | ||
{ | ||
code: "import Qux from '../Qux'", | ||
filename: fixtureFile( | ||
'basic-app/app/components/Foo/components/Baz/Baz.js', | ||
), | ||
}, | ||
{ | ||
code: "import Bar from '../../../Bar'", | ||
filename: fixtureFile( | ||
'basic-app/app/components/Foo/components/Baz/Baz.js', | ||
), | ||
}, | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: "import Foo from '../'", | ||
errors, | ||
filename: fixtureFile('basic-app/app/components/Foo/tests/Foo.test.js'), | ||
}, | ||
{ | ||
code: "import Foo from '..'", | ||
errors, | ||
filename: fixtureFile('basic-app/app/components/Foo/tests/Foo.test.js'), | ||
}, | ||
{ | ||
code: "import Foo from './'", | ||
errors, | ||
filename: fixtureFile('basic-app/app/components/Foo.js'), | ||
}, | ||
{ | ||
code: "import Foo from '.'", | ||
errors, | ||
filename: fixtureFile('basic-app/app/components/Foo/Foo.js'), | ||
}, | ||
{ | ||
code: "import Foo from '../../index'", | ||
errors, | ||
filename: fixtureFile('basic-app/app/components/Foo/tests/Foo.test.js'), | ||
}, | ||
{ | ||
code: "import Foo from '../../index.js'", | ||
errors, | ||
filename: fixtureFile('basic-app/app/components/Foo/tests/Foo.test.js'), | ||
}, | ||
{ | ||
code: "import Bar from '..'", | ||
errors, | ||
filename: fixtureFile('basic-app/app/components/Foo/Foo.js'), | ||
}, | ||
{ | ||
code: "import Bar from '../../..'", | ||
errors, | ||
filename: fixtureFile( | ||
'basic-app/app/components/Foo/components/Baz/Baz.js', | ||
), | ||
}, | ||
], | ||
}); |