ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
54 lines (53 loc) • 2.77 kB
JavaScript
;
var __extends = (this && this.__extends)/* istanbul ignore next */ || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var errors = require("./../../errors");
var StructureToText_1 = require("./../StructureToText");
var ImportDeclarationStructureToText = /** @class */ (function (_super) {
__extends(ImportDeclarationStructureToText, _super);
function ImportDeclarationStructureToText() {
return _super !== null && _super.apply(this, arguments) || this;
}
ImportDeclarationStructureToText.prototype.writeText = function (structure) {
var hasNamedImport = structure.namedImports != null && structure.namedImports.length > 0;
// validation
if (hasNamedImport && structure.namespaceImport != null)
throw new errors.InvalidOperationError("An import declaration cannot have both a namespace import and a named import.");
this.writer.write("import");
// default import
if (structure.defaultImport != null) {
this.writer.write(" " + structure.defaultImport);
this.writer.conditionalWrite(hasNamedImport || structure.namespaceImport != null, ",");
}
// namespace import
if (structure.namespaceImport != null)
this.writer.write(" * as " + structure.namespaceImport);
// named imports
if (structure.namedImports != null && structure.namedImports.length > 0) {
this.writer.write(" {");
for (var i = 0; i < structure.namedImports.length; i++) {
var namedImport = structure.namedImports[i];
this.writer.conditionalWrite(i > 0, ", ");
this.writer.write(namedImport.name);
this.writer.conditionalWrite(namedImport.alias != null, " as " + namedImport.alias);
}
this.writer.write("}");
}
// from keyword
this.writer.conditionalWrite(structure.defaultImport != null || hasNamedImport || structure.namespaceImport != null, " from");
this.writer.write(" ");
this.writer.quote(structure.moduleSpecifier);
this.writer.write(";");
};
return ImportDeclarationStructureToText;
}(StructureToText_1.StructureToText));
exports.ImportDeclarationStructureToText = ImportDeclarationStructureToText;