prettier-plugin-firestore-rules icon indicating copy to clipboard operation
prettier-plugin-firestore-rules copied to clipboard

Prettier format adding weird periods

Open MichaelSolati opened this issue 4 years ago • 0 comments

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;
    }
  }
}

MichaelSolati avatar Jan 02 '21 23:01 MichaelSolati