code-path-segment.js 7.1 KB

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