Skip to content

Commit ab49ed2

Browse files
keyiiiiiyoshiko-pg
andauthored
Added new option 'allowTypeImport' (#50)
* feat: added option to allow type import * test: add allowTypeImport test * docs: add allowTypeImport * Update __tests__/index.js Co-authored-by: よしこ <[email protected]> * Update strict-dependencies/index.js Co-authored-by: よしこ <[email protected]> * fix: #50 (review) --------- Co-authored-by: よしこ <[email protected]>
1 parent 2677606 commit ab49ed2

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ npm install eslint-plugin-strict-dependencies --save-dev
2222
- Paths of files where target module imports are allowed.
2323
- allowSameModule: `boolean`
2424
- Whether it can be imported by other files in the same directory
25+
- excludeTypeImportChecks: `boolean`
26+
- Whether to exclude type import checks
27+
- e.x. `import type { Suspense } from 'react'`
2528

2629
### Options
2730

@@ -75,7 +78,9 @@ npm install eslint-plugin-strict-dependencies --save-dev
7578
"module": "src/components/ui",
7679
"allowReferenceFrom": ["src/components/page"],
7780
// components/ui can import other components/ui
78-
"allowSameModule": true
81+
"allowSameModule": true,
82+
// components/ui exclude type import checks
83+
"excludeTypeImportChecks": true
7984
},
8085
8186
/**

__tests__/index.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ const mockImportDeclaration = {
7979
},
8080
};
8181

82+
const mockImportDeclarationImportKindType = {
83+
importKind: 'type',
84+
...mockImportDeclaration
85+
};
86+
8287
describe('create', () => {
8388
it('should return object', () => {
8489
const created = create({options: [[]]})
@@ -455,4 +460,31 @@ describe('create.ImportDeclaration', () => {
455460
expect(getFilename).toBeCalledTimes(1)
456461
expect(report).not.toBeCalled()
457462
})
463+
464+
it('should not report if excludeTypeImportChecks is true', () => {
465+
resolveImportPath.mockReturnValue('src/components/ui/Text')
466+
const getFilename = jest.fn(() =>
467+
path.join(process.cwd(), 'src/pages/index.tsx')
468+
)
469+
const report = jest.fn()
470+
const { ImportDeclaration: checkImport } = create({
471+
options: [
472+
[
473+
{
474+
module: 'src/components/ui',
475+
allowReferenceFrom: ['src/aaa'],
476+
allowSameModule: false,
477+
excludeTypeImportChecks: true,
478+
},
479+
],
480+
],
481+
getFilename,
482+
report,
483+
})
484+
485+
checkImport(mockImportDeclarationImportKindType)
486+
487+
expect(getFilename).toBeCalledTimes(1)
488+
expect(report).not.toBeCalled()
489+
})
458490
})

strict-dependencies/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ module.exports = {
4040
allowSameModule: {
4141
type: 'boolean',
4242
},
43+
excludeTypeImportChecks: {
44+
type: 'boolean'
45+
}
4346
},
4447
},
4548
],
@@ -88,6 +91,8 @@ module.exports = {
8891
isMatch(relativeFilePath, allowPath),
8992
) || // または同一モジュール間の参照が許可されている場合
9093
(dependency.allowSameModule && isMatch(relativeFilePath, dependency.module))
94+
// または明示的に対象外としたtype importである場合
95+
|| (dependency.excludeTypeImportChecks && node.importKind === 'type')
9196

9297
if (isAllowedByPath) return
9398

0 commit comments

Comments
 (0)