Skip to content

Commit 19ab2a6

Browse files
committed
Add markup text for JSX in Typescript, closes #878
1 parent bca29aa commit 19ab2a6

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

src/languages/typescript.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function(hljs) {
2222
'Float64Array Int16Array Int32Array Int8Array Uint16Array Uint32Array ' +
2323
'Uint8Array Uint8ClampedArray ArrayBuffer DataView JSON Intl arguments require ' +
2424
'module console window document any number boolean string void'
25-
}
25+
};
2626

2727
return {
2828
aliases: ['ts'],

test/markup/typescript/jsx.expect.txt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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) &amp;&amp; !(node.flags &amp; 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 =&gt; {
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((&lt;ModuleDeclaration&gt;node).body);
37+
}
38+
<span class="hljs-keyword">else</span> {
39+
<span class="hljs-keyword">return</span> ModuleInstanceState.Instantiated;
40+
}
41+
}

test/markup/typescript/jsx.txt

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

0 commit comments

Comments
 (0)