Skip to content

Commit 16f678b

Browse files
committed
Do not report type imports from flow-typed packages
When importing types from packages that are defined in a file inside the `flow-typed` directory, prevent the `named` rule from reporting that the package itself does not provide an export with that name. Currently there is support for type imports from local files. Restrict these checks to imports with relative paths.
1 parent b34d9ff commit 16f678b

File tree

6 files changed

+30
-2
lines changed

6 files changed

+30
-2
lines changed

src/rules/named.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ module.exports = {
1818
return // no named imports/exports
1919
}
2020

21+
if (isTypeFromPackage(node)) return
22+
2123
const imports = Exports.get(node.source.value, context)
2224
if (imports == null) return
2325

@@ -47,6 +49,14 @@ module.exports = {
4749
})
4850
}
4951

52+
function isTypeFromPackage(node) {
53+
return node.importKind === 'type' && !hasRelativePath(node.source)
54+
}
55+
56+
function hasRelativePath(source) {
57+
return source.value.match(/^\.\//)
58+
}
59+
5060
return {
5161
'ImportDeclaration': checkSpecifiers.bind( null
5262
, 'imported'
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// flow-typed signature: ____
22
// flow-typed version: ____/myflowtyped_v1.x.x/flow_>=v0.33.x
33

4-
declare module 'myflowtyped' {}
4+
declare module 'myflowtyped' {
5+
declare export type MyType = boolean;
6+
}

tests/files/with-flow-typed/node_modules/myflowtyped/index.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/files/with-flow-typed/node_modules/myflowtyped/package.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
{
2-
"dependencies": {}
2+
"dependencies": {
3+
"myflowtyped": "1.0.0"
4+
}
35
}

tests/src/rules/named.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { test, SYNTAX_CASES } from '../utils'
2+
import * as path from 'path'
23
import { RuleTester } from 'eslint'
34

45
import { CASE_SENSITIVE_FS } from 'eslint-module-utils/resolve'
@@ -12,6 +13,8 @@ function error(name, module) {
1213
, type: 'Identifier' }
1314
}
1415

16+
const packageDirWithFlowTyped = path.join(__dirname, '../../files/with-flow-typed')
17+
1518
ruleTester.run('named', rule, {
1619
valid: [
1720
test({ code: 'import "./malformed.js"' }),
@@ -80,6 +83,11 @@ ruleTester.run('named', rule, {
8083
code: 'import type { MyClass } from "./flowtypes"',
8184
'parser': 'babel-eslint',
8285
}),
86+
test({
87+
filename: packageDirWithFlowTyped + '/foo.js',
88+
code: 'import type { MyType } from "myflowtyped"',
89+
'parser': 'babel-eslint',
90+
}),
8391

8492
// TypeScript
8593
test({

0 commit comments

Comments
 (0)