1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var Lint = require("tslint");
|
4 | var utils = require("tsutils/typeguard/2.8");
|
5 | var Ignore = require("./shared/ignore");
|
6 | var check_node_1 = require("./shared/check-node");
|
7 | var typeguard_1 = require("./shared/typeguard");
|
8 |
|
9 | exports.Rule = check_node_1.createCheckNodeRule(Ignore.checkNodeWithIgnore(checkNode), "Only ReadonlyArray allowed.");
|
10 | function checkNode(node, ctx) {
|
11 | return {
|
12 | invalidNodes: checkArrayType(node, ctx).concat(checkTypeReference(node, ctx), checkImplicitType(node, ctx))
|
13 | };
|
14 | }
|
15 | function checkArrayType(node, ctx) {
|
16 |
|
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 && checkIsReturnType(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 | }
|
40 | function checkTypeReference(node, ctx) {
|
41 |
|
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 && checkIsReturnType(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 | }
|
59 | function checkImplicitType(node, ctx) {
|
60 | if (Ignore.shouldIgnorePrefix(node, ctx.options, ctx.sourceFile)) {
|
61 | return [];
|
62 | }
|
63 |
|
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 | }
|
77 | exports.checkImplicitType = checkImplicitType;
|
78 | function checkIsReturnType(node) {
|
79 | return Boolean(node.parent &&
|
80 | typeguard_1.isFunctionLikeDeclaration(node.parent) &&
|
81 | node === node.parent.type);
|
82 | }
|
83 | function isUntypedAndHasArrayLiteralExpressionInitializer(node) {
|
84 | return Boolean(!node.type &&
|
85 | (node.initializer && utils.isArrayLiteralExpression(node.initializer)));
|
86 | }
|
87 |
|
\ | No newline at end of file |