Runs Prettier as a TSLint rule and reports differences as individual TSLint issues.
npx @tessl/cli install tessl/npm-tslint-plugin-prettier@2.3.0TSLint Plugin Prettier integrates Prettier code formatting into the TSLint linting workflow. It runs Prettier as a TSLint rule and reports formatting differences as individual TSLint issues, enabling teams to enforce consistent code formatting through their existing TSLint setup with automatic fix suggestions.
npm install --save-dev tslint-plugin-prettier prettierThe package is used as a TSLint plugin, not directly imported in code. It extends TSLint's rule system:
{
"rulesDirectory": ["tslint-plugin-prettier"],
"rules": {
"prettier": true
}
}For TSLint 5.0.0+, you can also use the extends syntax:
{
"extends": ["tslint-plugin-prettier"],
"rules": {
"prettier": true
}
}Add the prettier rule to your tslint.json configuration:
{
"rulesDirectory": ["tslint-plugin-prettier"],
"rules": {
"prettier": true
}
}The plugin will automatically:
.prettierignore files to exclude files from formattingThe main export provides TSLint plugin configuration that registers the prettier rule.
// Main plugin export (rules/index.js)
export = {
rulesDirectory: string; // Value: '.'
};The core rule that integrates Prettier formatting into TSLint.
// prettier rule (rules/prettierRule.js)
export class Rule extends tslint.Rules.AbstractRule {
/**
* Apply the prettier rule to a TypeScript source file
* @param sourceFile - TypeScript source file to analyze
* @returns Array of TSLint rule failures for formatting differences
*/
apply(sourceFile: ts.SourceFile): tslint.RuleFailure[];
}The prettier rule accepts flexible configuration options:
Basic Usage (auto-config):
{
"rules": {
"prettier": true
}
}With Prettier Options Object:
{
"rules": {
"prettier": [true, { "singleQuote": true, "trailingComma": "es5" }]
}
}With Config File Path:
{
"rules": {
"prettier": [true, "configs/.prettierrc"]
}
}With Additional Options:
{
"rules": {
"prettier": [true, null, {
"editorconfig": false,
"ignorePath": "custom.prettierignore"
}]
}
}// Rule configuration signature
type PrettierRuleConfig = [
boolean, // Enable rule
PrettierOptions | string | null?, // Prettier config object, config file path, or null for auto-discovery
AdditionalOptions? // Additional plugin options
];
interface PrettierOptions {
// Standard Prettier options (singleQuote, trailingComma, etc.)
[key: string]: any;
}
interface AdditionalOptions {
/** Whether to load .editorconfig files (default: true) */
editorconfig?: boolean;
/** Path to custom .prettierignore file (default: '.prettierignore') */
ignorePath?: string;
}The rule follows this processing flow:
prettier.getFileInfo.sync() to check if file should be ignored via .prettierignoreprettier.format() with TypeScript parser on source codeThe rule handles two types of issues:
Syntax Errors: When Prettier encounters invalid TypeScript syntax
// Reported as TSLint failure with location information
type SyntaxErrorFailure = {
message: `SyntaxError: ${string}`;
position: number;
length: 1;
};Formatting Differences: When code doesn't match Prettier's formatting
// Three types of formatting fixes
type FormattingFailureType = 'insert' | 'delete' | 'replace';
interface FormattingFailure {
operation: FormattingFailureType;
message: string; // e.g., "Insert ` `", "Delete `;;`", "Replace `''` with `"";`"
fix: tslint.Replacement; // Automatic fix suggestion
}The plugin integrates with several key dependencies:
generateDifferences() and showInvisibles() utilities for comparing formatted codetslint.Rules.AbstractRule and uses tslint.RuleFailure systemEnforce consistent formatting:
# Run TSLint with prettier rule
npx tslint --project tsconfig.json
# Auto-fix formatting issues
npx tslint --project tsconfig.json --fixTypical TSLint configuration with prettier:
{
"extends": ["tslint-plugin-prettier", "tslint-config-prettier"],
"rules": {
"prettier": [true, {
"singleQuote": true,
"trailingComma": "es5",
"printWidth": 100
}]
}
}Custom ignore configuration:
{
"rules": {
"prettier": [true, null, {
"ignorePath": "build/.prettierignore",
"editorconfig": false
}]
}
}// TypeScript integration types
import * as ts from 'typescript';
import * as tslint from 'tslint';
import * as prettier from 'prettier';
// Rule implementation extends TSLint's AbstractRule
class Rule extends tslint.Rules.AbstractRule {
apply(sourceFile: ts.SourceFile): tslint.RuleFailure[];
}
// Configuration types for rule arguments
type RuleArguments = [
PrettierConfig | string | null,
{ editorconfig?: boolean; ignorePath?: string }?
];
type PrettierConfig = prettier.Options;