dts2as
Version:
A command line tool that converts TypeScript definitions (d.ts files) to ActionScript 3 classes and interfaces
327 lines (323 loc) • 12.5 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (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 });
/*
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.
*/
var BuiltIns;
(function (BuiltIns) {
BuiltIns[BuiltIns["Object"] = 0] = "Object";
BuiltIns[BuiltIns["Array"] = 1] = "Array";
BuiltIns[BuiltIns["Number"] = 2] = "Number";
BuiltIns[BuiltIns["Boolean"] = 3] = "Boolean";
BuiltIns[BuiltIns["String"] = 4] = "String";
BuiltIns[BuiltIns["Function"] = 5] = "Function";
BuiltIns[BuiltIns["Class"] = 6] = "Class";
BuiltIns[BuiltIns["int"] = 7] = "int";
BuiltIns[BuiltIns["uint"] = 8] = "uint";
BuiltIns[BuiltIns["void"] = 9] = "void";
})(BuiltIns = exports.BuiltIns || (exports.BuiltIns = {}));
var AccessModifiers;
(function (AccessModifiers) {
AccessModifiers[AccessModifiers["public"] = 0] = "public";
AccessModifiers[AccessModifiers["protected"] = 1] = "protected";
AccessModifiers[AccessModifiers["private"] = 2] = "private";
AccessModifiers[AccessModifiers["internal"] = 3] = "internal";
AccessModifiers[AccessModifiers["AS3"] = 4] = "AS3";
})(AccessModifiers = exports.AccessModifiers || (exports.AccessModifiers = {}));
var ParameterDefinition = (function () {
function ParameterDefinition(name, type, value, isRest) {
this.name = name;
this.type = type;
this.value = value;
this.isRest = isRest;
}
return ParameterDefinition;
}());
exports.ParameterDefinition = ParameterDefinition;
var FunctionDefinition = (function () {
function FunctionDefinition(name, type, parameters) {
if (type === void 0) { type = null; }
if (parameters === void 0) { parameters = null; }
this.name = name;
this.type = type;
this.parameters = parameters;
}
return FunctionDefinition;
}());
exports.FunctionDefinition = FunctionDefinition;
var ConstructorDefinition = (function (_super) {
__extends(ConstructorDefinition, _super);
function ConstructorDefinition(name, type, parameters) {
if (type === void 0) { type = null; }
if (parameters === void 0) { parameters = null; }
return _super.call(this, name, type, parameters) || this;
}
return ConstructorDefinition;
}(FunctionDefinition));
exports.ConstructorDefinition = ConstructorDefinition;
var MethodDefinition = (function (_super) {
__extends(MethodDefinition, _super);
function MethodDefinition(name, type, parameters, accessLevel, isStatic) {
if (type === void 0) { type = null; }
if (parameters === void 0) { parameters = null; }
if (accessLevel === void 0) { accessLevel = null; }
if (isStatic === void 0) { isStatic = false; }
var _this = _super.call(this, name, type, parameters) || this;
_this.accessLevel = accessLevel;
_this.isStatic = isStatic;
return _this;
}
return MethodDefinition;
}(FunctionDefinition));
exports.MethodDefinition = MethodDefinition;
var PackageFunctionDefinition = (function (_super) {
__extends(PackageFunctionDefinition, _super);
function PackageFunctionDefinition(name, packageName, accessLevel, sourceFile, moduleName, external) {
var _this = _super.call(this, name) || this;
_this.packageName = packageName;
_this.accessLevel = accessLevel;
_this.sourceFile = sourceFile;
_this.moduleName = moduleName;
_this.external = external;
return _this;
}
PackageFunctionDefinition.prototype.getFullyQualifiedName = function () {
if (this.packageName) {
return this.packageName + "." + this.name;
}
return this.name;
};
return PackageFunctionDefinition;
}(MethodDefinition));
exports.PackageFunctionDefinition = PackageFunctionDefinition;
var PropertyDefinition = (function () {
function PropertyDefinition(name, accessLevel, type, isStatic, isConstant) {
if (type === void 0) { type = null; }
if (isStatic === void 0) { isStatic = false; }
if (isConstant === void 0) { isConstant = false; }
this.name = name;
this.accessLevel = accessLevel;
this.type = type;
this.isStatic = isStatic;
this.isConstant = isConstant;
}
return PropertyDefinition;
}());
exports.PropertyDefinition = PropertyDefinition;
var NamespaceDefinition = (function () {
function NamespaceDefinition(name, packageName, accessLevel, uri, sourceFile, moduleName, external) {
this.name = name;
this.packageName = packageName;
this.accessLevel = accessLevel;
this.uri = uri;
this.sourceFile = sourceFile;
this.moduleName = moduleName;
this.external = external;
}
NamespaceDefinition.prototype.getFullyQualifiedName = function () {
if (this.packageName) {
return this.packageName + "." + this.name;
}
return this.name;
};
return NamespaceDefinition;
}());
exports.NamespaceDefinition = NamespaceDefinition;
var PackageVariableDefinition = (function (_super) {
__extends(PackageVariableDefinition, _super);
function PackageVariableDefinition(name, packageName, accessLevel, sourceFile, moduleName, external) {
var _this = _super.call(this, name, accessLevel) || this;
_this.packageName = packageName;
_this.sourceFile = sourceFile;
_this.moduleName = moduleName;
_this.external = external;
return _this;
}
PackageVariableDefinition.prototype.getFullyQualifiedName = function () {
if (this.packageName) {
return this.packageName + "." + this.name;
}
return this.name;
};
return PackageVariableDefinition;
}(PropertyDefinition));
exports.PackageVariableDefinition = PackageVariableDefinition;
var TypeDefinition = (function () {
function TypeDefinition(name, packageName, accessLevel, sourceFile, moduleName, external) {
this.name = name;
this.packageName = packageName;
this.accessLevel = accessLevel;
this.sourceFile = sourceFile;
this.moduleName = moduleName;
this.external = external;
this.properties = [];
this.methods = [];
}
TypeDefinition.prototype.getFullyQualifiedName = function () {
if (this.packageName) {
return this.packageName + "." + this.name;
}
return this.name;
};
TypeDefinition.prototype.getProperty = function (name, isStatic) {
for (var _i = 0, _a = this.properties; _i < _a.length; _i++) {
var property = _a[_i];
if (property.name === name) {
if (isStatic === undefined || property.isStatic === isStatic) {
return property;
}
}
}
return null;
};
TypeDefinition.prototype.getMethod = function (name, isStatic) {
for (var _i = 0, _a = this.methods; _i < _a.length; _i++) {
var method = _a[_i];
if (method.name === name) {
if (isStatic === undefined || method.isStatic === isStatic) {
return method;
}
}
}
return null;
};
return TypeDefinition;
}());
exports.TypeDefinition = TypeDefinition;
var InterfaceDefinition = (function (_super) {
__extends(InterfaceDefinition, _super);
function InterfaceDefinition(name, packageName, accessLevel, sourceFile, moduleName, external) {
var _this = _super.call(this, name, packageName, accessLevel, sourceFile, moduleName, external) || this;
_this.interfaces = [];
return _this;
}
return InterfaceDefinition;
}(TypeDefinition));
exports.InterfaceDefinition = InterfaceDefinition;
var ClassDefinition = (function (_super) {
__extends(ClassDefinition, _super);
function ClassDefinition(name, packageName, accessLevel, sourceFile, moduleName, external) {
var _this = _super.call(this, name, packageName, accessLevel, sourceFile, moduleName, external) || this;
_this.superClass = null;
_this.interfaces = [];
_this.constructorMethod = null;
_this.dynamic = false;
return _this;
}
return ClassDefinition;
}(TypeDefinition));
exports.ClassDefinition = ClassDefinition;
function getDefinitionByName(name, types) {
for (var _i = 0, types_1 = types; _i < types_1.length; _i++) {
var as3Type = types_1[_i];
if (as3Type.getFullyQualifiedName() === name) {
return as3Type;
}
}
return null;
}
exports.getDefinitionByName = getDefinitionByName;
var KEYWORD_NULL = "null";
var AS3_RETURN_VALUE_MAP = {};
AS3_RETURN_VALUE_MAP[BuiltIns[BuiltIns.Number]] = "0";
AS3_RETURN_VALUE_MAP[BuiltIns[BuiltIns.Boolean]] = "false";
function getDefaultReturnValueForType(type) {
var fullyQualifiedName = type.getFullyQualifiedName();
if (AS3_RETURN_VALUE_MAP.hasOwnProperty(fullyQualifiedName)) {
return AS3_RETURN_VALUE_MAP[fullyQualifiedName];
}
return KEYWORD_NULL;
}
exports.getDefaultReturnValueForType = getDefaultReturnValueForType;
function requiresImport(target, scope) {
if (!target) {
return false;
}
var packageName = target.packageName;
if (!packageName) {
return false;
}
var name = target.name;
if (packageName === scope.packageName) {
return false;
}
return true;
}
exports.requiresImport = requiresImport;
function requiresInterfaceOverride(target, scope) {
var memberName = target.name;
var interfaces = scope.interfaces;
var needsOverride = false;
var interfaceCount = interfaces.length;
for (var i = 0; i < interfaceCount; i++) {
var as3Interface = interfaces[i];
if (target instanceof MethodDefinition) {
needsOverride = as3Interface.getMethod(memberName) !== null;
}
else if (target instanceof PropertyDefinition) {
needsOverride = as3Interface.getProperty(memberName) !== null;
}
if (needsOverride) {
return true;
}
needsOverride = requiresInterfaceOverride(target, as3Interface);
if (needsOverride) {
return true;
}
}
return false;
}
exports.requiresInterfaceOverride = requiresInterfaceOverride;
function requiresClassOverride(target, scope) {
var memberName = target.name;
var superClass = scope.superClass;
var needsOverride = false;
while (needsOverride === false && superClass !== null) {
if (target instanceof MethodDefinition) {
needsOverride = superClass.getMethod(memberName) !== null;
}
else if (target instanceof PropertyDefinition) {
needsOverride = superClass.getProperty(memberName) !== null;
}
superClass = superClass.superClass;
}
return needsOverride;
}
exports.requiresClassOverride = requiresClassOverride;
function getCommonBaseClass(class1, class2) {
var savedClass2 = class2;
do {
var class1Name = class1.getFullyQualifiedName();
do {
var class2Name = class2.getFullyQualifiedName();
if (class1Name === class2Name) {
return class1;
}
class2 = class2.superClass;
} while (class2);
class2 = savedClass2;
class1 = class1.superClass;
} while (class1);
//no common base class, so it's best to default to Object
return null;
}
exports.getCommonBaseClass = getCommonBaseClass;