1 | "use strict";
|
2 | var __extends = (this && this.__extends) || (function () {
|
3 | var extendStatics = function (d, b) {
|
4 | extendStatics = Object.setPrototypeOf ||
|
5 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
6 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
7 | return extendStatics(d, b);
|
8 | }
|
9 | return function (d, b) {
|
10 | extendStatics(d, b);
|
11 | function __() { this.constructor = d; }
|
12 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
13 | };
|
14 | })();
|
15 | Object.defineProperty(exports, "__esModule", { value: true });
|
16 | var ts = require("typescript");
|
17 | var Lint = require("tslint");
|
18 | var tsutils = require("tsutils");
|
19 | var Rule = (function (_super) {
|
20 | __extends(Rule, _super);
|
21 | function Rule() {
|
22 | return _super !== null && _super.apply(this, arguments) || this;
|
23 | }
|
24 | Rule.prototype.apply = function (sourceFile) {
|
25 | return this.applyWithFunction(sourceFile, walk, this.parseOptions(this.getOptions()));
|
26 | };
|
27 | Rule.prototype.parseOptions = function (options) {
|
28 | var messageIndex;
|
29 | var signatures = Object.create(null);
|
30 | var ignores = Object.create(null);
|
31 | if (options.ruleArguments instanceof Array) {
|
32 | var args = options.ruleArguments.length > 0 ? options.ruleArguments[0] : undefined;
|
33 | if (args) {
|
34 | if (Array.isArray(args.signatures)) {
|
35 | args.signatures.forEach(function (signature) { return (signatures[signature] = true); });
|
36 | }
|
37 | if (Array.isArray(args.ignores)) {
|
38 | args.ignores.forEach(function (ignore) { return (ignores[ignore] = true); });
|
39 | }
|
40 | if (args.messageIndex !== undefined) {
|
41 | messageIndex = args.messageIndex;
|
42 | }
|
43 | }
|
44 | }
|
45 | return {
|
46 | signatures: signatures,
|
47 | messageIndex: messageIndex,
|
48 | ignores: ignores
|
49 | };
|
50 | };
|
51 | Rule.metadata = {
|
52 | ruleName: 'no-unexternalized-strings',
|
53 | type: 'maintainability',
|
54 | description: 'Ensures that double quoted strings are passed to a localize call to provide proper strings for different locales',
|
55 | options: null,
|
56 | optionsDescription: '',
|
57 | typescriptOnly: true,
|
58 | issueClass: 'Ignored',
|
59 | issueType: 'Warning',
|
60 | severity: 'Low',
|
61 | level: 'Opportunity for Excellence',
|
62 | group: 'Configurable',
|
63 | recommendation: 'false'
|
64 | };
|
65 | return Rule;
|
66 | }(Lint.Rules.AbstractRule));
|
67 | exports.Rule = Rule;
|
68 | function walk(ctx) {
|
69 | var SINGLE_QUOTE = "'";
|
70 | var _a = ctx.options, signatures = _a.signatures, messageIndex = _a.messageIndex, ignores = _a.ignores;
|
71 | function checkStringLiteral(node) {
|
72 | var text = node.getText();
|
73 | if (text.length >= 2 && text[0] === SINGLE_QUOTE && text[text.length - 1] === SINGLE_QUOTE) {
|
74 | return;
|
75 | }
|
76 | var info = findDescribingParent(node);
|
77 | if (info && info.ignoreUsage) {
|
78 | return;
|
79 | }
|
80 | var callInfo = info ? info.callInfo : undefined;
|
81 | if (callInfo && ignores[callInfo.callExpression.expression.getText()]) {
|
82 | return;
|
83 | }
|
84 | if (!callInfo || callInfo.argIndex === -1 || !signatures[callInfo.callExpression.expression.getText()]) {
|
85 | ctx.addFailureAt(node.getStart(), node.getWidth(), "Unexternalized string found: " + node.getText());
|
86 | return;
|
87 | }
|
88 | var messageArg = callInfo.argIndex === messageIndex ? callInfo.callExpression.arguments[messageIndex] : undefined;
|
89 | if (messageArg && messageArg !== node) {
|
90 | ctx.addFailureAt(node.getStart(), node.getWidth(), "Message argument to '" + callInfo.callExpression.expression.getText() + "' must be a string literal.");
|
91 | return;
|
92 | }
|
93 | }
|
94 | function findDescribingParent(node) {
|
95 | while (node.parent) {
|
96 | var parent_1 = node.parent;
|
97 | if (tsutils.isCallExpression(parent_1)) {
|
98 | var callExpression = parent_1;
|
99 | return {
|
100 | callInfo: {
|
101 | callExpression: callExpression,
|
102 | argIndex: callExpression.arguments.indexOf(node)
|
103 | }
|
104 | };
|
105 | }
|
106 | if (tsutils.isImportEqualsDeclaration(parent_1) || tsutils.isImportDeclaration(parent_1) || tsutils.isExportDeclaration(parent_1)) {
|
107 | return { ignoreUsage: true };
|
108 | }
|
109 | if (tsutils.isVariableDeclaration(parent_1) ||
|
110 | tsutils.isFunctionDeclaration(parent_1) ||
|
111 | tsutils.isPropertyDeclaration(parent_1) ||
|
112 | tsutils.isMethodDeclaration(parent_1) ||
|
113 | tsutils.isVariableDeclarationList(parent_1) ||
|
114 | tsutils.isInterfaceDeclaration(parent_1) ||
|
115 | tsutils.isClassDeclaration(parent_1) ||
|
116 | tsutils.isEnumDeclaration(parent_1) ||
|
117 | tsutils.isModuleDeclaration(parent_1) ||
|
118 | tsutils.isTypeAliasDeclaration(parent_1) ||
|
119 | tsutils.isSourceFile(parent_1)) {
|
120 | return undefined;
|
121 | }
|
122 | node = parent_1;
|
123 | }
|
124 | return undefined;
|
125 | }
|
126 | function cb(node) {
|
127 | if (tsutils.isStringLiteral(node)) {
|
128 | checkStringLiteral(node);
|
129 | }
|
130 | return ts.forEachChild(node, cb);
|
131 | }
|
132 | return ts.forEachChild(ctx.sourceFile, cb);
|
133 | }
|
134 |
|
\ | No newline at end of file |