Skip to content

Existence checks are missing in switch/pattern matching when using that #743

Description

@ruby-random

This code:

-> | foo? => 42

Produces this compiled output:

// Generated by LiveScript 1.4.0
(function(){
  switch (false) {
  case typeof foo == 'undefined' || foo === null:
    return 42;
  }
});

However, if you mention that in the return clause:

-> | foo? => that

It will compile to this:

// Generated by LiveScript 1.4.0
(function(){
  var that;
  switch (false) {
  case (that = foo) == null:
    return that;
  }
});

Note missing checks for foo existence, so this code will die with ReferenceError when foo is not defined, and it shouldn't happen.

Activity

  1. misterfish commented on Jun 4, 2016

    @misterfish
    Contributor

    Indeed this can be seen even more easily like this:

    if a?
        console.log that # => ReferenceError

    PR #895 might fix this.


    if a?
        console.log that
    var that;
    if (typeof a != 'undefined' && a !== null && (that = a, true)) {
      console.log(that);
    }

    if not a?
        console.log that
    var that;
    if ((that = undefined) || typeof a == 'undefined' || a === null) {
      console.log(that);
    }

    a = 10
    if a?
        console.log that
    var a;
    a = 10;
    if ((that = a) != null) {
      console.log(that);
    }

    a = 10
    if not a?
        console.log that
    var a;
    a = 10;
    if ((that = a) == null) {
      console.log(that);
    }

    The switch and while variants should work as well, and forms such as

    if a? and not b?
        that

    work the same as before I believe.

    Any other cases to think about? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions