UNPKG

2.77 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 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));
36Rule.FAILURE_STRING = "Lambdas are forbidden in JSX attributes due to their rendering performance impact";
37exports.Rule = Rule;
38var 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));