ntts
Version:
A CLI tool for refactoring an existing NodeJs application to a fully functional TypeScript application.
65 lines (64 loc) • 2.8 kB
JavaScript
;
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
var ts_morph_1 = require("ts-morph");
var ExportParser = /** @class */ (function () {
function ExportParser() {
}
var _a;
_a = ExportParser;
ExportParser.flatten = function (node, result) {
if (result === void 0) { result = []; }
if (ts_morph_1.Node.isPropertyAccessExpression(node)) {
return _a.flatten(node.getExpression(), __spreadArray([node.getNameNode()], result, true));
}
if (ts_morph_1.Node.isElementAccessExpression(node)) {
return _a.flatten(node.getExpression(), __spreadArray([null], result, true));
}
if (ts_morph_1.Node.isIdentifier(node)) {
return __spreadArray([node], result, true);
}
return result;
};
ExportParser.filterExportIdentifiers = function (identifiers) {
if (identifiers[0].getText() === 'module') {
return identifiers.slice(2);
}
return identifiers.slice(1);
};
ExportParser.getSourceFileIndex = function (node) {
var parent = node.getParent();
if (ts_morph_1.Node.isSourceFile(parent)) {
return node.getChildIndex();
}
return _a.getSourceFileIndex(node.getParentOrThrow());
};
ExportParser.exportVariableExists = function (variableName, exportedVariables, defaultExport) {
if (defaultExport) {
return exportedVariables.find(function (exported) { return !!exported.defaultExport; });
}
return exportedVariables
.find(function (exported) { return (!exported.alias && exported.name === variableName)
|| (!!exported.alias && exported.alias === variableName); });
};
ExportParser.getBaseExport = function (identifiers) {
var parent = identifiers[0].getParent();
return parent.asKindOrThrow(ts_morph_1.SyntaxKind.PropertyAccessExpression);
};
ExportParser.getElementAccessOrDefaultBaseExport = function (identifiers) {
if (identifiers.length === 1) {
return identifiers[0].getParent().asKind(ts_morph_1.SyntaxKind.ElementAccessExpression) || identifiers[0];
}
return identifiers[0].getParent().getParentIfKind(ts_morph_1.SyntaxKind.ElementAccessExpression) || _a.getBaseExport(identifiers);
};
return ExportParser;
}());
exports.default = ExportParser;