Skip to content

Commit

Permalink
Allow lambdas in non-attribute jsx expressions (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkillian authored and adidahiya committed Jun 22, 2016
1 parent e70cf03 commit 281ff1b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
26 changes: 15 additions & 11 deletions src/rules/jsxNoLambdaRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import * as ts from "typescript";
import * as Lint from "tslint/lib/lint";

export class Rule extends Lint.Rules.AbstractRule {
public static FAILURE_STRING = "Lambdas are forbidden in JSX due to their rendering performance impact.";
public static FAILURE_STRING = "Lambdas are forbidden in JSX attributes due to their rendering performance impact";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const jsxNoLambdaWalker = new JsxNoLambdaWalker(sourceFile, this.getOptions());
Expand All @@ -28,23 +28,27 @@ export class Rule extends Lint.Rules.AbstractRule {
}

class JsxNoLambdaWalker extends Lint.RuleWalker {
private isInJsxExpression = false;

public visitJsxExpression(node: ts.JsxExpression) {
this.isInJsxExpression = true;
super.visitJsxExpression(node);
this.isInJsxExpression = false;
private isInJsxAttribute = false;

protected visitNode(node: ts.Node) {
if (node.kind === ts.SyntaxKind.JsxAttribute) {
this.isInJsxAttribute = true;
super.visitNode(node);
this.isInJsxAttribute = false;
} else {
super.visitNode(node);
}
}

public visitFunctionExpression(node: ts.FunctionExpression) {
if (this.isInJsxExpression) {
protected visitFunctionExpression(node: ts.FunctionExpression) {
if (this.isInJsxAttribute) {
this.reportFailure(node);
}
super.visitFunctionExpression(node);
}

public visitArrowFunction(node: ts.ArrowFunction) {
if (this.isInJsxExpression) {
protected visitArrowFunction(node: ts.ArrowFunction) {
if (this.isInJsxAttribute) {
this.reportFailure(node);
}
super.visitArrowFunction(node);
Expand Down
6 changes: 2 additions & 4 deletions test/rules/jsx-no-lambda/test.tsx.lint
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export const myAnchorButton = (
</a>
);


function randomOkFunction() {
// I ain't doing no harm
}
Expand All @@ -45,10 +44,9 @@ const StatelessComponent: React.SFC<{}> = () => {
// component children can be optimized too
return (
<div>
{() => console.log("foo")}
~~~~~~~~~~~~~~~~~~~~~~~~ [0]
{myElements.map((e) => <span>{e}</span>)}
</div>
);
};

[0]: Lambdas are forbidden in JSX due to their rendering performance impact.
[0]: Lambdas are forbidden in JSX attributes due to their rendering performance impact

0 comments on commit 281ff1b

Please sign in to comment.