electrovir / prettier-plugin-multiline-arrays

Prettier plugin to force array elements to wrap onto new lines.
https://www.npmjs.com/package/prettier-plugin-multiline-arrays
Creative Commons Zero v1.0 Universal
135 stars 6 forks source link
array format formatting multiline newline prettier wrapping

prettier-plugin-multiline-arrays

Prettier plugin to force array elements to wrap onto new lines, even when there's only one element (if you so specify). Supports control of how many elements appear on each line. Insert leading new lines or trailing commas to manually force array wrapping.

TypeScript, JavaScript, and JSON files are officially supported (others may still work).

Please file issues in the GitHub repo and include code examples if you come across formatting errors.

Usage

Add this config to your prettierrc file:

module.exports = {
    plugins: [
        'prettier-plugin-multiline-arrays',
    ],
};

The order of your plugins array is very important, so if you have other plugins and they don't work initially, try rearranging them. For an example, check out the plugin ordering for this package's Prettier config: ./prettierrc.js

Options

This plugin provides two new options for your Prettier config:

Comment overrides

To set a comment override for all arrays in a file following the comment, change next to set. Like so:

To later undo a set comment, use prettier-multiline-arrays-reset, which resets the options to whatever you have set in prettierrc, or the default values.

Precedence

The precedence of forcing wrapping goes as follows:

  1. Comments override all else
  2. Manually forced wrapping (leading new lines, trailing commas) overrides configs and defaults
  3. Your specific Prettier options override the defaults
  4. The defaults are that no extra wrapping will be forced

Examples

Compatibility

Tested to be compatible with the following plugins. It is likely compatible with many others as well. This plugin must be placed in the order specified below.

  1. prettier-plugin-toml
  2. prettier-plugin-sort-json
  3. prettier-plugin-packagejson
  4. this plugin must be placed here
  5. prettier-plugin-organize-imports
  6. prettier-plugin-jsdoc
  7. prettier-plugin-interpolated-html-tags (needs to be updated to v3)

Dev

Debugging