tslint-immutable
Version:
TSLint rules to disable mutation in TypeScript.
79 lines • 3.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var ts = require("typescript");
var Lint = require("tslint");
var Ignore = require("./shared/ignore");
var check_node_1 = require("./shared/check-node");
// tslint:disable-next-line:variable-name
exports.Rule = check_node_1.createCheckNodeRule(Ignore.checkNodeWithIgnore(checkNode), "Only ReadonlyArray allowed.");
function checkNode(node, ctx) {
var explicitTypeFailures = checkArrayTypeOrReference(node, ctx);
var implicitTypeFailures = checkVariableOrParameterImplicitType(node, ctx);
return { invalidNodes: explicitTypeFailures.concat(implicitTypeFailures) };
}
function checkArrayTypeOrReference(node, ctx) {
// We need to check both shorthand syntax "number[]" and type reference "Array<number>"
if (node.kind === ts.SyntaxKind.ArrayType ||
(node.kind === ts.SyntaxKind.TypeReference &&
node.typeName.getText(ctx.sourceFile) ===
"Array")) {
if (node.parent &&
Ignore.shouldIgnorePrefix(node.parent, ctx.options, ctx.sourceFile)) {
return [];
}
var typeArgument = "T";
if (node.kind === ts.SyntaxKind.ArrayType) {
var typeNode = node;
typeArgument = typeNode.elementType.getFullText(ctx.sourceFile).trim();
}
else if (node.kind === ts.SyntaxKind.TypeReference) {
var typeNode = node;
if (typeNode.typeArguments) {
typeArgument = typeNode.typeArguments[0]
.getFullText(ctx.sourceFile)
.trim();
}
}
var length_1 = node.getWidth(ctx.sourceFile);
return [
check_node_1.createInvalidNode(node, new Lint.Replacement(node.end - length_1, length_1, "ReadonlyArray<" + typeArgument + ">"))
];
}
return [];
}
function checkVariableOrParameterImplicitType(node, ctx) {
if (node.kind === ts.SyntaxKind.VariableDeclaration ||
node.kind === ts.SyntaxKind.Parameter ||
node.kind === ts.SyntaxKind.PropertyDeclaration) {
// The initializer is used to set and implicit type
var varOrParamNode = node;
if (Ignore.shouldIgnorePrefix(node, ctx.options, ctx.sourceFile)) {
return [];
}
if (!varOrParamNode.type) {
if (varOrParamNode.initializer &&
varOrParamNode.initializer.kind === ts.SyntaxKind.ArrayLiteralExpression) {
var length_2 = varOrParamNode.name.getWidth(ctx.sourceFile);
var nameText = varOrParamNode.name.getText(ctx.sourceFile);
var typeArgument = "any";
// Not sure it is a good idea to guess what the element types are...
// const arrayLiteralNode = varOrParamNode.initializer as ts.ArrayLiteralExpression;
// if (arrayLiteralNode.elements.length > 0) {
// const element = arrayLiteralNode.elements[0];
// if (element.kind === ts.SyntaxKind.NumericLiteral) {
// typeArgument = "number";
// } else if (element.kind === ts.SyntaxKind.StringLiteral) {
// typeArgument = "string";
// } else if (element.kind === ts.SyntaxKind.TrueKeyword || element.kind === ts.SyntaxKind.FalseKeyword) {
// typeArgument = "boolean";
// }
// }
return [
check_node_1.createInvalidNode(varOrParamNode.name, new Lint.Replacement(varOrParamNode.name.end - length_2, length_2, nameText + ": ReadonlyArray<" + typeArgument + ">"))
];
}
}
}
return [];
}
//# sourceMappingURL=readonlyArrayRule.js.map