UNPKG

dts2as

Version:

A command line tool that converts TypeScript definitions (d.ts files) to ActionScript 3 classes and interfaces

340 lines (336 loc) 13.8 kB
"use strict"; /* Copyright 2015-2017 Bowler Hat LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); var as3 = require("./as3"); var NEW_LINE = "\n"; var AS_TO_EXTERNS_TYPE_MAP = {}; AS_TO_EXTERNS_TYPE_MAP[as3.BuiltIns[as3.BuiltIns.Number]] = "number"; AS_TO_EXTERNS_TYPE_MAP[as3.BuiltIns[as3.BuiltIns.Boolean]] = "boolean"; AS_TO_EXTERNS_TYPE_MAP[as3.BuiltIns[as3.BuiltIns.String]] = "string"; var default_1 = (function () { function default_1(types) { this._types = types; } default_1.prototype.emitFileHeader = function () { var header = "/**" + NEW_LINE; header += " * @fileoverview Externs generated by dts2as from NextGenActionScript.com" + NEW_LINE; header += " * @externs" + NEW_LINE; header += " * @suppress {duplicate}" + NEW_LINE; header += " */" + NEW_LINE + NEW_LINE; return header; }; default_1.prototype.emitPackages = function () { var _this = this; var packages = []; var packagesOutput = ""; this._types.forEach(function (as3Type) { if (as3Type.external) { return; } var packageName = as3Type.packageName; if (packageName.length > 0) { var packageParts = packageName.split("."); while (packageParts.length > 0) { var currentPackage = packageParts.join("."); packageParts.pop(); var existingType = as3.getDefinitionByName(currentPackage, _this._types); if (existingType || packages.indexOf(currentPackage) >= 0) { continue; } packages.push(currentPackage); var packageOutput = "/**" + NEW_LINE; packageOutput += " * @type {Object}" + NEW_LINE; packageOutput += " */" + NEW_LINE; if (packageParts.length === 0) { packageOutput += "var "; } packageOutput += currentPackage + " = {};" + NEW_LINE + NEW_LINE; packagesOutput += packageOutput; } } }); return packagesOutput; }; default_1.prototype.emitClass = function (as3Class) { var _this = this; var className = as3Class.getFullyQualifiedName(); var superClass = as3Class.superClass; var interfaces = as3Class.interfaces; var constructorMethod = this.getConstructorMethod(as3Class); var classOutput = "/**" + NEW_LINE; classOutput += " * @constructor" + NEW_LINE; if (superClass) { classOutput += " * @extends {"; classOutput += this.getNameToEmit(superClass, as3Class); classOutput += "}" + NEW_LINE; } if (interfaces.length > 0) { interfaces.forEach(function (otherInterface, index) { classOutput += " * @implements {"; classOutput += _this.getNameToEmit(otherInterface, as3Class); classOutput += "}" + NEW_LINE; }); } if (constructorMethod) { classOutput += this.emitParameterDocs(constructorMethod, as3Class); } classOutput += " */" + NEW_LINE; if (className === as3Class.name) { classOutput += "var "; } classOutput += className; classOutput += " = function"; if (constructorMethod) { classOutput += this.emitParameters(constructorMethod, as3Class); } else { classOutput += "()"; } classOutput += " {}" + NEW_LINE; classOutput += NEW_LINE; //static properties and methods first as3Class.properties.forEach(function (property) { if (!property.isStatic) { return; } classOutput += _this.emitProperty(property, as3Class); classOutput += NEW_LINE + NEW_LINE; }); as3Class.methods.forEach(function (method) { if (!method.isStatic) { return; } classOutput += _this.emitMethod(method, as3Class); classOutput += NEW_LINE + NEW_LINE; }); as3Class.properties.forEach(function (property) { if (property.isStatic || as3.requiresClassOverride(property, as3Class)) { return; } classOutput += _this.emitProperty(property, as3Class); classOutput += NEW_LINE + NEW_LINE; }); as3Class.methods.forEach(function (method) { if (method.isStatic || as3.requiresClassOverride(method, as3Class)) { return; } classOutput += _this.emitMethod(method, as3Class); classOutput += NEW_LINE + NEW_LINE; }); return classOutput; }; default_1.prototype.emitInterface = function (as3Interface) { var _this = this; var interfaceName = as3Interface.getFullyQualifiedName(); var interfaces = as3Interface.interfaces; var interfaceOutput = "/**" + NEW_LINE; interfaceOutput += " * @interface" + NEW_LINE; if (interfaces.length > 0) { interfaces.forEach(function (otherInterface, index) { interfaceOutput += " * @extends {"; interfaceOutput += _this.getNameToEmit(otherInterface, as3Interface); interfaceOutput += "}" + NEW_LINE; }); } interfaceOutput += " */" + NEW_LINE; if (interfaceName === as3Interface.name) { interfaceOutput += "var "; } interfaceOutput += interfaceName; interfaceOutput += " = function() {};"; interfaceOutput += NEW_LINE; as3Interface.properties.forEach(function (property) { if (as3.requiresInterfaceOverride(property, as3Interface)) { return; } interfaceOutput += _this.emitProperty(property, as3Interface); interfaceOutput += NEW_LINE + NEW_LINE; }); as3Interface.methods.forEach(function (method) { if (as3.requiresInterfaceOverride(method, as3Interface)) { return; } interfaceOutput += _this.emitMethod(method, as3Interface); interfaceOutput += NEW_LINE + NEW_LINE; }); return interfaceOutput; }; default_1.prototype.emitPackageFunction = function (as3PackageFunction) { return this.emitMethod(as3PackageFunction, as3PackageFunction) + NEW_LINE + NEW_LINE; }; default_1.prototype.emitPackageVariable = function (as3PackageVariable) { return this.emitVariable(as3PackageVariable, as3PackageVariable) + NEW_LINE + NEW_LINE; }; default_1.prototype.getNameToEmit = function (target, scope) { if (!target) { return "*"; } var fullyQualifiedName = target.getFullyQualifiedName(); if (fullyQualifiedName in AS_TO_EXTERNS_TYPE_MAP) { return AS_TO_EXTERNS_TYPE_MAP[fullyQualifiedName]; } return fullyQualifiedName; }; default_1.prototype.addMemberImports = function (as3Type, imports) { var _this = this; as3Type.properties.forEach(function (as3Property) { _this.addPropertyImport(as3Property, as3Type, imports); }); as3Type.methods.forEach(function (as3Method) { _this.addMethodImport(as3Method, as3Type, imports); }); }; default_1.prototype.addPropertyImport = function (as3Property, as3Type, imports) { var propertyType = as3Property.type; if (!as3.requiresImport(propertyType, as3Type)) { return; } imports.push(propertyType.getFullyQualifiedName()); }; default_1.prototype.addMethodImport = function (as3Method, as3Type, imports) { var methodType = as3Method.type; if (as3.requiresImport(methodType, as3Type)) { imports.push(methodType.getFullyQualifiedName()); } this.addParametersImports(as3Method, as3Type, imports); }; default_1.prototype.addParametersImports = function (as3Function, as3Type, imports) { as3Function.parameters.forEach(function (parameter) { var parameterType = parameter.type; if (as3.requiresImport(parameterType, as3Type)) { imports.push(parameterType.getFullyQualifiedName()); } }); }; default_1.prototype.emitMethod = function (as3Method, scope) { var scopeFullyQualifiedName = scope.getFullyQualifiedName(); var methodName = as3Method.name; var methodType = as3Method.type; var isStatic = as3Method.isStatic; var methodOutput = "/**" + NEW_LINE; methodOutput += this.emitParameterDocs(as3Method, scope); if (methodType.name !== as3.BuiltIns[as3.BuiltIns.void]) { methodOutput += " * @return {" + this.getNameToEmit(methodType, scope) + "}" + NEW_LINE; } methodOutput += " */" + NEW_LINE; if (as3Method instanceof as3.PackageFunctionDefinition && !as3Method.packageName) { methodOutput += "function "; methodOutput += scopeFullyQualifiedName; } else { methodOutput += scopeFullyQualifiedName; if ("methods" in scope) { if (isStatic) { methodOutput += "."; } else { methodOutput += ".prototype."; } methodOutput += methodName; } methodOutput += " = function"; } methodOutput += this.emitParameters(as3Method, scope); methodOutput += " {};"; return methodOutput; }; default_1.prototype.emitProperty = function (as3Property, scope) { var scopeFullyQualifiedName = scope.getFullyQualifiedName(); var propertyName = as3Property.name; var propertyType = as3Property.type; var isStatic = as3Property.isStatic; var propertyOutput = "/**" + NEW_LINE; propertyOutput += " * @type {" + this.getNameToEmit(propertyType, scope) + "}" + NEW_LINE; propertyOutput += " */" + NEW_LINE; propertyOutput += scopeFullyQualifiedName; if (isStatic) { propertyOutput += "."; } else { propertyOutput += ".prototype."; } propertyOutput += propertyName; propertyOutput += ";"; return propertyOutput; }; default_1.prototype.emitVariable = function (as3Property, scope) { var propertyName = as3Property.name; var propertyType = as3Property.type; var propertyOutput = "/**" + NEW_LINE; propertyOutput += " * @type {" + this.getNameToEmit(propertyType, scope) + "}" + NEW_LINE; propertyOutput += " */" + NEW_LINE; propertyOutput += "var "; propertyOutput += propertyName; propertyOutput += ";"; return propertyOutput; }; default_1.prototype.emitParameterDocs = function (as3Function, scope) { var parameters = as3Function.parameters; var parametersOutput = ""; if (parameters) { for (var i = 0, count = parameters.length; i < count; i++) { var parameter = parameters[i]; parametersOutput += " * @param {"; if (parameter.isRest) { parametersOutput += "...Object"; } else { var parameterType = parameter.type; if (parameterType) { parametersOutput += this.getNameToEmit(parameterType, scope); } else { parametersOutput += "?"; } if (parameter.value !== null) { parametersOutput += "="; } } parametersOutput += "} "; parametersOutput += parameter.name; parametersOutput += NEW_LINE; } } return parametersOutput; }; default_1.prototype.emitParameters = function (as3Function, scope) { var parameters = as3Function.parameters; var signatureOutput = "("; if (parameters) { for (var i = 0, count = parameters.length; i < count; i++) { var parameter = parameters[i]; if (i > 0) { signatureOutput += ", "; } signatureOutput += parameter.name; } } signatureOutput += ")"; return signatureOutput; }; default_1.prototype.getConstructorMethod = function (as3Class) { var constructorMethod = as3Class.constructorMethod; if (constructorMethod) { return constructorMethod; } var superClass = as3Class.superClass; if (superClass) { return this.getConstructorMethod(superClass); } return null; }; return default_1; }()); exports.default = default_1;