Skip to content

Commit 2098797

Browse files
bradzacherljharb
authored andcommittedApr 12, 2019
[fix] export: false positives for typescript type + value export
1 parent 70a59fe commit 2098797

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed
 

Diff for: ‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
"eslint": "2.x - 5.x"
7878
},
7979
"dependencies": {
80+
"array-includes": "^3.0.3",
8081
"contains-path": "^0.1.0",
8182
"debug": "^2.6.9",
8283
"doctrine": "1.5.0",

Diff for: ‎src/rules/export.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import ExportMap, { recursivePatternCapture } from '../ExportMap'
22
import docsUrl from '../docsUrl'
3+
import includes from 'array-includes'
34

45
module.exports = {
56
meta: {
@@ -12,12 +13,13 @@ module.exports = {
1213
create: function (context) {
1314
const named = new Map()
1415

15-
function addNamed(name, node) {
16-
let nodes = named.get(name)
16+
function addNamed(name, node, type) {
17+
const key = type ? `${type}:${name}` : name
18+
let nodes = named.get(key)
1719

1820
if (nodes == null) {
1921
nodes = new Set()
20-
named.set(name, nodes)
22+
named.set(key, nodes)
2123
}
2224

2325
nodes.add(node)
@@ -34,7 +36,14 @@ module.exports = {
3436
if (node.declaration == null) return
3537

3638
if (node.declaration.id != null) {
37-
addNamed(node.declaration.id.name, node.declaration.id)
39+
if (includes([
40+
'TSTypeAliasDeclaration',
41+
'TSInterfaceDeclaration',
42+
], node.declaration.type)) {
43+
addNamed(node.declaration.id.name, node.declaration.id, 'type')
44+
} else {
45+
addNamed(node.declaration.id.name, node.declaration.id)
46+
}
3847
}
3948

4049
if (node.declaration.declarations != null) {

Diff for: ‎tests/src/rules/export.js

+40-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { test, SYNTAX_CASES } from '../utils'
22

3-
import { RuleTester } from 'eslint'
3+
import { RuleTester, linter } from 'eslint'
4+
import semver from 'semver'
45

56
var ruleTester = new RuleTester()
67
, rule = require('rules/export')
@@ -106,3 +107,41 @@ ruleTester.run('export', rule, {
106107
}),
107108
],
108109
})
110+
111+
112+
context('Typescript', function () {
113+
// Typescript
114+
const parsers = ['typescript-eslint-parser']
115+
116+
if (semver.satisfies(linter.version, '>5.0.0')) {
117+
parsers.push('@typescript-eslint/parser')
118+
}
119+
120+
parsers.forEach((parser) => {
121+
const parserConfig = {
122+
parser: parser,
123+
settings: {
124+
'import/parsers': { [parser]: ['.ts'] },
125+
'import/resolver': { 'eslint-import-resolver-typescript': true },
126+
},
127+
}
128+
129+
ruleTester.run('export', rule, {
130+
valid: [
131+
test(Object.assign({
132+
code: `
133+
export const Foo = 1;
134+
export type Foo = number;
135+
`,
136+
}, parserConfig),
137+
test(Object.assign({
138+
code: `
139+
export const Foo = 1;
140+
export interface Foo {}
141+
`,
142+
}, parserConfig),
143+
],
144+
invalid: [],
145+
})
146+
})
147+
})

Diff for: ‎tests/src/utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export function testFilePath(relativePath) {
1212
export const FILENAME = testFilePath('foo.js')
1313

1414
export function testVersion(specifier, t) {
15-
return semver.satisfies(linter.version) && test(t)
15+
return semver.satisfies(linter.version, specifier) && test(t)
1616
}
1717

1818
export function test(t) {

0 commit comments

Comments
 (0)
Please sign in to comment.