Ohm-Management - Projektarbeit B-ME
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.

code-path-segment.js 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. *
  16. * @param {CodePathSegment} segment - A segment to check.
  17. * @returns {boolean} `true` if the segment is reachable.
  18. */
  19. function isReachable(segment) {
  20. return segment.reachable;
  21. }
  22. //------------------------------------------------------------------------------
  23. // Public Interface
  24. //------------------------------------------------------------------------------
  25. /**
  26. * A code path segment.
  27. */
  28. class CodePathSegment {
  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. *
  85. * @param {CodePathSegment} segment - A previous segment to check.
  86. * @returns {boolean} `true` if the segment is coming from the end of a loop.
  87. */
  88. isLoopedPrevSegment(segment) {
  89. return this.internal.loopedPrevSegments.indexOf(segment) !== -1;
  90. }
  91. /**
  92. * Creates the root segment.
  93. *
  94. * @param {string} id - An identifier.
  95. * @returns {CodePathSegment} The created segment.
  96. */
  97. static newRoot(id) {
  98. return new CodePathSegment(id, [], true);
  99. }
  100. /**
  101. * Creates a segment that follows given segments.
  102. *
  103. * @param {string} id - An identifier.
  104. * @param {CodePathSegment[]} allPrevSegments - An array of the previous segments.
  105. * @returns {CodePathSegment} The created segment.
  106. */
  107. static newNext(id, allPrevSegments) {
  108. return new CodePathSegment(
  109. id,
  110. CodePathSegment.flattenUnusedSegments(allPrevSegments),
  111. allPrevSegments.some(isReachable)
  112. );
  113. }
  114. /**
  115. * Creates an unreachable segment that follows given segments.
  116. *
  117. * @param {string} id - An identifier.
  118. * @param {CodePathSegment[]} allPrevSegments - An array of the previous segments.
  119. * @returns {CodePathSegment} The created segment.
  120. */
  121. static newUnreachable(id, allPrevSegments) {
  122. const segment = new CodePathSegment(id, CodePathSegment.flattenUnusedSegments(allPrevSegments), false);
  123. /*
  124. * In `if (a) return a; foo();` case, the unreachable segment preceded by
  125. * the return statement is not used but must not be remove.
  126. */
  127. CodePathSegment.markUsed(segment);
  128. return segment;
  129. }
  130. /**
  131. * Creates a segment that follows given segments.
  132. * This factory method does not connect with `allPrevSegments`.
  133. * But this inherits `reachable` flag.
  134. *
  135. * @param {string} id - An identifier.
  136. * @param {CodePathSegment[]} allPrevSegments - An array of the previous segments.
  137. * @returns {CodePathSegment} The created segment.
  138. */
  139. static newDisconnected(id, allPrevSegments) {
  140. return new CodePathSegment(id, [], allPrevSegments.some(isReachable));
  141. }
  142. /**
  143. * Makes a given segment being used.
  144. *
  145. * And this function registers the segment into the previous segments as a next.
  146. *
  147. * @param {CodePathSegment} segment - A segment to mark.
  148. * @returns {void}
  149. */
  150. static markUsed(segment) {
  151. if (segment.internal.used) {
  152. return;
  153. }
  154. segment.internal.used = true;
  155. let i;
  156. if (segment.reachable) {
  157. for (i = 0; i < segment.allPrevSegments.length; ++i) {
  158. const prevSegment = segment.allPrevSegments[i];
  159. prevSegment.allNextSegments.push(segment);
  160. prevSegment.nextSegments.push(segment);
  161. }
  162. } else {
  163. for (i = 0; i < segment.allPrevSegments.length; ++i) {
  164. segment.allPrevSegments[i].allNextSegments.push(segment);
  165. }
  166. }
  167. }
  168. /**
  169. * Marks a previous segment as looped.
  170. *
  171. * @param {CodePathSegment} segment - A segment.
  172. * @param {CodePathSegment} prevSegment - A previous segment to mark.
  173. * @returns {void}
  174. */
  175. static markPrevSegmentAsLooped(segment, prevSegment) {
  176. segment.internal.loopedPrevSegments.push(prevSegment);
  177. }
  178. /**
  179. * Replaces unused segments with the previous segments of each unused segment.
  180. *
  181. * @param {CodePathSegment[]} segments - An array of segments to replace.
  182. * @returns {CodePathSegment[]} The replaced array.
  183. */
  184. static flattenUnusedSegments(segments) {
  185. const done = Object.create(null);
  186. const retv = [];
  187. for (let i = 0; i < segments.length; ++i) {
  188. const segment = segments[i];
  189. // Ignores duplicated.
  190. if (done[segment.id]) {
  191. continue;
  192. }
  193. // Use previous segments if unused.
  194. if (!segment.internal.used) {
  195. for (let j = 0; j < segment.allPrevSegments.length; ++j) {
  196. const prevSegment = segment.allPrevSegments[j];
  197. if (!done[prevSegment.id]) {
  198. done[prevSegment.id] = true;
  199. retv.push(prevSegment);
  200. }
  201. }
  202. } else {
  203. done[segment.id] = true;
  204. retv.push(segment);
  205. }
  206. }
  207. return retv;
  208. }
  209. }
  210. module.exports = CodePathSegment;