UNPKG

3.67 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var Lint = require("tslint");
4var utils = require("tsutils/typeguard/2.8");
5var Ignore = require("./shared/ignore");
6var check_node_1 = require("./shared/check-node");
7var typeguard_1 = require("./shared/typeguard");
8// tslint:disable-next-line:variable-name
9exports.Rule = check_node_1.createCheckNodeRule(Ignore.checkNodeWithIgnore(checkNode), "Only ReadonlyArray allowed.");
10function checkNode(node, ctx) {
11 return {
12 invalidNodes: checkArrayType(node, ctx).concat(checkTypeReference(node, ctx), checkImplicitType(node, ctx))
13 };
14}
15function checkArrayType(node, ctx) {
16 // We need to check both shorthand syntax "number[]"...
17 if (utils.isArrayTypeNode(node)) {
18 if (node.parent &&
19 Ignore.shouldIgnorePrefix(node.parent, ctx.options, ctx.sourceFile)) {
20 return [];
21 }
22 if (ctx.options.ignoreRestParameters &&
23 node.parent &&
24 utils.isParameterDeclaration(node.parent) &&
25 node.parent.dotDotDotToken) {
26 return [];
27 }
28 if (ctx.options.ignoreReturnType && checkIsReturnTypeOrNestedWithIn(node)) {
29 return [];
30 }
31 return [
32 check_node_1.createInvalidNode(node, [
33 new Lint.Replacement(node.getStart(ctx.sourceFile), 0, "ReadonlyArray<"),
34 new Lint.Replacement(node.end - 2, 2, ">")
35 ])
36 ];
37 }
38 return [];
39}
40function checkTypeReference(node, ctx) {
41 // ...and type reference "Array<number>"
42 if (utils.isTypeReferenceNode(node) &&
43 node.typeName.getText(ctx.sourceFile) === "Array") {
44 if (node.parent &&
45 Ignore.shouldIgnorePrefix(node.parent, ctx.options, ctx.sourceFile)) {
46 return [];
47 }
48 if (ctx.options.ignoreReturnType && checkIsReturnTypeOrNestedWithIn(node)) {
49 return [];
50 }
51 return [
52 check_node_1.createInvalidNode(node, [
53 new Lint.Replacement(node.getStart(ctx.sourceFile), 0, "Readonly")
54 ])
55 ];
56 }
57 return [];
58}
59function checkImplicitType(node, ctx) {
60 if (Ignore.shouldIgnorePrefix(node, ctx.options, ctx.sourceFile)) {
61 return [];
62 }
63 // Check if the initializer is used to set an implicit array type
64 if (typeguard_1.isVariableOrParameterOrPropertyDeclaration(node) &&
65 isUntypedAndHasArrayLiteralExpressionInitializer(node)) {
66 var length_1 = node.name.getWidth(ctx.sourceFile);
67 var nameText = node.name.getText(ctx.sourceFile);
68 var typeArgument = "any";
69 return [
70 check_node_1.createInvalidNode(node.name, [
71 new Lint.Replacement(node.name.end - length_1, length_1, nameText + ": ReadonlyArray<" + typeArgument + ">")
72 ])
73 ];
74 }
75 return [];
76}
77exports.checkImplicitType = checkImplicitType;
78function checkIsReturnTypeOrNestedWithIn(node) {
79 var getRootTypeReferenceNode = function (typeNode) {
80 return utils.isTypeReferenceNode(typeNode.parent)
81 ? getRootTypeReferenceNode(typeNode.parent)
82 : typeNode;
83 };
84 var rootTypeReferenceNode = getRootTypeReferenceNode(node);
85 return (rootTypeReferenceNode.parent &&
86 typeguard_1.isFunctionLikeDeclaration(rootTypeReferenceNode.parent) &&
87 rootTypeReferenceNode === rootTypeReferenceNode.parent.type);
88}
89function isUntypedAndHasArrayLiteralExpressionInitializer(node) {
90 return Boolean(!node.type &&
91 (node.initializer && utils.isArrayLiteralExpression(node.initializer)));
92}
93//# sourceMappingURL=readonlyArrayRule.js.map
\No newline at end of file