ts-budgie
Version:
Converts TypeScript code to Budgie.
141 lines • 7.9 kB
JavaScript
"use strict";
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 __());
};
})();
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var budgie_1 = require("budgie");
var tsutils = require("tsutils");
var ts = require("typescript");
var budgieLine_1 = require("../../../output/budgieLine");
var transformation_1 = require("../../../output/transformation");
var propertyAccessChecker_1 = require("./propertyAccessChecker");
var MemberOrStaticFunctionChecker = /** @class */ (function (_super) {
__extends(MemberOrStaticFunctionChecker, _super);
function MemberOrStaticFunctionChecker() {
return _super !== null && _super.apply(this, arguments) || this;
}
MemberOrStaticFunctionChecker.prototype.visit = function (node) {
var _this = this;
if (node.parent === undefined || !ts.isCallExpression(node.parent)) {
return undefined;
}
// Handles the edge case of new Class().method
if (ts.isCallExpression(node.expression) || ts.isNewExpression(node.expression)) {
return this.handleCallOrNewExpression(node, node.expression, node.parent);
}
var hostContainerAndSignature = this.getHostContainerAndSignature(node);
if (hostContainerAndSignature === undefined) {
return undefined;
}
var commandName = hostContainerAndSignature.commandName, hostSignature = hostContainerAndSignature.hostSignature;
if (hostSignature.declarations === undefined) {
return undefined;
}
var caller = this.router.recurseIntoValue(node.expression);
var args = node.parent.arguments.map(function (arg) { return (arg === node ? node.name.text : _this.router.recurseIntoValue(arg)); });
var hostDeclaration = hostSignature.declarations[0];
var privacy = this.aliaser.getFriendlyPrivacyName(hostDeclaration);
var functionNameSplit = this.nameSplitter.split(node.name.getText(this.sourceFile));
var functionName = this.casing.convertToCase(budgie_1.CaseStyle.PascalCase, functionNameSplit);
return [transformation_1.Transformation.fromNode(node, this.sourceFile, [new (budgieLine_1.BudgieLine.bind.apply(budgieLine_1.BudgieLine, __spreadArrays([void 0, commandName, privacy, caller, functionName], args)))()])];
};
/**
* Handles the edges case of:
* methodCall().method()
* new Class().method()
*/
MemberOrStaticFunctionChecker.prototype.handleCallOrNewExpression = function (node, expression, parent) {
var _this = this;
var newBudgieLineCall = this.router.recurseIntoValue(expression);
var args = parent.arguments.map(function (arg) { return (arg === node ? node.name.text : _this.router.recurseIntoValue(arg)); });
var functionNameSplit = this.nameSplitter.split(node.name.getText(this.sourceFile));
var functionName = this.casing.convertToCase(budgie_1.CaseStyle.PascalCase, functionNameSplit);
return [
transformation_1.Transformation.fromNode(node, this.sourceFile, [
new (budgieLine_1.BudgieLine.bind.apply(budgieLine_1.BudgieLine, __spreadArrays([void 0, budgie_1.CommandNames.MemberFunction, budgie_1.KeywordNames.Public, newBudgieLineCall, functionName], args)))(),
]),
];
};
MemberOrStaticFunctionChecker.prototype.getHostContainerAndSignature = function (node) {
var direct = this.getHostContainerAndSignatureOfPropertyAccess(node.expression, node.name);
if (direct !== undefined) {
return direct;
}
if (ts.isIdentifier(node.expression)) {
return this.getHostContainerAndSignatureOfPropertyAccess(node.expression, node.name);
}
if (ts.isPropertyAccessExpression(node.expression)) {
return this.getHostContainerAndSignatureOfPropertyAccess(node.expression.expression, node.expression.name);
}
if (ts.isCallExpression(node.expression) && ts.isPropertyAccessExpression(node.expression.expression)) {
return this.getHostContainerAndSignatureOfPropertyAccess(node.expression.expression.expression, node.expression.expression.name);
}
return undefined;
};
MemberOrStaticFunctionChecker.prototype.getHostContainerAndSignatureOfPropertyAccess = function (expression, name) {
var nameSymbol = this.typeChecker.getSymbolAtLocation(name);
if (nameSymbol === undefined) {
return undefined;
}
var classSymbol = this.getClassSymbol(expression, nameSymbol);
if (classSymbol === undefined) {
return undefined;
}
var escapedName = nameSymbol.escapedName;
// If the class was imported from another file, this might be necessary to get the real type
var declaredClassSymbol = this.typeChecker.getDeclaredTypeOfSymbol(classSymbol).symbol;
var trueClassSymbol = declaredClassSymbol === undefined ? classSymbol : declaredClassSymbol;
// Protected properties are only listed as augmented properties (not in .members)
var expressionType = this.typeChecker.getTypeAtLocation(expression);
var classProperties = this.typeChecker.getAugmentedPropertiesOfType(expressionType);
for (var _i = 0, classProperties_1 = classProperties; _i < classProperties_1.length; _i++) {
var classProperty = classProperties_1[_i];
if (classProperty.escapedName !== escapedName) {
continue;
}
if (classProperty.valueDeclaration === undefined) {
return undefined;
}
return {
commandName: tsutils.hasModifier(classProperty.valueDeclaration.modifiers, ts.SyntaxKind.StaticKeyword)
? budgie_1.CommandNames.StaticFunction
: budgie_1.CommandNames.MemberFunction,
hostSignature: classProperty,
trueClassSymbol: trueClassSymbol,
};
}
return undefined;
};
MemberOrStaticFunctionChecker.prototype.getClassSymbol = function (expression, nameSymbol) {
// If the expression is a direct class usage, this will normally work
var direct = this.typeChecker.getSymbolAtLocation(expression);
if (direct !== undefined) {
return direct;
}
// Otherwise, we'll have to try to parse through the function's declarations
if (nameSymbol.valueDeclaration === undefined || nameSymbol.valueDeclaration.parent === undefined) {
return undefined;
}
return this.typeChecker.getTypeAtLocation(nameSymbol.valueDeclaration.parent).symbol;
};
return MemberOrStaticFunctionChecker;
}(propertyAccessChecker_1.PropertyAccessChecker));
exports.MemberOrStaticFunctionChecker = MemberOrStaticFunctionChecker;
//# sourceMappingURL=memberOrStaticFunctionChecker.js.map