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