Skip to content

Add whitelist to jsx-no-lambda #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions src/rules/jsxNoLambdaRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import * as Lint from "tslint";
import { isJsxAttribute, isJsxExpression } from "tsutils";
import * as ts from "typescript";

type IWhitelistedNames = Set<string>;

export class Rule extends Lint.Rules.AbstractRule {
/* tslint:disable:object-literal-sort-keys */
public static metadata: Lint.IRuleMetadata = {
Expand All @@ -29,9 +31,15 @@ export class Rule extends Lint.Rules.AbstractRule {
ES2015 arrow syntax) inside the render call stack works against pure component \
rendering. When doing an equality check between two lambdas, React will always \
consider them unequal values and force the component to re-render more often than necessary.`,
options: null,
optionsDescription: "",
optionExamples: ["true"],
options: {
type: "list",
listType: {
type: "array",
items: { type: "string" },
},
},
optionsDescription: "A list of whitelisted prop names to ignore",
optionExamples: [true, [true, "ref", "anotherWhitelistedProp"]],
type: "functionality",
typescriptOnly: false,
};
Expand All @@ -40,12 +48,16 @@ export class Rule extends Lint.Rules.AbstractRule {
public static FAILURE_STRING = "Lambdas are forbidden in JSX attributes due to their rendering performance impact";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithFunction(sourceFile, walk);
const whitelistedNames: IWhitelistedNames = new Set(this.ruleArguments);

return this.applyWithFunction(sourceFile, walk, whitelistedNames);
}
}

function walk(ctx: Lint.WalkContext<void>) {
function walk(ctx: Lint.WalkContext<IWhitelistedNames>) {
return ts.forEachChild(ctx.sourceFile, function cb(node: ts.Node): void {
const whitelistedNames = ctx.options;

// continue iterations until JsxAttribute will be found
if (isJsxAttribute(node)) {
const { initializer } = node;
Expand All @@ -54,9 +66,7 @@ function walk(ctx: Lint.WalkContext<void>) {
return;
}

// Ignore "ref" attribute.
// ref is not part of the props so using lambdas here will not trigger useless re-renders
if (node.name.text === "ref") {
if (whitelistedNames.has(node.name.text)) {
return;
}

Expand Down
4 changes: 3 additions & 1 deletion test/rules/jsx-no-lambda/test.tsx.lint
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const anotherOkFunction = () {
class TypicalRefHandler extends React.Component<{}, {}> {
private element: HTMLElement;
public render() {
// ref is not part of the props so using lambdas here will not trigger useless re-renders
// ref is whitelisted in the config
return <div ref={(ref) => this.element = ref} />
}
}
Expand Down Expand Up @@ -80,4 +80,6 @@ const ComplexComposableComponentWith: React.SFC<{}> = () => (
</ComposableComponent>
);

<Component whiteListedProp={() => {}} />

[0]: Lambdas are forbidden in JSX attributes due to their rendering performance impact
2 changes: 1 addition & 1 deletion test/rules/jsx-no-lambda/tslint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"rules": {
"jsx-no-lambda": true
"jsx-no-lambda": [true, "ref", "whiteListedProp"]
}
}