Skip to content
This repository was archived by the owner on May 19, 2025. It is now read-only.
This repository was archived by the owner on May 19, 2025. It is now read-only.

Optimize constant subexpressions in boolean and ternary operators #101

@mm-gmbd

Description

@mm-gmbd

I have a generic function that I use called isAuthType which takes a single input argument and the return value is the result of a ternary operator:

function isAuthType(type) {
  return (type == 'type1' ? true : root.users[auth.uid].type == type);
}

That function is used as a rule, and I would expect the following input/output:

path /path/A {
  read()  = isAuth('type1')
}

path /path/B {
  read()  = isAuthType('admin')
}

//Expected rules
"rules": {
  "path": {
    "A": {
      ".read": true
    },
    "B": {
      ".read": root.child('users').child(auth.uid).child('type').val() == 'admin'
    },
  }
}

However, the ternary operator, which can be evaluated (in some cases, I can see that it wouldn't if it was based on non-explicit variable), is not evaluated and I'm left with the following, clunky rules:

"rules": {
  "path": {
    "A": {
      ".read": 'type1' == 'type1' ? true : root.child('users').child(auth.uid).child('type').val() == 'admin'
    },
    "B": {
      ".read": 'admin' == 'type1' ? true : root.child('users').child(auth.uid).child('type').val() == 'admin'
    },
  }
}

I'll probably separate this into two functions in the meantime to clean things up, but having these evaluate would be handy.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions