From 196740cdcd1fb0defa021deb8e46d5c5ae3def71 Mon Sep 17 00:00:00 2001 From: kdy1 Date: Fri, 31 Jan 2020 15:59:47 +0000 Subject: [PATCH] Allow optional chaining in ecmascript (#619) Fixes #618 --- ecmascript/parser/src/lib.rs | 14 ++++++++++++++ ecmascript/parser/src/parser/expr.rs | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/ecmascript/parser/src/lib.rs b/ecmascript/parser/src/lib.rs index 0ca2935b24d..cb5dabb638e 100644 --- a/ecmascript/parser/src/lib.rs +++ b/ecmascript/parser/src/lib.rs @@ -137,6 +137,17 @@ impl Syntax { } } + pub fn optional_chaining(self) -> bool { + match self { + Syntax::Es(EsConfig { + optional_chaining: true, + .. + }) + | Syntax::Typescript(TsConfig { .. }) => true, + _ => false, + } + } + pub fn dynamic_import(self) -> bool { match self { Syntax::Es(EsConfig { @@ -361,6 +372,9 @@ pub struct EsConfig { #[serde(default)] pub nullish_coalescing: bool, + #[serde(default)] + pub optional_chaining: bool, + /// Stage 3. #[serde(default)] pub import_meta: bool, diff --git a/ecmascript/parser/src/parser/expr.rs b/ecmascript/parser/src/parser/expr.rs index b788151c06c..449cae8b76e 100644 --- a/ecmascript/parser/src/parser/expr.rs +++ b/ecmascript/parser/src/parser/expr.rs @@ -888,7 +888,7 @@ impl<'a, I: Tokens> Parser<'a, I> { } let is_optional_chaining = - self.input.syntax().typescript() && is!('?') && peeked_is!('.') && eat!('?'); + self.input.syntax().optional_chaining() && is!('?') && peeked_is!('.') && eat!('?'); /// Wrap with optional chaining macro_rules! wrap {