Skip to content

Prettier format adding weird periods #7

@MichaelSolati

Description

@MichaelSolati

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions