import-builder.js 5.0 KB

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