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 Rule = (function (_super) {
|
26 | __extends(Rule, _super);
|
27 | function Rule() {
|
28 | return _super.apply(this, arguments) || this;
|
29 | }
|
30 | Rule.prototype.apply = function (sourceFile) {
|
31 | var jsxNoLambdaWalker = new JsxNoLambdaWalker(sourceFile, this.getOptions());
|
32 | return this.applyWithWalker(jsxNoLambdaWalker);
|
33 | };
|
34 | return Rule;
|
35 | }(Lint.Rules.AbstractRule));
|
36 | Rule.FAILURE_STRING = "Lambdas are forbidden in JSX attributes due to their rendering performance impact";
|
37 | exports.Rule = Rule;
|
38 | var JsxNoLambdaWalker = (function (_super) {
|
39 | __extends(JsxNoLambdaWalker, _super);
|
40 | function JsxNoLambdaWalker() {
|
41 | var _this = _super.apply(this, arguments) || this;
|
42 | _this.isInJsxAttribute = false;
|
43 | return _this;
|
44 | }
|
45 | JsxNoLambdaWalker.prototype.visitNode = function (node) {
|
46 | if (node.kind === ts.SyntaxKind.JsxAttribute) {
|
47 | this.isInJsxAttribute = true;
|
48 | _super.prototype.visitNode.call(this, node);
|
49 | this.isInJsxAttribute = false;
|
50 | }
|
51 | else {
|
52 | _super.prototype.visitNode.call(this, node);
|
53 | }
|
54 | };
|
55 | JsxNoLambdaWalker.prototype.visitFunctionExpression = function (node) {
|
56 | if (this.isInJsxAttribute) {
|
57 | this.reportFailure(node);
|
58 | }
|
59 | _super.prototype.visitFunctionExpression.call(this, node);
|
60 | };
|
61 | JsxNoLambdaWalker.prototype.visitArrowFunction = function (node) {
|
62 | if (this.isInJsxAttribute) {
|
63 | this.reportFailure(node);
|
64 | }
|
65 | _super.prototype.visitArrowFunction.call(this, node);
|
66 | };
|
67 | JsxNoLambdaWalker.prototype.reportFailure = function (node) {
|
68 | this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING));
|
69 | };
|
70 | return JsxNoLambdaWalker;
|
71 | }(Lint.RuleWalker));
|