Skip to content

Commit

Permalink
feat: support ?.#x and ?.#m()
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed May 22, 2020
1 parent 0742d99 commit 785b6e8
Show file tree
Hide file tree
Showing 33 changed files with 1,400 additions and 7 deletions.
Expand Up @@ -125,12 +125,6 @@ const handle = {
throw member.buildCodeFrameError(`can't handle delete`);
}

if (node.optional) {
throw member.buildCodeFrameError(
`can't handle '?.' directly before ${node.property.type}`,
);
}

// Now, we're looking for the start of this optional chain, which is
// optional to the left of this member.
//
Expand All @@ -150,6 +144,8 @@ const handle = {
startingOptional = startingOptional.get("callee");
continue;
}
// prevent infinite loop: unreachable if the AST is well-formed
throw new Error("Internal error");
}

const { scope } = member;
Expand All @@ -160,8 +156,13 @@ const handle = {
const baseRef = scope.generateUidIdentifierBasedOnNode(startingNode);
scope.push({ id: baseRef });

// Compute parentIsOptionalCall before `startingOptional` is replaced
// as `node` may refer to `startingOptional.node` before replaced.
const parentIsOptionalCall = parentPath.isOptionalCallExpression({
callee: node,
});
startingOptional.replaceWith(toNonOptional(startingOptional, baseRef));
if (parentPath.isOptionalCallExpression({ callee: node })) {
if (parentIsOptionalCall) {
parentPath.replaceWith(this.call(member, parent.arguments));
} else {
member.replaceWith(this.get(member));
Expand Down
@@ -0,0 +1,122 @@
class Foo {
static #x = 1;
static #m = function() { return this.#x; };
static #self = Foo;
static self = Foo;
static getSelf() { return this }

static test() {
const o = { Foo: Foo };
const deep = { very: { o } };
function fn() {
return o;
}
function fnDeep() {
return deep;
}

expect(o?.Foo.#m()).toEqual(1);
expect(o?.Foo.#m().toString).toEqual(1..toString);
expect(o?.Foo.#m().toString()).toEqual('1');

expect(deep?.very.o?.Foo.#m()).toEqual(1);
expect(deep?.very.o?.Foo.#m().toString).toEqual(1..toString);
expect(deep?.very.o?.Foo.#m().toString()).toEqual('1');

expect(o?.Foo.#self.#m()).toEqual(1);
expect(o?.Foo.#self.self.#m()).toEqual(1);
expect(o?.Foo.#self?.self.#m()).toEqual(1);
expect(o?.Foo.#self.self?.self.#m()).toEqual(1);
expect(o?.Foo.#self?.self?.self.#m()).toEqual(1);

expect(o?.Foo.#self.getSelf().#m()).toEqual(1);
expect(o?.Foo.#self.getSelf?.().#m()).toEqual(1);
expect(o?.Foo.#self?.getSelf().#m()).toEqual(1);
expect(o?.Foo.#self?.getSelf?.().#m()).toEqual(1);
expect(o?.Foo.#self.getSelf()?.self.#m()).toEqual(1);
expect(o?.Foo.#self.getSelf?.()?.self.#m()).toEqual(1);
expect(o?.Foo.#self?.getSelf()?.self.#m()).toEqual(1);
expect(o?.Foo.#self?.getSelf?.()?.self.#m()).toEqual(1);

expect(fn?.().Foo.#m()).toEqual(1);
expect(fn?.().Foo.#m().toString).toEqual(1..toString);
expect(fn?.().Foo.#m().toString()).toEqual('1');

expect(fnDeep?.().very.o?.Foo.#m()).toEqual(1);
expect(fnDeep?.().very.o?.Foo.#m().toString).toEqual(1..toString);
expect(fnDeep?.().very.o?.Foo.#m().toString()).toEqual('1');

expect(fn?.().Foo.#self.#m()).toEqual(1);
expect(fn?.().Foo.#self.self.#m()).toEqual(1);
expect(fn?.().Foo.#self?.self.#m()).toEqual(1);
expect(fn?.().Foo.#self.self?.self.#m()).toEqual(1);
expect(fn?.().Foo.#self?.self?.self.#m()).toEqual(1);

expect(fn?.().Foo.#self.getSelf().#m()).toEqual(1);
expect(fn?.().Foo.#self.getSelf?.().#m()).toEqual(1);
expect(fn?.().Foo.#self?.getSelf().#m()).toEqual(1);
expect(fn?.().Foo.#self?.getSelf?.().#m()).toEqual(1);
expect(fn?.().Foo.#self.getSelf()?.self.#m()).toEqual(1);
expect(fn?.().Foo.#self.getSelf?.()?.self.#m()).toEqual(1);
expect(fn?.().Foo.#self?.getSelf()?.self.#m()).toEqual(1);
expect(fn?.().Foo.#self?.getSelf?.()?.self.#m()).toEqual(1);
}

static testNull() {
const o = null;;
const deep = { very: { o } };
const fn = null;
function fnDeep() {
return deep;
}

expect(o?.Foo.#m()).toEqual(undefined);
expect(o?.Foo.#m().toString).toEqual(undefined);
expect(o?.Foo.#m().toString()).toEqual(undefined);

expect(deep?.very.o?.Foo.#m()).toEqual(undefined);
expect(deep?.very.o?.Foo.#m().toString).toEqual(undefined);
expect(deep?.very.o?.Foo.#m().toString()).toEqual(undefined);

expect(o?.Foo.#self.#m()).toEqual(undefined);
expect(o?.Foo.#self.self.#m()).toEqual(undefined);
expect(o?.Foo.#self?.self.#m()).toEqual(undefined);
expect(o?.Foo.#self.self?.self.#m()).toEqual(undefined);
expect(o?.Foo.#self?.self?.self.#m()).toEqual(undefined);

expect(o?.Foo.#self.getSelf().#m()).toEqual(undefined);
expect(o?.Foo.#self.getSelf?.().#m()).toEqual(undefined);
expect(o?.Foo.#self?.getSelf().#m()).toEqual(undefined);
expect(o?.Foo.#self?.getSelf?.().#m()).toEqual(undefined);
expect(o?.Foo.#self.getSelf()?.self.#m()).toEqual(undefined);
expect(o?.Foo.#self.getSelf?.()?.self.#m()).toEqual(undefined);
expect(o?.Foo.#self?.getSelf()?.self.#m()).toEqual(undefined);
expect(o?.Foo.#self?.getSelf?.()?.self.#m()).toEqual(undefined);

expect(fn?.().Foo.#m()).toEqual(undefined);
expect(fn?.().Foo.#m().toString).toEqual(undefined);
expect(fn?.().Foo.#m().toString()).toEqual(undefined);

expect(fnDeep?.().very.o?.Foo.#m()).toEqual(undefined);
expect(fnDeep?.().very.o?.Foo.#m().toString).toEqual(undefined);
expect(fnDeep?.().very.o?.Foo.#m().toString()).toEqual(undefined);

expect(fn?.().Foo.#self.#m()).toEqual(undefined);
expect(fn?.().Foo.#self.self.#m()).toEqual(undefined);
expect(fn?.().Foo.#self?.self.#m()).toEqual(undefined);
expect(fn?.().Foo.#self.self?.self.#m()).toEqual(undefined);
expect(fn?.().Foo.#self?.self?.self.#m()).toEqual(undefined);

expect(fn?.().Foo.#self.getSelf().#m()).toEqual(undefined);
expect(fn?.().Foo.#self.getSelf?.().#m()).toEqual(undefined);
expect(fn?.().Foo.#self?.getSelf().#m()).toEqual(undefined);
expect(fn?.().Foo.#self?.getSelf?.().#m()).toEqual(undefined);
expect(fn?.().Foo.#self.getSelf()?.self.#m()).toEqual(undefined);
expect(fn?.().Foo.#self.getSelf?.()?.self.#m()).toEqual(undefined);
expect(fn?.().Foo.#self?.getSelf()?.self.#m()).toEqual(undefined);
expect(fn?.().Foo.#self?.getSelf?.()?.self.#m()).toEqual(undefined);
}
}

Foo.test();
Foo.testNull();
@@ -0,0 +1,121 @@
class Foo {
static #x = 1;
static #self = Foo;
static self = Foo;
static getSelf() { return this }

static test() {
const o = { Foo: Foo };
const deep = { very: { o } };
function fn() {
return o;
}
function fnDeep() {
return deep;
}

expect(o?.Foo.#x).toEqual(1);
expect(o?.Foo.#x.toString).toEqual(1..toString);
expect(o?.Foo.#x.toString()).toEqual('1');

expect(deep?.very.o?.Foo.#x).toEqual(1);
expect(deep?.very.o?.Foo.#x.toString).toEqual(1..toString);
expect(deep?.very.o?.Foo.#x.toString()).toEqual('1');

expect(o?.Foo.#self.#x).toEqual(1);
expect(o?.Foo.#self.self.#x).toEqual(1);
expect(o?.Foo.#self?.self.#x).toEqual(1);
expect(o?.Foo.#self.self?.self.#x).toEqual(1);
expect(o?.Foo.#self?.self?.self.#x).toEqual(1);

expect(o?.Foo.#self.getSelf().#x).toEqual(1);
expect(o?.Foo.#self.getSelf?.().#x).toEqual(1);
expect(o?.Foo.#self?.getSelf().#x).toEqual(1);
expect(o?.Foo.#self?.getSelf?.().#x).toEqual(1);
expect(o?.Foo.#self.getSelf()?.self.#x).toEqual(1);
expect(o?.Foo.#self.getSelf?.()?.self.#x).toEqual(1);
expect(o?.Foo.#self?.getSelf()?.self.#x).toEqual(1);
expect(o?.Foo.#self?.getSelf?.()?.self.#x).toEqual(1);

expect(fn?.().Foo.#x).toEqual(1);
expect(fn?.().Foo.#x.toString).toEqual(1..toString);
expect(fn?.().Foo.#x.toString()).toEqual('1');

expect(fnDeep?.().very.o?.Foo.#x).toEqual(1);
expect(fnDeep?.().very.o?.Foo.#x.toString).toEqual(1..toString);
expect(fnDeep?.().very.o?.Foo.#x.toString()).toEqual('1');

expect(fn?.().Foo.#self.#x).toEqual(1);
expect(fn?.().Foo.#self.self.#x).toEqual(1);
expect(fn?.().Foo.#self?.self.#x).toEqual(1);
expect(fn?.().Foo.#self.self?.self.#x).toEqual(1);
expect(fn?.().Foo.#self?.self?.self.#x).toEqual(1);

expect(fn?.().Foo.#self.getSelf().#x).toEqual(1);
expect(fn?.().Foo.#self.getSelf?.().#x).toEqual(1);
expect(fn?.().Foo.#self?.getSelf().#x).toEqual(1);
expect(fn?.().Foo.#self?.getSelf?.().#x).toEqual(1);
expect(fn?.().Foo.#self.getSelf()?.self.#x).toEqual(1);
expect(fn?.().Foo.#self.getSelf?.()?.self.#x).toEqual(1);
expect(fn?.().Foo.#self?.getSelf()?.self.#x).toEqual(1);
expect(fn?.().Foo.#self?.getSelf?.()?.self.#x).toEqual(1);
}

static testNull() {
const o = null;;
const deep = { very: { o } };
const fn = null;
function fnDeep() {
return deep;
}

expect(o?.Foo.#x).toEqual(undefined);
expect(o?.Foo.#x.toString).toEqual(undefined);
expect(o?.Foo.#x.toString()).toEqual(undefined);

expect(deep?.very.o?.Foo.#x).toEqual(undefined);
expect(deep?.very.o?.Foo.#x.toString).toEqual(undefined);
expect(deep?.very.o?.Foo.#x.toString()).toEqual(undefined);

expect(o?.Foo.#self.#x).toEqual(undefined);
expect(o?.Foo.#self.self.#x).toEqual(undefined);
expect(o?.Foo.#self?.self.#x).toEqual(undefined);
expect(o?.Foo.#self.self?.self.#x).toEqual(undefined);
expect(o?.Foo.#self?.self?.self.#x).toEqual(undefined);

expect(o?.Foo.#self.getSelf().#x).toEqual(undefined);
expect(o?.Foo.#self.getSelf?.().#x).toEqual(undefined);
expect(o?.Foo.#self?.getSelf().#x).toEqual(undefined);
expect(o?.Foo.#self?.getSelf?.().#x).toEqual(undefined);
expect(o?.Foo.#self.getSelf()?.self.#x).toEqual(undefined);
expect(o?.Foo.#self.getSelf?.()?.self.#x).toEqual(undefined);
expect(o?.Foo.#self?.getSelf()?.self.#x).toEqual(undefined);
expect(o?.Foo.#self?.getSelf?.()?.self.#x).toEqual(undefined);

expect(fn?.().Foo.#x).toEqual(undefined);
expect(fn?.().Foo.#x.toString).toEqual(undefined);
expect(fn?.().Foo.#x.toString()).toEqual(undefined);

expect(fnDeep?.().very.o?.Foo.#x).toEqual(undefined);
expect(fnDeep?.().very.o?.Foo.#x.toString).toEqual(undefined);
expect(fnDeep?.().very.o?.Foo.#x.toString()).toEqual(undefined);

expect(fn?.().Foo.#self.#x).toEqual(undefined);
expect(fn?.().Foo.#self.self.#x).toEqual(undefined);
expect(fn?.().Foo.#self?.self.#x).toEqual(undefined);
expect(fn?.().Foo.#self.self?.self.#x).toEqual(undefined);
expect(fn?.().Foo.#self?.self?.self.#x).toEqual(undefined);

expect(fn?.().Foo.#self.getSelf().#x).toEqual(undefined);
expect(fn?.().Foo.#self.getSelf?.().#x).toEqual(undefined);
expect(fn?.().Foo.#self?.getSelf().#x).toEqual(undefined);
expect(fn?.().Foo.#self?.getSelf?.().#x).toEqual(undefined);
expect(fn?.().Foo.#self.getSelf()?.self.#x).toEqual(undefined);
expect(fn?.().Foo.#self.getSelf?.()?.self.#x).toEqual(undefined);
expect(fn?.().Foo.#self?.getSelf()?.self.#x).toEqual(undefined);
expect(fn?.().Foo.#self?.getSelf?.()?.self.#x).toEqual(undefined);
}
}

Foo.test();
Foo.testNull();

0 comments on commit 785b6e8

Please sign in to comment.