prettier-plugin-jenkinsfile
v0.3.0
Published
Prettier plugin for Jenkins Declarative Pipeline files using tree-sitter-groovy
Downloads
48
Maintainers
Readme
prettier-plugin-jenkinsfile
A Prettier plugin for formatting Jenkins Declarative Pipeline files (Jenkinsfile) and Groovy files.
Uses a forked tree-sitter-groovy (extended from murtaza64/tree-sitter-groovy) for parsing, producing AST-aware formatting that understands Groovy's named parameter syntax and Jenkins DSL patterns.
Features
terraform(),ansible(), andtofu*calls always expand named arguments one per line with trailing commas- Short single-statement closures kept inline (e.g.
retry(3) { sh 'make' }) - Multi-statement closures expanded to block format
- Triple-quoted strings (YAML pod specs) preserved verbatim
- Proper 2-space indentation throughout
- Jenkins DSL blocks (
pipeline,stages,stage,steps,agent,dir,when, etc.) always kept in block form
Installation
npm install --save-dev prettier-plugin-jenkinsfileUsage
# Format a Jenkinsfile
npx prettier --write Jenkinsfile
# Format all Jenkinsfiles in a directory
npx prettier --write '**/Jenkinsfile'
# Check formatting (CI mode)
npx prettier --check JenkinsfileThe plugin automatically detects files named Jenkinsfile and files with .groovy or .gradle extensions.
Pre-commit / prek hook
This repo ships a pre-commit hook (also compatible with prek). Add it to your .pre-commit-config.yaml:
repos:
- repo: https://github.com/bootswithdefer/prettier-plugin-jenkinsfile
rev: v0.1.2
hooks:
- id: jenkinsfile-fmtThe hook formats files named Jenkinsfile and files with .groovy or .gradle extensions on commit. It runs Prettier with this plugin in an isolated Node environment managed by pre-commit — no Node project or global install required.
Example
Before:
#!groovy
def appName = 'my-service'
pipeline {
agent { label 'linux' }
stages {
stage('deploy') {
steps {
script {
terraform(workspace: 'production', auto_approve: true)
retry(3) {
sh './deploy.sh'
}
}
}
}
}
}After:
#!groovy
def appName = 'my-service'
pipeline {
agent {
label 'linux'
}
stages {
stage('deploy') {
steps {
script {
terraform(
workspace: 'production',
auto_approve: true,
)
retry(3) { sh './deploy.sh' }
}
}
}
}
}Configuration
This plugin uses Prettier's standard configuration. Relevant options:
| Option | Default | Description |
|--------|---------|-------------|
| tabWidth | 2 | Indentation width |
| printWidth | 80 | Line width for inline/expand decisions |
How It Works
- Parses the file using
tree-sitter-groovy(via WebAssembly) - Walks the concrete syntax tree (CST)
- Emits Prettier's intermediate representation (doc IR)
- Prettier handles line-breaking, indentation, and output
The plugin bundles a pre-compiled tree-sitter-groovy.wasm — no native dependencies required.
Limitations
- Does not format arbitrary Groovy (class definitions, complex expressions). Focused on Jenkins Pipeline DSL patterns.
- Comments attached to the end of a line may move to the next line.
- The parser handles most Declarative Pipeline patterns but may not parse highly unusual Scripted Pipeline code.
License
MIT
