Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Fix #518: Exclude generic function in TSX file #521

Merged
Merged
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
25 changes: 23 additions & 2 deletions src/noFunctionExpressionRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,48 @@ export class Rule extends Lint.Rules.AbstractRule {
}

class NoFunctionExpressionRuleWalker extends ErrorTolerantWalker {
private allowGenericFunctionExpression: boolean = false;

constructor(sourceFile: ts.SourceFile, options: Lint.IOptions) {
super(sourceFile, options);
if (sourceFile.fileName.endsWith('tsx')) {
this.allowGenericFunctionExpression = true;
}
}

protected visitFunctionExpression(node: ts.FunctionExpression): void {
const walker = new SingleFunctionWalker(this.getSourceFile(), this.getOptions());
node.getChildren().forEach((child: ts.Node) => {
walker.walk(child);
});

const isGenericFunctionInTSX = this.allowGenericFunctionExpression && walker.isGenericFunction;
// function expression that access 'this' is allowed
if (!walker.isAccessingThis && !node.asteriskToken) {
if (!walker.isAccessingThis && !node.asteriskToken
// generic function expression in .tsx file is allowed
&& !isGenericFunctionInTSX) {
this.addFailureAt(node.getStart(), node.getWidth(), Rule.FAILURE_STRING);
}

super.visitFunctionExpression(node);
}
}

class SingleFunctionWalker extends ErrorTolerantWalker {
public isAccessingThis: boolean = false;
public isGenericFunction: boolean = false;
protected visitNode(node: ts.Node): void {
if (node.getText() === 'this') {
this.isAccessingThis = true;
}
super.visitNode(node);
}

protected visitTypeReference(node: ts.TypeReferenceNode): void {
this.isGenericFunction = true;
super.visitTypeReference(node);
}

/* tslint:disable:no-empty */
protected visitFunctionExpression(): void {
// do not visit inner blocks
Expand All @@ -62,4 +83,4 @@ class SingleFunctionWalker extends ErrorTolerantWalker {
// do not visit inner blocks
}
/* tslint:enable:no-empty */
}
}
18 changes: 17 additions & 1 deletion src/tests/NoFunctionExpressionRuleTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ describe('noFunctionExpressionRule', (): void => {
TestHelper.assertViolations(ruleName, script, []);
});

it('should fail on not generic type function expression and pass generic type function within a .tsx file', (): void => {
TestHelper.assertViolations(
ruleName,
"test-data/NoFunctionExpressionWithInTSX.tsx",
[{
"failure": "Use arrow function instead of function expression",
"name": "test-data/NoFunctionExpressionWithInTSX.tsx",
"ruleName": "no-function-expression",
"startPosition": {
"character": 28,
"line": 1
}
}],
);
});

it('should fail on function expression', (): void => {
const script: string = `
var x = function() {
Expand Down Expand Up @@ -94,4 +110,4 @@ describe('noFunctionExpressionRule', (): void => {
}
]);
});
});
});
7 changes: 7 additions & 0 deletions test-data/NoFunctionExpressionWithInTSX.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const notGenericFunction = function() {
return 1 + 1;
};

const genericFunction = function<T>(x: T): T {
return x;
};