Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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-analyzer.js 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. /**
  2. * @fileoverview A class of the code path analyzer.
  3. * @author Toru Nagashima
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const assert = require("assert"),
  10. { breakableTypePattern } = require("../../shared/ast-utils"),
  11. CodePath = require("./code-path"),
  12. CodePathSegment = require("./code-path-segment"),
  13. IdGenerator = require("./id-generator"),
  14. debug = require("./debug-helpers");
  15. //------------------------------------------------------------------------------
  16. // Helpers
  17. //------------------------------------------------------------------------------
  18. /**
  19. * Checks whether or not a given node is a `case` node (not `default` node).
  20. * @param {ASTNode} node A `SwitchCase` node to check.
  21. * @returns {boolean} `true` if the node is a `case` node (not `default` node).
  22. */
  23. function isCaseNode(node) {
  24. return Boolean(node.test);
  25. }
  26. /**
  27. * Checks whether the given logical operator is taken into account for the code
  28. * path analysis.
  29. * @param {string} operator The operator found in the LogicalExpression node
  30. * @returns {boolean} `true` if the operator is "&&" or "||" or "??"
  31. */
  32. function isHandledLogicalOperator(operator) {
  33. return operator === "&&" || operator === "||" || operator === "??";
  34. }
  35. /**
  36. * Checks whether the given assignment operator is a logical assignment operator.
  37. * Logical assignments are taken into account for the code path analysis
  38. * because of their short-circuiting semantics.
  39. * @param {string} operator The operator found in the AssignmentExpression node
  40. * @returns {boolean} `true` if the operator is "&&=" or "||=" or "??="
  41. */
  42. function isLogicalAssignmentOperator(operator) {
  43. return operator === "&&=" || operator === "||=" || operator === "??=";
  44. }
  45. /**
  46. * Gets the label if the parent node of a given node is a LabeledStatement.
  47. * @param {ASTNode} node A node to get.
  48. * @returns {string|null} The label or `null`.
  49. */
  50. function getLabel(node) {
  51. if (node.parent.type === "LabeledStatement") {
  52. return node.parent.label.name;
  53. }
  54. return null;
  55. }
  56. /**
  57. * Checks whether or not a given logical expression node goes different path
  58. * between the `true` case and the `false` case.
  59. * @param {ASTNode} node A node to check.
  60. * @returns {boolean} `true` if the node is a test of a choice statement.
  61. */
  62. function isForkingByTrueOrFalse(node) {
  63. const parent = node.parent;
  64. switch (parent.type) {
  65. case "ConditionalExpression":
  66. case "IfStatement":
  67. case "WhileStatement":
  68. case "DoWhileStatement":
  69. case "ForStatement":
  70. return parent.test === node;
  71. case "LogicalExpression":
  72. return isHandledLogicalOperator(parent.operator);
  73. case "AssignmentExpression":
  74. return isLogicalAssignmentOperator(parent.operator);
  75. default:
  76. return false;
  77. }
  78. }
  79. /**
  80. * Gets the boolean value of a given literal node.
  81. *
  82. * This is used to detect infinity loops (e.g. `while (true) {}`).
  83. * Statements preceded by an infinity loop are unreachable if the loop didn't
  84. * have any `break` statement.
  85. * @param {ASTNode} node A node to get.
  86. * @returns {boolean|undefined} a boolean value if the node is a Literal node,
  87. * otherwise `undefined`.
  88. */
  89. function getBooleanValueIfSimpleConstant(node) {
  90. if (node.type === "Literal") {
  91. return Boolean(node.value);
  92. }
  93. return void 0;
  94. }
  95. /**
  96. * Checks that a given identifier node is a reference or not.
  97. *
  98. * This is used to detect the first throwable node in a `try` block.
  99. * @param {ASTNode} node An Identifier node to check.
  100. * @returns {boolean} `true` if the node is a reference.
  101. */
  102. function isIdentifierReference(node) {
  103. const parent = node.parent;
  104. switch (parent.type) {
  105. case "LabeledStatement":
  106. case "BreakStatement":
  107. case "ContinueStatement":
  108. case "ArrayPattern":
  109. case "RestElement":
  110. case "ImportSpecifier":
  111. case "ImportDefaultSpecifier":
  112. case "ImportNamespaceSpecifier":
  113. case "CatchClause":
  114. return false;
  115. case "FunctionDeclaration":
  116. case "FunctionExpression":
  117. case "ArrowFunctionExpression":
  118. case "ClassDeclaration":
  119. case "ClassExpression":
  120. case "VariableDeclarator":
  121. return parent.id !== node;
  122. case "Property":
  123. case "MethodDefinition":
  124. return (
  125. parent.key !== node ||
  126. parent.computed ||
  127. parent.shorthand
  128. );
  129. case "AssignmentPattern":
  130. return parent.key !== node;
  131. default:
  132. return true;
  133. }
  134. }
  135. /**
  136. * Updates the current segment with the head segment.
  137. * This is similar to local branches and tracking branches of git.
  138. *
  139. * To separate the current and the head is in order to not make useless segments.
  140. *
  141. * In this process, both "onCodePathSegmentStart" and "onCodePathSegmentEnd"
  142. * events are fired.
  143. * @param {CodePathAnalyzer} analyzer The instance.
  144. * @param {ASTNode} node The current AST node.
  145. * @returns {void}
  146. */
  147. function forwardCurrentToHead(analyzer, node) {
  148. const codePath = analyzer.codePath;
  149. const state = CodePath.getState(codePath);
  150. const currentSegments = state.currentSegments;
  151. const headSegments = state.headSegments;
  152. const end = Math.max(currentSegments.length, headSegments.length);
  153. let i, currentSegment, headSegment;
  154. // Fires leaving events.
  155. for (i = 0; i < end; ++i) {
  156. currentSegment = currentSegments[i];
  157. headSegment = headSegments[i];
  158. if (currentSegment !== headSegment && currentSegment) {
  159. debug.dump(`onCodePathSegmentEnd ${currentSegment.id}`);
  160. if (currentSegment.reachable) {
  161. analyzer.emitter.emit(
  162. "onCodePathSegmentEnd",
  163. currentSegment,
  164. node
  165. );
  166. }
  167. }
  168. }
  169. // Update state.
  170. state.currentSegments = headSegments;
  171. // Fires entering events.
  172. for (i = 0; i < end; ++i) {
  173. currentSegment = currentSegments[i];
  174. headSegment = headSegments[i];
  175. if (currentSegment !== headSegment && headSegment) {
  176. debug.dump(`onCodePathSegmentStart ${headSegment.id}`);
  177. CodePathSegment.markUsed(headSegment);
  178. if (headSegment.reachable) {
  179. analyzer.emitter.emit(
  180. "onCodePathSegmentStart",
  181. headSegment,
  182. node
  183. );
  184. }
  185. }
  186. }
  187. }
  188. /**
  189. * Updates the current segment with empty.
  190. * This is called at the last of functions or the program.
  191. * @param {CodePathAnalyzer} analyzer The instance.
  192. * @param {ASTNode} node The current AST node.
  193. * @returns {void}
  194. */
  195. function leaveFromCurrentSegment(analyzer, node) {
  196. const state = CodePath.getState(analyzer.codePath);
  197. const currentSegments = state.currentSegments;
  198. for (let i = 0; i < currentSegments.length; ++i) {
  199. const currentSegment = currentSegments[i];
  200. debug.dump(`onCodePathSegmentEnd ${currentSegment.id}`);
  201. if (currentSegment.reachable) {
  202. analyzer.emitter.emit(
  203. "onCodePathSegmentEnd",
  204. currentSegment,
  205. node
  206. );
  207. }
  208. }
  209. state.currentSegments = [];
  210. }
  211. /**
  212. * Updates the code path due to the position of a given node in the parent node
  213. * thereof.
  214. *
  215. * For example, if the node is `parent.consequent`, this creates a fork from the
  216. * current path.
  217. * @param {CodePathAnalyzer} analyzer The instance.
  218. * @param {ASTNode} node The current AST node.
  219. * @returns {void}
  220. */
  221. function preprocess(analyzer, node) {
  222. const codePath = analyzer.codePath;
  223. const state = CodePath.getState(codePath);
  224. const parent = node.parent;
  225. switch (parent.type) {
  226. // The `arguments.length == 0` case is in `postprocess` function.
  227. case "CallExpression":
  228. if (parent.optional === true && parent.arguments.length >= 1 && parent.arguments[0] === node) {
  229. state.makeOptionalRight();
  230. }
  231. break;
  232. case "MemberExpression":
  233. if (parent.optional === true && parent.property === node) {
  234. state.makeOptionalRight();
  235. }
  236. break;
  237. case "LogicalExpression":
  238. if (
  239. parent.right === node &&
  240. isHandledLogicalOperator(parent.operator)
  241. ) {
  242. state.makeLogicalRight();
  243. }
  244. break;
  245. case "AssignmentExpression":
  246. if (
  247. parent.right === node &&
  248. isLogicalAssignmentOperator(parent.operator)
  249. ) {
  250. state.makeLogicalRight();
  251. }
  252. break;
  253. case "ConditionalExpression":
  254. case "IfStatement":
  255. /*
  256. * Fork if this node is at `consequent`/`alternate`.
  257. * `popForkContext()` exists at `IfStatement:exit` and
  258. * `ConditionalExpression:exit`.
  259. */
  260. if (parent.consequent === node) {
  261. state.makeIfConsequent();
  262. } else if (parent.alternate === node) {
  263. state.makeIfAlternate();
  264. }
  265. break;
  266. case "SwitchCase":
  267. if (parent.consequent[0] === node) {
  268. state.makeSwitchCaseBody(false, !parent.test);
  269. }
  270. break;
  271. case "TryStatement":
  272. if (parent.handler === node) {
  273. state.makeCatchBlock();
  274. } else if (parent.finalizer === node) {
  275. state.makeFinallyBlock();
  276. }
  277. break;
  278. case "WhileStatement":
  279. if (parent.test === node) {
  280. state.makeWhileTest(getBooleanValueIfSimpleConstant(node));
  281. } else {
  282. assert(parent.body === node);
  283. state.makeWhileBody();
  284. }
  285. break;
  286. case "DoWhileStatement":
  287. if (parent.body === node) {
  288. state.makeDoWhileBody();
  289. } else {
  290. assert(parent.test === node);
  291. state.makeDoWhileTest(getBooleanValueIfSimpleConstant(node));
  292. }
  293. break;
  294. case "ForStatement":
  295. if (parent.test === node) {
  296. state.makeForTest(getBooleanValueIfSimpleConstant(node));
  297. } else if (parent.update === node) {
  298. state.makeForUpdate();
  299. } else if (parent.body === node) {
  300. state.makeForBody();
  301. }
  302. break;
  303. case "ForInStatement":
  304. case "ForOfStatement":
  305. if (parent.left === node) {
  306. state.makeForInOfLeft();
  307. } else if (parent.right === node) {
  308. state.makeForInOfRight();
  309. } else {
  310. assert(parent.body === node);
  311. state.makeForInOfBody();
  312. }
  313. break;
  314. case "AssignmentPattern":
  315. /*
  316. * Fork if this node is at `right`.
  317. * `left` is executed always, so it uses the current path.
  318. * `popForkContext()` exists at `AssignmentPattern:exit`.
  319. */
  320. if (parent.right === node) {
  321. state.pushForkContext();
  322. state.forkBypassPath();
  323. state.forkPath();
  324. }
  325. break;
  326. default:
  327. break;
  328. }
  329. }
  330. /**
  331. * Updates the code path due to the type of a given node in entering.
  332. * @param {CodePathAnalyzer} analyzer The instance.
  333. * @param {ASTNode} node The current AST node.
  334. * @returns {void}
  335. */
  336. function processCodePathToEnter(analyzer, node) {
  337. let codePath = analyzer.codePath;
  338. let state = codePath && CodePath.getState(codePath);
  339. const parent = node.parent;
  340. switch (node.type) {
  341. case "Program":
  342. case "FunctionDeclaration":
  343. case "FunctionExpression":
  344. case "ArrowFunctionExpression":
  345. if (codePath) {
  346. // Emits onCodePathSegmentStart events if updated.
  347. forwardCurrentToHead(analyzer, node);
  348. debug.dumpState(node, state, false);
  349. }
  350. // Create the code path of this scope.
  351. codePath = analyzer.codePath = new CodePath(
  352. analyzer.idGenerator.next(),
  353. codePath,
  354. analyzer.onLooped
  355. );
  356. state = CodePath.getState(codePath);
  357. // Emits onCodePathStart events.
  358. debug.dump(`onCodePathStart ${codePath.id}`);
  359. analyzer.emitter.emit("onCodePathStart", codePath, node);
  360. break;
  361. case "ChainExpression":
  362. state.pushChainContext();
  363. break;
  364. case "CallExpression":
  365. if (node.optional === true) {
  366. state.makeOptionalNode();
  367. }
  368. break;
  369. case "MemberExpression":
  370. if (node.optional === true) {
  371. state.makeOptionalNode();
  372. }
  373. break;
  374. case "LogicalExpression":
  375. if (isHandledLogicalOperator(node.operator)) {
  376. state.pushChoiceContext(
  377. node.operator,
  378. isForkingByTrueOrFalse(node)
  379. );
  380. }
  381. break;
  382. case "AssignmentExpression":
  383. if (isLogicalAssignmentOperator(node.operator)) {
  384. state.pushChoiceContext(
  385. node.operator.slice(0, -1), // removes `=` from the end
  386. isForkingByTrueOrFalse(node)
  387. );
  388. }
  389. break;
  390. case "ConditionalExpression":
  391. case "IfStatement":
  392. state.pushChoiceContext("test", false);
  393. break;
  394. case "SwitchStatement":
  395. state.pushSwitchContext(
  396. node.cases.some(isCaseNode),
  397. getLabel(node)
  398. );
  399. break;
  400. case "TryStatement":
  401. state.pushTryContext(Boolean(node.finalizer));
  402. break;
  403. case "SwitchCase":
  404. /*
  405. * Fork if this node is after the 2st node in `cases`.
  406. * It's similar to `else` blocks.
  407. * The next `test` node is processed in this path.
  408. */
  409. if (parent.discriminant !== node && parent.cases[0] !== node) {
  410. state.forkPath();
  411. }
  412. break;
  413. case "WhileStatement":
  414. case "DoWhileStatement":
  415. case "ForStatement":
  416. case "ForInStatement":
  417. case "ForOfStatement":
  418. state.pushLoopContext(node.type, getLabel(node));
  419. break;
  420. case "LabeledStatement":
  421. if (!breakableTypePattern.test(node.body.type)) {
  422. state.pushBreakContext(false, node.label.name);
  423. }
  424. break;
  425. default:
  426. break;
  427. }
  428. // Emits onCodePathSegmentStart events if updated.
  429. forwardCurrentToHead(analyzer, node);
  430. debug.dumpState(node, state, false);
  431. }
  432. /**
  433. * Updates the code path due to the type of a given node in leaving.
  434. * @param {CodePathAnalyzer} analyzer The instance.
  435. * @param {ASTNode} node The current AST node.
  436. * @returns {void}
  437. */
  438. function processCodePathToExit(analyzer, node) {
  439. const codePath = analyzer.codePath;
  440. const state = CodePath.getState(codePath);
  441. let dontForward = false;
  442. switch (node.type) {
  443. case "ChainExpression":
  444. state.popChainContext();
  445. break;
  446. case "IfStatement":
  447. case "ConditionalExpression":
  448. state.popChoiceContext();
  449. break;
  450. case "LogicalExpression":
  451. if (isHandledLogicalOperator(node.operator)) {
  452. state.popChoiceContext();
  453. }
  454. break;
  455. case "AssignmentExpression":
  456. if (isLogicalAssignmentOperator(node.operator)) {
  457. state.popChoiceContext();
  458. }
  459. break;
  460. case "SwitchStatement":
  461. state.popSwitchContext();
  462. break;
  463. case "SwitchCase":
  464. /*
  465. * This is the same as the process at the 1st `consequent` node in
  466. * `preprocess` function.
  467. * Must do if this `consequent` is empty.
  468. */
  469. if (node.consequent.length === 0) {
  470. state.makeSwitchCaseBody(true, !node.test);
  471. }
  472. if (state.forkContext.reachable) {
  473. dontForward = true;
  474. }
  475. break;
  476. case "TryStatement":
  477. state.popTryContext();
  478. break;
  479. case "BreakStatement":
  480. forwardCurrentToHead(analyzer, node);
  481. state.makeBreak(node.label && node.label.name);
  482. dontForward = true;
  483. break;
  484. case "ContinueStatement":
  485. forwardCurrentToHead(analyzer, node);
  486. state.makeContinue(node.label && node.label.name);
  487. dontForward = true;
  488. break;
  489. case "ReturnStatement":
  490. forwardCurrentToHead(analyzer, node);
  491. state.makeReturn();
  492. dontForward = true;
  493. break;
  494. case "ThrowStatement":
  495. forwardCurrentToHead(analyzer, node);
  496. state.makeThrow();
  497. dontForward = true;
  498. break;
  499. case "Identifier":
  500. if (isIdentifierReference(node)) {
  501. state.makeFirstThrowablePathInTryBlock();
  502. dontForward = true;
  503. }
  504. break;
  505. case "CallExpression":
  506. case "ImportExpression":
  507. case "MemberExpression":
  508. case "NewExpression":
  509. case "YieldExpression":
  510. state.makeFirstThrowablePathInTryBlock();
  511. break;
  512. case "WhileStatement":
  513. case "DoWhileStatement":
  514. case "ForStatement":
  515. case "ForInStatement":
  516. case "ForOfStatement":
  517. state.popLoopContext();
  518. break;
  519. case "AssignmentPattern":
  520. state.popForkContext();
  521. break;
  522. case "LabeledStatement":
  523. if (!breakableTypePattern.test(node.body.type)) {
  524. state.popBreakContext();
  525. }
  526. break;
  527. default:
  528. break;
  529. }
  530. // Emits onCodePathSegmentStart events if updated.
  531. if (!dontForward) {
  532. forwardCurrentToHead(analyzer, node);
  533. }
  534. debug.dumpState(node, state, true);
  535. }
  536. /**
  537. * Updates the code path to finalize the current code path.
  538. * @param {CodePathAnalyzer} analyzer The instance.
  539. * @param {ASTNode} node The current AST node.
  540. * @returns {void}
  541. */
  542. function postprocess(analyzer, node) {
  543. switch (node.type) {
  544. case "Program":
  545. case "FunctionDeclaration":
  546. case "FunctionExpression":
  547. case "ArrowFunctionExpression": {
  548. let codePath = analyzer.codePath;
  549. // Mark the current path as the final node.
  550. CodePath.getState(codePath).makeFinal();
  551. // Emits onCodePathSegmentEnd event of the current segments.
  552. leaveFromCurrentSegment(analyzer, node);
  553. // Emits onCodePathEnd event of this code path.
  554. debug.dump(`onCodePathEnd ${codePath.id}`);
  555. analyzer.emitter.emit("onCodePathEnd", codePath, node);
  556. debug.dumpDot(codePath);
  557. codePath = analyzer.codePath = analyzer.codePath.upper;
  558. if (codePath) {
  559. debug.dumpState(node, CodePath.getState(codePath), true);
  560. }
  561. break;
  562. }
  563. // The `arguments.length >= 1` case is in `preprocess` function.
  564. case "CallExpression":
  565. if (node.optional === true && node.arguments.length === 0) {
  566. CodePath.getState(analyzer.codePath).makeOptionalRight();
  567. }
  568. break;
  569. default:
  570. break;
  571. }
  572. }
  573. //------------------------------------------------------------------------------
  574. // Public Interface
  575. //------------------------------------------------------------------------------
  576. /**
  577. * The class to analyze code paths.
  578. * This class implements the EventGenerator interface.
  579. */
  580. class CodePathAnalyzer {
  581. // eslint-disable-next-line jsdoc/require-description
  582. /**
  583. * @param {EventGenerator} eventGenerator An event generator to wrap.
  584. */
  585. constructor(eventGenerator) {
  586. this.original = eventGenerator;
  587. this.emitter = eventGenerator.emitter;
  588. this.codePath = null;
  589. this.idGenerator = new IdGenerator("s");
  590. this.currentNode = null;
  591. this.onLooped = this.onLooped.bind(this);
  592. }
  593. /**
  594. * Does the process to enter a given AST node.
  595. * This updates state of analysis and calls `enterNode` of the wrapped.
  596. * @param {ASTNode} node A node which is entering.
  597. * @returns {void}
  598. */
  599. enterNode(node) {
  600. this.currentNode = node;
  601. // Updates the code path due to node's position in its parent node.
  602. if (node.parent) {
  603. preprocess(this, node);
  604. }
  605. /*
  606. * Updates the code path.
  607. * And emits onCodePathStart/onCodePathSegmentStart events.
  608. */
  609. processCodePathToEnter(this, node);
  610. // Emits node events.
  611. this.original.enterNode(node);
  612. this.currentNode = null;
  613. }
  614. /**
  615. * Does the process to leave a given AST node.
  616. * This updates state of analysis and calls `leaveNode` of the wrapped.
  617. * @param {ASTNode} node A node which is leaving.
  618. * @returns {void}
  619. */
  620. leaveNode(node) {
  621. this.currentNode = node;
  622. /*
  623. * Updates the code path.
  624. * And emits onCodePathStart/onCodePathSegmentStart events.
  625. */
  626. processCodePathToExit(this, node);
  627. // Emits node events.
  628. this.original.leaveNode(node);
  629. // Emits the last onCodePathStart/onCodePathSegmentStart events.
  630. postprocess(this, node);
  631. this.currentNode = null;
  632. }
  633. /**
  634. * This is called on a code path looped.
  635. * Then this raises a looped event.
  636. * @param {CodePathSegment} fromSegment A segment of prev.
  637. * @param {CodePathSegment} toSegment A segment of next.
  638. * @returns {void}
  639. */
  640. onLooped(fromSegment, toSegment) {
  641. if (fromSegment.reachable && toSegment.reachable) {
  642. debug.dump(`onCodePathSegmentLoop ${fromSegment.id} -> ${toSegment.id}`);
  643. this.emitter.emit(
  644. "onCodePathSegmentLoop",
  645. fromSegment,
  646. toSegment,
  647. this.currentNode
  648. );
  649. }
  650. }
  651. }
  652. module.exports = CodePathAnalyzer;