UI for Zipcoin Blue

RequireEnsureDependenciesBlockParserPlugin.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const RequireEnsureDependenciesBlock = require("./RequireEnsureDependenciesBlock");
  7. const RequireEnsureItemDependency = require("./RequireEnsureItemDependency");
  8. const getFunctionExpression = require("./getFunctionExpression");
  9. module.exports = class RequireEnsureDependenciesBlockParserPlugin {
  10. apply(parser) {
  11. parser.plugin("call require.ensure", expr => {
  12. let chunkName = null;
  13. let chunkNameRange = null;
  14. let errorExpressionArg = null;
  15. let errorExpression = null;
  16. switch(expr.arguments.length) {
  17. case 4:
  18. {
  19. const chunkNameExpr = parser.evaluateExpression(expr.arguments[3]);
  20. if(!chunkNameExpr.isString()) return;
  21. chunkNameRange = chunkNameExpr.range;
  22. chunkName = chunkNameExpr.string;
  23. }
  24. // falls through
  25. case 3:
  26. {
  27. errorExpressionArg = expr.arguments[2];
  28. errorExpression = getFunctionExpression(errorExpressionArg);
  29. if(!errorExpression && !chunkName) {
  30. const chunkNameExpr = parser.evaluateExpression(expr.arguments[2]);
  31. if(!chunkNameExpr.isString()) return;
  32. chunkNameRange = chunkNameExpr.range;
  33. chunkName = chunkNameExpr.string;
  34. }
  35. }
  36. // falls through
  37. case 2:
  38. {
  39. const dependenciesExpr = parser.evaluateExpression(expr.arguments[0]);
  40. const dependenciesItems = dependenciesExpr.isArray() ? dependenciesExpr.items : [dependenciesExpr];
  41. const successExpressionArg = expr.arguments[1];
  42. const successExpression = getFunctionExpression(successExpressionArg);
  43. if(successExpression) {
  44. parser.walkExpressions(successExpression.expressions);
  45. }
  46. if(errorExpression) {
  47. parser.walkExpressions(errorExpression.expressions);
  48. }
  49. const dep = new RequireEnsureDependenciesBlock(expr,
  50. successExpression ? successExpression.fn : successExpressionArg,
  51. errorExpression ? errorExpression.fn : errorExpressionArg,
  52. chunkName, chunkNameRange, parser.state.module, expr.loc);
  53. const old = parser.state.current;
  54. parser.state.current = dep;
  55. try {
  56. let failed = false;
  57. parser.inScope([], () => {
  58. dependenciesItems.forEach(ee => {
  59. if(ee.isString()) {
  60. const edep = new RequireEnsureItemDependency(ee.string, ee.range);
  61. edep.loc = dep.loc;
  62. dep.addDependency(edep);
  63. } else {
  64. failed = true;
  65. }
  66. });
  67. });
  68. if(failed) {
  69. return;
  70. }
  71. if(successExpression) {
  72. if(successExpression.fn.body.type === "BlockStatement")
  73. parser.walkStatement(successExpression.fn.body);
  74. else
  75. parser.walkExpression(successExpression.fn.body);
  76. }
  77. old.addBlock(dep);
  78. } finally {
  79. parser.state.current = old;
  80. }
  81. if(!successExpression) {
  82. parser.walkExpression(successExpressionArg);
  83. }
  84. if(errorExpression) {
  85. if(errorExpression.fn.body.type === "BlockStatement")
  86. parser.walkStatement(errorExpression.fn.body);
  87. else
  88. parser.walkExpression(errorExpression.fn.body);
  89. } else if(errorExpressionArg) {
  90. parser.walkExpression(errorExpressionArg);
  91. }
  92. return true;
  93. }
  94. }
  95. });
  96. }
  97. };