Skip to content

Latest commit

 

History

History
371 lines (319 loc) · 15.4 KB

no-deprecated-api.md

File metadata and controls

371 lines (319 loc) · 15.4 KB

Disallow deprecated APIs (n/no-deprecated-api)

💼 This rule is enabled in the following configs: ☑️ flat/recommended, 🟢 flat/recommended-module, ✅ flat/recommended-script, ☑️ recommended, 🟢 recommended-module, ✅ recommended-script.

Node has many deprecated API. The community is going to remove those API from Node in future, so we should not use those.

📖 Rule Details

Examples of 👎 incorrect code for this rule:

/*eslint n/no-deprecated-api: "error" */

var fs = require("fs");
fs.exists("./foo.js", function() {}); /*ERROR: 'fs.exists' was deprecated since v4. Use 'fs.stat()' or 'fs.access()' instead.*/

// Also, it can report the following patterns.
var exists = require("fs").exists;    /*ERROR: 'fs.exists' was deprecated since v4. Use 'fs.stat()' or 'fs.access()' instead.*/
const {exists} = require("fs");       /*ERROR: 'fs.exists' was deprecated since v4. Use 'fs.stat()' or 'fs.access()' instead.*/


// And other deprecated API below.

This rule reports the following deprecated API.

⚠️ Note that userland modules don't hide core modules. For example, require("punycode") still imports the deprecated core module even if you executed npm install punycode. Use require("punycode/") to import userland modules rather than core modules.

Configured Node.js version range

Configured Node.js version range

Options

This rule has 4 options.

{
    "rules": {
        "n/no-deprecated-api": ["error", {
            "version": ">=16.0.0",
            "ignoreModuleItems": [],
            "ignoreGlobalItems": []
            "ignoreIndirectDependencies": true
        }]
    }
}

version

As mentioned above, this rule reads the [engines] field of package.json. But, you can overwrite the version by version option.

The version option accepts the valid version range of node-semver.

ignoreModuleItems

This is the array of module names and module's member names. Default is an empty array.

This rule ignores APIs that ignoreModuleItems includes. This option can include the following values:

  • _linklist
  • _stream_wrap
  • async_hooks.currentId
  • async_hooks.triggerId
  • buffer.Buffer()
  • new buffer.Buffer()
  • buffer.SlowBuffer
  • constants
  • crypto._toBuf
  • crypto.Credentials
  • crypto.DEFAULT_ENCODING
  • crypto.createCipher
  • crypto.createCredentials
  • crypto.createDecipher
  • crypto.fips
  • crypto.prng
  • crypto.pseudoRandomBytes
  • crypto.rng
  • domain
  • events.EventEmitter.listenerCount
  • events.listenerCount
  • freelist
  • fs.SyncWriteStream
  • fs.exists
  • fs.lchmod
  • fs.lchmodSync
  • http.createClient
  • module.Module.createRequireFromPath
  • module.createRequireFromPath
  • module.Module.requireRepl
  • module.requireRepl
  • module.Module._debug
  • module._debug
  • net._setSimultaneousAccepts
  • os.tmpDir
  • path._makeLong
  • process.EventEmitter
  • process.assert
  • process.binding
  • process.env.NODE_REPL_HISTORY_FILE
  • process.report.triggerReport
  • punycode
  • readline.codePointAt
  • readline.getStringWidth
  • readline.isFullWidthCodePoint
  • readline.stripVTControlCharacters
  • sys
  • timers.enroll
  • timers.unenroll
  • tls.CleartextStream
  • tls.CryptoStream
  • tls.SecurePair
  • tls.convertNPNProtocols
  • tls.createSecurePair
  • tls.parseCertString
  • tty.setRawMode
  • url.parse
  • url.resolve
  • util.debug
  • util.error
  • util.isArray
  • util.isBoolean
  • util.isBuffer
  • util.isDate
  • util.isError
  • util.isFunction
  • util.isNull
  • util.isNullOrUndefined
  • util.isNumber
  • util.isObject
  • util.isPrimitive
  • util.isRegExp
  • util.isString
  • util.isSymbol
  • util.isUndefined
  • util.log
  • util.print
  • util.pump
  • util.puts
  • util._extend
  • vm.runInDebugContext

Examples of 👍 correct code for the {"ignoreModuleItems": ["new buffer.Buffer()"]}:

/*eslint n/no-deprecated-api: [error, {ignoreModuleItems: ["new buffer.Buffer()"]}] */

const buffer = require("buffer")
const data = new buffer.Buffer(10) // OK since it's in ignoreModuleItems.

ignoreGlobalItems

This is the array of global variable names and global variable's member names. Default is an empty array.

This rule ignores APIs that ignoreGlobalItems includes. This option can include the following values:

  • Buffer()
  • new Buffer()
  • COUNTER_NET_SERVER_CONNECTION
  • COUNTER_NET_SERVER_CONNECTION_CLOSE
  • COUNTER_HTTP_SERVER_REQUEST
  • COUNTER_HTTP_SERVER_RESPONSE
  • COUNTER_HTTP_CLIENT_REQUEST
  • COUNTER_HTTP_CLIENT_RESPONSE
  • Intl.v8BreakIterator
  • require.extensions
  • process.EventEmitter
  • process.assert
  • process.binding
  • process.env.NODE_REPL_HISTORY_FILE

Examples of 👍 correct code for the {"ignoreGlobalItems": ["new Buffer()"]}:

/*eslint n/no-deprecated-api: [error, {ignoreGlobalItems: ["new Buffer()"]}] */

const data = new Buffer(10) // OK since it's in ignoreGlobalItems.

⚠️ Known Limitations

This rule cannot report the following cases:

non-static properties

types of arguments

  • fs
    • fs.truncate() and fs.truncateSync() usage with a file descriptor has been deprecated.
  • url
    • url.format() with legacy urlObject has been deprecated.

dynamic things

require(foo).aDeprecatedProperty;
require("http")[A_DEPRECATED_PROPERTY]();

assignments to properties

var obj = {
    Buffer: require("buffer").Buffer
};
new obj.Buffer(); /* missing. */
var obj = {};
obj.Buffer = require("buffer").Buffer
new obj.Buffer(); /* missing. */

giving arguments

(function(Buffer) {
    new Buffer(); /* missing. */
})(require("buffer").Buffer);

reassignments

var Buffer = require("buffer").Buffer;
Buffer = require("another-buffer");
new Buffer(); /*ERROR: 'buffer.Buffer' constructor was deprecated.*/

🔎 Implementation