Prettier Code Formatter

repository·main·Indexed 13 days ago

https://github.com/prettier/prettier

An opinionated code formatter that ensures consistent style across a codebase by parsing code and re-printing it with its own rules. Version 3.10.0-dev includes a programmatic API with functions such as `prettier.format()`, `prettier.check()`, `prettier.resolveConfig()`, and `prettier.getFileInfo()` for editor integrations and CI/CD pipelines.

Tokens
121.5K
Snippets
567
Records
763
Agent score
98%

What's inside Prettier

  1. What is Prettier?

    main

    Prettier is an opinionated code formatter that enforces a consistent code style across your entire codebase. It works by parsing your code into an Abstract Syntax Tree (AST) and then reprinting it from scratch based on its own rules and the configured maximum line length. This process removes original styling and ensures code conforms to a consistent format, automatically wrapping lines when they exceed the allowed length.

    Supported languages include:

    • JavaScript (including experimental features) and JSX
    • TypeScript and Flow
    • Angular, Vue, and Lightning Web Components (LWC)
    • CSS, Less, and SCSS
    • HTML
    • Ember/Handlebars
    • JSON
    • GraphQL
    • Markdown (including GFM and MDX v1)
    • YAML
    • MJML
  2. CSS: Improved handling of at-rule comments and unquoted URLs

    main

    Prettier 2.1 includes several CSS improvements:

    • Does not break code when a comment is placed at the end of an at-rule.
    • Improved handling of unquoted url() content to prevent unexpected modifications.
    • Correctly handles whitespace after escaped colons in CSS grid line names.
    • Supports the @supports selector(<custom-selector>) syntax.
    • Improved handling of arbitrary arguments (e.g., in functions like rgba()).
  3. Benefits of adopting Prettier

    main

    Prettier is an automatic code formatter designed to eliminate debates over coding styles and reduce the mental overhead of manual formatting. Key benefits include:

    • Automated Style Enforcement: Replaces manual style guides with a fully automatic process, ending discussions over 'nits' (minor stylistic details).
    • Reduced Cognitive Load: Allows developers to focus on logic rather than syntax and formatting (e.g., line breaks, spacing, or indentation).
    • Onboarding Support: Helps newcomers and developers from different language backgrounds quickly adapt to the project's specific coding style.
    • Codebase Uniformity: Enables quick cleanup of inconsistent, legacy codebases to make them uniform and readable.
    • Low Friction Integration: Designed for easy adoption via pre-commit scripts, editor extensions (format on save), and fast execution speeds.
  4. Explore Prettier ecosystem tools and alternatives

    main

    The Prettier ecosystem includes various tools for performance, specific environments, and different languages:

    Performance and Workflow

    • parallel-prettier: An alternative CLI that formats files in parallel to speed up large projects.
    • prettier_d: Runs Prettier as a server to avoid Node.js startup delays.
    • pretty-quick: Formats only the files that have changed.

    Build Tools and CI/CD

    • rollup-plugin-prettier: Integrates Prettier into the Rollup build process.
    • jest-runner-prettier: Uses Prettier as a Jest runner.
    • reviewdog-action-prettier: Runs Prettier within GitHub Actions CI/CD workflows.
    • MegaLinter: An open-source linter aggregator for CI that includes Prettier support out of the box.
    • spotless: Allows running Prettier from Gradle or Maven.

    Editors and Browsers

    • prettier-chrome: A browser extension that runs Prettier.
    • monaco-prettier: Integrates Prettier into the Monaco editor.

    Language Ports and Forks

    • prettierx: A less opinionated fork of Prettier.
    • csharpier: A port of Prettier for C#.
    • Prettier (Swift): A Swift version based on Prettier.
  5. SCSS: Fixed indentation and comment placement

    main

    Prettier 2.1 improves SCSS formatting:

    • Prevents extra indentation on lines following comments within SCSS maps.
    • Correctly handles inline comments placed between a property and its value.
    • Ensures comments at the end of a file are preserved even if the final semicolon is omitted.
  6. Overview of Prettier Plugins

    main
    Plugins are the mechanism used to add new languages or formatting rules to Prettier. While the core prettier package includes built-in support for JavaScript and other web-focused languages, additional languages require the installation of specific plugins.
  7. Format JSX with do expressions and specific attribute behaviors

    main

    Prettier supports inlining do expressions within JSX containers to reduce unnecessary indentation. It also prevents adding softlines after arrow attributes with comments and avoids wrapping JSX elements as attributes in parentheses () to prevent syntax errors.

    // Inlined do expressions
    {
      do { /* ... */ }
    }
    
    // No unnecessary parens around JSX attributes
    <Foo
      content=<div />
    />
  8. Best practice: Install Prettier locally in every project

    main
    To ensure consistency across different environments and team members, it is important to install Prettier locally in every project (e.g., as a devDependency in your package.json). This ensures that each project uses the specific version of Prettier it was configured for, rather than a global version that might behave differently.
  9. Stabilized arrow function body indentation in TypeScript

    main

    To prevent large git diffs and conflicts when changing the length of type annotations, Prettier has stabilized the function body offset for arrow functions. The indentation of the function body is no longer directly affected by the length of the type declaration in the same way as previous versions.

    // Prettier 2.5 output for long type annotations
    const MyComponentWithLongName: React.VoidFunctionComponent<
      MyComponentWithLongNameProps
    > = ({ x, y }) => {
      const a = useA();
      return <div>{x + y + a}</div>;
    };