🚀 DAY 5 OF LAUNCH WEEK: Introducing Socket Firewall Enterprise.Learn more →
Socket
Book a DemoInstallSign in
Socket

babel-plugin-log-deprecated

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-log-deprecated

Adds a console.warn statement to the functions annotated with @deprecated tag.

latest
Source
npmnpm
Version
1.1.0
Version published
Maintainers
1
Created
Source

babel-plugin-log-deprecated

Travis build status NPM version Canonical Code Style Twitter Follow

Adds a console.warn statement to the functions annotated with the JSDoc @deprecated tag.

The console.warn is added at the beginning of the function body.

Example transpilation

Input:

/**
 * @deprecated Deprecated in favour of quux.
 */
function foo () {
  bar();
};

Output:

/**
 * @deprecated Deprecated in favour of quux.
 */
function foo () {
  console.warn("Deprecated: Function \"foo\" is deprecated in /fixtures/preset-options/adds-console-warn-to-function-declaration/actual.js on line 4", {
    functionName: "foo",
    message: "Deprecated in favour of quux.",
    packageName: "bar",
    packageVersion: "1.0.0",
    scriptColumn: 0,
    scriptLine: 4,
    scriptPath: "fixtures/preset-options/adds-console-warn-to-function-declaration/actual.js"
  });

  bar();
};

Motivation

Working on a code base that has a large public API surface requires a method to instruct the consumer about deprecation of methods. The most common approach at the moment is to add an ad-hoc console.warn statement used to warn the consumer. There are multiple problems with this approach:

  • It duplicates the role of the JSDoc @deprecated tag.
  • It restricts the developer to the amount of the information that can be included in the deprecation message.
  • It leads to a proliferation of arbitrary format console.log messages that can be hard to filter out.

To address these issues, babel-plugin-log-deprecated:

  • Uses JSDoc @deprecated tag to construct the deprecation log message property.
  • Utilises build time information (package.json configuration, script path) to enrich the deprecation message.
  • Enforces a consistent error message format.

Using in production

This transpiler can be used to produce code for client-side use and Node.js. Whether to leave the warnings in the production build depends on the use case.

Regarding the client-side use, I have been asked:

inu-no-policemen:

Wouldn't it make more sense to get these messages at compile time?

Messages which only appear if a particular path is taken through the application are easy to miss.

– https://www.reddit.com/r/javascript/comments/5f71la/i_have_written_a_babel_transpiler_that_adds/dai062z/

I am using this plugin to add warning messages that are visible at a runtime on purpose. Think about large organizations that have multiple teams working on a frontend of the same website. There is a lot of code in the window namespace:

WebsiteObject.keys(window).length
https://www.google.com/blank.html181 (base figure)
https://www.ft.com/229 (+48)
https://www.theguardian.com/uk269 (+88)
http://www.dailymail.co.uk/home/index.html599 (+418)
http://time.com/669 (+488)

Furthermore, this is just the count of the properties in the window namespace. A number of these properties will expose complex APIs that tie together the entire website (think user targeting, advertising, utilities, etc.).

You cannot simply remove properties because you risk of breaking things. Therefore, a sensible solution is to communicate the upcoming changes to the other teams and add deprecation warnings well ahead of making the change.

In a similar fashion, a public library can use the deprecation warning to inform the consumers of the upcoming changes.

Implementation

Deprecation message fields

NameValue
functionNameName of the function that has the @deprecated tag associated with. In case of an anonymous function - name of the left hand side identifier.
messageMessage provided in the description of the JSDoc @deprecated tag
packageNameValue of the name property in the package.json. See package.json resolution.
packageVersionValue of the version property in the package.json. See package.json resolution.
scriptColumnNumber of the column where the function is declared.
scriptLineNumber of the line where the function is declared.
scriptPathPath to the script file relative to package.json. See package.json resolution.

package.json resolution

package.json is resolved relative to the file being transpiled.

Keywords

babel-plugin

FAQs

Package last updated on 28 Nov 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts