diff --git a/package.json b/package.json index b41398961..7975e244f 100644 --- a/package.json +++ b/package.json @@ -77,6 +77,7 @@ "eslint": "2.x - 5.x" }, "dependencies": { + "array-includes": "^3.0.3", "contains-path": "^0.1.0", "debug": "^2.6.9", "doctrine": "1.5.0", diff --git a/src/rules/export.js b/src/rules/export.js index db5c8c3c1..a4691bbe9 100644 --- a/src/rules/export.js +++ b/src/rules/export.js @@ -1,5 +1,6 @@ import ExportMap, { recursivePatternCapture } from '../ExportMap' import docsUrl from '../docsUrl' +import includes from 'array-includes' module.exports = { meta: { @@ -12,12 +13,13 @@ module.exports = { create: function (context) { const named = new Map() - function addNamed(name, node) { - let nodes = named.get(name) + function addNamed(name, node, type) { + const key = type ? `${type}:${name}` : name + let nodes = named.get(key) if (nodes == null) { nodes = new Set() - named.set(name, nodes) + named.set(key, nodes) } nodes.add(node) @@ -34,7 +36,14 @@ module.exports = { if (node.declaration == null) return if (node.declaration.id != null) { - addNamed(node.declaration.id.name, node.declaration.id) + if (includes([ + 'TSTypeAliasDeclaration', + 'TSInterfaceDeclaration', + ], node.declaration.type)) { + addNamed(node.declaration.id.name, node.declaration.id, 'type') + } else { + addNamed(node.declaration.id.name, node.declaration.id) + } } if (node.declaration.declarations != null) { diff --git a/tests/src/rules/export.js b/tests/src/rules/export.js index 33f012123..771cbb431 100644 --- a/tests/src/rules/export.js +++ b/tests/src/rules/export.js @@ -1,6 +1,7 @@ import { test, SYNTAX_CASES } from '../utils' -import { RuleTester } from 'eslint' +import { RuleTester, linter } from 'eslint' +import semver from 'semver' var ruleTester = new RuleTester() , rule = require('rules/export') @@ -106,3 +107,41 @@ ruleTester.run('export', rule, { }), ], }) + + +context('Typescript', function () { + // Typescript + const parsers = ['typescript-eslint-parser'] + + if (semver.satisfies(linter.version, '>5.0.0')) { + parsers.push('@typescript-eslint/parser') + } + + parsers.forEach((parser) => { + const parserConfig = { + parser: parser, + settings: { + 'import/parsers': { [parser]: ['.ts'] }, + 'import/resolver': { 'eslint-import-resolver-typescript': true }, + }, + } + + ruleTester.run('export', rule, { + valid: [ + test(Object.assign({ + code: ` + export const Foo = 1; + export type Foo = number; + `, + }, parserConfig), + test(Object.assign({ + code: ` + export const Foo = 1; + export interface Foo {} + `, + }, parserConfig), + ], + invalid: [], + }) + }) +}) diff --git a/tests/src/utils.js b/tests/src/utils.js index 70c5971a6..4d4f42dc8 100644 --- a/tests/src/utils.js +++ b/tests/src/utils.js @@ -12,7 +12,7 @@ export function testFilePath(relativePath) { export const FILENAME = testFilePath('foo.js') export function testVersion(specifier, t) { - return semver.satisfies(linter.version) && test(t) + return semver.satisfies(linter.version, specifier) && test(t) } export function test(t) {