src/rules.ts, src/formatter.ts, and related files.src/rules.ts (or src/formatter.ts if the logic is more complex).export function myCustomRule(lines: string[]): string[] {
// Your formatting logic here
return lines;
}
src/formatter.ts.
if (rules.myCustomRuleEnabled) {
lines = myCustomRule(lines);
}
src/types.ts.src/rules-xml.ts to parse a new option or rule reference from your ruleset XML.
if (hasRuleRef(xml, 'MyCustom.Rule.Name')) {
parsed.myCustomRuleEnabled = true;
}
sample/ directory to test your rule.If you want to share your new rule with others:
CONTRIBUTING.md for coding standards and contribution process.Suppose you want to add a custom rule that ensures there is exactly one blank line after every function’s closing brace. Here’s how you could do it:
src/rules.ts and add the following function:export function blankLineAfterFunction(lines: string[]): string[] {
const output: string[] = [];
for (let i = 0; i < lines.length; i++) {
output.push(lines[i]);
// Detect end of function (simple heuristic: line with only '}' and previous line starts with 'function')
if (
lines[i].trim() === '}' &&
i > 0 &&
/^\s*function\b/.test(lines[i - 1])
) {
// If next line exists and is not blank, insert a blank line
if (lines[i + 1] && lines[i + 1].trim() !== '') {
output.push('');
}
}
}
return output;
}
src/formatter.ts, add:import { blankLineAfterFunction } from './rules';
// ...existing code...
if (rules.blankLineAfterFunction) {
lines = blankLineAfterFunction(lines);
}
src/types.ts, add to your rules interface/type:blankLineAfterFunction?: boolean;
src/rules-xml.ts, add logic to enable the rule from XML:if (hasRuleRef(xml, 'Custom.Functions.BlankLineAfterFunction')) {
parsed.blankLineAfterFunction = true;
}
sample/input.php and the expected output to sample/expected.php.To enable your new rule via the ruleset XML file, add a <rule ref="..."/> entry that matches the reference you used in your XML parsing logic. For the example above, add this to your ruleset XML:
<rule ref="Custom.Functions.BlankLineAfterFunction"/>
This will activate the rule if your src/rules-xml.ts file includes logic like:
if (hasRuleRef(xml, 'Custom.Functions.BlankLineAfterFunction')) {
parsed.blankLineAfterFunction = true;
}
Make sure to reload or re-run the formatter after updating your ruleset XML.
This example demonstrates how to add a brand new custom rule, register it, and (optionally) enable it via your ruleset XML file.