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.

138 lines
5.1 KiB

4 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _assert = _interopRequireDefault(require("assert"));
  7. var t = _interopRequireWildcard(require("@babel/types"));
  8. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  9. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. class ImportBuilder {
  12. constructor(importedSource, scope, hub) {
  13. this._statements = [];
  14. this._resultName = null;
  15. this._scope = null;
  16. this._hub = null;
  17. this._scope = scope;
  18. this._hub = hub;
  19. this._importedSource = importedSource;
  20. }
  21. done() {
  22. return {
  23. statements: this._statements,
  24. resultName: this._resultName
  25. };
  26. }
  27. import() {
  28. this._statements.push(t.importDeclaration([], t.stringLiteral(this._importedSource)));
  29. return this;
  30. }
  31. require() {
  32. this._statements.push(t.expressionStatement(t.callExpression(t.identifier("require"), [t.stringLiteral(this._importedSource)])));
  33. return this;
  34. }
  35. namespace(name = "namespace") {
  36. name = this._scope.generateUidIdentifier(name);
  37. const statement = this._statements[this._statements.length - 1];
  38. (0, _assert.default)(statement.type === "ImportDeclaration");
  39. (0, _assert.default)(statement.specifiers.length === 0);
  40. statement.specifiers = [t.importNamespaceSpecifier(name)];
  41. this._resultName = t.cloneNode(name);
  42. return this;
  43. }
  44. default(name) {
  45. name = this._scope.generateUidIdentifier(name);
  46. const statement = this._statements[this._statements.length - 1];
  47. (0, _assert.default)(statement.type === "ImportDeclaration");
  48. (0, _assert.default)(statement.specifiers.length === 0);
  49. statement.specifiers = [t.importDefaultSpecifier(name)];
  50. this._resultName = t.cloneNode(name);
  51. return this;
  52. }
  53. named(name, importName) {
  54. if (importName === "default") return this.default(name);
  55. name = this._scope.generateUidIdentifier(name);
  56. const statement = this._statements[this._statements.length - 1];
  57. (0, _assert.default)(statement.type === "ImportDeclaration");
  58. (0, _assert.default)(statement.specifiers.length === 0);
  59. statement.specifiers = [t.importSpecifier(name, t.identifier(importName))];
  60. this._resultName = t.cloneNode(name);
  61. return this;
  62. }
  63. var(name) {
  64. name = this._scope.generateUidIdentifier(name);
  65. let statement = this._statements[this._statements.length - 1];
  66. if (statement.type !== "ExpressionStatement") {
  67. (0, _assert.default)(this._resultName);
  68. statement = t.expressionStatement(this._resultName);
  69. this._statements.push(statement);
  70. }
  71. this._statements[this._statements.length - 1] = t.variableDeclaration("var", [t.variableDeclarator(name, statement.expression)]);
  72. this._resultName = t.cloneNode(name);
  73. return this;
  74. }
  75. defaultInterop() {
  76. return this._interop(this._hub.addHelper("interopRequireDefault"));
  77. }
  78. wildcardInterop() {
  79. return this._interop(this._hub.addHelper("interopRequireWildcard"));
  80. }
  81. _interop(callee) {
  82. const statement = this._statements[this._statements.length - 1];
  83. if (statement.type === "ExpressionStatement") {
  84. statement.expression = t.callExpression(callee, [statement.expression]);
  85. } else if (statement.type === "VariableDeclaration") {
  86. (0, _assert.default)(statement.declarations.length === 1);
  87. statement.declarations[0].init = t.callExpression(callee, [statement.declarations[0].init]);
  88. } else {
  89. _assert.default.fail("Unexpected type.");
  90. }
  91. return this;
  92. }
  93. prop(name) {
  94. const statement = this._statements[this._statements.length - 1];
  95. if (statement.type === "ExpressionStatement") {
  96. statement.expression = t.memberExpression(statement.expression, t.identifier(name));
  97. } else if (statement.type === "VariableDeclaration") {
  98. (0, _assert.default)(statement.declarations.length === 1);
  99. statement.declarations[0].init = t.memberExpression(statement.declarations[0].init, t.identifier(name));
  100. } else {
  101. _assert.default.fail("Unexpected type:" + statement.type);
  102. }
  103. return this;
  104. }
  105. read(name) {
  106. this._resultName = t.memberExpression(this._resultName, t.identifier(name));
  107. }
  108. }
  109. exports.default = ImportBuilder;