tslint-immutable
Version:
TSLint rules to disable mutation in TypeScript.
95 lines • 3.88 kB
JavaScript
/**
* This file has code that is shared for all the ignore options.
*/
;
Object.defineProperty(exports, "__esModule", { value: true });
var ts = require("typescript");
function checkNodeWithIgnore(checkNode) {
return function (node, ctx) {
// Skip checking in functions if ignore-local is set
if (ctx.options.ignoreLocal &&
(node.kind === ts.SyntaxKind.FunctionDeclaration ||
node.kind === ts.SyntaxKind.ArrowFunction ||
node.kind === ts.SyntaxKind.FunctionExpression ||
node.kind === ts.SyntaxKind.MethodDeclaration)) {
// We still need to check the parameters and return type
var functionNode = node; //tslint:disable-line
var invalidNodes = checkIgnoreLocalFunctionNode(functionNode, ctx, checkNode);
// Now skip this whole branch
return { invalidNodes: invalidNodes, skipChildren: true };
}
// Skip checking in classes/interfaces if ignore-class/ignore-interface is set
if ((ctx.options.ignoreClass &&
node.kind === ts.SyntaxKind.PropertyDeclaration) ||
(ctx.options.ignoreInterface &&
node.kind === ts.SyntaxKind.PropertySignature)) {
// Now skip this whole branch
return { invalidNodes: [], skipChildren: true };
}
// Forward to check node
return checkNode(node, ctx);
};
}
exports.checkNodeWithIgnore = checkNodeWithIgnore;
function checkIgnoreLocalFunctionNode(functionNode, ctx, checkNode) {
var myInvalidNodes = [];
// Check either the parameter's explicit type if it has one, or itself for implict type
for (var _i = 0, _a = functionNode.parameters.map(function (p) { return (p.type ? p.type : p); }); _i < _a.length; _i++) {
var n = _a[_i];
var invalidCheckNodes = checkNode(n, ctx).invalidNodes;
if (invalidCheckNodes) {
myInvalidNodes = myInvalidNodes.concat.apply(myInvalidNodes, invalidCheckNodes);
}
}
// Check the return type
if (functionNode.type) {
var invalidCheckNodes = checkNode(functionNode.type, ctx).invalidNodes;
if (invalidCheckNodes) {
myInvalidNodes = myInvalidNodes.concat.apply(myInvalidNodes, invalidCheckNodes);
}
}
return myInvalidNodes;
}
function shouldIgnorePrefix(node, options, sourceFile) {
// Check ignore-prefix for VariableDeclaration, PropertySignature, TypeAliasDeclaration, Parameter
if (options.ignorePrefix) {
if (node &&
(node.kind === ts.SyntaxKind.VariableDeclaration ||
node.kind === ts.SyntaxKind.Parameter ||
node.kind === ts.SyntaxKind.PropertySignature ||
node.kind === ts.SyntaxKind.PropertyDeclaration ||
node.kind === ts.SyntaxKind.TypeAliasDeclaration)) {
var variableDeclarationNode = node;
var variableText = variableDeclarationNode.name.getText(sourceFile);
// if (
// variableText.substr(0, options.ignorePrefix.length) ===
// options.ignorePrefix
// ) {
// return true;
// }
if (isIgnoredPrefix(variableText, options.ignorePrefix)) {
return true;
}
}
}
return false;
}
exports.shouldIgnorePrefix = shouldIgnorePrefix;
function isIgnoredPrefix(text, ignorePrefix) {
if (!ignorePrefix) {
return false;
}
if (Array.isArray(ignorePrefix)) {
if (ignorePrefix.find(function (pfx) { return text.indexOf(pfx) === 0; })) {
return true;
}
}
else {
if (text.indexOf(ignorePrefix) === 0) {
return true;
}
}
return false;
}
exports.isIgnoredPrefix = isIgnoredPrefix;
//# sourceMappingURL=ignore.js.map