UNPKG

1.43 kBPlain TextView Raw
1import * as tsm from 'ts-morph';
2import {
3 DeclarationKinds,
4 ModuleDeclarations,
5 NamespaceDeclaration,
6} from '../types/module-declarations';
7import { formatText } from './format';
8import { getJSDocs } from './get-jsdocs';
9import { SourceProvider } from './source-provider';
10
11export function isNamespace(
12 declaration: tsm.Node
13): declaration is tsm.ModuleDeclaration {
14 return tsm.Node.isModuleDeclaration(declaration);
15}
16
17export function newNamespace({
18 id,
19 name,
20 declaration,
21 declarations,
22 getSource,
23}: {
24 id: string;
25 name: string;
26 declaration: tsm.ModuleDeclaration;
27 declarations: ModuleDeclarations;
28 getSource: SourceProvider;
29}): NamespaceDeclaration {
30 const kind = DeclarationKinds.NamespaceDeclaration;
31 const docs = getJSDocs({ declaration });
32 const source = getSource({ declaration });
33 const signature = getNamespaceSignature({ id, name });
34
35 return {
36 kind,
37 id,
38 name: id,
39 docs,
40 source,
41 signature,
42 declarations,
43 };
44}
45
46function getNamespaceSignature({
47 id,
48 name,
49}: {
50 id: string;
51 name: string;
52}): string {
53 const signature = isAmbientModule({ name })
54 ? `module ${name} {}`
55 : `namespace ${id} {}`;
56 return formatText(signature);
57}
58
59function isAmbientModule({ name }: { name: string }): boolean {
60 return name.startsWith("'") || name.startsWith('"');
61}