Skip to content

Commit

Permalink
fix: Adds support for JavaScript optional chaining syntax in bindings (
Browse files Browse the repository at this point in the history
…#6788)

* Adds support for JavaScript optional chaining syntax

* adding tests for other volatile binding cases

* Change files

---------

Co-authored-by: nicholasrice <[email protected]>
  • Loading branch information
nicholasrice and nicholasrice authored Jul 21, 2023
1 parent ff0b93a commit 9f6451f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Adds volatile binding support for JavaScript optional chaining syntax",
"packageName": "@microsoft/fast-element",
"email": "[email protected]",
"dependentChangeType": "prerelease"
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from "chai";
import { Updates } from "./update-queue.js";
import { PropertyChangeNotifier, SubscriberSet } from "./notifier.js";
import { ExecutionContext, Observable, observable, volatile } from "./observable.js";
import { ExecutionContext, Expression, Observable, observable, volatile } from "./observable.js";
import { Fake } from "../testing/fakes.js";

describe("The Observable", () => {
Expand Down Expand Up @@ -627,4 +627,34 @@ describe("The Observable", () => {
expect(model.child2ChangedCalled).to.be.true;
});
});

context("isVolatileBinding", () => {
it("should return true when expression uses ternary operator", () => {
const expression = (a) => a !== undefined ? a : undefined;

expect(Observable.isVolatileBinding(expression)).to.equal(true)
});
it("should return true when expression uses 'if' condition", () => {
const expression = (a) => { if (a !== undefined) { return a }};

expect(Observable.isVolatileBinding(expression)).to.equal(true)
});
it("should return true when expression uses '&&' operator", () => {
const expression = (a) => { a && true};

expect(Observable.isVolatileBinding(expression)).to.equal(true)
});
it("should return true when expression uses '||' operator", () => {
const expression = (a) => { a || true};

expect(Observable.isVolatileBinding(expression)).to.equal(true)
});
it("should return true when when expression uses JavaScript optional chaining", () => {
// Avoid TS Compiling Optional property syntax away into ternary
// by using Function constructor
const expression = Function("(a) => a?.b") as Expression;

expect(Observable.isVolatileBinding(expression)).to.equal(true)
})
})
});
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export interface ExpressionNotifier<TSource = any, TReturn = any, TParent = any>
*/
export const Observable = FAST.getById(KernelServiceId.observable, () => {
const queueUpdate = Updates.enqueue;
const volatileRegex = /(:|&&|\|\||if)/;
const volatileRegex = /(:|&&|\|\||if|\?\.)/;
const notifierLookup = new WeakMap<any, Notifier>();
let watcher: ExpressionNotifierImplementation | undefined = void 0;
let createArrayObserver = (array: any[]): Notifier => {
Expand Down

0 comments on commit 9f6451f

Please sign in to comment.