npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

prettier-plugin-apex-imo

v0.2.0

Published

Opinionated multiline formatting for Apex Lists and Maps - extends prettier-plugin-apex

Readme

prettier-plugin-apex-imo

npm version License: MIT CI

IMO = In My Opinion. Because Prettier is opinionated, and so am I.

An opinionated enhancement for prettier-plugin-apex that provides comprehensive Apex code formatting including multiline collection formatting, casing normalization, annotation formatting, modifier ordering, and ApexDoc code block formatting.

The Problem

When using prettier-plugin-apex, code like this:

final String expectedJson = String.join(new List<String>{
  '{',
  '  "tags" : [ "reading", "gaming", "coding" ]',
  '}'
}, '\n');

Gets reformatted to a single line, defeating the purpose of readable formatting:

final String expectedJson = String.join(new List<String>{ '{', '  \"tags\" : [ \"reading\", \"gaming\", \"coding\" ]', '}' }, '\n');

The Solution

This plugin wraps prettier-plugin-apex and modifies the printing behaviour with comprehensive formatting enhancements:

  • Collection formatting → Lists, Sets, and Maps with 2+ entries are always multiline with braces on separate lines
  • Casing normalization → Type names, reserved words, and annotations are normalized to proper casing
  • ApexDoc {@code} blocks → Code inside is formatted using Prettier
  • Annotation formatting → Annotations are normalized and properly formatted
  • Modifier ordering → Modifiers are sorted in a consistent order
  • Enhanced comment handling → Better comment placement and attachment using Prettier's comment system

This is non-configurable behaviour. Once installed, it just works.

Architecture

This plugin has been architected with AST-based processing as the primary approach, minimizing regex usage and maximizing leverage of Prettier's infrastructure:

  • AST-First Processing: Works with comment AST nodes and token structures rather than raw text parsing
  • Prettier Integration: Uses Prettier's doc builders (fill, join) for text formatting instead of regex-based word splitting
  • Minimal Regex: Only uses regex where absolutely necessary (complex annotation parsing, preprocessing)
  • Character-Based Fallbacks: Uses simple character scanning only for text content within AST nodes (like code blocks in comments)

Benefits

  • Better Performance: Reduced regex compilation overhead
  • Improved Reliability: AST operations are more predictable than complex regex patterns
  • Enhanced Maintainability: Structured processing is easier to understand and modify
  • Future Compatibility: Leverages Prettier's AST infrastructure for better long-term compatibility

Features

Collection Formatting

Collections (Lists, Sets, and Maps) with 2 or more entries are always formatted multiline with opening and closing braces on separate lines:

// Before
List<String> items = new List<String>{ 'one', 'two', 'three' };

// After
List<String> items = new List<String>{
  'one',
  'two',
  'three'
};

Single-item collections remain on one line for compactness.

Casing Normalization

The plugin normalizes casing throughout your codebase:

  • Type names: stringString, accountAccount, list<integer>List<Integer>
  • Reserved words: PUBLICpublic, Classclass, STATICstatic
  • Standard objects: accountAccount, contactContact
  • Object suffixes: MyObject__CMyObject__c, Custom__datacategoryselectionCustom__DataCategorySelection
  • Annotation names: auraenabledAuraEnabled, testmethodTestMethod
  • Annotation options: cacheablecacheable (normalized to camelCase)

This ensures consistent casing across your entire codebase, making it easier to read and maintain.

Annotation Formatting

Annotations are normalized and formatted with proper multiline support:

// Before
@auraenabled(cacheable=true)
@testmethod

// After
@AuraEnabled(cacheable = true)
@TestMethod

Annotations with multiple parameters are automatically formatted multiline when needed.

Modifier Ordering

Modifiers are automatically sorted in a consistent order:

  • Fields: Access modifier → staticfinaltransient → other
  • Methods: Access modifier → staticoverridevirtualabstract → other
  • Annotations: Always appear before keyword modifiers

This ensures a consistent code style across your project.

ApexDoc Formatting

Code inside ApexDoc {@code} blocks is automatically formatted using Prettier, with proper indentation and alignment:

