From ec87b6410d651b327df2b2ee20ea337b7de09e8c Mon Sep 17 00:00:00 2001 From: Mattijs Bliek Date: Fri, 13 Apr 2018 12:38:42 +0200 Subject: [PATCH] Ignore type imports for named rule. (#1057) --- CHANGELOG.md | 1 + docs/rules/named.md | 3 ++- src/rules/named.js | 3 ++- tests/src/rules/named.js | 24 +++--------------------- 4 files changed, 8 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7467a8f62..a13962d23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). This change log adheres to standards from [Keep a CHANGELOG](http://keepachangelog.com). ## [Unreleased] +- Ignore type imports for named rule ([#931], thanks [@mattijsbliek]) - Add documentation for [`no-useless-path-segments`] rule ([#1068], thanks [@manovotny]) - Fixer for [`first`] ([#1046], thanks [@fengkfengk]) diff --git a/docs/rules/named.md b/docs/rules/named.md index 6dc7c60e5..0830af5e4 100644 --- a/docs/rules/named.md +++ b/docs/rules/named.md @@ -8,10 +8,11 @@ Note: for packages, the plugin will find exported names from [`jsnext:main`], if present in `package.json`. Redux's npm module includes this key, and thereby is lintable, for example. -A module path that is [ignored] or not [unambiguously an ES module] will not be reported when imported. +A module path that is [ignored] or not [unambiguously an ES module] will not be reported when imported. Note that type imports, as used by [Flow], are always ignored. [ignored]: ../../README.md#importignore [unambiguously an ES module]: https://github.com/bmeck/UnambiguousJavaScriptGrammar +[Flow]: https://flow.org/ ## Rule Details diff --git a/src/rules/named.js b/src/rules/named.js index aa17dfb40..8c2acd714 100644 --- a/src/rules/named.js +++ b/src/rules/named.js @@ -11,7 +11,8 @@ module.exports = { create: function (context) { function checkSpecifiers(key, type, node) { - if (node.source == null) return // local export, ignore + // ignore local exports and type imports + if (node.source == null || node.importKind === 'type') return if (!node.specifiers .some(function (im) { return im.type === type })) { diff --git a/tests/src/rules/named.js b/tests/src/rules/named.js index 8bd78f6eb..4fdd3434f 100644 --- a/tests/src/rules/named.js +++ b/tests/src/rules/named.js @@ -67,18 +67,10 @@ ruleTester.run('named', rule, { test({ code: 'import { deepProp } from "./named-exports"' }), test({ code: 'import { deepSparseElement } from "./named-exports"' }), - // flow types + // should ignore imported flow types, even if they don’t exist test({ - code: 'import type { MyType } from "./flowtypes"', - 'parser': 'babel-eslint', - }), - test({ - code: 'import type { MyInterface } from "./flowtypes"', - 'parser': 'babel-eslint', - }), - test({ - code: 'import type { MyClass } from "./flowtypes"', - 'parser': 'babel-eslint', + code: 'import type { MissingType } from "./flowtypes"', + parser: 'babel-eslint', }), // TypeScript @@ -244,16 +236,6 @@ ruleTester.run('named', rule, { // }], // }), - // flow types - test({ - code: 'import type { MissingType } from "./flowtypes"', - parser: 'babel-eslint', - errors: [{ - message: "MissingType not found in './flowtypes'", - type: 'Identifier', - }], - }), - // TypeScript test({ code: 'import { MissingType } from "./typescript"',