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.

callsite-tostring.js 2.2KB

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