Skip to content

Commit

Permalink
[JavaScript] Fix ST freezing (#4003)
Browse files Browse the repository at this point in the history
Fixes sublimehq/sublime_text#6416

Regressed by #3836

This commit refactors for loop conditions to prevent ST's syntax engine to run
into an infinite loop, if multiple nested for loops appear within branches,
such as arrow functions.

The issue seems to be triggered by

```yml
  for-condition-contents:
    - match: ''
      branch_point: for-in-of
      branch:
        - for-in-of
        - for-oldstyle
      pop: 1
```

in conjunction with newlines.

Note: It somewhat reorganizes expressions contexts a little bit.
  • Loading branch information
deathaxe authored Jun 29, 2024
1 parent 3cda1fd commit 16c47e7
Showing 1 changed file with 29 additions and 29 deletions.
58 changes: 29 additions & 29 deletions JavaScript/JavaScript.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -842,31 +842,25 @@ contexts:
- include: else-pop

for-condition:
- match: '\('
scope: punctuation.section.group.begin.js
set:
- for-condition-end
- for-condition-contents
- match: \(
scope: meta.group.js punctuation.section.group.begin.js
branch_point: for-in-of
branch:
- for-in-of
- for-oldstyle
- include: else-pop

for-condition-end:
- meta_scope: meta.group.js

- match: '\)'
- match: \)
scope: punctuation.section.group.end.js
pop: 1

for-condition-contents:
- match: ''
branch_point: for-in-of
branch:
- for-in-of
- for-oldstyle
pop: 1

for-in-of:
- meta_include_prototype: false
- match: ''
set:
- for-condition-end
- expression
- for-in-of-word
- for-in-of-declaration
Expand Down Expand Up @@ -916,6 +910,7 @@ contexts:
fail: for-in-of

for-oldstyle:
- meta_include_prototype: false
- match: ''
set:
- for-oldstyle-rest
Expand All @@ -927,16 +922,14 @@ contexts:
set:
- variable-binding-list-top
- variable-binding-top
- match: (?=\S)
set: expression
- include: else-pop

for-oldstyle-rest:
- match: (?=\))
pop: 1
- meta_scope: meta.group.js
- match: ;
scope: punctuation.separator.expression.js
- match: (?=\S)
push: expression
- include: for-condition-end
- include: expressions

block-scope:
- include: block
Expand Down Expand Up @@ -1055,25 +1048,32 @@ contexts:
- include: parenthesized-expression
- include: expression-begin

expression-break:
- match: (?=[;})\]])
pop: 1
expressions:
- match: (?=\S)
push: [ expression-end, expression-begin ]

expression:
- meta_include_prototype: false
- match: ''
set: [ expression-end, expression-begin ]

expression-list:
- include: expression-break
- include: comma-separator
- include: expressions-no-comma

expressions-no-comma:
- match: (?=\S)
push: [ expression-end-no-comma, expression-begin ]

expression-no-comma:
- meta_include_prototype: false
- match: ''
set: [ expression-end-no-comma, expression-begin ]

expression-list:
- include: expression-break
- include: comma-separator
- match: (?=\S)
push: expression-no-comma
expression-break:
- match: (?=[;})\]])
pop: 1

left-expression-end:
- include: expression-break
Expand Down

0 comments on commit 16c47e7

Please sign in to comment.