Runs prettier as an eslint rule
npx @tessl/cli install tessl/npm-eslint-plugin-prettier@4.2.0ESLint Plugin Prettier integrates Prettier code formatting directly into the ESLint linting process. It runs Prettier as an ESLint rule and reports formatting differences as individual ESLint issues, enabling developers to enforce consistent code formatting within their existing ESLint workflow.
npm install --save-dev eslint-plugin-prettierconst eslintPluginPrettier = require('eslint-plugin-prettier');For ESM environments:
import eslintPluginPrettier from 'eslint-plugin-prettier';The plugin exports an object with two main properties:
module.exports = {
configs: {
recommended: {
extends: ['prettier'],
plugins: ['prettier'],
rules: {
'prettier/prettier': 'error',
'arrow-body-style': 'off',
'prefer-arrow-callback': 'off'
}
}
},
rules: {
prettier: {
meta: { /* ... */ },
create: function(context) { /* ... */ }
}
}
};{
"plugins": ["prettier"],
"rules": {
"prettier/prettier": "error"
}
}{
"extends": ["plugin:prettier/recommended"]
}{
"rules": {
"prettier/prettier": [
"error",
{
"semi": false,
"singleQuote": true
},
{
"usePrettierrc": false
}
]
}
}ESLint Plugin Prettier consists of two main components:
Pre-configured ESLint configurations that set up the plugin with proper defaults.
const configs = {
recommended: {
extends: ['prettier'],
plugins: ['prettier'],
rules: {
'prettier/prettier': 'error',
'arrow-body-style': 'off',
'prefer-arrow-callback': 'off'
}
}
};The recommended configuration:
eslint-config-prettier to disable conflicting formatting rulesprettier/prettier rule as an errorarrow-body-style, prefer-arrow-callback)The main ESLint rule that runs Prettier formatting and reports differences.
const rules = {
prettier: {
meta: {
docs: {
url: 'https://github.com/prettier/eslint-plugin-prettier#options'
},
type: 'layout',
fixable: 'code',
schema: [ /* Prettier options schema */ ],
messages: {
INSERT: 'Insert `{{ insertText }}`',
DELETE: 'Delete `{{ deleteText }}`',
REPLACE: 'Replace `{{ deleteText }}` with `{{ insertText }}`'
}
},
create: function(context) {
// Rule implementation
return {
Program() {
// Prettier formatting logic
}
};
}
}
};Configure the prettier rule with options for both Prettier formatting and plugin behavior.
interface PrettierRuleOptions {
// First parameter: Prettier formatting options (any valid Prettier options)
prettierOptions?: {
[key: string]: any;
};
// Second parameter: Plugin-specific options
pluginOptions?: {
usePrettierrc?: boolean;
fileInfoOptions?: {
[key: string]: any;
};
};
}Configuration Parameters:
prettierOptions (optional): Any valid Prettier configuration options (semi, singleQuote, tabWidth, etc.)pluginOptions (optional): Plugin-specific configuration
usePrettierrc (boolean, default: true): Whether to use .prettierrc configuration filesfileInfoOptions (object): Additional options passed to prettier.getFileInfo()Usage Examples:
// Using Prettier options only
{
"prettier/prettier": ["error", { "semi": false, "singleQuote": true }]
}
// Using both Prettier and plugin options
{
"prettier/prettier": [
"error",
{ "semi": false },
{ "usePrettierrc": false, "fileInfoOptions": { "ignorePath": ".prettierignore" } }
]
}
// Using plugin options only
{
"prettier/prettier": ["error", {}, { "usePrettierrc": false }]
}The rule metadata defines the ESLint rule characteristics and configuration schema.
interface RuleMeta {
docs: {
url: string;
};
type: 'layout';
fixable: 'code';
schema: [
{
type: 'object',
properties: {},
additionalProperties: true
},
{
type: 'object',
properties: {
usePrettierrc: { type: 'boolean' },
fileInfoOptions: {
type: 'object',
properties: {},
additionalProperties: true
}
},
additionalProperties: true
}
];
messages: {
INSERT: string;
DELETE: string;
REPLACE: string;
};
}The rule metadata includes:
eslint --fixMessage Templates:
The rule uses three message template constants for different types of formatting differences:
const messages = {
INSERT: 'Insert `{{ insertText }}`',
DELETE: 'Delete `{{ deleteText }}`',
REPLACE: 'Replace `{{ deleteText }}` with `{{ insertText }}`'
};These correspond to the operations generated by prettier-linter-helpers when comparing original and formatted code:
INSERT: When Prettier determines additional text should be addedDELETE: When Prettier determines text should be removedREPLACE: When Prettier determines text should be replaced with different textThe plugin automatically handles various file types and ESLint processor integrations:
Processor Compatibility:
The plugin includes special logic for ESLint processors that extract JavaScript from other file types. It automatically detects when code has been processed and adjusts parser selection accordingly.
Ignored Files:
Files listed in .prettierignore are automatically skipped during processing.
interface RuleContext {
options: any[];
getSourceCode(): SourceCode;
getFilename(): string;
getPhysicalFilename(): string;
report(descriptor: ReportDescriptor): void;
}
interface SourceCode {
text: string;
getLocFromIndex(index: number): Location;
}
interface Location {
line: number;
column: number;
}
interface ReportDescriptor {
messageId: string;
data?: { [key: string]: string };
loc: { start: Location; end: Location };
fix?: (fixer: RuleFixer) => Fix;
}
interface RuleFixer {
replaceTextRange(range: [number, number], text: string): Fix;
}
interface Fix {
range: [number, number];
text: string;
}
interface RuleDefinition {
Program(): void;
}