You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.2 KiB

7 years ago
  1. /*!
  2. * depd
  3. * Copyright(c) 2014 Douglas Christopher Wilson
  4. * MIT Licensed
  5. */
  6. /**
  7. * Module exports.
  8. */
  9. module.exports = callSiteToString
  10. /**
  11. * Format a CallSite file location to a string.
  12. */
  13. function callSiteFileLocation(callSite) {
  14. var fileName
  15. var fileLocation = ''
  16. if (callSite.isNative()) {
  17. fileLocation = 'native'
  18. } else if (callSite.isEval()) {
  19. fileName = callSite.getScriptNameOrSourceURL()
  20. if (!fileName) {
  21. fileLocation = callSite.getEvalOrigin()
  22. }
  23. } else {
  24. fileName = callSite.getFileName()
  25. }
  26. if (fileName) {
  27. fileLocation += fileName
  28. var lineNumber = callSite.getLineNumber()
  29. if (lineNumber != null) {
  30. fileLocation += ':' + lineNumber
  31. var columnNumber = callSite.getColumnNumber()
  32. if (columnNumber) {
  33. fileLocation += ':' + columnNumber
  34. }
  35. }
  36. }
  37. return fileLocation || 'unknown source'
  38. }
  39. /**
  40. * Format a CallSite to a string.
  41. */
  42. function callSiteToString(callSite) {
  43. var addSuffix = true
  44. var fileLocation = callSiteFileLocation(callSite)
  45. var functionName = callSite.getFunctionName()
  46. var isConstructor = callSite.isConstructor()
  47. var isMethodCall = !(callSite.isToplevel() || isConstructor)
  48. var line = ''
  49. if (isMethodCall) {
  50. var methodName = callSite.getMethodName()
  51. var typeName = getConstructorName(callSite)
  52. if (functionName) {
  53. if (typeName && functionName.indexOf(typeName) !== 0) {
  54. line += typeName + '.'
  55. }
  56. line += functionName
  57. if (methodName && functionName.lastIndexOf('.' + methodName) !== functionName.length - methodName.length - 1) {
  58. line += ' [as ' + methodName + ']'
  59. }
  60. } else {
  61. line += typeName + '.' + (methodName || '<anonymous>')
  62. }
  63. } else if (isConstructor) {
  64. line += 'new ' + (functionName || '<anonymous>')
  65. } else if (functionName) {
  66. line += functionName
  67. } else {
  68. addSuffix = false
  69. line += fileLocation
  70. }
  71. if (addSuffix) {
  72. line += ' (' + fileLocation + ')'
  73. }
  74. return line
  75. }
  76. /**
  77. * Get constructor name of reviver.
  78. */
  79. function getConstructorName(obj) {
  80. var receiver = obj.receiver
  81. return (receiver.constructor && receiver.constructor.name) || null
  82. }