Skip to content

Commit

Permalink
[TypeScript] Fix bugs in object/interface types (#3848)
Browse files Browse the repository at this point in the history
* [TypeScript] Fix bugs in object/interface types

* [TypeScript] atomic ts-mapped-or-indexed-member context

This commit modifies `ts-mapped-or-indexed-member` context so it only consumes
a possible member variable and then pops.

As a result `ts-type-expression` becomes obsolete as its parts are be pushed
on stack directly.

---------

Co-authored-by: deathaxe <[email protected]>
  • Loading branch information
Thom1729 and deathaxe authored Sep 30, 2023
1 parent 0eaf9fa commit 4ac1898
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 52 deletions.
114 changes: 66 additions & 48 deletions JavaScript/TypeScript.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -373,69 +373,81 @@ contexts:
- match: '[,;]'
scope: punctuation.separator.js

- match: '\+|-'
scope: storage.modifier.js

- match: (?={{modifier}})
push: class-element-modifiers

- match: (?=[<(])
- match: (?=\S)
push:
- ts-type-annotation
- function-declaration-expect-parameters
- ts-type-parameter-list
- ts-type-annotation-optional
- ts-type-annotation-modifier
- ts-type-member-name
- ts-type-element-modifiers

- match: \[
scope: punctuation.section.brackets.begin.js
push:
- - include: immediately-pop
- ts-type-annotation
- ts-type-annotation-optional
- - match: '\+|-'
scope: storage.modifier.js
pop: 1
- include: else-pop
- - meta_scope: meta.brackets.js
- match: \]
scope: punctuation.section.brackets.end.js
pop: 1
- match: '{{identifier_name}}'
scope: variable.other.readwrite.js
push:
- match: in{{identifier_break}}
scope: keyword.operator.type.js
set:
- ts-type-expression-end
- ts-type-expression-end-no-line-terminator
- ts-type-expression-begin
- include: ts-type-annotation
ts-type-element-modifiers:
- match: '\+|-'
scope: storage.modifier.js
- match: (?={{modifier}})
pop: 1
branch_point: ts-type-member-modifier
branch:
- ts-type-member-modifier
- immediately-pop
- include: else-pop

ts-type-member-modifier:
- match: '{{modifier}}'
scope: storage.modifier.js
set:
- match: (?={{identifier_start}}|[-+*''"\[\d])
set: ts-type-element-modifiers
- match: (?=\S)
fail: ts-type-member-modifier

ts-type-member-name:
- match: new{{identifier_break}}
scope: keyword.operator.new.js
push:
- ts-type-annotation
- function-declaration-expect-parameters
- ts-type-parameter-list

- match: (?={{identifier_start}}|['"]|\d)
push:
- - match: (?=[<(])
set:
- ts-type-annotation
- function-declaration-expect-parameters
- ts-type-parameter-list
- match: (?=\S)
set: ts-type-annotation
- ts-type-annotation-optional
- ts-type-member-name
pop: 1

ts-type-member-name:
- match: '{{identifier_name}}'
scope: variable.other.readwrite.js
pop: 1
- include: literal-string
- include: literal-number

- match: \[
scope: punctuation.section.brackets.begin.js
set:
- - meta_scope: meta.brackets.js
- match: \]
scope: punctuation.section.brackets.end.js
pop: 1
- ts-type-expression-end
- ts-type-expression-end-no-line-terminator
- ts-type-expression-begin
- ts-mapped-or-indexed-member

- include: else-pop

ts-mapped-or-indexed-member:
- meta_include_prototype: false
- match: ''
pop: 1
branch_point: ts-mapped-or-indexed-member
branch:
- - match: '{{identifier_name}}'
scope: variable.other.readwrite.js
set:
- match: in{{identifier_break}}
scope: keyword.operator.type.js
pop: 1
- match: ':'
scope: punctuation.separator.type.js
pop: 1
- match: (?=\S)
fail: ts-mapped-or-indexed-member
- include: else-pop
- immediately-pop

ts-enum:
- match: enum{{identifier_break}}
scope: keyword.declaration.enum.js
Expand Down Expand Up @@ -778,6 +790,12 @@ contexts:
pop: 1
- include: else-pop

ts-type-annotation-modifier:
- match: '\+|-'
scope: storage.modifier.js
pop: 1
- include: else-pop

ts-type-meta:
- meta_include_prototype: false
- meta_content_scope: meta.type.js
Expand Down
39 changes: 35 additions & 4 deletions JavaScript/tests/syntax_test_typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,10 +375,10 @@ import foo;
// https://github.com/sublimehq/Packages/issues/3598
type x = {
bar: (cb: (
// ^^^^^^ meta.type-alias meta.mapping - meta.group
// ^^^ meta.type-alias meta.mapping meta.type meta.group
// ^^ meta.type-alias meta.mapping - meta.group
// ^ meta.type-alias meta.function.parameters
//^^^^^^^^^^^^^^^^^^ meta.type-alias meta.mapping
// ^^^^^^ - meta.group
// ^^^ meta.type meta.group
// ^^ - meta.group
// ^^^ variable.other.readwrite
// ^ punctuation.separator.type
// ^ punctuation.section.group.begin
Expand Down Expand Up @@ -1007,6 +1007,12 @@ let x: {
// ^^^ support.type.any
// ^ punctuation.separator

readonly: any;
// ^^^^^^^^ variable.other.readwrite - storage.modifier
// ^ punctuation.separator.type
// ^^^ support.type.any
// ^ punctuation.separator

( foo : any ) : any ;
// ^ punctuation.section.group.begin
// ^^^ meta.binding.name variable.parameter.function
Expand Down Expand Up @@ -1059,6 +1065,14 @@ let x: {
// ^^^ support.type.any
// ^ punctuation.separator

a ? () : any ;
// ^ variable.other.readwrite
// ^ storage.modifier.optional
// ^^ meta.function.parameters
// ^ punctuation.separator.type
// ^^^ support.type.any
// ^ punctuation.separator

new ( foo : any ) : any ;
// ^ punctuation.section.group.begin
// ^^^ meta.binding.name variable.parameter.function
Expand Down Expand Up @@ -1152,6 +1166,23 @@ let x: {
// ^ punctuation.section.brackets.end
// ^ punctuation.separator

private +readonly x;
// ^^^^^^^ storage.modifier
// ^^^^^^^^^ storage.modifier
// ^ variable.other.readwrite

[Symbol.iterator](): any;
// ^^^^^^^^^^^^^^^^^ meta.brackets
// ^ punctuation.section.brackets.begin
// ^^^^^^ support.class
// ^ punctuation.accessor
// ^^^^^^^^ support.class
// ^ punctuation.section.brackets.end
// ^^ meta.function.parameters
// ^ punctuation.separator.type
// ^^^ support.type.any
// ^ punctuation.separator

}
// ^ meta.type punctuation.section.mapping.end

Expand Down

0 comments on commit 4ac1898

Please sign in to comment.