Giter Site home page Giter Site logo

chflick / prettier-plugin-firestore-rules Goto Github PK

View Code? Open in Web Editor NEW
20.0 3.0 2.0 71.6 MB

Format your firestore security rules using prettier.

License: MIT License

JavaScript 0.32% TypeScript 99.68%
prettier-plugin prettier firebase firestore firestore-rules autoformat

prettier-plugin-firestore-rules's Issues

SyntaxError after `if` gets wrapped into a new line

For example:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{userId} {
      allow get: if request.auth != null
        && request.auth.uid == userId;
    }
  }
}

On the 1st format run, if will be moved into a new line, then all subsequent formats will fail because of a syntax error.

SyntaxError: Expected ";", "if", comment, or whitespace but "\n" found.

Prettier format adding weird periods

So while using this library to format some code, I've noticed that it's adding weird . before &&s in my rules.

For example take this string:

const rules = `
rules_version = '2';
    service cloud.firestore {
  match /databases/{database}/documents {
    allow read, write: if false;
  match /Example/{key} {
function canCreate(data) {
      return data.firstName is string &&
        data.lastName is string &&
        data.hairColor is string;
      }
      allow create: if canCreate(request.resource.data);
    }
  }
}
`;

If I fed it into the following...

import * as fs from 'fs';
import * as prettier from 'prettier';

const rules = ``; // Rules above
const prettyRules = prettier.format(rules, { parser: 'firestore'});

fs.writeFileSync('./firestore.rules'. prettyRules);

It ends up generating this:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    allow read, write: if false;
    match /Example/{key} {
      function canCreate(data) {
        return
        data.firstName is string.&& data.lastName is string
        .&& data.hairColor is string;
      }
      allow create: if canCreate(request.resource.data);
    }
  }
}

As you can see there's a weird period happening between the joining of these different statements.

This does not happen to be an issue if the validations were inline rather than in a function. So these rules:

const rules = `
rules_version = '2';
    service cloud.firestore {
  match /databases/{database}/documents {
    allow read, write: if false;
  match /Example/{key} {
      allow create: if data.firstName is string &&
        data.lastName is string &&
        data.hairColor is string;
    }
  }
}
`;

Generates the appropriate formatting:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    allow read, write: if false;
    match /Example/{key} {
      allow create:
      if data.firstName is string
        && data.lastName is string
        && data.hairColor is string;
    }
  }
}

Print line numbers on error

Right now it doesn't seem to print line numbers at all on errors. I put a simple console.log(location) into the place in the parser the error is generated and it worked. I think you just need to wrap the call to parse catch exceptions and print out the line number.

How do you use this? :)

FOund it, but I cannot seem to install and use it. Put this in the project.json but it doesn't seem to work with prettier

"prettier-plugin-firestore-rules": "github:ChFlick/prettier-plugin-firestore-rules#master",

Support for "existence in list" expressions

If I feed the following snippet to prettier:

rules_version = '2';
service cloud.firestore {
  function foo() {
    return 1 in [1, 2]
  }
}

I get:

firestore.rules: SyntaxError: Expected "!=", "&&", "(", ";", "<", "<=", "==", ">", ">=", "is", "||", "}", comment, or whitespace but "i" found.

I'm guessing the issue is that the in (Security Rules) operator isn't considered part of the grammar?

SyntaxError: appears and does not work properly.

I get the following error code and it does not work properly.

["ERROR" - 11:15:39] Error formatting document.
["ERROR" - 11:15:39] Expected "allow", "match", comment, or whitespace but "f" found.
SyntaxError: Expected "allow", "match", comment, or whitespace but "f" found.
at peg$buildStructuredError (/Users/sagawayuusei/work/gawasa/node_modules/prettier-plugin-firestore-rules/out/parser/parser.js:210:16)
at Object.peg$parse [as parse] (/Users/sagawayuusei/work/gawasa/node_modules/prettier-plugin-firestore-rules/out/parser/parser.js:3659:15)
at Object.parse (/Users/sagawayuusei/work/gawasa/node_modules/prettier/index.js:7515:23)
at coreFormat (/Users/sagawayuusei/work/gawasa/node_modules/prettier/index.js:8829:18)
at formatWithCursor2 (/Users/sagawayuusei/work/gawasa/node_modules/prettier/index.js:9021:18)
at /Users/sagawayuusei/work/gawasa/node_modules/prettier/index.js:38183:12
at Object.format (/Users/sagawayuusei/work/gawasa/node_modules/prettier/index.js:38197:12)
at t.default.format (/Users/sagawayuusei/.vscode/extensions/esbenp.prettier-vscode-9.12.0/dist/extension.js:1:14756)
at async t.default.provideEdits (/Users/sagawayuusei/.vscode/extensions/esbenp.prettier-vscode-9.12.0/dist/extension.js:1:11429)
at async forceFormatDocument (/Users/sagawayuusei/.vscode/extensions/esbenp.prettier-vscode-9.12.0/dist/extension.js:1:8922)
at async m.h (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:98:125789)

Support more than a single function

Hello,

Would it be possible to add support for multiple functions?

Currently it results in:

SyntaxError: Expected "allow", "match", comment, or whitespace but "f" found.

Sample rules to reproduce:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    function getAuth() {
      return request.auth.token;
    }
    function getRequestData() {
      return request.resource.data;
    }
    match /someData/{targetUserId} {
      allow read;
    }
  }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.