Skip to content

fix: false positives for typescript type + value export #1319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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