Skip to content

prefer-pattern-replacements

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 "eslint-plugin-cease-nonsense";
export default [
{
plugins: { "cease-nonsense": ceaseNonsense },
rules: {
"cease-nonsense/prefer-pattern-replacements": [
"error",
{
patterns: [
{
match: "new Vector2($x, $x)",
replacement: "fromUniform($x)",
},
],
},
],
},
},
];
OptionTypeDefaultNotes
patternsPattern[][]Ordered list of constructor or static method rewrites

Each pattern object has these fields:

FieldRequiredDescription
matchYesA constructor form like new Vector2($x, $y) or a static method form like UDim2.fromScale($x, $y)
replacementYesReplacement text such as vec($x, $y) or Vector2.zero
whenNoNumeric conditions for captures, such as { x: "> 0" }
SyntaxMeaning
$xCapture 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, $xBoth 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)",
});