Skip to content

Commit

Permalink
[TypeScript] Implement labeled tuple members (#3837)
Browse files Browse the repository at this point in the history
* [TypeScript] Implement labeled tuple members

* [TypeScript] Use reserved word in test

* [TypeScript] Use .js instead of .ts

* [TypeScript] Optimize labeled tuple members

This commit ...

1. optimizes implementation to speed it up by about 15%
2. rescopes labels to `variable.other.member` as variable scope is what C# uses
   and this is what labels are for. Create named access to tuple members as if
   they were normal struct members.

Note:
1. `meta.mapping.key` should be used in mappings only.
2. We shouldn't mix up sequence and mapping scopes, although a mapping is just
   a sequence of key-value pairs.

---------

Co-authored-by: deathaxe <[email protected]>
  • Loading branch information
Thom1729 and deathaxe authored Sep 24, 2023
1 parent f4a13bb commit 01cce12
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
33 changes: 31 additions & 2 deletions JavaScript/TypeScript.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -921,14 +921,43 @@ contexts:
scope: punctuation.section.sequence.end.js
pop: 1
- include: comma-separator
- match: \.\.\.
scope: keyword.operator.spread.js
- match: (?=\S)
push:
- ts-type-annotation-optional
- ts-type-expression-end
- ts-type-expression-end-no-line-terminator
- ts-type-expression-begin
- ts-type-tuple-spread
- ts-type-tuple-possible-member-label

ts-type-tuple-possible-member-label:
- match: ''
branch_point: ts-type-tuple-member-label
branch:
- ts-type-tuple-member-label
- immediately-pop
pop: 1

ts-type-tuple-member-label:
- match: '{{identifier_name}}'
scope: variable.other.member.js
set:
- ts-type-tuple-member-label-separator
- ts-type-annotation-optional
- include: else-pop

ts-type-tuple-member-label-separator:
- match: ':'
scope: punctuation.separator.type.js
pop: 1
- match: (?=\S)
fail: ts-type-tuple-member-label

ts-type-tuple-spread:
- match: \.\.\.
scope: keyword.operator.spread.js
pop: 1
- include: else-pop

ts-type-object:
- match: \{
Expand Down
31 changes: 31 additions & 0 deletions JavaScript/tests/syntax_test_typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,37 @@ let x: [ any , any ? , ... any [] ];
// ^^ storage.modifier.array
// ^ punctuation.section.sequence.end

let x: [ first: any, rest: ...any ];
//^ keyword.declaration
// ^ meta.binding.name variable.other.readwrite
// ^ punctuation.separator.type
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.type
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.sequence
// ^ punctuation.section.sequence.begin
// ^^^^^ variable.other.member
// ^ punctuation.separator.type
// ^^^ support.type.any
// ^ punctuation.separator.comma
// ^^^^ variable.other.member
// ^ punctuation.separator.type
// ^^^ keyword.operator.spread
// ^^^ support.type.any
// ^ punctuation.section.sequence.end
// ^ punctuation.terminator.statement

let x: [
typeof
// ^^^^^ variable.other.member
?
// ^ storage.modifier.optional
:
// ^ punctuation.separator.type
...
// ^^^ keyword.operator.spread
any
// ^^^ support.type.any
];

let x: any & any;
// ^^^^^^^^^ meta.type
// ^^^ support.type.any
Expand Down

0 comments on commit 01cce12

Please sign in to comment.