Skip to content

Commit

Permalink
[JavaScript] Simplify handling of modifiers in class members
Browse files Browse the repository at this point in the history
  • Loading branch information
Thom1729 committed Sep 30, 2023
1 parent 2992392 commit f32e4bd
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 40 deletions.
71 changes: 35 additions & 36 deletions JavaScript/JavaScript.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ contexts:
- match: ''
pop: 1

immediately-pop-2:
- match: ''
pop: 2

comma-separator:
- match: ','
scope: punctuation.separator.comma.js
Expand Down Expand Up @@ -1497,7 +1501,7 @@ contexts:
pop: 1

- match: \;
scope: punctuation.terminator.statement.js
scope: punctuation.terminator.statement.empty.js

- include: decorator

Expand All @@ -1512,15 +1516,9 @@ contexts:
branch_point: static-block
branch:
- static-block
- class-element-modifiers

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

- match: (?=\*)
push: method-declaration
- class-element

- match: (?={{class_element_name}})
- match: (?=\S)
push: class-element

static-block:
Expand All @@ -1531,23 +1529,6 @@ contexts:
- match: (?=\S)
fail: static-block

class-element-modifiers:
- match: ''
pop: 1
branch_point: class-element-modifier
branch:
- class-element-modifier
- class-element

class-element-modifier:
- match: '{{modifier}}'
scope: storage.modifier.js
set:
- match: (?={{class_element_name}}|\*)
pop: 1
- match: (?=\S)
fail: class-element-modifier

class-extends:
- match: extends{{identifier_break}}
scope: storage.modifier.extends.js
Expand Down Expand Up @@ -1581,6 +1562,8 @@ contexts:
- include: else-pop

class-element:
- match: (?=\*)
set: method-declaration
- match: ''
pop: 1
branch_point: class-field
Expand All @@ -1591,15 +1574,32 @@ contexts:
class-field:
- match: ''
set:
- expect-semicolon
- initializer
- class-field-check
- field-name
- class-element-modifiers

class-field-check:
- match: (?=\()
fail: class-field
- include: else-pop

class-element-modifiers:
- match: (?={{modifier}})
branch_point: class-element-modifier
branch:
- - match: '{{modifier}}'
scope: storage.modifier.js
set:
- match: (?={{class_element_name}}|\*)
pop: 1
- match: (?=\S)
fail: class-element-modifier

- immediately-pop-2
- include: else-pop

special-name:
- match: true{{identifier_break}}
scope: constant.language.boolean.true.js
Expand Down Expand Up @@ -1938,6 +1938,7 @@ contexts:
- method-name
- method-declaration-expect-asterisk
- method-declaration-prefix
- class-element-modifiers

method-declaration-expect-asterisk:
- match: \*
Expand All @@ -1951,21 +1952,19 @@ contexts:
branch:
- - match: (?:get|set){{identifier_break}}
scope: storage.type.accessor.js
set:
- match: (?={{class_element_name}})
pop: 1
- match: (?=\S)
fail: method-declaration-prefix
set: method-declaration-prefix-check
- match: (?:async){{identifier_break}}
scope: keyword.declaration.async.js
set:
- match: (?=\*|{{class_element_name}})
pop: 1
- match: (?=\S)
fail: method-declaration-prefix
set: method-declaration-prefix-check
- immediately-pop
- include: else-pop

method-declaration-prefix-check:
- match: (?=\*|{{class_element_name}})
pop: 1
- match: (?=\S)
fail: method-declaration-prefix

parenthesized-expression:
- match: \(
scope: punctuation.section.group.begin.js
Expand Down
8 changes: 5 additions & 3 deletions JavaScript/TypeScript.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -387,19 +387,18 @@ contexts:
- match: '\+|-'
scope: storage.modifier.js
- match: (?={{modifier}})
pop: 1
branch_point: ts-type-member-modifier
branch:
- ts-type-member-modifier
- immediately-pop
- immediately-pop-2
- include: else-pop

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

Expand Down Expand Up @@ -655,12 +654,14 @@ contexts:
- meta_include_prototype: false
- match: ''
set:
- expect-semicolon
- initializer
- ts-type-annotation
- ts-type-annotation-optional
- ts-type-annotation-definite
- class-field-check
- field-name
- class-element-modifiers

class-field-check:
- match: (?=[(<])
Expand All @@ -679,6 +680,7 @@ contexts:
- method-name
- method-declaration-expect-asterisk
- method-declaration-prefix
- class-element-modifiers

expression-end:
- meta_prepend: true
Expand Down
9 changes: 8 additions & 1 deletion JavaScript/tests/syntax_test_js_class.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class MyClass extends TheirClass {
// ^ variable.other.readwrite
// ^ keyword.operator.assignment
// ^^ constant.numeric
// ^ punctuation.terminator.statement - punctuation.terminator.statement.empty

'y' = 42;
// ^^^ meta.string string.quoted.single
Expand Down Expand Up @@ -142,7 +143,8 @@ class MyClass extends TheirClass {
for (const param of this.#data.get('value')) {}
// ^ punctuation.definition.variable
// ^^^^ meta.property.object
}
};
// ^ punctuation.terminator.statement.empty.js

#privateMethod() {}
// ^^^^^^^^^^^^^^^^^^^ meta.function
Expand All @@ -168,8 +170,13 @@ class MyClass extends TheirClass {
}

get *foo()
// ^^^^^^^^ meta.function
// ^^^ storage.type.accessor
// ^ keyword.generator.asterisk
// ^^^ entity.name.function

static foo(baz) {
// ^^^^^^^^^^^^^^^^^ meta.function
// ^^^^^^ storage.modifier
// ^^^^^^^^^^ meta.function
// ^^^ entity.name.function
Expand Down

0 comments on commit f32e4bd

Please sign in to comment.