Skip to content

Commit

Permalink
fix: support MemberExpression.optional (but leave support for non-sta…
Browse files Browse the repository at this point in the history
…ndard OptionalMemberExpression type)

see estree/estree#204
  • Loading branch information
6utt3rfly committed Sep 26, 2021
1 parent 155cd50 commit df2ae7a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/Expression.js
Expand Up @@ -139,7 +139,7 @@ class Expression extends ExpressionSync {
return Expression.FAIL;
}

let prop = unset ? value : await this.visit(property, data, node);
let prop = unset ? value : await this.visit(property, node.optional ? data || {} : data, node);
if (prop === undefined && property.name && data) {
prop = data[property.name];
}
Expand Down
11 changes: 10 additions & 1 deletion lib/ExpressionSync.js
Expand Up @@ -307,7 +307,16 @@ class ExpressionSync {
return ExpressionSync.FAIL;
}

let prop = unset ? value : this.visit(property, data, node);
let prop;
if (unset) {
prop = value;
} else if (node.optional) {
const optional = v => this.visit(property, data || {}, node);
prop = property instanceof Promise ? property.then(v => optional(v)) : optional(property);
} else {
prop = this.visit(property, data, node);
}

if (prop == null && property.name && data) {
prop = data[property.name];
}
Expand Down

0 comments on commit df2ae7a

Please sign in to comment.