check-unsupported-builtins.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * @author Toru Nagashima
  3. * See LICENSE file in root directory for full license.
  4. */
  5. "use strict"
  6. const { Range } = require("semver") //eslint-disable-line no-unused-vars
  7. const { ReferenceTracker } = require("eslint-utils")
  8. const getConfiguredNodeVersion = require("./get-configured-node-version")
  9. const getSemverRange = require("./get-semver-range")
  10. /**
  11. * Parses the options.
  12. * @param {RuleContext} context The rule context.
  13. * @returns {{version:Range,ignores:Set<string>}} Parsed value.
  14. */
  15. function parseOptions(context) {
  16. const raw = context.options[0] || {}
  17. const filePath = context.getFilename()
  18. const version = getConfiguredNodeVersion(raw.version, filePath)
  19. const ignores = new Set(raw.ignores || [])
  20. return Object.freeze({ version, ignores })
  21. }
  22. /**
  23. * Verify the code to report unsupported APIs.
  24. * @param {RuleContext} context The rule context.
  25. * @param {{modules:object,globals:object}} trackMap The map for APIs to report.
  26. * @returns {void}
  27. */
  28. module.exports = function checkUnsupportedBuiltins(context, trackMap) {
  29. const options = parseOptions(context)
  30. const tracker = new ReferenceTracker(context.getScope(), {
  31. mode: "legacy",
  32. })
  33. const references = [
  34. ...tracker.iterateCjsReferences(trackMap.modules || {}),
  35. ...tracker.iterateEsmReferences(trackMap.modules || {}),
  36. ...tracker.iterateGlobalReferences(trackMap.globals || {}),
  37. ]
  38. for (const { node, path, info } of references) {
  39. const name = path.join(".")
  40. const supported = options.version.intersects(
  41. getSemverRange(`<${info.supported}`)
  42. )
  43. if (supported && !options.ignores.has(name)) {
  44. context.report({
  45. node,
  46. messageId: "unsupported",
  47. data: {
  48. name,
  49. supported: info.supported,
  50. version: options.version.raw,
  51. },
  52. })
  53. }
  54. }
  55. }