eslint-plugin-codelyzer
Version:
133 lines (132 loc) • 5.75 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var AngularClassDecorators;
(function (AngularClassDecorators) {
AngularClassDecorators["Component"] = "Component";
AngularClassDecorators["Directive"] = "Directive";
AngularClassDecorators["Injectable"] = "Injectable";
AngularClassDecorators["NgModule"] = "NgModule";
AngularClassDecorators["Pipe"] = "Pipe";
})(AngularClassDecorators = exports.AngularClassDecorators || (exports.AngularClassDecorators = {}));
var AngularConstructorParameterDecorators;
(function (AngularConstructorParameterDecorators) {
AngularConstructorParameterDecorators["Attribute"] = "Attribute";
AngularConstructorParameterDecorators["Host"] = "Host";
AngularConstructorParameterDecorators["Inject"] = "Inject";
AngularConstructorParameterDecorators["Optional"] = "Optional";
AngularConstructorParameterDecorators["Self"] = "Self";
AngularConstructorParameterDecorators["SkipSelf"] = "SkipSelf";
})(AngularConstructorParameterDecorators || (AngularConstructorParameterDecorators = {}));
var AngularMethodDecorators;
(function (AngularMethodDecorators) {
AngularMethodDecorators["HostListener"] = "HostListener";
})(AngularMethodDecorators || (AngularMethodDecorators = {}));
var AngularPropertyAccessorDecorators;
(function (AngularPropertyAccessorDecorators) {
AngularPropertyAccessorDecorators["ContentChild"] = "ContentChild";
AngularPropertyAccessorDecorators["ContentChildren"] = "ContentChildren";
AngularPropertyAccessorDecorators["HostBinding"] = "HostBinding";
AngularPropertyAccessorDecorators["Input"] = "Input";
AngularPropertyAccessorDecorators["Output"] = "Output";
AngularPropertyAccessorDecorators["ViewChild"] = "ViewChild";
AngularPropertyAccessorDecorators["ViewChildren"] = "ViewChildren";
})(AngularPropertyAccessorDecorators || (AngularPropertyAccessorDecorators = {}));
exports.AngularInnerClassDecorators = __assign({}, AngularConstructorParameterDecorators, AngularMethodDecorators, AngularPropertyAccessorDecorators);
function isCallExpression(node) {
return node.type === 'CallExpression';
}
function isIdentifier(node) {
return node.type === 'Identifier';
}
function isMemberExpression(node) {
return node.type === 'MemberExpression';
}
function isClassDeclaration(node) {
return node.type === 'ClassDeclaration';
}
function isObjectExpression(node) {
return node.type === 'ObjectExpression';
}
function isProperty(node) {
return node.type === 'Property';
}
exports.getClassName = function (node) {
if (isClassDeclaration(node)) {
return node.id ? node.id.name : undefined;
}
if (node.parent) {
return exports.getClassName(node.parent);
}
return undefined;
};
exports.getDecorator = function (node, decoratorName) {
if (!node.decorators) {
return undefined;
}
return node.decorators.find(function (decorator) {
return isCallExpression(decorator.expression) &&
decorator.expression.arguments &&
decorator.expression.arguments.length > 0 &&
exports.getDecoratorName(decorator) === decoratorName;
});
};
exports.getDecoratorArgument = function (decorator) {
var expression = decorator.expression;
if (!isCallExpression(expression) || !expression.arguments || expression.arguments.length === 0) {
return undefined;
}
var arg = expression.arguments[0];
return isObjectExpression(arg) && arg.properties ? arg : undefined;
};
exports.getDecoratorName = function (decorator) {
var expression = decorator.expression;
if (isIdentifier(expression))
return expression.name;
if (isCallExpression(expression) && isIdentifier(expression.callee)) {
return expression.callee.name;
}
return undefined;
};
exports.getPipeDecorator = function (node) { return exports.getDecorator(node, 'Pipe'); };
exports.getSymbolName = function (expression) {
var childExpression = expression.expression;
return isMemberExpression(childExpression) ? childExpression.property.name : childExpression.name;
};
exports.getDeclaredInterfaces = function (node) {
return node.implements || [];
};
exports.getDeclaredInterfaceNames = function (node) { return exports.getDeclaredInterfaces(node).map(exports.getSymbolName); };
exports.getDeclaredInterfaceName = function (node, value) {
return exports.getDeclaredInterfaceNames(node).find(function (interfaceName) { return interfaceName === value; });
};
function getClassPropertyName(classProperty) {
if (classProperty.key.type === 'Identifier') {
return classProperty.key.name;
}
if (classProperty.key.type === 'Literal') {
return classProperty.key.raw;
}
throw new Error("Unexpected \"ClassProperty.key.type\" provided: " + classProperty.key.type);
}
exports.getClassPropertyName = getClassPropertyName;
exports.getDecoratorPropertyValue = function (decorator, name) {
var arg = exports.getDecoratorArgument(decorator);
if (!arg || !isObjectExpression(arg))
return undefined;
var properties = arg.properties;
var property = properties.find(function (prop) { return !!(prop.key && isIdentifier(prop.key) && prop.key.name === name); });
if (!property || !isProperty(property))
return undefined;
return property.value;
};