AMDRequireDependenciesBlockParserPlugin.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const RuntimeGlobals = require("../RuntimeGlobals");
  7. const UnsupportedFeatureWarning = require("../UnsupportedFeatureWarning");
  8. const AMDRequireArrayDependency = require("./AMDRequireArrayDependency");
  9. const AMDRequireContextDependency = require("./AMDRequireContextDependency");
  10. const AMDRequireDependenciesBlock = require("./AMDRequireDependenciesBlock");
  11. const AMDRequireDependency = require("./AMDRequireDependency");
  12. const AMDRequireItemDependency = require("./AMDRequireItemDependency");
  13. const ConstDependency = require("./ConstDependency");
  14. const ContextDependencyHelpers = require("./ContextDependencyHelpers");
  15. const LocalModuleDependency = require("./LocalModuleDependency");
  16. const { getLocalModule } = require("./LocalModulesHelpers");
  17. const UnsupportedDependency = require("./UnsupportedDependency");
  18. const getFunctionExpression = require("./getFunctionExpression");
  19. /** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
  20. class AMDRequireDependenciesBlockParserPlugin {
  21. constructor(options) {
  22. this.options = options;
  23. }
  24. processFunctionArgument(parser, expression) {
  25. let bindThis = true;
  26. const fnData = getFunctionExpression(expression);
  27. if (fnData) {
  28. parser.inScope(
  29. fnData.fn.params.filter(i => {
  30. return !["require", "module", "exports"].includes(i.name);
  31. }),
  32. () => {
  33. if (fnData.fn.body.type === "BlockStatement") {
  34. parser.walkStatement(fnData.fn.body);
  35. } else {
  36. parser.walkExpression(fnData.fn.body);
  37. }
  38. }
  39. );
  40. parser.walkExpressions(fnData.expressions);
  41. if (fnData.needThis === false) {
  42. bindThis = false;
  43. }
  44. } else {
  45. parser.walkExpression(expression);
  46. }
  47. return bindThis;
  48. }
  49. /**
  50. * @param {JavascriptParser} parser the parser
  51. * @returns {void}
  52. */
  53. apply(parser) {
  54. parser.hooks.call
  55. .for("require")
  56. .tap(
  57. "AMDRequireDependenciesBlockParserPlugin",
  58. this.processCallRequire.bind(this, parser)
  59. );
  60. }
  61. processArray(parser, expr, param) {
  62. if (param.isArray()) {
  63. for (const p of param.items) {
  64. const result = this.processItem(parser, expr, p);
  65. if (result === undefined) {
  66. this.processContext(parser, expr, p);
  67. }
  68. }
  69. return true;
  70. } else if (param.isConstArray()) {
  71. const deps = [];
  72. for (const request of param.array) {
  73. let dep, localModule;
  74. if (request === "require") {
  75. dep = RuntimeGlobals.require;
  76. } else if (["exports", "module"].includes(request)) {
  77. dep = request;
  78. } else if ((localModule = getLocalModule(parser.state, request))) {
  79. localModule.flagUsed();
  80. dep = new LocalModuleDependency(localModule, undefined, false);
  81. dep.loc = expr.loc;
  82. parser.state.module.addPresentationalDependency(dep);
  83. } else {
  84. dep = this.newRequireItemDependency(request);
  85. dep.loc = expr.loc;
  86. dep.optional = !!parser.scope.inTry;
  87. parser.state.current.addDependency(dep);
  88. }
  89. deps.push(dep);
  90. }
  91. const dep = this.newRequireArrayDependency(deps, param.range);
  92. dep.loc = expr.loc;
  93. dep.optional = !!parser.scope.inTry;
  94. parser.state.module.addPresentationalDependency(dep);
  95. return true;
  96. }
  97. }
  98. processItem(parser, expr, param) {
  99. if (param.isConditional()) {
  100. for (const p of param.options) {
  101. const result = this.processItem(parser, expr, p);
  102. if (result === undefined) {
  103. this.processContext(parser, expr, p);
  104. }
  105. }
  106. return true;
  107. } else if (param.isString()) {
  108. let dep, localModule;
  109. if (param.string === "require") {
  110. dep = new ConstDependency(RuntimeGlobals.require, param.string, [
  111. RuntimeGlobals.require
  112. ]);
  113. } else if (param.string === "module") {
  114. dep = new ConstDependency(
  115. parser.state.module.buildInfo.moduleArgument,
  116. param.range,
  117. [RuntimeGlobals.module]
  118. );
  119. } else if (param.string === "exports") {
  120. dep = new ConstDependency(
  121. parser.state.module.buildInfo.exportsArgument,
  122. param.range,
  123. [RuntimeGlobals.exports]
  124. );
  125. } else if ((localModule = getLocalModule(parser.state, param.string))) {
  126. localModule.flagUsed();
  127. dep = new LocalModuleDependency(localModule, param.range, false);
  128. } else {
  129. dep = this.newRequireItemDependency(param.string, param.range);
  130. dep.loc = expr.loc;
  131. dep.optional = !!parser.scope.inTry;
  132. parser.state.current.addDependency(dep);
  133. return true;
  134. }
  135. dep.loc = expr.loc;
  136. parser.state.module.addPresentationalDependency(dep);
  137. return true;
  138. }
  139. }
  140. processContext(parser, expr, param) {
  141. const dep = ContextDependencyHelpers.create(
  142. AMDRequireContextDependency,
  143. param.range,
  144. param,
  145. expr,
  146. this.options,
  147. {
  148. category: "amd"
  149. },
  150. parser
  151. );
  152. if (!dep) return;
  153. dep.loc = expr.loc;
  154. dep.optional = !!parser.scope.inTry;
  155. parser.state.current.addDependency(dep);
  156. return true;
  157. }
  158. processArrayForRequestString(param) {
  159. if (param.isArray()) {
  160. const result = param.items.map(item =>
  161. this.processItemForRequestString(item)
  162. );
  163. if (result.every(Boolean)) return result.join(" ");
  164. } else if (param.isConstArray()) {
  165. return param.array.join(" ");
  166. }
  167. }
  168. processItemForRequestString(param) {
  169. if (param.isConditional()) {
  170. const result = param.options.map(item =>
  171. this.processItemForRequestString(item)
  172. );
  173. if (result.every(Boolean)) return result.join("|");
  174. } else if (param.isString()) {
  175. return param.string;
  176. }
  177. }
  178. processCallRequire(parser, expr) {
  179. let param;
  180. let depBlock;
  181. let dep;
  182. let result;
  183. const old = parser.state.current;
  184. if (expr.arguments.length >= 1) {
  185. param = parser.evaluateExpression(expr.arguments[0]);
  186. depBlock = this.newRequireDependenciesBlock(
  187. expr.loc,
  188. this.processArrayForRequestString(param)
  189. );
  190. dep = this.newRequireDependency(
  191. expr.range,
  192. param.range,
  193. expr.arguments.length > 1 ? expr.arguments[1].range : null,
  194. expr.arguments.length > 2 ? expr.arguments[2].range : null
  195. );
  196. dep.loc = expr.loc;
  197. depBlock.addDependency(dep);
  198. parser.state.current = depBlock;
  199. }
  200. if (expr.arguments.length === 1) {
  201. parser.inScope([], () => {
  202. result = this.processArray(parser, expr, param);
  203. });
  204. parser.state.current = old;
  205. if (!result) return;
  206. parser.state.current.addBlock(depBlock);
  207. return true;
  208. }
  209. if (expr.arguments.length === 2 || expr.arguments.length === 3) {
  210. try {
  211. parser.inScope([], () => {
  212. result = this.processArray(parser, expr, param);
  213. });
  214. if (!result) {
  215. const dep = new UnsupportedDependency("unsupported", expr.range);
  216. old.addPresentationalDependency(dep);
  217. if (parser.state.module) {
  218. parser.state.module.addError(
  219. new UnsupportedFeatureWarning(
  220. "Cannot statically analyse 'require(…, …)' in line " +
  221. expr.loc.start.line,
  222. expr.loc
  223. )
  224. );
  225. }
  226. depBlock = null;
  227. return true;
  228. }
  229. dep.functionBindThis = this.processFunctionArgument(
  230. parser,
  231. expr.arguments[1]
  232. );
  233. if (expr.arguments.length === 3) {
  234. dep.errorCallbackBindThis = this.processFunctionArgument(
  235. parser,
  236. expr.arguments[2]
  237. );
  238. }
  239. } finally {
  240. parser.state.current = old;
  241. if (depBlock) parser.state.current.addBlock(depBlock);
  242. }
  243. return true;
  244. }
  245. }
  246. newRequireDependenciesBlock(loc, request) {
  247. return new AMDRequireDependenciesBlock(loc, request);
  248. }
  249. newRequireDependency(
  250. outerRange,
  251. arrayRange,
  252. functionRange,
  253. errorCallbackRange
  254. ) {
  255. return new AMDRequireDependency(
  256. outerRange,
  257. arrayRange,
  258. functionRange,
  259. errorCallbackRange
  260. );
  261. }
  262. newRequireItemDependency(request, range) {
  263. return new AMDRequireItemDependency(request, range);
  264. }
  265. newRequireArrayDependency(depsArray, range) {
  266. return new AMDRequireArrayDependency(depsArray, range);
  267. }
  268. }
  269. module.exports = AMDRequireDependenciesBlockParserPlugin;