contextCompat.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. 'use strict';
  2. exports.__esModule = true;
  3. /** @type {import('./contextCompat').getAncestors} */
  4. function getAncestors(context, node) {
  5. const sourceCode = getSourceCode(context);
  6. if (sourceCode && sourceCode.getAncestors) {
  7. return sourceCode.getAncestors(node);
  8. }
  9. return context.getAncestors();
  10. }
  11. /** @type {import('./contextCompat').getDeclaredVariables} */
  12. function getDeclaredVariables(context, node) {
  13. const sourceCode = getSourceCode(context);
  14. if (sourceCode && sourceCode.getDeclaredVariables) {
  15. return sourceCode.getDeclaredVariables(node);
  16. }
  17. return context.getDeclaredVariables(node);
  18. }
  19. /** @type {import('./contextCompat').getFilename} */
  20. function getFilename(context) {
  21. if ('filename' in context) {
  22. return context.filename;
  23. }
  24. return context.getFilename();
  25. }
  26. /** @type {import('./contextCompat').getPhysicalFilename} */
  27. function getPhysicalFilename(context) {
  28. if (context.getPhysicalFilename) {
  29. return context.getPhysicalFilename();
  30. }
  31. return getFilename(context);
  32. }
  33. /** @type {import('./contextCompat').getScope} */
  34. function getScope(context, node) {
  35. const sourceCode = getSourceCode(context);
  36. if (sourceCode && sourceCode.getScope) {
  37. return sourceCode.getScope(node);
  38. }
  39. return context.getScope();
  40. }
  41. /** @type {import('./contextCompat').getSourceCode} */
  42. function getSourceCode(context) {
  43. if ('sourceCode' in context) {
  44. return context.sourceCode;
  45. }
  46. return context.getSourceCode();
  47. }
  48. module.exports = {
  49. getAncestors,
  50. getDeclaredVariables,
  51. getFilename,
  52. getPhysicalFilename,
  53. getScope,
  54. getSourceCode,
  55. };