UNPKG

1.5 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var ts = require("typescript");
4var Lint = require("tslint");
5var utils = require("tsutils/typeguard/2.8");
6var Ignore = require("./shared/ignore");
7var check_node_1 = require("./shared/check-node");
8/**
9 * This rule checks that the readonly keyword is used in all PropertySignature and
10 * IndexerSignature nodes (which are the only places that the readonly keyword can exist).
11 */
12// tslint:disable-next-line:variable-name
13exports.Rule = check_node_1.createCheckNodeRule(Ignore.checkNodeWithIgnore(checkNode), "A readonly modifier is required.");
14function checkNode(node, ctx) {
15 return { invalidNodes: checkPropertySignatureAndIndexSignature(node, ctx) };
16}
17function checkPropertySignatureAndIndexSignature(node, ctx) {
18 if ((utils.isPropertySignature(node) ||
19 utils.isIndexSignatureDeclaration(node) ||
20 utils.isPropertyDeclaration(node)) &&
21 !(node.modifiers &&
22 node.modifiers.filter(function (m) { return m.kind === ts.SyntaxKind.ReadonlyKeyword; })
23 .length > 0)) {
24 // Check if ignore-prefix applies
25 if (Ignore.shouldIgnorePrefix(node, ctx.options, ctx.sourceFile)) {
26 return [];
27 }
28 return [
29 check_node_1.createInvalidNode(node, [
30 new Lint.Replacement(node.getStart(ctx.sourceFile), 0, "readonly ")
31 ])
32 ];
33 }
34 return [];
35}
36//# sourceMappingURL=readonlyKeywordRule.js.map
\No newline at end of file