Skip to content

Commit

Permalink
Add nested namespace handling for TS
Browse files Browse the repository at this point in the history
  • Loading branch information
julien1619 committed May 13, 2020
1 parent 92caa35 commit 1c7c10f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 20 deletions.
56 changes: 37 additions & 19 deletions src/ExportMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,26 +551,44 @@ ExportMap.parse = function (path, content, context) {
return
}
exportedDecls.forEach((decl) => {
if (decl.type === 'TSModuleDeclaration' && decl && decl.body && decl.body.body) {
decl.body.body.forEach((moduleBlockNode) => {
// Export-assignment exports all members in the namespace, explicitly exported or not.
const namespaceDecl = moduleBlockNode.type === 'ExportNamedDeclaration' ?
moduleBlockNode.declaration :
moduleBlockNode

if (namespaceDecl.type === 'VariableDeclaration') {
namespaceDecl.declarations.forEach((d) =>
recursivePatternCapture(d.id, (id) => m.namespace.set(
id.name,
captureDoc(source, docStyleParsers, decl, namespaceDecl, moduleBlockNode))
if (decl.type === 'TSModuleDeclaration') {
let currentDecl = decl
let moduleDecls = [decl]

// Find recursive TSModuleDeclaration
while (currentDecl.body && currentDecl.body.type === 'TSModuleDeclaration') {
currentDecl = currentDecl.body
moduleDecls.push(currentDecl)
}

if (currentDecl.body && currentDecl.body.body) {
currentDecl.body.body.forEach((moduleBlockNode) => {
// Export-assignment exports all members in the namespace,
// explicitly exported or not.
const namespaceDecl = moduleBlockNode.type === 'ExportNamedDeclaration' ?
moduleBlockNode.declaration :
moduleBlockNode

if (namespaceDecl.type === 'VariableDeclaration') {
namespaceDecl.declarations.forEach((d) =>
recursivePatternCapture(d.id, (id) => m.namespace.set(
id.name,
captureDoc(
source,
docStyleParsers,
...moduleDecls,
namespaceDecl,
moduleBlockNode
)
))
)
)
} else {
m.namespace.set(
namespaceDecl.id.name,
captureDoc(source, docStyleParsers, moduleBlockNode))
}
})
} else {
m.namespace.set(
namespaceDecl.id.name,
captureDoc(source, docStyleParsers, moduleBlockNode))
}
})
}
} else {
// Export as default
m.namespace.set('default', captureDoc(source, docStyleParsers, decl))
Expand Down
13 changes: 13 additions & 0 deletions tests/files/typescript-declare-nested.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
declare namespace foo {
interface SomeInterface {
a: string;
}
}

declare namespace foo.bar {
interface SomeOtherInterface {
b: string;
}
}

export = foo;
12 changes: 11 additions & 1 deletion tests/src/rules/namespace.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test, SYNTAX_CASES } from '../utils'
import { test, SYNTAX_CASES, getTSParsers } from '../utils'
import { RuleTester } from 'eslint'

var ruleTester = new RuleTester({ env: { es6: true }})
Expand Down Expand Up @@ -120,6 +120,16 @@ const valid = [
},
}),

// Typescript
...getTSParsers().map((parser) => test({
code: `import * as foo from "./typescript-declare-nested"`,
parser: parser,
settings: {
'import/parsers': { [parser]: ['.ts'] },
'import/resolver': { 'eslint-import-resolver-typescript': true },
},
})),

...SYNTAX_CASES,
]

Expand Down

0 comments on commit 1c7c10f

Please sign in to comment.