Skip to content

Commit 8677453

Browse files
committed
typescript.lang: Add highlighting for type-only imports and exports
This syntax was added in TypeScript 3.8. The supported forms are from the feature pull request[1]. [1]: microsoft/TypeScript#35200
1 parent 780b587 commit 8677453

File tree

5 files changed

+99
-26
lines changed

5 files changed

+99
-26
lines changed

data/language-specs/typescript-js-modules.lang

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,42 @@
9292
</include>
9393
</context> <!-- /_choice-export-assignment -->
9494

95+
<!-- ## Type-only export or export type alias declaration
96+
97+
type-only export:
98+
export type { T };
99+
export type { T } from './mod';
100+
101+
export type alias declaration:
102+
export type Name = string;
103+
-->
104+
105+
<context id="_choice-export-type-alias-declaration" style-ref="typescript:type-alias-declaration" end-parent="true">
106+
<start>\%{js:before-next-token}</start>
107+
<end>\%{js:statement-end}</end>
108+
<include>
109+
<context ref="js:comments"/>
110+
<context ref="typescript-js-st:_type-alias-declaration-content"/>
111+
</include>
112+
</context> <!-- /_choice-export-type-alias-declaration -->
113+
114+
<context id="_choice-export-type-only-or-type-alias-declaration" end-parent="true">
115+
<start>\%{typescript:type-keyword}</start>
116+
<end>\%{js:before-next-token}</end>
117+
<include>
118+
<context sub-pattern="0" where="start" style-ref="typescript:type-keyword"/>
119+
<context ref="js:comments"/>
120+
121+
<context id="_export-type-only-or-type-alias-declaration-content">
122+
<include>
123+
<context ref="js-mod:_choice-export-named"/>
124+
<context ref="_choice-export-type-alias-declaration"/>
125+
</include>
126+
</context> <!-- /_export-type-only-or-type-alias-declaration-content -->
127+
128+
</include>
129+
</context> <!-- /_choice-export-type-only-or-type-alias-declaration -->
130+
95131
<!-- ## Export declaration -->
96132

97133
<!-- replaces js-mod:_export-declaration-content -->
@@ -105,7 +141,7 @@
105141
<context ref="_choice-import-default-declaration"/> <!-- for import alias declaration -->
106142
<context ref="typescript-js-st:choice-interface-declaration"/>
107143
<context ref="typescript-js-st:choice-namespace-declaration"/>
108-
<context ref="typescript-js-st:choice-type-alias-declaration"/>
144+
<context ref="_choice-export-type-only-or-type-alias-declaration"/>
109145
<context ref="js-mod:_export-declaration-content" original="true"/>
110146
</include>
111147
</context> <!-- /export-declaration-content -->
@@ -187,6 +223,31 @@
187223
</include>
188224
</context> <!-- /_choice-import-alias-or-require -->
189225

226+
<!-- ## Type-only import
227+
228+
import type T from './mod';
229+
import type { A, B } from './mod';
230+
import type * as Types from './mod';
231+
-->
232+
233+
<context id="_choice-import-type-only" end-parent="true">
234+
<start>\%{typescript:type-keyword}</start>
235+
<end>\%{js:before-next-token}</end>
236+
<include>
237+
<context sub-pattern="0" where="start" style-ref="typescript:type-keyword"/>
238+
<context ref="js:comments"/>
239+
240+
<context id="_import-type-only-content">
241+
<include>
242+
<context ref="js-mod:_choice-import-named"/>
243+
<context ref="js-mod:_choice-all-as-namespace"/>
244+
<context ref="js-mod:_choice-import-default"/>
245+
</include>
246+
</context> <!-- _import-type-only-content -->
247+
248+
</include>
249+
</context> <!-- /_choice-import-type-only -->
250+
190251
<!-- ## Import default -->
191252

192253
<!-- replaces js-mod:_import-default-content -->
@@ -219,5 +280,13 @@
219280
</include>
220281
</context> <!-- /_choice-import-default-declaration -->
221282

283+
<!-- replaces js-mod:_import-declaration-content -->
284+
<context id="import-declaration-content">
285+
<include>
286+
<context ref="_choice-import-type-only"/>
287+
<context ref="js-mod:_import-declaration-content" original="true"/>
288+
</include>
289+
</context> <!-- /import-declaration-content -->
290+
222291
</definitions>
223292
</language>

