process-exit-as-throw.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. * @author Toru Nagashima
  3. * @copyright 2016 Toru Nagashima. All rights reserved.
  4. * See LICENSE file in root directory for full license.
  5. */
  6. "use strict"
  7. //------------------------------------------------------------------------------
  8. // Requirements
  9. //------------------------------------------------------------------------------
  10. const CodePathAnalyzer = safeRequire("eslint/lib/code-path-analysis/code-path-analyzer")
  11. const CodePath = safeRequire("eslint/lib/code-path-analysis/code-path")
  12. const CodePathSegment = safeRequire("eslint/lib/code-path-analysis/code-path-segment")
  13. //------------------------------------------------------------------------------
  14. // Helpers
  15. //------------------------------------------------------------------------------
  16. const originalLeaveNode = CodePathAnalyzer && CodePathAnalyzer.prototype.leaveNode
  17. /**
  18. * Imports a specific module.
  19. *
  20. * @param {string} moduleName - A module name to import.
  21. * @returns {object|null} The imported object, or null.
  22. */
  23. function safeRequire(moduleName) {
  24. try {
  25. return require(moduleName)
  26. }
  27. catch (_err) {
  28. return null
  29. }
  30. }
  31. /* istanbul ignore next */
  32. /**
  33. * Copied from https://github.com/eslint/eslint/blob/16fad5880bb70e9dddbeab8ed0f425ae51f5841f/lib/code-path-analysis/code-path-analyzer.js#L137
  34. *
  35. * @param {CodePathAnalyzer} analyzer - The instance.
  36. * @param {ASTNode} node - The current AST node.
  37. * @returns {void}
  38. */
  39. function forwardCurrentToHead(analyzer, node) {
  40. const codePath = analyzer.codePath
  41. const state = CodePath.getState(codePath)
  42. const currentSegments = state.currentSegments
  43. const headSegments = state.headSegments
  44. const end = Math.max(currentSegments.length, headSegments.length)
  45. let i = 0
  46. let currentSegment = null
  47. let headSegment = null
  48. // Fires leaving events.
  49. for (i = 0; i < end; ++i) {
  50. currentSegment = currentSegments[i]
  51. headSegment = headSegments[i]
  52. if (currentSegment !== headSegment && currentSegment) {
  53. if (currentSegment.reachable) {
  54. analyzer.emitter.emit(
  55. "onCodePathSegmentEnd",
  56. currentSegment,
  57. node)
  58. }
  59. }
  60. }
  61. // Update state.
  62. state.currentSegments = headSegments
  63. // Fires entering events.
  64. for (i = 0; i < end; ++i) {
  65. currentSegment = currentSegments[i]
  66. headSegment = headSegments[i]
  67. if (currentSegment !== headSegment && headSegment) {
  68. CodePathSegment.markUsed(headSegment)
  69. if (headSegment.reachable) {
  70. analyzer.emitter.emit(
  71. "onCodePathSegmentStart",
  72. headSegment,
  73. node)
  74. }
  75. }
  76. }
  77. }
  78. /**
  79. * Checks whether a given node is `process.exit()` or not.
  80. *
  81. * @param {ASTNode} node - A node to check.
  82. * @returns {boolean} `true` if the node is `process.exit()`.
  83. */
  84. function isProcessExit(node) {
  85. return (
  86. node.type === "CallExpression" &&
  87. node.callee.type === "MemberExpression" &&
  88. node.callee.computed === false &&
  89. node.callee.object.type === "Identifier" &&
  90. node.callee.object.name === "process" &&
  91. node.callee.property.type === "Identifier" &&
  92. node.callee.property.name === "exit"
  93. )
  94. }
  95. /**
  96. * The function to override `CodePathAnalyzer.prototype.leaveNode` in order to
  97. * address `process.exit()` as throw.
  98. *
  99. * @this CodePathAnalyzer
  100. * @param {ASTNode} node - A node to be left.
  101. * @returns {void}
  102. */
  103. function overrideLeaveNode(node) {
  104. if (isProcessExit(node)) {
  105. this.currentNode = node
  106. forwardCurrentToHead(this, node)
  107. CodePath.getState(this.codePath).makeThrow()
  108. this.original.leaveNode(node)
  109. this.currentNode = null
  110. }
  111. else {
  112. originalLeaveNode.call(this, node)
  113. }
  114. }
  115. const visitor = CodePathAnalyzer == null ? {} : {
  116. "Program": function installProcessExitAsThrow() {
  117. CodePathAnalyzer.prototype.leaveNode = overrideLeaveNode
  118. },
  119. "Program:exit": function restoreProcessExitAsThrow() {
  120. CodePathAnalyzer.prototype.leaveNode = originalLeaveNode
  121. },
  122. }
  123. //------------------------------------------------------------------------------
  124. // Rule Definition
  125. //------------------------------------------------------------------------------
  126. module.exports = {
  127. meta: {
  128. docs: {
  129. description: "make `process.exit()` expressions the same code path as `throw`",
  130. category: "Possible Errors",
  131. recommended: true,
  132. },
  133. fixable: false,
  134. schema: [],
  135. supported: CodePathAnalyzer != null,
  136. },
  137. create() {
  138. return visitor
  139. },
  140. }