/**
 * Example method.
 * {@code
 *   List<String> items = new List<String>{
 *     'a',
 *     'b',
 *     'c'
 *   };
 * }
 */

The formatting maintains the * vertical alignment and handles nested braces correctly.

Comment Handling Improvements

This plugin includes enhanced comment handling that leverages Prettier's built-in comment attachment system:

  • Smart comment placement for Apex-specific constructs (classes, interfaces, block statements)
  • Dangling comment support for empty code blocks
  • Binary expression comments properly attached to right operands
  • Block statement leading comments moved into block bodies for better formatting
  • ApexDoc normalization → Missing or extra asterisks are normalized, indentation is corrected

These improvements ensure comments are placed more intelligently and consistently with Prettier's standards.

Installation

pnpm add -D prettier prettier-plugin-apex prettier-plugin-apex-imo

Or with npm:

npm install --save-dev prettier prettier-plugin-apex prettier-plugin-apex-imo

Usage in Salesforce Projects

If you're working with a Salesforce project (created with sf project generate or Salesforce DX), follow these steps:

  1. Install the plugin:

    npm install --save-dev prettier-plugin-apex-imo

    Standard Salesforce projects already include prettier and prettier-plugin-apex in their package.json, so you only need to install prettier-plugin-apex-imo.

  2. Update your .prettierrc file:

    Replace prettier-plugin-apex with prettier-plugin-apex-imo in the plugins array:

    {
    	"trailingComma": "none",
    	"plugins": ["prettier-plugin-apex-imo", "@prettier/plugin-xml"]
    }

    The prettier-plugin-apex-imo plugin wraps prettier-plugin-apex, so you only need to specify prettier-plugin-apex-imo in your config. However, both plugins must be installed since prettier-plugin-apex is a peer dependency.

  3. Verify the configuration:

    npm run prettier:verify

    Or format your files:

    npm run prettier

    Standard Salesforce projects typically include a prettier script in package.json that formats all relevant files including Apex classes (.cls) and triggers (.trigger).

Examples

Before (prettier-plugin-apex)

List<String> items = new List<String>{ 'one', 'two', 'three' };
Set<String> tags = new Set<String>{ 'reading', 'gaming', 'coding' };
Map<String, Integer> counts = new Map<String, Integer>{ 'a' => 1, 'b' => 2 };

After (prettier-plugin-apex-imo)

List<String> items = new List<String>{
  'one',
  'two',
  'three'
};
Set<String> tags = new Set<String>{
  'reading',
  'gaming',
  'coding'
};
Map<String, Integer> counts = new Map<String, Integer>{
  'a' => 1,
  'b' => 2
};

Single Items (unchanged)

// These stay on one line
List<String> single = new List<String>{ 'only' };
Set<String> singleSet = new Set<String>{ 'only' };
Map<String, Integer> singleMap = new Map<String, Integer>{ 'key' => 1 };

Additional Examples

Casing Normalization

// Before
public class myclass {
    private string name;
    private account acc;
    private list<integer> numbers;
}

// After
public class MyClass {
    private String name;
    private Account acc;
    private List<Integer> numbers;
}

Modifier Ordering

// Before
final public static Integer count;

// After
public static final Integer count;

Annotation Normalization

// Before
@auraenabled(cacheable=true)
@testmethod
public void myMethod() {}

// After
@AuraEnabled(cacheable = true)
@TestMethod
public void myMethod() {}

Requirements

  • Node.js >= 20
  • Prettier >= 3.0.0
  • prettier-plugin-apex >= 2.0.0

Why "imo"?

Prettier has a strict option philosophy that discourages adding new formatting options. While I respect this philosophy, I believe the current behaviour for multi-item Lists and Maps is suboptimal for code readability.

Rather than fork prettier-plugin-apex or maintain options, this plugin provides a simple, opinionated wrapper that enforces the behaviour I (and hopefully others) prefer.

Contributing

Contributions are welcome! Please read CONTRIBUTING.md for details.

Security

For security issues, please email [email protected]. See SECURITY.md for details.

License

This project is licensed under the MIT License. See the LICENSE.md file for details.

Acknowledgements