Skip to content

Commit

Permalink
[JavaScript] Add optional chaining and nullish coalescing. (sublimehq…
Browse files Browse the repository at this point in the history
…#2132)

* [JavaScript] Add optional chaining and nullish coalescing.
* [JavaScript] Use variable for dot accessor.
* [JavaScript] Don't allow import.?meta.
  • Loading branch information
Thom1729 authored and mitranim committed Mar 20, 2022
1 parent d878c60 commit b7f1b03
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 36 deletions.
83 changes: 47 additions & 36 deletions JavaScript/JavaScript.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ variables:
)
))
dot_accessor: |-
(?x: # Match . and .?, but not .?( or .?[
\.
(?! \? [\[(] )
\??
)
contexts:
main:
- include: comments-top-level
Expand Down Expand Up @@ -815,7 +822,7 @@ contexts:
pop: true

decorator-expression-end:
- match: \.
- match: '{{dot_accessor}}'
scope: punctuation.accessor.js
push:
- include: decorator-name
Expand Down Expand Up @@ -850,14 +857,14 @@ contexts:
left-expression-end:
- include: expression-break

- include: property-access

- match: (?=`)
push: literal-string-template

- match: (?=\()
- match: (?=(?:\.\?)?\()
push: function-call-arguments

- include: property-access

- include: fallthrough

- include: else-pop
Expand Down Expand Up @@ -1078,7 +1085,7 @@ contexts:
- match: in{{identifier_break}}
scope: keyword.operator.js
push: expression-begin
- match: '&&|\|\|'
- match: '&&|\|\||\?\?'
scope: keyword.operator.logical.js
push: expression-begin
- match: '=(?![=>])'
Expand Down Expand Up @@ -1235,7 +1242,7 @@ contexts:
pop: true

inherited-class-expression-end:
- match: \.
- match: '{{dot_accessor}}'
scope: punctuation.accessor.js
push:
- include: inherited-class-name
Expand Down Expand Up @@ -1332,7 +1339,7 @@ contexts:
- literal-variable-base

expect-dot-accessor:
- match: '\.'
- match: '{{dot_accessor}}'
scope: punctuation.accessor.js
pop: true
- include: else-pop
Expand Down Expand Up @@ -1689,8 +1696,10 @@ contexts:
push: expression

function-call-arguments:
- match: \(
scope: punctuation.section.group.begin.js
- match: (\.\?)?(\()
captures:
1: punctuation.accessor.js
2: punctuation.section.group.begin.js
set:
- meta_scope: meta.group.js
- match: \)
Expand All @@ -1709,8 +1718,10 @@ contexts:
- include: expression-list

property-access:
- match: '\['
scope: punctuation.section.brackets.begin.js
- match: (\.\?)?(\[)
captures:
1: punctuation.accessor.js
2: punctuation.section.brackets.begin.js
push:
- meta_scope: meta.brackets.js
- match: '\]'
Expand All @@ -1719,10 +1730,10 @@ contexts:
- match: (?=\S)
push: expression

- match: \.
- match: \.(?:\?)?
scope: punctuation.accessor.js
push:
- match: '(?={{identifier}}\s*\()'
- match: '(?={{identifier}}\s*(?:\.\?)?\()'
set:
- call-method-meta
- function-call-arguments
Expand Down Expand Up @@ -1796,21 +1807,21 @@ contexts:
pop: true

literal-call:
- match: (?={{identifier}}\s*\()
- match: (?={{identifier}}\s*(?:\.\?)?\()
set:
- call-function-meta
- function-call-arguments
- literal-variable

- match: (?={{identifier}}\s*(?:\.\s*{{identifier}}\s*)+\()
- match: (?={{identifier}}\s*(?:\.\s*{{identifier}}\s*)+(?:\.\?)?\()
set:
- call-method-meta
- function-call-arguments
- call-path
- literal-variable

call-path:
- match: \.
- match: '{{dot_accessor}}'
scope: punctuation.accessor.js
push: object-property
- include: else-pop
Expand Down Expand Up @@ -1849,7 +1860,7 @@ contexts:
scope: support.class.js
pop: true

- match: (?={{identifier}}\s*\()
- match: (?={{identifier}}\s*(?:\.\?)?\()
set: call-function-name

- include: literal-variable-base
Expand Down Expand Up @@ -1910,7 +1921,7 @@ contexts:
- match: Array{{identifier_break}}
scope: support.class.builtin.js
set:
- match: \.
- match: '{{dot_accessor}}'
scope: punctuation.accessor.js
set:
- include: support-property-ecma-array
Expand All @@ -1921,7 +1932,7 @@ contexts:
- match: ArrayBuffer{{identifier_break}}
scope: support.class.builtin.js
set:
- match: \.
- match: '{{dot_accessor}}'
scope: punctuation.accessor.js
set:
- include: support-property-ecma-arraybuffer
Expand All @@ -1932,7 +1943,7 @@ contexts:
- match: Atomics{{identifier_break}}
scope: support.constant.builtin.js
set:
- match: \.
- match: '{{dot_accessor}}'
scope: punctuation.accessor.js
set:
- include: support-property-ecma-atomics
Expand All @@ -1943,7 +1954,7 @@ contexts:
- match: BigInt{{identifier_break}}
scope: support.class.builtin.js
set:
- match: \.
- match: '{{dot_accessor}}'
scope: punctuation.accessor.js
set:
- include: support-property-ecma-bigint
Expand All @@ -1954,7 +1965,7 @@ contexts:
- match: Date{{identifier_break}}
scope: support.class.builtin.js
set:
- match: \.
- match: '{{dot_accessor}}'
scope: punctuation.accessor.js
set:
- include: support-property-ecma-date
Expand All @@ -1965,7 +1976,7 @@ contexts:
- match: JSON{{identifier_break}}
scope: support.constant.builtin.js
set:
- match: \.
- match: '{{dot_accessor}}'
scope: punctuation.accessor.js
set:
- include: support-property-ecma-json
Expand All @@ -1976,7 +1987,7 @@ contexts:
- match: Math{{identifier_break}}
scope: support.constant.builtin.js
set:
- match: \.
- match: '{{dot_accessor}}'
scope: punctuation.accessor.js
set:
- include: support-property-ecma-math
Expand All @@ -1987,7 +1998,7 @@ contexts:
- match: Number{{identifier_break}}
scope: support.class.builtin.js
set:
- match: \.
- match: '{{dot_accessor}}'
scope: punctuation.accessor.js
set:
- include: support-property-ecma-number
Expand All @@ -1998,7 +2009,7 @@ contexts:
- match: Object{{identifier_break}}
scope: support.class.builtin.js
set:
- match: \.
- match: '{{dot_accessor}}'
scope: punctuation.accessor.js
set:
- include: support-property-ecma-object
Expand All @@ -2009,7 +2020,7 @@ contexts:
- match: Promise{{identifier_break}}
scope: support.class.builtin.js
set:
- match: \.
- match: '{{dot_accessor}}'
scope: punctuation.accessor.js
set:
- include: support-property-ecma-promise
Expand All @@ -2020,7 +2031,7 @@ contexts:
- match: Proxy{{identifier_break}}
scope: support.class.builtin.js
set:
- match: \.
- match: '{{dot_accessor}}'
scope: punctuation.accessor.js
set:
- include: support-property-ecma-proxy
Expand All @@ -2031,7 +2042,7 @@ contexts:
- match: Reflect{{identifier_break}}
scope: support.constant.builtin.js
set:
- match: \.
- match: '{{dot_accessor}}'
scope: punctuation.accessor.js
set:
- include: support-property-ecma-reflect
Expand All @@ -2042,7 +2053,7 @@ contexts:
- match: String{{identifier_break}}
scope: support.class.builtin.js
set:
- match: \.
- match: '{{dot_accessor}}'
scope: punctuation.accessor.js
set:
- include: support-property-ecma-string
Expand All @@ -2053,7 +2064,7 @@ contexts:
- match: Symbol{{identifier_break}}
scope: support.class.builtin.js
set:
- match: \.
- match: '{{dot_accessor}}'
scope: punctuation.accessor.js
set:
- include: support-property-ecma-symbol
Expand All @@ -2074,7 +2085,7 @@ contexts:
)
scope: support.class.builtin.js
set:
- match: \.
- match: '{{dot_accessor}}'
scope: punctuation.accessor.js
set:
- include: support-property-ecma-typedarray
Expand Down Expand Up @@ -2183,7 +2194,7 @@ contexts:
- match: console{{identifier_break}}
scope: support.type.object.console.js
set:
- match: \.
- match: '{{dot_accessor}}'
scope: punctuation.accessor.js
set: builtin-console-properties
- include: else-pop
Expand Down Expand Up @@ -2211,7 +2222,7 @@ contexts:
- match: process{{identifier_break}}
scope: support.constant.node.js
set:
- match: \.
- match: '{{dot_accessor}}'
scope: punctuation.accessor.js
set:
- include: support-property-node-process
Expand All @@ -2226,7 +2237,7 @@ contexts:
- match: module{{identifier_break}}
scope: support.constant.node.js
set:
- match: \.
- match: '{{dot_accessor}}'
scope: punctuation.accessor.js
set:
- include: support-property-node-module
Expand Down Expand Up @@ -2273,7 +2284,7 @@ contexts:
- include: support-property

- match: '(?={{identifier}}\s*\()'
- match: '(?={{identifier}}\s*(?:\.\?)?\()'
set: call-method-name

- include: object-property-base
Expand Down
26 changes: 26 additions & 0 deletions JavaScript/tests/syntax_test_js.js
Original file line number Diff line number Diff line change
Expand Up @@ -1944,3 +1944,29 @@ debugger;
debugger
[]
// <- meta.sequence

a ?? b;
// ^^ keyword.operator.logical

a.?b.?c;
// ^^ punctuation.accessor
// ^ meta.property.object
// ^^ punctuation.accessor
// ^ meta.property.object

a.?[propName];
// ^^^^^^^^^^^^ meta.brackets
// ^^ punctuation.accessor
// ^ punctuation.section.brackets.begin

a.?();
// ^^^^^ meta.function-call
// ^ variable.function
// ^^^^ meta.group
// ^^ punctuation.accessor
// ^ punctuation.section.group.begin

a.b.?();
// ^^^^^^^ meta.function-call.method
// ^ variable.function
//

0 comments on commit b7f1b03

Please sign in to comment.