Skip to content

prefer-pattern-replacements

FixableSuggestion

Replace configured constructor and static method patterns with shorter forms.

This rule matches new Type(...) and Type.method(...) calls against the patterns you configure. When a pattern matches, the rule reports the original call and can replace it with the configured text.

Pattern order matters. The rule tries them in the order you list them and stops at the first safe match.

import ceaseNonsense from "@pobammer-ts/eslint-cease-nonsense-rules";
export default [
{
plugins: { "cease-nonsense": ceaseNonsense },
rules: {
"cease-nonsense/prefer-pattern-replacements": [
"error",
{
patterns: [
{
match: "new Vector2($x, $x)",
replacement: "fromUniform($x)",
},
],
},
],
},
},
];
Option Type Default Notes
patterns Pattern[] [] Ordered list of constructor or static method rewrites

Each pattern object has these fields:

Field Required Description
match Yes A constructor form like new Vector2($x, $y) or a static method form like UDim2.fromScale($x, $y)
replacement Yes Replacement text such as vec($x, $y) or Vector2.zero
when No Numeric conditions for captures, such as { x: "> 0" }
Syntax Meaning
$x Capture a value and reuse it in the replacement
_ Match any single argument without capturing it
number? Match that numeric literal, undefined, or a missing argument
repeated capture, like $x, $x Both arguments must be the same value

when conditions only work for captures that resolve to numeric constants.

pattern({
match: "new Vector2($x, $x)",
replacement: "fromUniform($x)",
});
pattern({
match: "UDim2.fromScale($x, $x)",
replacement: "scale($x)",
when: { x: "> 0" },
});
pattern({
match: "new Vector2($x, 0?)",
replacement: "fromX($x)",
});