noUnexternalizedStringsRule.js 8.5 KB
Newer Older
J
Joao Moreno 已提交
1 2 3 4 5 6 7 8 9 10
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
J
Joao Moreno 已提交
11 12
var ts = require("typescript");
var Lint = require("tslint");
J
Joao Moreno 已提交
13 14 15 16 17 18
/**
 * Implementation of the no-unexternalized-strings rule.
 */
var Rule = (function (_super) {
    __extends(Rule, _super);
    function Rule() {
J
Joao Moreno 已提交
19
        return _super.apply(this, arguments) || this;
J
Joao Moreno 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
    }
    Rule.prototype.apply = function (sourceFile) {
        return this.applyWithWalker(new NoUnexternalizedStringsRuleWalker(sourceFile, this.getOptions()));
    };
    return Rule;
}(Lint.Rules.AbstractRule));
exports.Rule = Rule;
function isStringLiteral(node) {
    return node && node.kind === ts.SyntaxKind.StringLiteral;
}
function isObjectLiteral(node) {
    return node && node.kind === ts.SyntaxKind.ObjectLiteralExpression;
}
function isPropertyAssignment(node) {
    return node && node.kind === ts.SyntaxKind.PropertyAssignment;
}
var NoUnexternalizedStringsRuleWalker = (function (_super) {
    __extends(NoUnexternalizedStringsRuleWalker, _super);
    function NoUnexternalizedStringsRuleWalker(file, opts) {
J
Joao Moreno 已提交
39 40 41 42 43 44 45
        var _this = _super.call(this, file, opts) || this;
        _this.signatures = Object.create(null);
        _this.ignores = Object.create(null);
        _this.messageIndex = undefined;
        _this.keyIndex = undefined;
        _this.usedKeys = Object.create(null);
        var options = _this.getOptions();
J
Joao Moreno 已提交
46 47 48 49 50 51 52 53 54
        var first = options && options.length > 0 ? options[0] : null;
        if (first) {
            if (Array.isArray(first.signatures)) {
                first.signatures.forEach(function (signature) { return _this.signatures[signature] = true; });
            }
            if (Array.isArray(first.ignores)) {
                first.ignores.forEach(function (ignore) { return _this.ignores[ignore] = true; });
            }
            if (typeof first.messageIndex !== 'undefined') {
J
Joao Moreno 已提交
55
                _this.messageIndex = first.messageIndex;
J
Joao Moreno 已提交
56 57
            }
            if (typeof first.keyIndex !== 'undefined') {
J
Joao Moreno 已提交
58
                _this.keyIndex = first.keyIndex;
J
Joao Moreno 已提交
59 60
            }
        }
J
Joao Moreno 已提交
61
        return _this;
J
Joao Moreno 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    }
    NoUnexternalizedStringsRuleWalker.prototype.visitSourceFile = function (node) {
        var _this = this;
        _super.prototype.visitSourceFile.call(this, node);
        Object.keys(this.usedKeys).forEach(function (key) {
            var occurences = _this.usedKeys[key];
            if (occurences.length > 1) {
                occurences.forEach(function (occurence) {
                    _this.addFailure((_this.createFailure(occurence.key.getStart(), occurence.key.getWidth(), "Duplicate key " + occurence.key.getText() + " with different message value.")));
                });
            }
        });
    };
    NoUnexternalizedStringsRuleWalker.prototype.visitStringLiteral = function (node) {
        this.checkStringLiteral(node);
        _super.prototype.visitStringLiteral.call(this, node);
    };
    NoUnexternalizedStringsRuleWalker.prototype.checkStringLiteral = function (node) {
        var text = node.getText();
        var doubleQuoted = text.length >= 2 && text[0] === NoUnexternalizedStringsRuleWalker.DOUBLE_QUOTE && text[text.length - 1] === NoUnexternalizedStringsRuleWalker.DOUBLE_QUOTE;
        var info = this.findDescribingParent(node);
        // Ignore strings in import and export nodes.
        if (info && info.ignoreUsage) {
            return;
        }
        var callInfo = info ? info.callInfo : null;
        var functionName = callInfo ? callInfo.callExpression.expression.getText() : null;
        if (functionName && this.ignores[functionName]) {
            return;
        }
        if (doubleQuoted && (!callInfo || callInfo.argIndex === -1 || !this.signatures[functionName])) {
M
Matt Bierner 已提交
93 94
            var s_1 = node.getText();
            var replacement = new Lint.Replacement(node.getStart(), node.getWidth(), "nls.localize('KEY-" + s_1.substring(1, s_1.length - 1) + "', " + s_1 + ")");
95 96
            var fix = new Lint.Fix("Unexternalitzed string", [replacement]);
            this.addFailure(this.createFailure(node.getStart(), node.getWidth(), "Unexternalized string found: " + node.getText(), fix));
J
Joao Moreno 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
            return;
        }
        // We have a single quoted string outside a localize function name.
        if (!doubleQuoted && !this.signatures[functionName]) {
            return;
        }
        // We have a string that is a direct argument into the localize call.
        var keyArg = callInfo.argIndex === this.keyIndex
            ? callInfo.callExpression.arguments[this.keyIndex]
            : null;
        if (keyArg) {
            if (isStringLiteral(keyArg)) {
                this.recordKey(keyArg, this.messageIndex ? callInfo.callExpression.arguments[this.messageIndex] : undefined);
            }
            else if (isObjectLiteral(keyArg)) {
                for (var i = 0; i < keyArg.properties.length; i++) {
                    var property = keyArg.properties[i];
                    if (isPropertyAssignment(property)) {
J
Joao Moreno 已提交
115 116
                        var name_1 = property.name.getText();
                        if (name_1 === 'key') {
J
Joao Moreno 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
                            var initializer = property.initializer;
                            if (isStringLiteral(initializer)) {
                                this.recordKey(initializer, this.messageIndex ? callInfo.callExpression.arguments[this.messageIndex] : undefined);
                            }
                            break;
                        }
                    }
                }
            }
        }
        var messageArg = callInfo.argIndex === this.messageIndex
            ? callInfo.callExpression.arguments[this.messageIndex]
            : null;
        if (messageArg && messageArg !== node) {
            this.addFailure(this.createFailure(messageArg.getStart(), messageArg.getWidth(), "Message argument to '" + callInfo.callExpression.expression.getText() + "' must be a string literal."));
            return;
        }
    };
    NoUnexternalizedStringsRuleWalker.prototype.recordKey = function (keyNode, messageNode) {
        var text = keyNode.getText();
        var occurences = this.usedKeys[text];
        if (!occurences) {
            occurences = [];
            this.usedKeys[text] = occurences;
        }
        if (messageNode) {
            if (occurences.some(function (pair) { return pair.message ? pair.message.getText() === messageNode.getText() : false; })) {
                return;
            }
        }
        occurences.push({ key: keyNode, message: messageNode });
    };
    NoUnexternalizedStringsRuleWalker.prototype.findDescribingParent = function (node) {
        var parent;
        while ((parent = node.parent)) {
            var kind = parent.kind;
            if (kind === ts.SyntaxKind.CallExpression) {
                var callExpression = parent;
                return { callInfo: { callExpression: callExpression, argIndex: callExpression.arguments.indexOf(node) } };
            }
            else if (kind === ts.SyntaxKind.ImportEqualsDeclaration || kind === ts.SyntaxKind.ImportDeclaration || kind === ts.SyntaxKind.ExportDeclaration) {
                return { ignoreUsage: true };
            }
            else if (kind === ts.SyntaxKind.VariableDeclaration || kind === ts.SyntaxKind.FunctionDeclaration || kind === ts.SyntaxKind.PropertyDeclaration
                || kind === ts.SyntaxKind.MethodDeclaration || kind === ts.SyntaxKind.VariableDeclarationList || kind === ts.SyntaxKind.InterfaceDeclaration
                || kind === ts.SyntaxKind.ClassDeclaration || kind === ts.SyntaxKind.EnumDeclaration || kind === ts.SyntaxKind.ModuleDeclaration
                || kind === ts.SyntaxKind.TypeAliasDeclaration || kind === ts.SyntaxKind.SourceFile) {
                return null;
            }
            node = parent;
        }
    };
    return NoUnexternalizedStringsRuleWalker;
}(Lint.RuleWalker));
J
Joao Moreno 已提交
171
NoUnexternalizedStringsRuleWalker.DOUBLE_QUOTE = '"';