node-builtins.js 10.7 KB
/**
 * @author Toru Nagashima
 * See LICENSE file in root directory for full license.
 */
"use strict"

const { READ } = require("eslint-utils")
const checkUnsupportedBuiltins = require("../../util/check-unsupported-builtins")
const enumeratePropertyNames = require("../../util/enumerate-property-names")

/*eslint-disable camelcase */
const trackMap = {
    globals: {
        queueMicrotask: { [READ]: { supported: "11.0.0" } },
        require: {
            resolve: {
                paths: { [READ]: { supported: "8.9.0" } },
            },
        },
    },
    modules: {
        assert: {
            strict: {
                [READ]: { supported: "9.9.0" },
                doesNotReject: { [READ]: { supported: "10.0.0" } },
                rejects: { [READ]: { supported: "10.0.0" } },
            },
            deepStrictEqual: { [READ]: { supported: "4.0.0" } },
            doesNotReject: { [READ]: { supported: "10.0.0" } },
            notDeepStrictEqual: { [READ]: { supported: "4.0.0" } },
            rejects: { [READ]: { supported: "10.0.0" } },
        },
        async_hooks: {
            [READ]: { supported: "8.0.0" },
            createHook: { [READ]: { supported: "8.1.0" } },
        },
        buffer: {
            Buffer: {
                alloc: { [READ]: { supported: "4.5.0" } },
                allocUnsafe: { [READ]: { supported: "4.5.0" } },
                allocUnsafeSlow: { [READ]: { supported: "4.5.0" } },
                from: { [READ]: { supported: "4.5.0" } },
            },
            kMaxLength: { [READ]: { supported: "3.0.0" } },
            transcode: { [READ]: { supported: "7.1.0" } },
            constants: { [READ]: { supported: "8.2.0" } },
        },
        child_process: {
            ChildProcess: { [READ]: { supported: "2.2.0" } },
        },
        console: {
            clear: { [READ]: { supported: "8.3.0" } },
            count: { [READ]: { supported: "8.3.0" } },
            countReset: { [READ]: { supported: "8.3.0" } },
            debug: { [READ]: { supported: "8.0.0" } },
            dirxml: { [READ]: { supported: "8.0.0" } },
            group: { [READ]: { supported: "8.5.0" } },
            groupCollapsed: { [READ]: { supported: "8.5.0" } },
            groupEnd: { [READ]: { supported: "8.5.0" } },
            table: { [READ]: { supported: "10.0.0" } },
            markTimeline: { [READ]: { supported: "8.0.0" } },
            profile: { [READ]: { supported: "8.0.0" } },
            profileEnd: { [READ]: { supported: "8.0.0" } },
            timeLog: { [READ]: { supported: "10.7.0" } },
            timeStamp: { [READ]: { supported: "8.0.0" } },
            timeline: { [READ]: { supported: "8.0.0" } },
            timelineEnd: { [READ]: { supported: "8.0.0" } },
        },
        crypto: {
            Certificate: {
                exportChallenge: { [READ]: { supported: "9.0.0" } },
                exportPublicKey: { [READ]: { supported: "9.0.0" } },
                verifySpkac: { [READ]: { supported: "9.0.0" } },
            },
            constants: { [READ]: { supported: "6.3.0" } },
            fips: { [READ]: { supported: "6.0.0" } },
            generateKeyPair: { [READ]: { supported: "10.12.0" } },
            generateKeyPairSync: { [READ]: { supported: "10.12.0" } },
            getCurves: { [READ]: { supported: "2.3.0" } },
            getFips: { [READ]: { supported: "10.0.0" } },
            privateEncrypt: { [READ]: { supported: "1.1.0" } },
            publicDecrypt: { [READ]: { supported: "1.1.0" } },
            randomFillSync: { [READ]: { supported: "7.10.0" } },
            randomFill: { [READ]: { supported: "7.10.0" } },
            scrypt: { [READ]: { supported: "10.5.0" } },
            scryptSync: { [READ]: { supported: "10.5.0" } },
            setFips: { [READ]: { supported: "10.0.0" } },
            timingSafeEqual: { [READ]: { supported: "6.6.0" } },
        },
        dns: {
            Resolver: { [READ]: { supported: "8.3.0" } },
            resolvePtr: { [READ]: { supported: "6.0.0" } },
            promises: {
                [READ]: { supported: "10.6.0" },
            },
        },
        fs: {
            Dirent: { [READ]: { supported: "10.10.0" } },
            copyFile: { [READ]: { supported: "8.5.0" } },
            copyFileSync: { [READ]: { supported: "8.5.0" } },
            mkdtemp: { [READ]: { supported: "5.10.0" } },
            mkdtempSync: { [READ]: { supported: "5.10.0" } },
            realpath: {
                native: { [READ]: { supported: "9.2.0" } },
            },
            realpathSync: {
                native: { [READ]: { supported: "9.2.0" } },
            },
            promises: {
                [READ]: { supported: "10.1.0" },
            },
        },
        http2: {
            [READ]: { supported: "8.4.0" },
        },
        inspector: {
            [READ]: { supported: "8.0.0" },
        },
        module: {
            builtinModules: { [READ]: { supported: "9.3.0" } },
        },
        os: {
            constants: {
                [READ]: { supported: "6.3.0" },
                priority: { [READ]: { supported: "10.10.0" } },
            },
            getPriority: { [READ]: { supported: "10.10.0" } },
            homedir: { [READ]: { supported: "2.3.0" } },
            setPriority: { [READ]: { supported: "10.10.0" } },
            userInfo: { [READ]: { supported: "6.0.0" } },
        },
        path: {
            toNamespacedPath: { [READ]: { supported: "9.0.0" } },
        },
        perf_hooks: {
            [READ]: { supported: "8.5.0" },
        },
        process: {
            allowedNodeEnvironmentFlags: { [READ]: { supported: "10.10.0" } },
            argv0: { [READ]: { supported: "6.4.0" } },
            channel: { [READ]: { supported: "7.1.0" } },
            cpuUsage: { [READ]: { supported: "6.1.0" } },
            emitWarning: { [READ]: { supported: "6.0.0" } },
            getegid: { [READ]: { supported: "2.0.0" } },
            geteuid: { [READ]: { supported: "2.0.0" } },
            hasUncaughtExceptionCaptureCallback: {
                [READ]: { supported: "9.3.0" },
            },
            hrtime: {
                bigint: { [READ]: { supported: "10.7.0" } },
            },
            ppid: { [READ]: { supported: "9.2.0" } },
            release: { [READ]: { supported: "3.0.0" } },
            setegid: { [READ]: { supported: "2.0.0" } },
            seteuid: { [READ]: { supported: "2.0.0" } },
            setUncaughtExceptionCaptureCallback: {
                [READ]: { supported: "9.3.0" },
            },
        },
        stream: {
            finished: { [READ]: { supported: "10.0.0" } },
            pipeline: { [READ]: { supported: "10.0.0" } },
        },
        trace_events: {
            [READ]: { supported: "10.0.0" },
        },
        url: {
            URL: { [READ]: { supported: "7.0.0" } },
            URLSearchParams: { [READ]: { supported: "7.5.0" } },
            domainToASCII: { [READ]: { supported: "7.4.0" } },
            domainToUnicode: { [READ]: { supported: "7.4.0" } },
        },
        util: {
            callbackify: { [READ]: { supported: "8.2.0" } },
            formatWithOptions: { [READ]: { supported: "10.0.0" } },
            getSystemErrorName: { [READ]: { supported: "9.7.0" } },
            inspect: {
                custom: { [READ]: { supported: "6.6.0" } },
                defaultOptions: { [READ]: { supported: "6.4.0" } },
            },
            isDeepStrictEqual: { [READ]: { supported: "9.0.0" } },
            promisify: { [READ]: { supported: "8.0.0" } },
            TextDecoder: { [READ]: { supported: "8.3.0" } },
            TextEncoder: { [READ]: { supported: "8.3.0" } },
            types: {
                [READ]: { supported: "10.0.0" },
                isBoxedPrimitive: { [READ]: { supported: "10.11.0" } },
            },
        },
        v8: {
            [READ]: { supported: "1.0.0" },
            cachedDataVersionTag: { [READ]: { supported: "8.0.0" } },
            getHeapSpaceStatistics: { [READ]: { supported: "6.0.0" } },
            serialize: { [READ]: { supported: "8.0.0" } },
            deserialize: { [READ]: { supported: "8.0.0" } },
            Serializer: { [READ]: { supported: "8.0.0" } },
            Deserializer: { [READ]: { supported: "8.0.0" } },
            DefaultSerializer: { [READ]: { supported: "8.0.0" } },
            DefaultDeserializer: { [READ]: { supported: "8.0.0" } },
        },
        vm: {
            Module: { [READ]: { supported: "9.6.0" } },
            compileFunction: { [READ]: { supported: "10.10.0" } },
        },
        worker_threads: {
            [READ]: { supported: "10.5.0" },
        },
    },
}
Object.assign(trackMap.globals, {
    Buffer: trackMap.modules.buffer.Buffer,
    TextDecoder: Object.assign({}, trackMap.modules.util.TextDecoder, {
        [READ]: { supported: "11.0.0" },
    }),
    TextEncoder: Object.assign({}, trackMap.modules.util.TextEncoder, {
        [READ]: { supported: "11.0.0" },
    }),
    URL: Object.assign({}, trackMap.modules.url.URL, {
        [READ]: { supported: "10.0.0" },
    }),
    URLSearchParams: Object.assign({}, trackMap.modules.url.URLSearchParams, {
        [READ]: { supported: "10.0.0" },
    }),
    console: trackMap.modules.console,
    process: trackMap.modules.process,
})
/*eslint-enable camelcase */

module.exports = {
    meta: {
        docs: {
            description:
                "disallow unsupported Node.js built-in APIs on the specified version",
            category: "Possible Errors",
            recommended: true,
            url:
                "https://github.com/mysticatea/eslint-plugin-node/blob/v8.0.1/docs/rules/no-unsupported-features/node-builtins.md",
        },
        type: "problem",
        fixable: null,
        schema: [
            {
                type: "object",
                properties: {
                    version: {
                        type: "string",
                    },
                    ignores: {
                        type: "array",
                        items: {
                            enum: Array.from(
                                new Set([
                                    ...enumeratePropertyNames(trackMap.globals),
                                    ...enumeratePropertyNames(trackMap.modules),
                                ])
                            ),
                        },
                        uniqueItems: true,
                    },
                },
                additionalProperties: false,
            },
        ],
        messages: {
            unsupported:
                "The '{{name}}' is not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
        },
    },
    create(context) {
        return {
            "Program:exit"() {
                checkUnsupportedBuiltins(context, trackMap)
            },
        }
    },
}