โก Github Stats
chflick / prettier-plugin-firestore-rules Goto Github PK
View Code? Open in Web Editor NEWFormat your firestore security rules using prettier.
License: MIT License
Format your firestore security rules using prettier.
License: MIT License
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?
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.
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;
}
}
}
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.
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;
}
}
}
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",
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)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.