data/language-specs/typescript-js-statements.lang

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -471,10 +471,6 @@
471471
type Container<T> = { value: T };
472472
-->
473473

474-
<define-regex id="_type-alias-declaration-keyword" extended="true">
475-
\%{js:keyword-start} type \%{js:keyword-end}
476-
</define-regex> <!-- /_type-alias-declaration-keyword -->
477-
478474
<context id="_type-alias-value" once-only="true">
479475
<start>=</start>
480476
<end>\%{js:before-next-token}</end>
@@ -498,35 +494,24 @@
498494
</include>
499495
</context> <!-- /_ordered-type-alias-value -->
500496

501-
<context id="_type-alias-declaration-content">
502-
<include>
503-
<context ref="js:ordered-identifier"/>
504-
<context ref="typescript-type-gen:ordered-type-parameters-list"/>
505-
<context ref="_ordered-type-alias-value"/>
506-
</include>
507-
</context> <!-- /_type-alias-declaration-content -->
508-
509497
<!-- <TypeAliasDeclaration> -->
510498
<context id="_type-alias-declarations" style-ref="typescript:type-alias-declaration">
511-
<start>\%{_type-alias-declaration-keyword}</start>
499+
<start>\%{typescript:type-keyword}</start>
512500
<end>\%{js:statement-end}</end>
513501
<include>
514502
<context sub-pattern="0" where="start" style-ref="typescript:type-keyword"/>
515503
<context ref="js:comments"/>
516-
<context ref="_type-alias-declaration-content"/>
517-
</include>
518-
</context> <!-- /_type-alias-declarations -->
519504

520-
<!-- <TypeAliasDeclaration> -->
521-
<context id="choice-type-alias-declaration" style-ref="typescript:type-alias-declaration" end-parent="true">
522-
<start>\%{_type-alias-declaration-keyword}</start>
523-
<end>\%{js:statement-end}</end>
524-
<include>
525-
<context sub-pattern="0" where="start" style-ref="typescript:type-keyword"/>
526-
<context ref="js:comments"/>
527-
<context ref="_type-alias-declaration-content"/>
505+
<context id="_type-alias-declaration-content">
506+
<include>
507+
<context ref="js:ordered-identifier"/>
508+
<context ref="typescript-type-gen:ordered-type-parameters-list"/>
509+
<context ref="_ordered-type-alias-value"/>
510+
</include>
511+
</context> <!-- /_type-alias-declaration-content -->
512+
528513
</include>
529-
</context> <!-- /choice-type-alias-declaration -->
514+
</context> <!-- /_type-alias-declarations -->
530515

531516

532517
<!-- # Statements -->

data/language-specs/typescript.lang

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@
122122
\%{js:keyword-start} class \%{js:keyword-end}
123123
</define-regex> <!-- /class-expression-keyword -->
124124

125+
<define-regex id="type-keyword" extended="true">
126+
\%{js:keyword-start} type \%{js:keyword-end}
127+
</define-regex> <!-- /type-keyword -->
128+
125129
<context id="js-ordered-string" once-only="true">
126130
<start>\%{js:before-next-token}</start>
127131
<end>\%{js:before-next-token}</end>
@@ -798,6 +802,7 @@
798802
<!-- ### Import declaration -->
799803

800804
<replace id="js-mod:_import-default-content" ref="typescript-js-mod:import-default-content"/>
805+
<replace id="js-mod:_import-declaration-content" ref="typescript-js-mod:import-declaration-content"/>
801806

802807

803808
<!-- # Main context -->

tests/syntax-highlighting/file.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,13 @@ import shortname = Long.Namespace.Name;
569569
// Import require
570570
import mod = require("module");
571571

572+
// Type-only imports and exports
573+
import type T from './mod';
574+
import type { A, B } from './mod';
575+
import type * as Types from './mod';
576+
export type { T };
577+
export type { T } from './mod';
578+
572579

573580
/* Variable declaration */
574581

tests/syntax-highlighting/file.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,13 @@ import shortname = Long.Namespace.Name;
677677
// Import require
678678
import mod = require("module");
679679

680+
// Type-only imports and exports
681+
import type T from './mod';
682+
import type { A, B } from './mod';
683+
import type * as Types from './mod';
684+
export type { T };
685+
export type { T } from './mod';
686+
680687

681688
/* Variable declaration */
682689

0 commit comments

Comments
 (0)