Skip to content

Commit 4ff9b92

Browse files
committed
[Fix] TypeScript: export: avoid a crash with export =
Fixes #1801.
1 parent 0d6d12e commit 4ff9b92

File tree

8 files changed

+32
-4
lines changed

8 files changed

+32
-4
lines changed

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ node_modules
55
tests/files/malformed.js
66
tests/files/with-syntax-error
77
tests/files/just-json-files/invalid.json
8+
tests/files/typescript-d-ts/
89
resolvers/webpack/test/files
910
# we want to ignore "tests/files" here, but unfortunately doing so would
1011
# interfere with unit test and fail it for some reason.

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
2424
- [`namespace`]/`ExportMap`: Fix interface declarations for TypeScript ([#1764], thanks [@julien1619])
2525
- [`no-unused-modules`]: avoid order-dependence ([#1744], thanks [@darkartur])
2626
- [`no-internal-modules`]: also check `export from` syntax ([#1691], thanks [@adjerbetian])
27+
- TypeScript: [`export`]: avoid a crash with `export =` ([#1801], thanks [@ljharb])
2728

2829
### Changed
2930
- [Refactor] `no-extraneous-dependencies`: use moduleVisitor ([#1735], thanks [@adamborowski])
@@ -690,6 +691,7 @@ for info on changes for earlier releases.
690691
[`memo-parser`]: ./memo-parser/README.md
691692

692693
[#1802]: https://github.com/benmosher/eslint-plugin-import/pull/1802
694+
[#1801]: https://github.com/benmosher/eslint-plugin-import/issues/1801
693695
[#1788]: https://github.com/benmosher/eslint-plugin-import/pull/1788
694696
[#1786]: https://github.com/benmosher/eslint-plugin-import/pull/1786
695697
[#1785]: https://github.com/benmosher/eslint-plugin-import/pull/1785

src/ExportMap.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,9 @@ ExportMap.parse = function (path, content, context) {
590590
moduleBlockNode.declaration :
591591
moduleBlockNode
592592

593-
if (namespaceDecl.type === 'VariableDeclaration') {
593+
if (!namespaceDecl) {
594+
// TypeScript can check this for us; we needn't
595+
} else if (namespaceDecl.type === 'VariableDeclaration') {
594596
namespaceDecl.declarations.forEach((d) =>
595597
recursivePatternCapture(d.id, (id) => m.namespace.set(
596598
id.name,

tests/files/typescript-d-ts/.eslintrc

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"overrides": [
3+
{
4+
"files": "**.ts",
5+
"parser": "@typescript-eslint/parser",
6+
"extends": "../../../config/typescript",
7+
"rules": {
8+
"import/export": "error",
9+
},
10+
},
11+
],
12+
}

tests/files/typescript-d-ts/file1.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
declare namespace ts {
2+
const x: string;
3+
export { x };
4+
}
5+
6+
export = ts;

tests/files/typescript-d-ts/file2.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './file1.ts'

tests/src/cli.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('CLI regression tests', function () {
2222
})
2323
})
2424
it("doesn't throw an error on gratuitous, erroneous self-reference", function () {
25-
expect(() => cli.executeOnFiles(['./tests/files/issue210.js'])).not.to.throw(Error)
25+
expect(() => cli.executeOnFiles(['./tests/files/issue210.js'])).not.to.throw()
2626
})
2727
})
2828

@@ -41,7 +41,7 @@ describe('CLI regression tests', function () {
4141
}
4242
})
4343

44-
it('throws an error on invalid JSON', function () {
44+
it('throws an error on invalid JSON', () => {
4545
const invalidJSON = './tests/files/just-json-files/invalid.json'
4646
const results = cli.executeOnFiles([invalidJSON])
4747
expect(results).to.eql({

tests/src/rules/export.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { test, SYNTAX_CASES, getTSParsers } from '../utils'
1+
import { test, testFilePath, SYNTAX_CASES, getTSParsers } from '../utils'
22

33
import { RuleTester } from 'eslint'
44

@@ -187,6 +187,10 @@ context('TypeScript', function () {
187187
}
188188
`,
189189
}, parserConfig)),
190+
test(Object.assign({
191+
code: 'export * from "./file1.ts"',
192+
filename: testFilePath('typescript-d-ts/file-2.ts'),
193+
}, parserConfig)),
190194
],
191195
invalid: [
192196
// type/value name clash

0 commit comments

Comments
 (0)