Skip to content

Commit

Permalink
[fix] export: false positives for typescript type + value export
Browse files Browse the repository at this point in the history
  • Loading branch information
bradzacher authored and ljharb committed Apr 12, 2019
1 parent 70a59fe commit 2098797
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -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",
Expand Down
17 changes: 13 additions & 4 deletions 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: {
Expand All @@ -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)
Expand All @@ -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) {
Expand Down
41 changes: 40 additions & 1 deletion 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')
Expand Down Expand Up @@ -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: [],
})
})
})
2 changes: 1 addition & 1 deletion tests/src/utils.js
Expand Up @@ -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) {
Expand Down

0 comments on commit 2098797

Please sign in to comment.