FileState.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
  2. if (kind === "m") throw new TypeError("Private method is not writable");
  3. if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
  4. if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
  5. return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
  6. };
  7. var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
  8. if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
  9. if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
  10. return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
  11. };
  12. var _FileState_importer;
  13. import babelTraverse, { NodePath } from '@babel/traverse';
  14. import babelParse from './babelParser.js';
  15. // Workaround while babel is not a proper ES module
  16. const traverse = babelTraverse.default ?? babelTraverse;
  17. class FileState {
  18. constructor(options, { code, ast, importer }) {
  19. _FileState_importer.set(this, void 0);
  20. this.hub = {
  21. // keep it for the usage in babel-core, ex: path.hub.file.opts.filename
  22. file: this,
  23. parse: this.parse.bind(this),
  24. import: this.import.bind(this),
  25. getCode: () => this.code,
  26. getScope: () => this.scope,
  27. addHelper: () => undefined,
  28. buildError: (node, msg, Error) => {
  29. const err = new Error(msg);
  30. err.node = node;
  31. return err;
  32. },
  33. };
  34. this.opts = options;
  35. this.code = code;
  36. this.ast = ast;
  37. __classPrivateFieldSet(this, _FileState_importer, importer, "f");
  38. this.path = NodePath.get({
  39. hub: this.hub,
  40. parentPath: null,
  41. parent: this.ast,
  42. container: this.ast,
  43. key: 'program',
  44. }).setContext();
  45. this.scope = this.path.scope;
  46. }
  47. /**
  48. * Try to resolve and import the ImportPath with the `name`
  49. */
  50. import(path, name) {
  51. return __classPrivateFieldGet(this, _FileState_importer, "f").call(this, path, name, this);
  52. }
  53. /**
  54. * Parse the content of a new file
  55. * The `filename` is required so that potential imports inside the content can be correctly resolved and
  56. * the correct babel config file could be loaded. `filename` needs to be an absolute path.
  57. */
  58. parse(code, filename) {
  59. const newOptions = { ...this.opts, filename };
  60. // We need to build a new parser, because there might be a new
  61. // babel config file in effect, so we need to load it
  62. const ast = babelParse(code, newOptions);
  63. return new FileState(newOptions, {
  64. ast,
  65. code,
  66. importer: __classPrivateFieldGet(this, _FileState_importer, "f"),
  67. });
  68. }
  69. /**
  70. * Traverse the current file
  71. */
  72. traverse(visitors, state) {
  73. traverse(this.ast, visitors, this.scope, state);
  74. }
  75. }
  76. _FileState_importer = new WeakMap();
  77. export default FileState;