ntts
Version:
A CLI tool for refactoring an existing NodeJs application to a fully functional TypeScript application.
72 lines (71 loc) • 3 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var ts_morph_1 = require("ts-morph");
var export_parser_1 = __importDefault(require("./export-parser"));
var ExportValidator = /** @class */ (function () {
function ExportValidator() {
}
var _a;
_a = ExportValidator;
ExportValidator.isExport = function (identifiers) { return _a.isDefaultExport(identifiers)
|| _a.isNamedExport(identifiers)
|| _a.isElementAccessExport(identifiers); };
ExportValidator.isExportAssigment = function (binary) {
var left = binary.getLeft().asKind(ts_morph_1.SyntaxKind.Identifier)
|| binary.getLeft().asKind(ts_morph_1.SyntaxKind.PropertyAccessExpression)
|| binary.getLeft().asKind(ts_morph_1.SyntaxKind.ElementAccessExpression);
if (!binary.getOperatorToken().asKind(ts_morph_1.SyntaxKind.EqualsToken) || !left) {
return undefined;
}
var identifiers = _a.isExport(export_parser_1.default.flatten(left));
if (identifiers && identifiers[0]) {
return identifiers;
}
return undefined;
};
ExportValidator.isNamedExport = function (identifiers) {
if (identifiers.length > 2
&& identifiers[0] && identifiers[0].getText() === 'module'
&& identifiers[1] && identifiers[1].getText() === 'exports'
&& identifiers[2]) {
return [identifiers[0], identifiers[1], identifiers[2]];
}
if (identifiers.length > 1
&& identifiers[0] && identifiers[0].getText() === 'exports'
&& identifiers[1]) {
return [identifiers[0], identifiers[1]];
}
return undefined;
};
ExportValidator.isDefaultExport = function (identifiers) {
if (identifiers.length === 2
&& identifiers[0] && identifiers[0].getText() === 'module'
&& identifiers[1] && identifiers[1].getText() === 'exports') {
return [identifiers[0], identifiers[1]];
}
if (identifiers.length === 1
&& identifiers[0] && identifiers[0].getText() === 'exports') {
return [identifiers[0]];
}
return undefined;
};
ExportValidator.isElementAccessExport = function (identifiers) {
if (identifiers.length > 2
&& identifiers[0] && identifiers[0].getText() === 'module'
&& identifiers[1] && identifiers[1].getText() === 'exports'
&& !identifiers[2]) {
return [identifiers[0], identifiers[1]];
}
if (identifiers.length > 1
&& identifiers[0] && identifiers[0].getText() === 'exports'
&& !identifiers[1]) {
return [identifiers[0]];
}
return undefined;
};
return ExportValidator;
}());
exports.default = ExportValidator;