Skip to content

Prefer UDim2 Shorthand

SuggestionAuto-fixableRoblox
small-rules/prefer-udim2-shorthand

Prefer UDim2.fromScale() or UDim2.fromOffset() over new UDim2() when all offsets or all scales are zero.

Rationale

When you write new UDim2(scaleX, 0, scaleY, 0) those zeros are dead weight. Same story for new UDim2(0, offsetX, 0, offsetY). You are ONLY using scale, or ONLY using offset, and the constructor makes you spell out all four anyway.

UDim2.fromScale(scaleX, scaleY) and UDim2.fromOffset(offsetX, offsetY) say exactly what you meant without the noise. Auto-fixes, so there is no excuse to keep the long form around.

Diagnostic Messages

preferFromOffset
Use UDim2.fromOffset() instead of new UDim2() when all scales are 0.
preferFromScale
Use UDim2.fromScale() instead of new UDim2() when all offsets are 0.

Configuration

This rule does not accept options.

{
"jsPlugins": [
"@pobammer-ts/small-rules"
],
"rules": {
"small-rules/prefer-udim2-shorthand": "error"
}
}

Examples

long-form UDim2 scale
new UDim2(1, 0, 1, 0);

After auto-fix

UDim2.fromScale(1, 1);
mixed UDim2 components
new UDim2(1, 2, 3, 4);