clasp-types
Version:
A d.ts generator for clasp projects
68 lines (67 loc) • 3.32 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
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 extendStatics(d, b);
};
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 });
exports.Interface = void 0;
var Builder_1 = require("./builders/Builder");
var Definition_1 = require("./Definition");
var Method_1 = require("./Method");
var Property_1 = require("./Property");
// Simple builder to capture type rendering as string
var StringBuilderCapture = /** @class */ (function (_super) {
__extends(StringBuilderCapture, _super);
function StringBuilderCapture() {
return _super !== null && _super.apply(this, arguments) || this;
}
StringBuilderCapture.prototype.build = function () {
return this;
};
return StringBuilderCapture;
}(Builder_1.Builder));
var Interface = /** @class */ (function (_super) {
__extends(Interface, _super);
function Interface(kind, depth) {
return _super.call(this, kind, depth) || this;
}
Interface.prototype.render = function (builder) {
var _this = this;
var methods = this.kind.children.filter(function (k) { return _this.kind.kindString === 'Interface' ? true : k.flags.isPublic; }).filter(function (k) { return k.kindString === 'Method' || k.kindString === 'Function'; }).map(function (k) { return new Method_1.Method(k, _this.tab()); });
var properties = this.kind.children.filter(function (k) { return _this.kind.kindString === 'Interface' ? true : k.flags.isPublic; }).filter(function (k) { return k.kindString === 'Property'; }).map(function (k) { return new Property_1.Property(k, _this.tab()); });
this.addComment(builder, this.kind.comment);
if (methods.length > 0 || properties.length > 0) {
var typeParams = this.buildTypeParameters();
builder.append(this.ident() + "export interface " + this.kind.name + typeParams + " {").doubleLine();
properties.forEach(function (p) { return p.render(builder); });
methods.forEach(function (m) { return m.render(builder); });
builder.append(this.ident() + "}").doubleLine();
}
};
Interface.prototype.buildTypeParameters = function () {
var _this = this;
if (!this.kind.typeParameter || this.kind.typeParameter.length === 0) {
return '';
}
var params = this.kind.typeParameter.map(function (param) {
if (param.type) {
var constraintBuilder = new StringBuilderCapture();
_this.buildType(constraintBuilder, param.type);
return param.name + " extends " + constraintBuilder.getText();
}
return param.name;
});
return "<" + params.join(', ') + ">";
};
return Interface;
}(Definition_1.Definition));
exports.Interface = Interface;