[gtksourceview: 11/14] typescript.lang: Add highlighting for @ts-expect-error
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtksourceview: 11/14] typescript.lang: Add highlighting for @ts-expect-error
- Date: Thu, 11 Jun 2020 17:27:13 +0000 (UTC)
commit 9a2be9da51a060b41247c550eb08463a32a2b8e2
Author: Jeffery To <jeffery to gmail com>
Date: Wed Jun 10 16:25:03 2020 +0800
typescript.lang: Add highlighting for @ts-expect-error
The @ts-expect-error comment pragma was added in TypeScript 3.9.
Also:
* Highlight when @ts-ignore / @ts-expect-error starts the last line of a
multiline comment
* Highlight @jsx anywhere in a multiline comment (but the comment must
appear at the top of the file, before any code)
* Make highlighting of triple-slash directives more strict (must be a
complete, self-closed XML tag)
* Enable spell-checking in comments with pragmas/directives, but disable
spell-checking inside the pragma/directive itself
data/language-specs/javascript.lang | 22 +++--
data/language-specs/typescript-jsx.lang | 51 +++++------
data/language-specs/typescript.lang | 154 ++++++++++++++++++++++++--------
tests/syntax-highlighting/file.ts | 30 +++++--
tests/syntax-highlighting/file.tsx | 45 +++++++---
5 files changed, 211 insertions(+), 91 deletions(-)
---
diff --git a/data/language-specs/javascript.lang b/data/language-specs/javascript.lang
index ef6b4fcd..01e5e54f 100644
--- a/data/language-specs/javascript.lang
+++ b/data/language-specs/javascript.lang
@@ -535,11 +535,23 @@
</include>
</context> <!-- /_comment-content -->
+ <context id="_single-line-comment-content">
+ <include>
+ <context ref="_comment-content"/>
+ </include>
+ </context> <!-- /_single-line-comment-content -->
+
+ <context id="_multiline-comment-content">
+ <include>
+ <context ref="_comment-content"/>
+ </include>
+ </context> <!-- /_multiline-comment-content -->
+
<!-- line continuations are not allowed inside single-line comments -->
<context id="_single-line-comments" style-ref="comment" end-at-line-end="true"
class-disabled="no-spell-check" class="comment">
<start>//</start>
<include>
- <context ref="_comment-content"/>
+ <context ref="_single-line-comment-content"/>
</include>
</context> <!-- /_single-line-comments -->
@@ -547,14 +559,14 @@
<start>/\*</start>
<end>\*/</end>
<include>
- <context ref="_comment-content"/>
+ <context ref="_multiline-comment-content"/>
</include>
</context> <!-- /_multiline-comments -->
<context id="_single-line-comments-no-extend-parent" style-ref="comment" end-at-line-end="true"
class-disabled="no-spell-check" class="comment" extend-parent="false">
<start>//</start>
<include>
- <context ref="_comment-content"/>
+ <context ref="_single-line-comment-content"/>
</include>
</context> <!-- /_single-line-comments-no-extend-parent -->
@@ -562,7 +574,7 @@
<start>/\*</start>
<end>\*/</end>
<include>
- <context ref="_comment-content"/>
+ <context ref="_multiline-comment-content"/>
</include>
</context> <!-- /_multiline-comments-no-extend-parent -->
@@ -583,7 +595,7 @@
<context id="_doc-comment-content">
<include>
- <context ref="_comment-content"/>
+ <context ref="_multiline-comment-content"/>
<context ref="jsdoc:jsdoc"/>
</include>
</context> <!-- /_doc-comment-content -->
diff --git a/data/language-specs/typescript-jsx.lang b/data/language-specs/typescript-jsx.lang
index 635c7cea..4704e3ca 100644
--- a/data/language-specs/typescript-jsx.lang
+++ b/data/language-specs/typescript-jsx.lang
@@ -55,46 +55,40 @@
<!-- ## @jsx comment pragma
/* @jsx dom */
+
+ can only be preceded by comments or other triple-slash
+ directives (i.e. at the top of a file)
+ can appear anywhere inside the comment(!)
+ appears to be case-insensitive
+
https://github.com/microsoft/TypeScript/blob/3c49fb0d9ffb67fd645835589d66687b4fb7543a/src/services/classifier.ts#L768
-->
- <!-- I believe the allowed syntax is less strict but I haven't found
- any definitive documentation -->
- <context id="_jsx-comment-pragmas" style-ref="js:directive" class="comment">
- <start case-sensitive="false" extended="true">
- /\*\*? \%{js:whitespace}* @jsx \%{js:keyword-end}
- </start>
- <end>\*/</end>
- <include>
- <context ref="js:_comment-content"/>
- </include>
- </context> <!-- /_jsx-comment-pragmas -->
+ <define-regex id="_jsx-comment-pragma" case-sensitive="false" extended="true">
+ @jsx \s+ (?> \%{js:identifier} (?: \. \%{js:identifier} )* ) (?! \S )
+ </define-regex> <!-- /_jsx-comment-pragma -->
- <context id="_jsx-comment-pragmas-no-extend-parent" style-ref="js:directive" class="comment"
extend-parent="false">
+ <context id="_jsx-comment-pragma" style-ref="js:directive" class="no-spell-check" once-only="true">
+ <match>\%{_jsx-comment-pragma}</match>
+ </context> <!-- /_jsx-comment-pragma -->
+
+ <context id="_jsx-comment-pragma-multiline-comments" style-ref="js:directive"
class-disabled="no-spell-check" class="comment">
<start extended="true">
- /\*\*? \%{js:whitespace}* @jsx \%{js:keyword-end}
+ /\* (?= /* \** \s* \%{_jsx-comment-pragma} .*? \*/ )
</start>
<end>\*/</end>
<include>
- <context ref="js:_comment-content"/>
- </include>
- </context> <!-- /_jsx-comment-pragmas-no-extend-parent -->
-
- <context id="_comments">
- <include>
- <context ref="_jsx-comment-pragmas"/>
- <context ref="typescript:_comments" original="true"/>
+ <context ref="js:_multiline-comment-content"/>
</include>
- </context> <!-- /_comments -->
+ </context> <!-- /_jsx-comment-pragma-multiline-comments -->
- <context id="_comments-no-extend-parent">
+ <context id="_multiline-comment-content">
<include>
- <context ref="_jsx-comment-pragmas-no-extend-parent"/>
- <context ref="typescript:_comments-no-extend-parent" original="true"/>
+ <context ref="_jsx-comment-pragma"/>
+ <context ref="typescript:_multiline-comment-content" original="true"/>
</include>
- </context> <!-- /_comments-no-extend-parent -->
+ </context> <!-- /_multiline-comment-content -->
- <replace id="js:comments" ref="_comments"/>
- <replace id="js:comments-no-extend-parent" ref="_comments-no-extend-parent"/>
+ <replace id="js:_multiline-comment-content" ref="_multiline-comment-content"/>
<!-- # Types -->
@@ -190,6 +184,7 @@
<context id="typescript-jsx" class="no-spell-check">
<include>
+ <context ref="_jsx-comment-pragma-multiline-comments"/>
<context ref="jsx:jsx"/>
</include>
</context> <!-- /typescript-jsx -->
diff --git a/data/language-specs/typescript.lang b/data/language-specs/typescript.lang
index 971589fe..e93e18e9 100644
--- a/data/language-specs/typescript.lang
+++ b/data/language-specs/typescript.lang
@@ -243,33 +243,84 @@
</include>
</context> <!-- /js-ordered-string -->
- <!-- ## @ts-ignore comment pragma
+ <!-- ## @ts-ignore / @ts-expect-error comment pragma
// @ts-ignore
- -->
+ /* @ts-expect-error some reasons */
+
+ can be at the start of a double- or triple-slash comment
+
https://github.com/microsoft/TypeScript/blob/3c49fb0d9ffb67fd645835589d66687b4fb7543a/src/compiler/scanner.ts#L275
+
+ can also be on the last line of a multi-line comment
+
https://github.com/microsoft/TypeScript/blob/3c49fb0d9ffb67fd645835589d66687b4fb7543a/src/compiler/scanner.ts#L280
- <!-- can be double- or triple-slash
- pragma must be at start of comment
- technically, must be at the start of the line
-
https://github.com/microsoft/TypeScript/blob/e82d0af554d96738b2e8c7f9027e53ac8bc31007/src/compiler/program.ts#L2
+ in both cases, the pragma should be at the start of the
+ line/comment, but in practice there can be code before the
+ comment (on the same line)
+
+ also the regexes don't test for a word break, so any characters
+ can immediately follow the pragmas
-->
- <context id="_ts-ignore-comment-pragmas" style-ref="js:directive" end-at-line-end="true" class="comment">
+
+ <define-regex id="_ts-ignore-comment-pragma" extended="true">
+ @ (?: ts-expect-error | ts-ignore )
+ </define-regex> <!-- /_ts-ignore-comment-pragma -->
+
+ <context id="_ts-ignore-comment-pragma" style-ref="js:directive" class="no-spell-check" once-only="true">
+ <match>\%{_ts-ignore-comment-pragma}</match>
+ </context> <!-- /_ts-ignore-comment-pragma -->
+
+ <context id="_ts-ignore-comment-pragma-single-line-comments" style-ref="js:directive"
end-at-line-end="true" class-disabled="no-spell-check" class="comment">
+ <start extended="true">
+ // (?= /? \s* \%{_ts-ignore-comment-pragma} )
+ </start>
+ <include>
+ <context ref="_ts-ignore-comment-pragma"/>
+ <context ref="js:_single-line-comment-content"/>
+ </include>
+ </context> <!-- /_ts-ignore-comment-pragma-single-line-comments -->
+
+ <context id="_ts-ignore-comment-pragma-single-line-comments-no-extend-parent" style-ref="js:directive"
end-at-line-end="true" class-disabled="no-spell-check" class="comment" extend-parent="false">
+ <start extended="true">
+ // (?= /? \s* \%{_ts-ignore-comment-pragma} )
+ </start>
+ <include>
+ <context ref="_ts-ignore-comment-pragma"/>
+ <context ref="js:_single-line-comment-content"/>
+ </include>
+ </context> <!-- /_ts-ignore-comment-pragma-single-line-comments-no-extend-parent -->
+
+ <context id="_ts-ignore-comment-pragmas-multiline-comments" style-ref="js:directive"
class-disabled="no-spell-check" class="comment">
<start extended="true">
- ///? \%{js:whitespace}* @ts-ignore \%{js:keyword-end}
+ /\* (?= /* \** \s* \%{_ts-ignore-comment-pragma} .*? \*/ )
</start>
+ <end>\*/</end>
<include>
- <context ref="js:_comment-content"/>
+ <context ref="_ts-ignore-comment-pragma"/>
+ <context ref="_multiline-comment-content"/>
</include>
- </context> <!-- /_ts-ignore-comment-pragmas -->
+ </context> <!-- /_ts-ignore-comment-pragmas-multiline-comments -->
- <context id="_ts-ignore-comment-pragmas-no-extend-parent" style-ref="js:directive"
end-at-line-end="true" class="comment" extend-parent="false">
+ <context id="_ts-ignore-comment-pragmas-multiline-comments-no-extend-parent" style-ref="js:directive"
class-disabled="no-spell-check" class="comment" extend-parent="false">
<start extended="true">
- ///? \%{js:whitespace}* @ts-ignore \%{js:keyword-end}
+ /\* (?= /* \** \s* \%{_ts-ignore-comment-pragma} .*? \*/ )
</start>
+ <end>\*/</end>
<include>
- <context ref="js:_comment-content"/>
+ <context ref="_ts-ignore-comment-pragma"/>
+ <context ref="_multiline-comment-content"/>
</include>
- </context> <!-- /_ts-ignore-comment-pragmas-no-extend-parent -->
+ </context> <!-- /_ts-ignore-comment-pragmas-multiline-comments-no-extend-parent -->
+
+ <context id="_ts-ignore-comment-pragma-multiline-comment-last-line" once-only="true">
+ <start extended="true">
+ ^ \s* /* \** \s* (?= \%{_ts-ignore-comment-pragma} .*? \*/ )
+ </start>
+ <end>\%{def:always-match}</end>
+ <include>
+ <context ref="_ts-ignore-comment-pragma"/>
+ </include>
+ </context> <!-- /_ts-ignore-comment-pragma-multiline-comment-last-line -->
<!-- ## @ts-nocheck comment pragma
@@ -277,42 +328,63 @@
can only be preceded by comments or other triple-slash
directives (i.e. at the top of a file)
- appears to be case-insensitive (cannot find code reference though)
+ case-insensitive ("i" modifier on regex)
+
https://github.com/microsoft/TypeScript/blob/3c49fb0d9ffb67fd645835589d66687b4fb7543a/src/services/classifier.ts#L768
-->
- <context id="_ts-nocheck-comment-pragmas" style-ref="js:directive" end-at-line-end="true"
class="comment">
+ <define-regex id="_ts-nocheck-comment-pragma" case-sensitive="false" extended="true">
+ @ts-nocheck (?! \S )
+ </define-regex> <!-- /_ts-nocheck-comment-pragma -->
+
+ <context id="_ts-nocheck-comment-pragma-comments" style-ref="js:directive" end-at-line-end="true"
class-disabled="no-spell-check" class="comment">
<start case-sensitive="false" extended="true">
- ///? \s* @ts-nocheck \%{js:keyword-end}
+ // (?= /? \s* \%{_ts-nocheck-comment-pragma} )
</start>
<include>
- <context ref="js:_comment-content"/>
+
+ <context id="_ts-nocheck-comment-pragma" class="no-spell-check" once-only="true">
+ <match>\%{_ts-nocheck-comment-pragma}</match>
+ </context> <!-- /_ts-nocheck-comment-pragma -->
+
+ <context ref="js:_single-line-comment-content"/>
</include>
- </context> <!-- /_ts-nocheck-comment-pragmas -->
+ </context> <!-- /_ts-nocheck-comment-pragma-comments -->
<!-- ## Triple-slash directive
- /// <reference path="..."
+ /// <reference path="..." />
can only be preceded by comments or other triple-slash
directives (i.e. at the top of a file)
- appears to be case-insensitive
-
https://github.com/microsoft/TypeScript/blob/ef0cca7d12a320bc98cefebbf4d391de36003149/src/services/classifier.ts#L768
+ case-insensitive ("i" modifier on regex)
+
https://github.com/microsoft/TypeScript/blob/3c49fb0d9ffb67fd645835589d66687b4fb7543a/src/services/classifier.ts#L768
-->
- <context id="_triple-slash-directives" style-ref="js:directive" end-at-line-end="true" class="comment">
- <start case-sensitive="false" extended="true">
- /// \%{js:whitespace}* <
+ <define-regex id="_triple-slash-directive" case-sensitive="false" extended="true">
+ <
+ (?: amd-module | amd-dependency | reference )
+ (?>
(?:
- amd-module |
- amd-dependency |
- reference
- )
- \%{js:whitespace}
+ \s+ |
+ (?<= \s ) [\w-]+ \s* = \s* (?: '[^']*' | "[^"]*" )
+ )*
+ )
+ />
+ </define-regex> <!-- /_triple-slash-directive -->
+
+ <context id="_triple-slash-directive-comments" style-ref="js:directive" end-at-line-end="true"
class-disabled="no-spell-check" class="comment">
+ <start extended="true">
+ /// (?= \s* \%{_triple-slash-directive} )
</start>
<include>
- <context ref="js:_comment-content"/>
+
+ <context id="_triple-slash-directive" class="no-spell-check" once-only="true">
+ <match>\%{_triple-slash-directive}</match>
+ </context> <!-- /_triple-slash-directive -->
+
+ <context ref="js:_single-line-comment-content"/>
</include>
- </context> <!-- /_triple-slash-directives -->
+ </context> <!-- /_triple-slash-directive-comments -->
<!-- ## Namespace name
@@ -835,20 +907,30 @@
<!-- ## General -->
+ <context id="_multiline-comment-content">
+ <include>
+ <context ref="_ts-ignore-comment-pragma-multiline-comment-last-line"/>
+ <context ref="js:_multiline-comment-content" original="true"/>
+ </include>
+ </context> <!-- /_multiline-comment-content -->
+
<context id="_comments">
<include>
- <context ref="_ts-ignore-comment-pragmas"/>
+ <context ref="_ts-ignore-comment-pragma-single-line-comments"/>
+ <context ref="_ts-ignore-comment-pragmas-multiline-comments"/>
<context ref="js:comments" original="true"/>
</include>
</context> <!-- /_comments -->
<context id="_comments-no-extend-parent">
<include>
- <context ref="_ts-ignore-comment-pragmas-no-extend-parent"/>
+ <context ref="_ts-ignore-comment-pragma-single-line-comments-no-extend-parent"/>
+ <context ref="_ts-ignore-comment-pragmas-multiline-comments-no-extend-parent"/>
<context ref="js:comments-no-extend-parent" original="true"/>
</include>
</context> <!-- /_comments-no-extend-parent -->
+ <replace id="js:_multiline-comment-content" ref="_multiline-comment-content"/>
<replace id="js:comments" ref="_comments"/>
<replace id="js:comments-no-extend-parent" ref="_comments-no-extend-parent"/>
@@ -946,8 +1028,8 @@
<context id="typescript" class="no-spell-check">
<include>
- <context ref="_ts-nocheck-comment-pragmas"/>
- <context ref="_triple-slash-directives"/>
+ <context ref="_ts-nocheck-comment-pragma-comments"/>
+ <context ref="_triple-slash-directive-comments"/>
<context ref="js:js"/>
</include>
</context> <!-- /typescript -->
diff --git a/tests/syntax-highlighting/file.ts b/tests/syntax-highlighting/file.ts
index 57ddff95..4271c154 100644
--- a/tests/syntax-highlighting/file.ts
+++ b/tests/syntax-highlighting/file.ts
@@ -240,19 +240,23 @@ let a: string;
* TypeScript-specific statements and declarations
*/
-/* @ts-ignore comment pragmas */
+/* @ts-ignore / @ts-expect-error comment pragmas */
// Valid pragmas
//@ts-ignore
-/// @ts-ignore add reason here
+/// @ts-expect-error add reason here
{
// @ts-ignore
}
+/* @ts-expect-error*/
+/*
+ * @ts-ignore more reasons */
// Invalid pragmas
// @ ts-ignore
/// @TS-IGNORE
-/* @ts-ignore */
+/* @ts-expect-error
+ */
/* @ts-nocheck comment pragmas */
@@ -274,15 +278,23 @@ let a: string;
// Valid directives
///<reference path="foo" />
-/// <REFERENCE lib="es2017.string" />
-/// <amd-module name="bar" />
-/// <aMd-dEpEnDeNcY />
+/// <REFERENCE lib='es2017.string'/>
+/// <amd-module name = "bar/>" />
+/// <aMd-dEpEnDeNcY/>
// Invalid directives
/// comment
-/// <comment
-/// < reference
-/// <reference-path
+/// <comment />
+/// <reference /
+/// < reference />
+/// <reference / >
+/// <reference-path="foo" />
+/// <reference path />
+/// <reference path "foo" />
+/// <reference path="/>
+/// <reference path=foo />
+/// <reference path='foo" />
+/// <reference path="foo"name="bar" />
{
/// <reference path="foo" />
}
diff --git a/tests/syntax-highlighting/file.tsx b/tests/syntax-highlighting/file.tsx
index e8eb8e91..73ae26a1 100644
--- a/tests/syntax-highlighting/file.tsx
+++ b/tests/syntax-highlighting/file.tsx
@@ -7,14 +7,21 @@
// Valid pragmas
/*@jsx dom */
/** @JSX preact.h */
-{
- /* @jsx dom */
-}
+/*
+ * @jsx dom additional text
+ */
// Invalid pragmas
/* @ jsx dom */
-/*** @jsx dom */
// @jsx dom
+/* @jsx */
+/* @jsx 1 */
+/* @jsx dom. */
+/* @jsx dom.1 */
+/* @jsx dom() */
+{
+ /* @jsx dom */ // Incorrectly highlighted
+}
/* Type parameters for arrow function vs JSX element */
@@ -346,19 +353,23 @@ let a: string;
* TypeScript-specific statements and declarations
*/
-/* @ts-ignore comment pragmas */
+/* @ts-ignore / @ts-expect-error comment pragmas */
// Valid pragmas
//@ts-ignore
-/// @ts-ignore add reason here
+/// @ts-expect-error add reason here
{
// @ts-ignore
}
+/* @ts-expect-error*/
+/*
+ * @ts-ignore more reasons */
// Invalid pragmas
// @ ts-ignore
/// @TS-IGNORE
-/* @ts-ignore */
+/* @ts-expect-error
+ */
/* @ts-nocheck comment pragmas */
@@ -380,15 +391,23 @@ let a: string;
// Valid directives
///<reference path="foo" />
-/// <REFERENCE lib="es2017.string" />
-/// <amd-module name="bar" />
-/// <aMd-dEpEnDeNcY />
+/// <REFERENCE lib='es2017.string'/>
+/// <amd-module name = "bar/>" />
+/// <aMd-dEpEnDeNcY/>
// Invalid directives
/// comment
-/// <comment
-/// < reference
-/// <reference-path
+/// <comment />
+/// <reference /
+/// < reference />
+/// <reference / >
+/// <reference-path="foo" />
+/// <reference path />
+/// <reference path "foo" />
+/// <reference path="/>
+/// <reference path=foo />
+/// <reference path='foo" />
+/// <reference path="foo"name="bar" />
{
/// <reference path="foo" />
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]