Skip to content

Commit

Permalink
typescript.lang: Add highlighting for type-only imports and exports
Browse files Browse the repository at this point in the history
This syntax was added in TypeScript 3.8. The supported forms are from
the feature pull request[1].

[1]: microsoft/TypeScript#35200
  • Loading branch information
jefferyto committed Jun 9, 2020
1 parent 780b587 commit 8677453
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 26 deletions.
71 changes: 70 additions & 1 deletion data/language-specs/typescript-js-modules.lang
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,42 @@
</include>
</context> <!-- /_choice-export-assignment -->

<!-- ## Type-only export or export type alias declaration
type-only export:
export type { T };
export type { T } from './mod';
export type alias declaration:
export type Name = string;
-->

<context id="_choice-export-type-alias-declaration" style-ref="typescript:type-alias-declaration" end-parent="true">
<start>\%{js:before-next-token}</start>
<end>\%{js:statement-end}</end>
<include>
<context ref="js:comments"/>
<context ref="typescript-js-st:_type-alias-declaration-content"/>
</include>
</context> <!-- /_choice-export-type-alias-declaration -->

<context id="_choice-export-type-only-or-type-alias-declaration" end-parent="true">
<start>\%{typescript:type-keyword}</start>
<end>\%{js:before-next-token}</end>
<include>
<context sub-pattern="0" where="start" style-ref="typescript:type-keyword"/>
<context ref="js:comments"/>

<context id="_export-type-only-or-type-alias-declaration-content">
<include>
<context ref="js-mod:_choice-export-named"/>
<context ref="_choice-export-type-alias-declaration"/>
</include>
</context> <!-- /_export-type-only-or-type-alias-declaration-content -->

</include>
</context> <!-- /_choice-export-type-only-or-type-alias-declaration -->

<!-- ## Export declaration -->

<!-- replaces js-mod:_export-declaration-content -->
Expand All @@ -105,7 +141,7 @@
<context ref="_choice-import-default-declaration"/> <!-- for import alias declaration -->
<context ref="typescript-js-st:choice-interface-declaration"/>
<context ref="typescript-js-st:choice-namespace-declaration"/>
<context ref="typescript-js-st:choice-type-alias-declaration"/>
<context ref="_choice-export-type-only-or-type-alias-declaration"/>
<context ref="js-mod:_export-declaration-content" original="true"/>
</include>
</context> <!-- /export-declaration-content -->
Expand Down Expand Up @@ -187,6 +223,31 @@
</include>
</context> <!-- /_choice-import-alias-or-require -->

<!-- ## Type-only import
import type T from './mod';
import type { A, B } from './mod';
import type * as Types from './mod';
-->

<context id="_choice-import-type-only" end-parent="true">
<start>\%{typescript:type-keyword}</start>
<end>\%{js:before-next-token}</end>
<include>
<context sub-pattern="0" where="start" style-ref="typescript:type-keyword"/>
<context ref="js:comments"/>

<context id="_import-type-only-content">
<include>
<context ref="js-mod:_choice-import-named"/>
<context ref="js-mod:_choice-all-as-namespace"/>
<context ref="js-mod:_choice-import-default"/>
</include>
</context> <!-- _import-type-only-content -->

</include>
</context> <!-- /_choice-import-type-only -->

<!-- ## Import default -->

<!-- replaces js-mod:_import-default-content -->
Expand Down Expand Up @@ -219,5 +280,13 @@
</include>
</context> <!-- /_choice-import-default-declaration -->

<!-- replaces js-mod:_import-declaration-content -->
<context id="import-declaration-content">
<include>
<context ref="_choice-import-type-only"/>
<context ref="js-mod:_import-declaration-content" original="true"/>
</include>
</context> <!-- /import-declaration-content -->

</definitions>
</language>
35 changes: 10 additions & 25 deletions data/language-specs/typescript-js-statements.lang
Original file line number Diff line number Diff line change
Expand Up @@ -471,10 +471,6 @@
type Container<T> = { value: T };
-->

<define-regex id="_type-alias-declaration-keyword" extended="true">
\%{js:keyword-start} type \%{js:keyword-end}
</define-regex> <!-- /_type-alias-declaration-keyword -->

<context id="_type-alias-value" once-only="true">
<start>=</start>
<end>\%{js:before-next-token}</end>
Expand All @@ -498,35 +494,24 @@
</include>
</context> <!-- /_ordered-type-alias-value -->

<context id="_type-alias-declaration-content">
<include>
<context ref="js:ordered-identifier"/>
<context ref="typescript-type-gen:ordered-type-parameters-list"/>
<context ref="_ordered-type-alias-value"/>
</include>
</context> <!-- /_type-alias-declaration-content -->

<!-- <TypeAliasDeclaration> -->
<context id="_type-alias-declarations" style-ref="typescript:type-alias-declaration">
<start>\%{_type-alias-declaration-keyword}</start>
<start>\%{typescript:type-keyword}</start>
<end>\%{js:statement-end}</end>
<include>
<context sub-pattern="0" where="start" style-ref="typescript:type-keyword"/>
<context ref="js:comments"/>
<context ref="_type-alias-declaration-content"/>
</include>
</context> <!-- /_type-alias-declarations -->

<!-- <TypeAliasDeclaration> -->
<context id="choice-type-alias-declaration" style-ref="typescript:type-alias-declaration" end-parent="true">
<start>\%{_type-alias-declaration-keyword}</start>
<end>\%{js:statement-end}</end>
<include>
<context sub-pattern="0" where="start" style-ref="typescript:type-keyword"/>
<context ref="js:comments"/>
<context ref="_type-alias-declaration-content"/>
<context id="_type-alias-declaration-content">
<include>
<context ref="js:ordered-identifier"/>
<context ref="typescript-type-gen:ordered-type-parameters-list"/>
<context ref="_ordered-type-alias-value"/>
</include>
</context> <!-- /_type-alias-declaration-content -->

</include>
</context> <!-- /choice-type-alias-declaration -->
</context> <!-- /_type-alias-declarations -->


<!-- # Statements -->
Expand Down
5 changes: 5 additions & 0 deletions data/language-specs/typescript.lang
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@
\%{js:keyword-start} class \%{js:keyword-end}
</define-regex> <!-- /class-expression-keyword -->

<define-regex id="type-keyword" extended="true">
\%{js:keyword-start} type \%{js:keyword-end}
</define-regex> <!-- /type-keyword -->

<context id="js-ordered-string" once-only="true">
<start>\%{js:before-next-token}</start>
<end>\%{js:before-next-token}</end>
Expand Down Expand Up @@ -798,6 +802,7 @@
<!-- ### Import declaration -->

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


<!-- # Main context -->
Expand Down
7 changes: 7 additions & 0 deletions tests/syntax-highlighting/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,13 @@ import shortname = Long.Namespace.Name;
// Import require
import mod = require("module");

// Type-only imports and exports
import type T from './mod';
import type { A, B } from './mod';
import type * as Types from './mod';
export type { T };
export type { T } from './mod';


/* Variable declaration */

Expand Down
7 changes: 7 additions & 0 deletions tests/syntax-highlighting/file.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,13 @@ import shortname = Long.Namespace.Name;
// Import require
import mod = require("module");

// Type-only imports and exports
import type T from './mod';
import type { A, B } from './mod';
import type * as Types from './mod';
export type { T };
export type { T } from './mod';


/* Variable declaration */

Expand Down

0 comments on commit 8677453

Please sign in to comment.