Skip to content

Commit

Permalink
Merge 6ec0c03 into e1ed323
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim-Mazurok committed Jun 4, 2020
2 parents e1ed323 + 6ec0c03 commit 0145ec0
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@
"minimatch": "^3.0.4",
"object.values": "^1.1.0",
"read-pkg-up": "^2.0.0",
"resolve": "^1.12.0"
"resolve": "^1.12.0",
"tsconfig-paths": "^3.9.0"
},
"nyc": {
"require": [
Expand Down
17 changes: 17 additions & 0 deletions src/ExportMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import isIgnored, { hasValidExtension } from 'eslint-module-utils/ignore'
import { hashObject } from 'eslint-module-utils/hash'
import * as unambiguous from 'eslint-module-utils/unambiguous'

import { tsConfigLoader } from 'tsconfig-paths/lib/tsconfig-loader'

const log = debug('eslint-plugin-import:ExportMap')

const exportCache = new Map()
Expand Down Expand Up @@ -445,6 +447,18 @@ ExportMap.parse = function (path, content, context) {

const source = makeSourceCode(content, ast)

function isEsModuleInterop() {
const tsConfig = tsConfigLoader({
cwd: context.parserOptions.tsconfigRootDir || process.cwd(),
getEnv: (key) => process.env[key],
})
if (tsConfig.tsConfigPath !== undefined) {
const json = fs.readFileSync(tsConfig.tsConfigPath)
return JSON.parse(json).compilerOptions.esModuleInterop
}
return false
}

ast.body.forEach(function (n) {

if (n.type === 'ExportDefaultDeclaration') {
Expand Down Expand Up @@ -553,6 +567,9 @@ ExportMap.parse = function (path, content, context) {
m.namespace.set('default', captureDoc(source, docStyleParsers, n))
return
}
if (isEsModuleInterop()) {
m.namespace.set('default', {})
}
exportedDecls.forEach((decl) => {
if (decl.type === 'TSModuleDeclaration') {
if (decl.body && decl.body.type === 'TSModuleDeclaration') {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export = FooBar;

declare namespace FooBar {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"esModuleInterop": true
}
}
21 changes: 21 additions & 0 deletions tests/src/rules/default.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from 'path'
import { test, SYNTAX_CASES, getTSParsers } from '../utils'
import { RuleTester } from 'eslint'

Expand Down Expand Up @@ -189,6 +190,17 @@ context('TypeScript', function () {
'import/resolver': { 'eslint-import-resolver-typescript': true },
},
}),
test({
code: `import React from "./typescript-export-assign-default-namespace"`,
parser: parser,
settings: {
'import/parsers': { [parser]: ['.ts'] },
'import/resolver': { 'eslint-import-resolver-typescript': true },
},
parserOptions: {
tsconfigRootDir: path.resolve(__dirname, '../../files/typescript-export-assign-default-namespace/'),
},
}),
],

invalid: [
Expand All @@ -201,6 +213,15 @@ context('TypeScript', function () {
},
errors: ['No default export found in imported module "./typescript".'],
}),
test({
code: `import React from "./typescript-export-assign-default-namespace"`,
parser: parser,
settings: {
'import/parsers': { [parser]: ['.ts'] },
'import/resolver': { 'eslint-import-resolver-typescript': true },
},
errors: ['No default export found in imported module "./typescript-export-assign-default-namespace".'],
}),
],
})
})
Expand Down

0 comments on commit 0145ec0

Please sign in to comment.