UNPKG

5.74 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = Object.setPrototypeOf ||
4 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
6 return function (d, b) {
7 extendStatics(d, b);
8 function __() { this.constructor = d; }
9 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
10 };
11})();
12Object.defineProperty(exports, "__esModule", { value: true });
13var ts = require("typescript");
14var Lint = require("tslint");
15var ErrorTolerantWalker_1 = require("./utils/ErrorTolerantWalker");
16var Rule = (function (_super) {
17 __extends(Rule, _super);
18 function Rule() {
19 return _super !== null && _super.apply(this, arguments) || this;
20 }
21 Rule.prototype.apply = function (sourceFile) {
22 return this.applyWithWalker(new NoUnexternalizedStringsRuleWalker(sourceFile, this.getOptions()));
23 };
24 Rule.metadata = {
25 ruleName: 'no-unexternalized-strings',
26 type: 'maintainability',
27 description: 'Ensures that double quoted strings are passed to a localize call to provide proper strings for different locales',
28 options: null,
29 optionsDescription: '',
30 typescriptOnly: true,
31 issueClass: 'Ignored',
32 issueType: 'Warning',
33 severity: 'Low',
34 level: 'Opportunity for Excellence',
35 group: 'Configurable',
36 recommendation: 'false, // the VS Code team has a specific localization process that this rule enforces'
37 };
38 return Rule;
39}(Lint.Rules.AbstractRule));
40exports.Rule = Rule;
41var NoUnexternalizedStringsRuleWalker = (function (_super) {
42 __extends(NoUnexternalizedStringsRuleWalker, _super);
43 function NoUnexternalizedStringsRuleWalker(sourceFile, opt) {
44 var _this = _super.call(this, sourceFile, opt) || this;
45 _this.signatures = Object.create(null);
46 _this.ignores = Object.create(null);
47 var options = _this.getOptions();
48 var first = options && options.length > 0 ? options[0] : null;
49 if (first) {
50 if (Array.isArray(first.signatures)) {
51 first.signatures.forEach(function (signature) { return _this.signatures[signature] = true; });
52 }
53 if (Array.isArray(first.ignores)) {
54 first.ignores.forEach(function (ignore) { return _this.ignores[ignore] = true; });
55 }
56 if (first.messageIndex !== undefined) {
57 _this.messageIndex = first.messageIndex;
58 }
59 }
60 return _this;
61 }
62 NoUnexternalizedStringsRuleWalker.prototype.visitStringLiteral = function (node) {
63 this.checkStringLiteral(node);
64 _super.prototype.visitStringLiteral.call(this, node);
65 };
66 NoUnexternalizedStringsRuleWalker.prototype.checkStringLiteral = function (node) {
67 var text = node.getText();
68 if (text.length >= 2 && text[0] === NoUnexternalizedStringsRuleWalker.SINGLE_QUOTE
69 && text[text.length - 1] === NoUnexternalizedStringsRuleWalker.SINGLE_QUOTE) {
70 return;
71 }
72 var info = this.findDescribingParent(node);
73 if (info && info.ignoreUsage) {
74 return;
75 }
76 var callInfo = info ? info.callInfo : null;
77 if (callInfo && this.ignores[callInfo.callExpression.expression.getText()]) {
78 return;
79 }
80 if (!callInfo || callInfo.argIndex === -1 || !this.signatures[callInfo.callExpression.expression.getText()]) {
81 this.addFailureAt(node.getStart(), node.getWidth(), "Unexternalized string found: " + node.getText());
82 return;
83 }
84 var messageArg = callInfo.argIndex === this.messageIndex
85 ? callInfo.callExpression.arguments[this.messageIndex]
86 : null;
87 if (messageArg && messageArg !== node) {
88 this.addFailureAt(node.getStart(), node.getWidth(), "Message argument to '" + callInfo.callExpression.expression.getText() + "' must be a string literal.");
89 return;
90 }
91 };
92 NoUnexternalizedStringsRuleWalker.prototype.findDescribingParent = function (node) {
93 var kinds = ts.SyntaxKind;
94 while ((node.parent != null)) {
95 var parent_1 = node.parent;
96 var kind = parent_1.kind;
97 if (kind === kinds.CallExpression) {
98 var callExpression = parent_1;
99 return { callInfo: { callExpression: callExpression, argIndex: callExpression.arguments.indexOf(node) } };
100 }
101 else if (kind === kinds.ImportEqualsDeclaration || kind === kinds.ImportDeclaration || kind === kinds.ExportDeclaration) {
102 return { ignoreUsage: true };
103 }
104 else if (kind === kinds.VariableDeclaration || kind === kinds.FunctionDeclaration || kind === kinds.PropertyDeclaration
105 || kind === kinds.MethodDeclaration || kind === kinds.VariableDeclarationList || kind === kinds.InterfaceDeclaration
106 || kind === kinds.ClassDeclaration || kind === kinds.EnumDeclaration || kind === kinds.ModuleDeclaration
107 || kind === kinds.TypeAliasDeclaration || kind === kinds.SourceFile) {
108 return null;
109 }
110 node = parent_1;
111 }
112 return null;
113 };
114 NoUnexternalizedStringsRuleWalker.SINGLE_QUOTE = '\'';
115 return NoUnexternalizedStringsRuleWalker;
116}(ErrorTolerantWalker_1.ErrorTolerantWalker));
117//# sourceMappingURL=noUnexternalizedStringsRule.js.map
\No newline at end of file