AbsoluteJS

absolute/no-useless-function

Disallow functions that take no parameters and just return an object literal, unless used as a callback.

Code QualitySuggestion
View on GitHub

#Rule Details

Flags a zero-parameter arrow function whose concise body is an object literal (() => ({ ... })). Wrapping a constant object in a parameterless function adds a call with no benefit — export the object directly so callers reference the value, not a thunk.

The important exception is when the arrow is passed directly as a callback argument to a function call. That covers react-spring’s useSpring(() => ({ ... })), where the function form is required. Arrows with parameters, block bodies, or non-object returns are never flagged.

#Examples

Incorrect
TS
const getConfig = () => ({ tension: 200, friction: 20 });
Correct
TS
// Export the object directly
export const springConfig = { tension: 200, friction: 20 };

const withArg = (x: number) => ({ value: x }); // has a parameter

Callback arguments are exempt

TS
// Used as a callback — allowed (covers react-spring)
const [boxSprings, boxApi] = useSpring(() => ({ opacity: 1 }));

#Configuration

Enable this rule in your ESLint configuration:

JS
{
	rules: {
		'absolute/no-useless-function': 'error'
	}
}

#When Not To Use It

If you intentionally return fresh object instances to avoid shared-reference mutation (each call producing a new object), keep the function and disable the rule for it.

#Resources