@carlosv2/glue
Version:
Dependency injection library that stays out of the way
36 lines (35 loc) • 1.19 kB
JavaScript
import ts from 'typescript';
import { Compilable } from '../compilable.js';
import { isSymbol } from '../utils.js';
import { DiError } from '../error.js';
const { factory } = ts;
const moduleSymbol = Symbol();
const defaultSymbol = Symbol();
export class Importation extends Compilable {
constructor(path, symbol) {
super();
this.path = path;
this.symbol = symbol;
}
compile(importer) {
if (this.symbol === moduleSymbol) {
return factory.createIdentifier(importer.module(this.path));
}
else if (this.symbol === defaultSymbol) {
return factory.createIdentifier(importer.default(this.path));
}
else if (!isSymbol(this.symbol)) {
return factory.createIdentifier(importer.symbol(this.path, this.symbol));
}
throw new DiError(`Unable to determine the import declaration for ${String(this.symbol)}`);
}
static module(path) {
return new Importation(path, moduleSymbol);
}
static default(path) {
return new Importation(path, defaultSymbol);
}
static named(path, symbol) {
return new Importation(path, symbol);
}
}