Prettier Plugin for Firestore Rules

raw JSON →
0.1.5 verified Sat Apr 25 auth: no javascript

A prettier plugin to format Firebase Firestore Security Rules files (.rules). Version 0.1.5 adds support for the 'in' operator. It is a lightweight, single-purpose plugin that integrates with Prettier, providing consistent formatting for Firestore rules alongside other code. Compared to manual formatting or IDE extensions, it offers automated, reproducible formatting within the Prettier ecosystem. The plugin is actively maintained with regular releases and supports Node.js ≥11. It is an ideal choice for Firebase projects using Prettier, ensuring rules keep the same code style as the rest of the codebase.

error Error: Cannot find module 'prettier-plugin-firestore-rules'
cause Plugin not installed or not in node_modules.
fix
Run 'npm install --save-dev prettier-plugin-firestore-rules'.
error TypeError: plugin is not a function
cause Incorrect usage: trying to require the plugin as a standalone module instead of via prettier configuration.
fix
Remove direct require/import and add the plugin name to prettier's plugins config.
error Error: No parser could be inferred for file: firestore.rules
cause Plugin not registered in prettier config; prettier does not know how to parse .rules files.
fix
Add the plugin to .prettierrc plugins array.
breaking v0.1.5 changed formatting for 'in' operator. Previous versions may have formatted 'in' differently, causing formatting diffs.
fix Update to >=0.1.5 and re-format .rules files.
deprecated Node.js v10 and below are not supported. The plugin requires Node >=11.
fix Upgrade Node.js to version 11 or later.
gotcha Plugin must be listed in prettier config or passed via CLI argument; otherwise, .rules files will not be formatted.
fix Ensure plugins array includes "prettier-plugin-firestore-rules" in .prettierrc or use --plugin=... on CLI.
gotcha The plugin only formats .rules files; other Firebase configuration files (e.g., firestore.indexes.json) are not affected.
fix Use separate prettier plugins or formatters for other file types.
gotcha No TypeScript typings are provided in this version; the plugin is plain JavaScript.
fix If using TypeScript in your project, ignore missing types or write a declaration file.
npm install prettier-plugin-firestore-rules
yarn add prettier-plugin-firestore-rules
pnpm add prettier-plugin-firestore-rules

Install the plugin, configure .prettierrc to include the plugin, then format .rules files using prettier CLI.

// 1. Install plugin and prettier
npm install --save-dev prettier prettier-plugin-firestore-rules

// 2. Create .prettierrc in project root
// .prettierrc
{
  "plugins": ["prettier-plugin-firestore-rules"],
  "singleQuote": true,
  "tabWidth": 2
}

// 3. Format a .rules file (e.g., firestore.rules)
// Run: npx prettier --write firestore.rules

// Example firestore.rules before formatting:
// rules_version = '2';
// service cloud.firestore {
// match /databases/{database}/documents {
//   match /{document=**} {
//     allow read, write: if false;
//   }
// }
// }

// After formatting:
// rules_version = '2';
// service cloud.firestore {
//   match /databases/{database}/documents {
//     match /{document=**} {
//       allow read, write: if false;
//     }
//   }
// }