code-path-segment.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /**
  2. * @fileoverview A class of the code path segment.
  3. * @author Toru Nagashima
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const debug = require("./debug-helpers");
  10. //------------------------------------------------------------------------------
  11. // Helpers
  12. //------------------------------------------------------------------------------
  13. /**
  14. * Checks whether or not a given segment is reachable.
  15. * @param {CodePathSegment} segment A segment to check.
  16. * @returns {boolean} `true` if the segment is reachable.
  17. */
  18. function isReachable(segment) {
  19. return segment.reachable;
  20. }
  21. //------------------------------------------------------------------------------
  22. // Public Interface
  23. //------------------------------------------------------------------------------
  24. /**
  25. * A code path segment.
  26. */
  27. class CodePathSegment {
  28. // eslint-disable-next-line jsdoc/require-description
  29. /**
  30. * @param {string} id An identifier.
  31. * @param {CodePathSegment[]} allPrevSegments An array of the previous segments.
  32. * This array includes unreachable segments.
  33. * @param {boolean} reachable A flag which shows this is reachable.
  34. */
  35. constructor(id, allPrevSegments, reachable) {
  36. /**
  37. * The identifier of this code path.
  38. * Rules use it to store additional information of each rule.
  39. * @type {string}
  40. */
  41. this.id = id;
  42. /**
  43. * An array of the next segments.
  44. * @type {CodePathSegment[]}
  45. */
  46. this.nextSegments = [];
  47. /**
  48. * An array of the previous segments.
  49. * @type {CodePathSegment[]}
  50. */
  51. this.prevSegments = allPrevSegments.filter(isReachable);
  52. /**
  53. * An array of the next segments.
  54. * This array includes unreachable segments.
  55. * @type {CodePathSegment[]}
  56. */
  57. this.allNextSegments = [];
  58. /**
  59. * An array of the previous segments.
  60. * This array includes unreachable segments.
  61. * @type {CodePathSegment[]}
  62. */
  63. this.allPrevSegments = allPrevSegments;
  64. /**
  65. * A flag which shows this is reachable.
  66. * @type {boolean}
  67. */
  68. this.reachable = reachable;
  69. // Internal data.
  70. Object.defineProperty(this, "internal", {
  71. value: {
  72. used: false,
  73. loopedPrevSegments: []
  74. }
  75. });
  76. /* istanbul ignore if */
  77. if (debug.enabled) {
  78. this.internal.nodes = [];
  79. this.internal.exitNodes = [];
  80. }
  81. }
  82. /**
  83. * Checks a given previous segment is coming from the end of a loop.
  84. * @param {CodePathSegment} segment A previous segment to check.
  85. * @returns {boolean} `true` if the segment is coming from the end of a loop.
  86. */
  87. isLoopedPrevSegment(segment) {
  88. return this.internal.loopedPrevSegments.indexOf(segment) !== -1;
  89. }
  90. /**
  91. * Creates the root segment.
  92. * @param {string} id An identifier.
  93. * @returns {CodePathSegment} The created segment.
  94. */
  95. static newRoot(id) {
  96. return new CodePathSegment(id, [], true);
  97. }
  98. /**
  99. * Creates a segment that follows given segments.
  100. * @param {string} id An identifier.
  101. * @param {CodePathSegment[]} allPrevSegments An array of the previous segments.
  102. * @returns {CodePathSegment} The created segment.
  103. */
  104. static newNext(id, allPrevSegments) {
  105. return new CodePathSegment(
  106. id,
  107. CodePathSegment.flattenUnusedSegments(allPrevSegments),
  108. allPrevSegments.some(isReachable)
  109. );
  110. }
  111. /**
  112. * Creates an unreachable segment that follows given segments.
  113. * @param {string} id An identifier.
  114. * @param {CodePathSegment[]} allPrevSegments An array of the previous segments.
  115. * @returns {CodePathSegment} The created segment.
  116. */
  117. static newUnreachable(id, allPrevSegments) {
  118. const segment = new CodePathSegment(id, CodePathSegment.flattenUnusedSegments(allPrevSegments), false);
  119. /*
  120. * In `if (a) return a; foo();` case, the unreachable segment preceded by
  121. * the return statement is not used but must not be remove.
  122. */
  123. CodePathSegment.markUsed(segment);
  124. return segment;
  125. }
  126. /**
  127. * Creates a segment that follows given segments.
  128. * This factory method does not connect with `allPrevSegments`.
  129. * But this inherits `reachable` flag.
  130. * @param {string} id An identifier.
  131. * @param {CodePathSegment[]} allPrevSegments An array of the previous segments.
  132. * @returns {CodePathSegment} The created segment.
  133. */
  134. static newDisconnected(id, allPrevSegments) {
  135. return new CodePathSegment(id, [], allPrevSegments.some(isReachable));
  136. }
  137. /**
  138. * Makes a given segment being used.
  139. *
  140. * And this function registers the segment into the previous segments as a next.
  141. * @param {CodePathSegment} segment A segment to mark.
  142. * @returns {void}
  143. */
  144. static markUsed(segment) {
  145. if (segment.internal.used) {
  146. return;
  147. }
  148. segment.internal.used = true;
  149. let i;
  150. if (segment.reachable) {
  151. for (i = 0; i < segment.allPrevSegments.length; ++i) {
  152. const prevSegment = segment.allPrevSegments[i];
  153. prevSegment.allNextSegments.push(segment);
  154. prevSegment.nextSegments.push(segment);
  155. }
  156. } else {
  157. for (i = 0; i < segment.allPrevSegments.length; ++i) {
  158. segment.allPrevSegments[i].allNextSegments.push(segment);
  159. }
  160. }
  161. }
  162. /**
  163. * Marks a previous segment as looped.
  164. * @param {CodePathSegment} segment A segment.
  165. * @param {CodePathSegment} prevSegment A previous segment to mark.
  166. * @returns {void}
  167. */
  168. static markPrevSegmentAsLooped(segment, prevSegment) {
  169. segment.internal.loopedPrevSegments.push(prevSegment);
  170. }
  171. /**
  172. * Replaces unused segments with the previous segments of each unused segment.
  173. * @param {CodePathSegment[]} segments An array of segments to replace.
  174. * @returns {CodePathSegment[]} The replaced array.
  175. */
  176. static flattenUnusedSegments(segments) {
  177. const done = Object.create(null);
  178. const retv = [];
  179. for (let i = 0; i < segments.length; ++i) {
  180. const segment = segments[i];
  181. // Ignores duplicated.
  182. if (done[segment.id]) {
  183. continue;
  184. }
  185. // Use previous segments if unused.
  186. if (!segment.internal.used) {
  187. for (let j = 0; j < segment.allPrevSegments.length; ++j) {
  188. const prevSegment = segment.allPrevSegments[j];
  189. if (!done[prevSegment.id]) {
  190. done[prevSegment.id] = true;
  191. retv.push(prevSegment);
  192. }
  193. }
  194. } else {
  195. done[segment.id] = true;
  196. retv.push(segment);
  197. }
  198. }
  199. return retv;
  200. }
  201. }
  202. module.exports = CodePathSegment;