Skip to content

Commit

Permalink
Do not report type imports from flow-typed packages
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tf committed Apr 3, 2018
1 parent b34d9ff commit 16f678b
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/rules/named.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ module.exports = {
return // no named imports/exports
}

if (isTypeFromPackage(node)) return

const imports = Exports.get(node.source.value, context)
if (imports == null) return

Expand Down Expand Up @@ -47,6 +49,14 @@ module.exports = {
})
}

function isTypeFromPackage(node) {
return node.importKind === 'type' && !hasRelativePath(node.source)
}

function hasRelativePath(source) {
return source.value.match(/^\.\//)
}

return {
'ImportDeclaration': checkSpecifiers.bind( null
, 'imported'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// flow-typed signature: ____
// flow-typed version: ____/myflowtyped_v1.x.x/flow_>=v0.33.x

declare module 'myflowtyped' {}
declare module 'myflowtyped' {
declare export type MyType = boolean;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion tests/files/with-flow-typed/package.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"dependencies": {}
"dependencies": {
"myflowtyped": "1.0.0"
}
}
8 changes: 8 additions & 0 deletions tests/src/rules/named.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { test, SYNTAX_CASES } from '../utils'
import * as path from 'path'
import { RuleTester } from 'eslint'

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

const packageDirWithFlowTyped = path.join(__dirname, '../../files/with-flow-typed')

ruleTester.run('named', rule, {
valid: [
test({ code: 'import "./malformed.js"' }),
Expand Down Expand Up @@ -80,6 +83,11 @@ ruleTester.run('named', rule, {
code: 'import type { MyClass } from "./flowtypes"',
'parser': 'babel-eslint',
}),
test({
filename: packageDirWithFlowTyped + '/foo.js',
code: 'import type { MyType } from "myflowtyped"',
'parser': 'babel-eslint',
}),

// TypeScript
test({
Expand Down

0 comments on commit 16f678b

Please sign in to comment.