Go to content
Blog / Tools /

Function deprecation in JavaScript

When a codebase grows, and gets old is inevitable that functions, or whole areas become obsolete, and their usage is discouraged in favour of better alternatives.

If the codebase is big enough, it might be not possible, or not easy to replace the usages of legacy components with their alternative in a single step … in such cases it’s important to have a way to signal a particular component is deprecated, and shouldn’t be used anymore, so that at least it doesn’t spread further.

To deal with such situations, I’ve found helpful the deprecate ESLint plugin.

It permits to emit a warning, or error - I prefer the first in such case - when it finds a reference for a certain component marked as deprecated.
Below an example of configuration:

{
    "plugins": [
        "deprecate"
    ],
    "rules": {
        "deprecate/function": [
            "warn",
            {
                "name": "doSomething",
                "use": "Use doSomethingBetter instead"
            }
        ],
        "deprecate/import": [
            "warn",
            {
                "name": "leftpad",
                "use": "Use built-in padStart instead"
            }
        ]
    }
}

There is also the option to deprecate object members - through the deprecate/member-expression rule - but I didn’t find any use case for it so far.