node-builtins.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /**
  2. * @author Toru Nagashima
  3. * See LICENSE file in root directory for full license.
  4. */
  5. "use strict"
  6. const { READ } = require("eslint-utils")
  7. const checkUnsupportedBuiltins = require("../../util/check-unsupported-builtins")
  8. const enumeratePropertyNames = require("../../util/enumerate-property-names")
  9. /*eslint-disable camelcase */
  10. const trackMap = {
  11. globals: {
  12. queueMicrotask: {
  13. [READ]: { supported: "12.0.0", experimental: "11.0.0" },
  14. },
  15. require: {
  16. resolve: {
  17. paths: { [READ]: { supported: "8.9.0" } },
  18. },
  19. },
  20. },
  21. modules: {
  22. assert: {
  23. strict: {
  24. [READ]: { supported: "9.9.0", backported: ["8.13.0"] },
  25. doesNotReject: { [READ]: { supported: "10.0.0" } },
  26. rejects: { [READ]: { supported: "10.0.0" } },
  27. },
  28. deepStrictEqual: { [READ]: { supported: "4.0.0" } },
  29. doesNotReject: { [READ]: { supported: "10.0.0" } },
  30. notDeepStrictEqual: { [READ]: { supported: "4.0.0" } },
  31. rejects: { [READ]: { supported: "10.0.0" } },
  32. },
  33. async_hooks: {
  34. [READ]: { supported: "8.0.0" },
  35. createHook: { [READ]: { supported: "8.1.0" } },
  36. },
  37. buffer: {
  38. Buffer: {
  39. alloc: { [READ]: { supported: "4.5.0" } },
  40. allocUnsafe: { [READ]: { supported: "4.5.0" } },
  41. allocUnsafeSlow: { [READ]: { supported: "4.5.0" } },
  42. from: { [READ]: { supported: "4.5.0" } },
  43. },
  44. kMaxLength: { [READ]: { supported: "3.0.0" } },
  45. transcode: { [READ]: { supported: "7.1.0" } },
  46. constants: { [READ]: { supported: "8.2.0" } },
  47. },
  48. child_process: {
  49. ChildProcess: { [READ]: { supported: "2.2.0" } },
  50. },
  51. console: {
  52. clear: { [READ]: { supported: "8.3.0", backported: ["6.13.0"] } },
  53. count: { [READ]: { supported: "8.3.0", backported: ["6.13.0"] } },
  54. countReset: {
  55. [READ]: { supported: "8.3.0", backported: ["6.13.0"] },
  56. },
  57. debug: { [READ]: { supported: "8.0.0" } },
  58. dirxml: { [READ]: { supported: "8.0.0" } },
  59. group: { [READ]: { supported: "8.5.0" } },
  60. groupCollapsed: { [READ]: { supported: "8.5.0" } },
  61. groupEnd: { [READ]: { supported: "8.5.0" } },
  62. table: { [READ]: { supported: "10.0.0" } },
  63. markTimeline: { [READ]: { supported: "8.0.0" } },
  64. profile: { [READ]: { supported: "8.0.0" } },
  65. profileEnd: { [READ]: { supported: "8.0.0" } },
  66. timeLog: { [READ]: { supported: "10.7.0" } },
  67. timeStamp: { [READ]: { supported: "8.0.0" } },
  68. timeline: { [READ]: { supported: "8.0.0" } },
  69. timelineEnd: { [READ]: { supported: "8.0.0" } },
  70. },
  71. crypto: {
  72. Certificate: {
  73. exportChallenge: { [READ]: { supported: "9.0.0" } },
  74. exportPublicKey: { [READ]: { supported: "9.0.0" } },
  75. verifySpkac: { [READ]: { supported: "9.0.0" } },
  76. },
  77. ECDH: { [READ]: { supported: "8.8.0", backported: ["6.13.0"] } },
  78. KeyObject: { [READ]: { supported: "11.6.0" } },
  79. createPrivateKey: { [READ]: { supported: "11.6.0" } },
  80. createPublicKey: { [READ]: { supported: "11.6.0" } },
  81. createSecretKey: { [READ]: { supported: "11.6.0" } },
  82. constants: { [READ]: { supported: "6.3.0" } },
  83. fips: { [READ]: { supported: "6.0.0" } },
  84. generateKeyPair: { [READ]: { supported: "10.12.0" } },
  85. generateKeyPairSync: { [READ]: { supported: "10.12.0" } },
  86. getCurves: { [READ]: { supported: "2.3.0" } },
  87. getFips: { [READ]: { supported: "10.0.0" } },
  88. privateEncrypt: { [READ]: { supported: "1.1.0" } },
  89. publicDecrypt: { [READ]: { supported: "1.1.0" } },
  90. randomFillSync: {
  91. [READ]: { supported: "7.10.0", backported: ["6.13.0"] },
  92. },
  93. randomFill: {
  94. [READ]: { supported: "7.10.0", backported: ["6.13.0"] },
  95. },
  96. scrypt: { [READ]: { supported: "10.5.0" } },
  97. scryptSync: { [READ]: { supported: "10.5.0" } },
  98. setFips: { [READ]: { supported: "10.0.0" } },
  99. sign: { [READ]: { supported: "12.0.0" } },
  100. timingSafeEqual: { [READ]: { supported: "6.6.0" } },
  101. verify: { [READ]: { supported: "12.0.0" } },
  102. },
  103. dns: {
  104. Resolver: { [READ]: { supported: "8.3.0" } },
  105. resolvePtr: { [READ]: { supported: "6.0.0" } },
  106. promises: {
  107. [READ]: {
  108. supported: "11.14.0",
  109. backported: ["10.17.0"],
  110. experimental: "10.6.0",
  111. },
  112. },
  113. },
  114. events: {
  115. EventEmitter: {
  116. once: {
  117. [READ]: { supported: "11.13.0", backported: ["10.16.0"] },
  118. },
  119. },
  120. once: { [READ]: { supported: "11.13.0", backported: ["10.16.0"] } },
  121. },
  122. fs: {
  123. Dirent: { [READ]: { supported: "10.10.0" } },
  124. copyFile: { [READ]: { supported: "8.5.0" } },
  125. copyFileSync: { [READ]: { supported: "8.5.0" } },
  126. mkdtemp: { [READ]: { supported: "5.10.0" } },
  127. mkdtempSync: { [READ]: { supported: "5.10.0" } },
  128. realpath: {
  129. native: { [READ]: { supported: "9.2.0" } },
  130. },
  131. realpathSync: {
  132. native: { [READ]: { supported: "9.2.0" } },
  133. },
  134. promises: {
  135. [READ]: {
  136. supported: "11.14.0",
  137. backported: ["10.17.0"],
  138. experimental: "10.1.0",
  139. },
  140. },
  141. writev: { [READ]: { supported: "12.9.0" } },
  142. writevSync: { [READ]: { supported: "12.9.0" } },
  143. },
  144. http2: {
  145. [READ]: {
  146. supported: "10.10.0",
  147. backported: ["8.13.0"],
  148. experimental: "8.4.0",
  149. },
  150. },
  151. inspector: {
  152. [READ]: { supported: null, experimental: "8.0.0" },
  153. },
  154. module: {
  155. Module: {
  156. builtinModules: {
  157. [READ]: {
  158. supported: "9.3.0",
  159. backported: ["6.13.0", "8.10.0"],
  160. },
  161. },
  162. createRequireFromPath: { [READ]: { supported: "10.12.0" } },
  163. createRequire: { [READ]: { supported: "12.2.0" } },
  164. syncBuiltinESMExports: { [READ]: { supported: "12.12.0" } },
  165. },
  166. builtinModules: {
  167. [READ]: {
  168. supported: "9.3.0",
  169. backported: ["6.13.0", "8.10.0"],
  170. },
  171. },
  172. createRequireFromPath: { [READ]: { supported: "10.12.0" } },
  173. createRequire: { [READ]: { supported: "12.2.0" } },
  174. syncBuiltinESMExports: { [READ]: { supported: "12.12.0" } },
  175. },
  176. os: {
  177. constants: {
  178. [READ]: { supported: "6.3.0" },
  179. priority: { [READ]: { supported: "10.10.0" } },
  180. },
  181. getPriority: { [READ]: { supported: "10.10.0" } },
  182. homedir: { [READ]: { supported: "2.3.0" } },
  183. setPriority: { [READ]: { supported: "10.10.0" } },
  184. userInfo: { [READ]: { supported: "6.0.0" } },
  185. },
  186. path: {
  187. toNamespacedPath: { [READ]: { supported: "9.0.0" } },
  188. },
  189. perf_hooks: {
  190. [READ]: { supported: "8.5.0" },
  191. monitorEventLoopDelay: { [READ]: { supported: "11.10.0" } },
  192. },
  193. process: {
  194. allowedNodeEnvironmentFlags: { [READ]: { supported: "10.10.0" } },
  195. argv0: { [READ]: { supported: "6.4.0" } },
  196. channel: { [READ]: { supported: "7.1.0" } },
  197. cpuUsage: { [READ]: { supported: "6.1.0" } },
  198. emitWarning: { [READ]: { supported: "6.0.0" } },
  199. getegid: { [READ]: { supported: "2.0.0" } },
  200. geteuid: { [READ]: { supported: "2.0.0" } },
  201. hasUncaughtExceptionCaptureCallback: {
  202. [READ]: { supported: "9.3.0" },
  203. },
  204. hrtime: {
  205. bigint: { [READ]: { supported: "10.7.0" } },
  206. },
  207. ppid: {
  208. [READ]: {
  209. supported: "9.2.0",
  210. backported: ["6.13.0", "8.10.0"],
  211. },
  212. },
  213. release: { [READ]: { supported: "3.0.0" } },
  214. report: { [READ]: { supported: null, experimental: "11.8.0" } },
  215. resourceUsage: { [READ]: { supported: "12.6.0" } },
  216. setegid: { [READ]: { supported: "2.0.0" } },
  217. seteuid: { [READ]: { supported: "2.0.0" } },
  218. setUncaughtExceptionCaptureCallback: {
  219. [READ]: { supported: "9.3.0" },
  220. },
  221. stdout: {
  222. getColorDepth: { [READ]: { supported: "9.9.0" } },
  223. hasColor: { [READ]: { supported: "11.13.0" } },
  224. },
  225. stderr: {
  226. getColorDepth: { [READ]: { supported: "9.9.0" } },
  227. hasColor: { [READ]: { supported: "11.13.0" } },
  228. },
  229. },
  230. stream: {
  231. Readable: {
  232. from: {
  233. [READ]: { supported: "12.3.0", backported: ["10.17.0"] },
  234. },
  235. },
  236. finished: { [READ]: { supported: "10.0.0" } },
  237. pipeline: { [READ]: { supported: "10.0.0" } },
  238. },
  239. trace_events: {
  240. [READ]: { supported: "10.0.0" },
  241. },
  242. url: {
  243. URL: { [READ]: { supported: "7.0.0", backported: ["6.13.0"] } },
  244. URLSearchParams: {
  245. [READ]: { supported: "7.5.0", backported: ["6.13.0"] },
  246. },
  247. domainToASCII: { [READ]: { supported: "7.4.0" } },
  248. domainToUnicode: { [READ]: { supported: "7.4.0" } },
  249. },
  250. util: {
  251. callbackify: { [READ]: { supported: "8.2.0" } },
  252. formatWithOptions: { [READ]: { supported: "10.0.0" } },
  253. getSystemErrorName: {
  254. [READ]: { supported: "9.7.0", backported: ["8.12.0"] },
  255. },
  256. inspect: {
  257. custom: { [READ]: { supported: "6.6.0" } },
  258. defaultOptions: { [READ]: { supported: "6.4.0" } },
  259. replDefaults: { [READ]: { supported: "11.12.0" } },
  260. },
  261. isDeepStrictEqual: { [READ]: { supported: "9.0.0" } },
  262. promisify: { [READ]: { supported: "8.0.0" } },
  263. TextDecoder: {
  264. [READ]: { supported: "8.9.0", experimental: "8.3.0" },
  265. },
  266. TextEncoder: {
  267. [READ]: { supported: "8.9.0", experimental: "8.3.0" },
  268. },
  269. types: {
  270. [READ]: { supported: "10.0.0" },
  271. isBoxedPrimitive: { [READ]: { supported: "10.11.0" } },
  272. },
  273. },
  274. v8: {
  275. [READ]: { supported: "1.0.0" },
  276. DefaultDeserializer: { [READ]: { supported: "8.0.0" } },
  277. DefaultSerializer: { [READ]: { supported: "8.0.0" } },
  278. Deserializer: { [READ]: { supported: "8.0.0" } },
  279. Serializer: { [READ]: { supported: "8.0.0" } },
  280. cachedDataVersionTag: { [READ]: { supported: "8.0.0" } },
  281. deserialize: { [READ]: { supported: "8.0.0" } },
  282. getHeapCodeStatistics: { [READ]: { supported: "12.8.0" } },
  283. getHeapSnapshot: { [READ]: { supported: "11.13.0" } },
  284. getHeapSpaceStatistics: { [READ]: { supported: "6.0.0" } },
  285. serialize: { [READ]: { supported: "8.0.0" } },
  286. writeHeapSnapshot: { [READ]: { supported: "11.13.0" } },
  287. },
  288. vm: {
  289. Module: { [READ]: { supported: "9.6.0" } },
  290. compileFunction: { [READ]: { supported: "10.10.0" } },
  291. },
  292. worker_threads: {
  293. [READ]: { supported: "12.11.0", experimental: "10.5.0" },
  294. },
  295. },
  296. }
  297. Object.assign(trackMap.globals, {
  298. Buffer: trackMap.modules.buffer.Buffer,
  299. TextDecoder: {
  300. ...trackMap.modules.util.TextDecoder,
  301. [READ]: { supported: "11.0.0" },
  302. },
  303. TextEncoder: {
  304. ...trackMap.modules.util.TextEncoder,
  305. [READ]: { supported: "11.0.0" },
  306. },
  307. URL: {
  308. ...trackMap.modules.url.URL,
  309. [READ]: { supported: "10.0.0" },
  310. },
  311. URLSearchParams: {
  312. ...trackMap.modules.url.URLSearchParams,
  313. [READ]: { supported: "10.0.0" },
  314. },
  315. console: trackMap.modules.console,
  316. process: trackMap.modules.process,
  317. })
  318. /*eslint-enable camelcase */
  319. module.exports = {
  320. meta: {
  321. docs: {
  322. description:
  323. "disallow unsupported Node.js built-in APIs on the specified version",
  324. category: "Possible Errors",
  325. recommended: true,
  326. url:
  327. "https://github.com/mysticatea/eslint-plugin-node/blob/v11.1.0/docs/rules/no-unsupported-features/node-builtins.md",
  328. },
  329. type: "problem",
  330. fixable: null,
  331. schema: [
  332. {
  333. type: "object",
  334. properties: {
  335. version: {
  336. type: "string",
  337. },
  338. ignores: {
  339. type: "array",
  340. items: {
  341. enum: Array.from(
  342. new Set([
  343. ...enumeratePropertyNames(trackMap.globals),
  344. ...enumeratePropertyNames(trackMap.modules),
  345. ])
  346. ),
  347. },
  348. uniqueItems: true,
  349. },
  350. },
  351. additionalProperties: false,
  352. },
  353. ],
  354. messages: {
  355. unsupported:
  356. "The '{{name}}' is not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  357. },
  358. },
  359. create(context) {
  360. return {
  361. "Program:exit"() {
  362. checkUnsupportedBuiltins(context, trackMap)
  363. },
  364. }
  365. },
  366. }