UNPKG

2.74 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright 2016 Palantir Technologies, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17"use strict";
18var __extends = (this && this.__extends) || function (d, b) {
19 for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
20 function __() { this.constructor = d; }
21 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
22};
23var Lint = require("tslint");
24var ts = require("typescript");
25var guards_1 = require("../guards");
26var Rule = (function (_super) {
27 __extends(Rule, _super);
28 function Rule() {
29 return _super.apply(this, arguments) || this;
30 }
31 Rule.prototype.apply = function (sourceFile) {
32 var walker = new JsxNoStringRefWalker(sourceFile, this.getOptions());
33 return this.applyWithWalker(walker);
34 };
35 return Rule;
36}(Lint.Rules.AbstractRule));
37Rule.FAILURE_STRING = "Pass a callback to ref prop instead of a string literal";
38exports.Rule = Rule;
39var JsxNoStringRefWalker = (function (_super) {
40 __extends(JsxNoStringRefWalker, _super);
41 function JsxNoStringRefWalker() {
42 return _super.apply(this, arguments) || this;
43 }
44 JsxNoStringRefWalker.prototype.visitNode = function (node) {
45 if (guards_1.nodeIsKind(node, ts.SyntaxKind.JsxAttribute)) {
46 var name_1 = node.name, initializer = node.initializer;
47 var isRefAttribute = name_1 != null && name_1.text === "ref";
48 if (isRefAttribute && initializer != null) {
49 var hasStringInitializer = initializer.kind === ts.SyntaxKind.StringLiteral;
50 var hasStringExpressionInitializer = guards_1.nodeIsKind(initializer, ts.SyntaxKind.JsxExpression)
51 && (initializer.expression.kind === ts.SyntaxKind.StringLiteral
52 || initializer.expression.kind === ts.SyntaxKind.TemplateExpression);
53 if (hasStringInitializer || hasStringExpressionInitializer) {
54 this.addFailure(this.createFailure(initializer.getStart(), initializer.getWidth(), Rule.FAILURE_STRING));
55 }
56 }
57 }
58 _super.prototype.visitNode.call(this, node);
59 };
60 return JsxNoStringRefWalker;
61}(Lint.RuleWalker));