1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | "use strict";
|
18 | var __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 | };
|
23 | var Lint = require("tslint");
|
24 | var ts = require("typescript");
|
25 | var guards_1 = require("../guards");
|
26 | var 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));
|
37 | Rule.FAILURE_STRING = "Pass a callback to ref prop instead of a string literal";
|
38 | exports.Rule = Rule;
|
39 | var 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));
|