Skip to content

Commit

Permalink
[Fix] TypeScript: export: avoid a crash with export =
Browse files Browse the repository at this point in the history
Fixes #1801.
  • Loading branch information
ljharb committed Jun 7, 2020
1 parent 0d6d12e commit 4ff9b92
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 4 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ node_modules
tests/files/malformed.js
tests/files/with-syntax-error
tests/files/just-json-files/invalid.json
tests/files/typescript-d-ts/
resolvers/webpack/test/files
# we want to ignore "tests/files" here, but unfortunately doing so would
# interfere with unit test and fail it for some reason.
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
- [`namespace`]/`ExportMap`: Fix interface declarations for TypeScript ([#1764], thanks [@julien1619])
- [`no-unused-modules`]: avoid order-dependence ([#1744], thanks [@darkartur])
- [`no-internal-modules`]: also check `export from` syntax ([#1691], thanks [@adjerbetian])
- TypeScript: [`export`]: avoid a crash with `export =` ([#1801], thanks [@ljharb])

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

[#1802]: https://github.com/benmosher/eslint-plugin-import/pull/1802
[#1801]: https://github.com/benmosher/eslint-plugin-import/issues/1801
[#1788]: https://github.com/benmosher/eslint-plugin-import/pull/1788
[#1786]: https://github.com/benmosher/eslint-plugin-import/pull/1786
[#1785]: https://github.com/benmosher/eslint-plugin-import/pull/1785
Expand Down
4 changes: 3 additions & 1 deletion src/ExportMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,9 @@ ExportMap.parse = function (path, content, context) {
moduleBlockNode.declaration :
moduleBlockNode

if (namespaceDecl.type === 'VariableDeclaration') {
if (!namespaceDecl) {
// TypeScript can check this for us; we needn't
} else if (namespaceDecl.type === 'VariableDeclaration') {
namespaceDecl.declarations.forEach((d) =>
recursivePatternCapture(d.id, (id) => m.namespace.set(
id.name,
Expand Down
12 changes: 12 additions & 0 deletions tests/files/typescript-d-ts/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"overrides": [
{
"files": "**.ts",
"parser": "@typescript-eslint/parser",
"extends": "../../../config/typescript",
"rules": {
"import/export": "error",
},
},
],
}
6 changes: 6 additions & 0 deletions tests/files/typescript-d-ts/file1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
declare namespace ts {
const x: string;
export { x };
}

export = ts;
1 change: 1 addition & 0 deletions tests/files/typescript-d-ts/file2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './file1.ts'
4 changes: 2 additions & 2 deletions tests/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('CLI regression tests', function () {
})
})
it("doesn't throw an error on gratuitous, erroneous self-reference", function () {
expect(() => cli.executeOnFiles(['./tests/files/issue210.js'])).not.to.throw(Error)
expect(() => cli.executeOnFiles(['./tests/files/issue210.js'])).not.to.throw()
})
})

Expand All @@ -41,7 +41,7 @@ describe('CLI regression tests', function () {
}
})

it('throws an error on invalid JSON', function () {
it('throws an error on invalid JSON', () => {
const invalidJSON = './tests/files/just-json-files/invalid.json'
const results = cli.executeOnFiles([invalidJSON])
expect(results).to.eql({
Expand Down
6 changes: 5 additions & 1 deletion tests/src/rules/export.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test, SYNTAX_CASES, getTSParsers } from '../utils'
import { test, testFilePath, SYNTAX_CASES, getTSParsers } from '../utils'

import { RuleTester } from 'eslint'

Expand Down Expand Up @@ -187,6 +187,10 @@ context('TypeScript', function () {
}
`,
}, parserConfig)),
test(Object.assign({
code: 'export * from "./file1.ts"',
filename: testFilePath('typescript-d-ts/file-2.ts'),
}, parserConfig)),
],
invalid: [
// type/value name clash
Expand Down

0 comments on commit 4ff9b92

Please sign in to comment.