1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getModuleInstanceState</span>(<span class="hljs-params">node: Node</span>): <span class="hljs-title">ModuleInstanceState</span> </span>{
<span class="hljs-comment">// A module is uninstantiated if it contains only</span>
<span class="hljs-comment">// 1. interface declarations, type alias declarations</span>
<span class="hljs-keyword">if</span> (node.kind === SyntaxKind.InterfaceDeclaration || node.kind === SyntaxKind.TypeAliasDeclaration) {
<span class="hljs-keyword">return</span> ModuleInstanceState.NonInstantiated;
}
<span class="hljs-comment">// 2. const enum declarations</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (isConstEnumDeclaration(node)) {
<span class="hljs-keyword">return</span> ModuleInstanceState.ConstEnumOnly;
}
<span class="hljs-comment">// 3. non-exported import declarations</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> ((node.kind === SyntaxKind.ImportDeclaration || node.kind === SyntaxKind.ImportEqualsDeclaration) && !(node.flags & NodeFlags.Export)) {
<span class="hljs-keyword">return</span> ModuleInstanceState.NonInstantiated;
}
<span class="hljs-comment">// 4. other uninstantiated module declarations.</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (node.kind === SyntaxKind.ModuleBlock) {
<span class="hljs-keyword">let</span> state = ModuleInstanceState.NonInstantiated;
forEachChild(node, <span class="hljs-function"><span class="hljs-params">n</span> =></span> {
<span class="hljs-keyword">switch</span> (getModuleInstanceState(n)) {
<span class="hljs-keyword">case</span> ModuleInstanceState.NonInstantiated:
<span class="hljs-comment">// child is non-instantiated - continue searching</span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
<span class="hljs-keyword">case</span> ModuleInstanceState.ConstEnumOnly:
<span class="hljs-comment">// child is const enum only - record state and continue searching</span>
state = ModuleInstanceState.ConstEnumOnly;
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
<span class="hljs-keyword">case</span> ModuleInstanceState.Instantiated:
<span class="hljs-comment">// child is instantiated - record state and stop</span>
state = ModuleInstanceState.Instantiated;
<span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
}
});
<span class="hljs-keyword">return</span> state;
}
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (node.kind === SyntaxKind.ModuleDeclaration) {
<span class="hljs-keyword">return</span> getModuleInstanceState((<ModuleDeclaration>node).body);
}
<span class="hljs-keyword">else</span> {
<span class="hljs-keyword">return</span> ModuleInstanceState.Instantiated;
}
}
|