node-builtins.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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: { [READ]: { supported: "11.0.0" } },
  13. require: {
  14. resolve: {
  15. paths: { [READ]: { supported: "8.9.0" } },
  16. },
  17. },
  18. },
  19. modules: {
  20. assert: {
  21. strict: {
  22. [READ]: { supported: "9.9.0" },
  23. doesNotReject: { [READ]: { supported: "10.0.0" } },
  24. rejects: { [READ]: { supported: "10.0.0" } },
  25. },
  26. deepStrictEqual: { [READ]: { supported: "4.0.0" } },
  27. doesNotReject: { [READ]: { supported: "10.0.0" } },
  28. notDeepStrictEqual: { [READ]: { supported: "4.0.0" } },
  29. rejects: { [READ]: { supported: "10.0.0" } },
  30. },
  31. async_hooks: {
  32. [READ]: { supported: "8.0.0" },
  33. createHook: { [READ]: { supported: "8.1.0" } },
  34. },
  35. buffer: {
  36. Buffer: {
  37. alloc: { [READ]: { supported: "4.5.0" } },
  38. allocUnsafe: { [READ]: { supported: "4.5.0" } },
  39. allocUnsafeSlow: { [READ]: { supported: "4.5.0" } },
  40. from: { [READ]: { supported: "4.5.0" } },
  41. },
  42. kMaxLength: { [READ]: { supported: "3.0.0" } },
  43. transcode: { [READ]: { supported: "7.1.0" } },
  44. constants: { [READ]: { supported: "8.2.0" } },
  45. },
  46. child_process: {
  47. ChildProcess: { [READ]: { supported: "2.2.0" } },
  48. },
  49. console: {
  50. clear: { [READ]: { supported: "8.3.0" } },
  51. count: { [READ]: { supported: "8.3.0" } },
  52. countReset: { [READ]: { supported: "8.3.0" } },
  53. debug: { [READ]: { supported: "8.0.0" } },
  54. dirxml: { [READ]: { supported: "8.0.0" } },
  55. group: { [READ]: { supported: "8.5.0" } },
  56. groupCollapsed: { [READ]: { supported: "8.5.0" } },
  57. groupEnd: { [READ]: { supported: "8.5.0" } },
  58. table: { [READ]: { supported: "10.0.0" } },
  59. markTimeline: { [READ]: { supported: "8.0.0" } },
  60. profile: { [READ]: { supported: "8.0.0" } },
  61. profileEnd: { [READ]: { supported: "8.0.0" } },
  62. timeLog: { [READ]: { supported: "10.7.0" } },
  63. timeStamp: { [READ]: { supported: "8.0.0" } },
  64. timeline: { [READ]: { supported: "8.0.0" } },
  65. timelineEnd: { [READ]: { supported: "8.0.0" } },
  66. },
  67. crypto: {
  68. Certificate: {
  69. exportChallenge: { [READ]: { supported: "9.0.0" } },
  70. exportPublicKey: { [READ]: { supported: "9.0.0" } },
  71. verifySpkac: { [READ]: { supported: "9.0.0" } },
  72. },
  73. constants: { [READ]: { supported: "6.3.0" } },
  74. fips: { [READ]: { supported: "6.0.0" } },
  75. generateKeyPair: { [READ]: { supported: "10.12.0" } },
  76. generateKeyPairSync: { [READ]: { supported: "10.12.0" } },
  77. getCurves: { [READ]: { supported: "2.3.0" } },
  78. getFips: { [READ]: { supported: "10.0.0" } },
  79. privateEncrypt: { [READ]: { supported: "1.1.0" } },
  80. publicDecrypt: { [READ]: { supported: "1.1.0" } },
  81. randomFillSync: { [READ]: { supported: "7.10.0" } },
  82. randomFill: { [READ]: { supported: "7.10.0" } },
  83. scrypt: { [READ]: { supported: "10.5.0" } },
  84. scryptSync: { [READ]: { supported: "10.5.0" } },
  85. setFips: { [READ]: { supported: "10.0.0" } },
  86. timingSafeEqual: { [READ]: { supported: "6.6.0" } },
  87. },
  88. dns: {
  89. Resolver: { [READ]: { supported: "8.3.0" } },
  90. resolvePtr: { [READ]: { supported: "6.0.0" } },
  91. promises: {
  92. [READ]: { supported: "10.6.0" },
  93. },
  94. },
  95. fs: {
  96. Dirent: { [READ]: { supported: "10.10.0" } },
  97. copyFile: { [READ]: { supported: "8.5.0" } },
  98. copyFileSync: { [READ]: { supported: "8.5.0" } },
  99. mkdtemp: { [READ]: { supported: "5.10.0" } },
  100. mkdtempSync: { [READ]: { supported: "5.10.0" } },
  101. realpath: {
  102. native: { [READ]: { supported: "9.2.0" } },
  103. },
  104. realpathSync: {
  105. native: { [READ]: { supported: "9.2.0" } },
  106. },
  107. promises: {
  108. [READ]: { supported: "10.1.0" },
  109. },
  110. },
  111. http2: {
  112. [READ]: { supported: "8.4.0" },
  113. },
  114. inspector: {
  115. [READ]: { supported: "8.0.0" },
  116. },
  117. module: {
  118. builtinModules: { [READ]: { supported: "9.3.0" } },
  119. },
  120. os: {
  121. constants: {
  122. [READ]: { supported: "6.3.0" },
  123. priority: { [READ]: { supported: "10.10.0" } },
  124. },
  125. getPriority: { [READ]: { supported: "10.10.0" } },
  126. homedir: { [READ]: { supported: "2.3.0" } },
  127. setPriority: { [READ]: { supported: "10.10.0" } },
  128. userInfo: { [READ]: { supported: "6.0.0" } },
  129. },
  130. path: {
  131. toNamespacedPath: { [READ]: { supported: "9.0.0" } },
  132. },
  133. perf_hooks: {
  134. [READ]: { supported: "8.5.0" },
  135. },
  136. process: {
  137. allowedNodeEnvironmentFlags: { [READ]: { supported: "10.10.0" } },
  138. argv0: { [READ]: { supported: "6.4.0" } },
  139. channel: { [READ]: { supported: "7.1.0" } },
  140. cpuUsage: { [READ]: { supported: "6.1.0" } },
  141. emitWarning: { [READ]: { supported: "6.0.0" } },
  142. getegid: { [READ]: { supported: "2.0.0" } },
  143. geteuid: { [READ]: { supported: "2.0.0" } },
  144. hasUncaughtExceptionCaptureCallback: {
  145. [READ]: { supported: "9.3.0" },
  146. },
  147. hrtime: {
  148. bigint: { [READ]: { supported: "10.7.0" } },
  149. },
  150. ppid: { [READ]: { supported: "9.2.0" } },
  151. release: { [READ]: { supported: "3.0.0" } },
  152. setegid: { [READ]: { supported: "2.0.0" } },
  153. seteuid: { [READ]: { supported: "2.0.0" } },
  154. setUncaughtExceptionCaptureCallback: {
  155. [READ]: { supported: "9.3.0" },
  156. },
  157. },
  158. stream: {
  159. finished: { [READ]: { supported: "10.0.0" } },
  160. pipeline: { [READ]: { supported: "10.0.0" } },
  161. },
  162. trace_events: {
  163. [READ]: { supported: "10.0.0" },
  164. },
  165. url: {
  166. URL: { [READ]: { supported: "7.0.0" } },
  167. URLSearchParams: { [READ]: { supported: "7.5.0" } },
  168. domainToASCII: { [READ]: { supported: "7.4.0" } },
  169. domainToUnicode: { [READ]: { supported: "7.4.0" } },
  170. },
  171. util: {
  172. callbackify: { [READ]: { supported: "8.2.0" } },
  173. formatWithOptions: { [READ]: { supported: "10.0.0" } },
  174. getSystemErrorName: { [READ]: { supported: "9.7.0" } },
  175. inspect: {
  176. custom: { [READ]: { supported: "6.6.0" } },
  177. defaultOptions: { [READ]: { supported: "6.4.0" } },
  178. },
  179. isDeepStrictEqual: { [READ]: { supported: "9.0.0" } },
  180. promisify: { [READ]: { supported: "8.0.0" } },
  181. TextDecoder: { [READ]: { supported: "8.3.0" } },
  182. TextEncoder: { [READ]: { supported: "8.3.0" } },
  183. types: {
  184. [READ]: { supported: "10.0.0" },
  185. isBoxedPrimitive: { [READ]: { supported: "10.11.0" } },
  186. },
  187. },
  188. v8: {
  189. [READ]: { supported: "1.0.0" },
  190. cachedDataVersionTag: { [READ]: { supported: "8.0.0" } },
  191. getHeapSpaceStatistics: { [READ]: { supported: "6.0.0" } },
  192. serialize: { [READ]: { supported: "8.0.0" } },
  193. deserialize: { [READ]: { supported: "8.0.0" } },
  194. Serializer: { [READ]: { supported: "8.0.0" } },
  195. Deserializer: { [READ]: { supported: "8.0.0" } },
  196. DefaultSerializer: { [READ]: { supported: "8.0.0" } },
  197. DefaultDeserializer: { [READ]: { supported: "8.0.0" } },
  198. },
  199. vm: {
  200. Module: { [READ]: { supported: "9.6.0" } },
  201. compileFunction: { [READ]: { supported: "10.10.0" } },
  202. },
  203. worker_threads: {
  204. [READ]: { supported: "10.5.0" },
  205. },
  206. },
  207. }
  208. Object.assign(trackMap.globals, {
  209. Buffer: trackMap.modules.buffer.Buffer,
  210. TextDecoder: Object.assign({}, trackMap.modules.util.TextDecoder, {
  211. [READ]: { supported: "11.0.0" },
  212. }),
  213. TextEncoder: Object.assign({}, trackMap.modules.util.TextEncoder, {
  214. [READ]: { supported: "11.0.0" },
  215. }),
  216. URL: Object.assign({}, trackMap.modules.url.URL, {
  217. [READ]: { supported: "10.0.0" },
  218. }),
  219. URLSearchParams: Object.assign({}, trackMap.modules.url.URLSearchParams, {
  220. [READ]: { supported: "10.0.0" },
  221. }),
  222. console: trackMap.modules.console,
  223. process: trackMap.modules.process,
  224. })
  225. /*eslint-enable camelcase */
  226. module.exports = {
  227. meta: {
  228. docs: {
  229. description:
  230. "disallow unsupported Node.js built-in APIs on the specified version",
  231. category: "Possible Errors",
  232. recommended: true,
  233. url:
  234. "https://github.com/mysticatea/eslint-plugin-node/blob/v8.0.1/docs/rules/no-unsupported-features/node-builtins.md",
  235. },
  236. type: "problem",
  237. fixable: null,
  238. schema: [
  239. {
  240. type: "object",
  241. properties: {
  242. version: {
  243. type: "string",
  244. },
  245. ignores: {
  246. type: "array",
  247. items: {
  248. enum: Array.from(
  249. new Set([
  250. ...enumeratePropertyNames(trackMap.globals),
  251. ...enumeratePropertyNames(trackMap.modules),
  252. ])
  253. ),
  254. },
  255. uniqueItems: true,
  256. },
  257. },
  258. additionalProperties: false,
  259. },
  260. ],
  261. messages: {
  262. unsupported:
  263. "The '{{name}}' is not supported until Node.js {{supported}}. The configured version range is '{{version}}'.",
  264. },
  265. },
  266. create(context) {
  267. return {
  268. "Program:exit"() {
  269. checkUnsupportedBuiltins(context, trackMap)
  270. },
  271. }
  272. },
  273. }