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

Commit

Permalink
Fix #459: function-name rule should ignore computed names (#505)
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielCastro authored and Josh Goldberg committed Oct 2, 2018
1 parent a15acea commit c32dec7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/functionNameRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ class FunctionNameRuleWalker extends ErrorTolerantWalker {

protected visitMethodDeclaration(node: ts.MethodDeclaration): void {
const name: string = node.name.getText();
if (AstUtils.isPrivate(node)) {
if (AstUtils.hasComputedName(node)) {
// allow computed names
} else if (AstUtils.isPrivate(node)) {
if (
!this.privateMethodRegex.test(name)
&& this.args.validateStatics === VALIDATE_PRIVATE_STATICS_AS_PRIVATE) {
Expand Down
21 changes: 21 additions & 0 deletions src/tests/FunctionNameRuleTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,27 @@ describe('functionNameRule', () : void => {
TestHelper.assertViolations(ruleName, script, [ ]);
});

it('should pass on functions as symbol properties', (): void => {
const script : string = `
class MyClass {
static [Symbol.staticDefault](): any {}
public static [Symbol.staticPublic](): any {}
protected static [Symbol.staticProtected](): any {}
private static [Symbol.staticPrivate](): any {}
static [Symbol.methodDefault](): any {}
public static [Symbol.methodPublic](): any {}
protected static [Symbol.methodProtected](): any {}
private static [Symbol.methodPrivate](): any {}
}
const objLiteral = {
[Symbol.toStringTag](): string => "hello world"
}
`;
TestHelper.assertViolations(ruleName, script, [ ]);
});

it('should pass on correctly public static methods', () : void => {
const script : string = `
class MyClass {
Expand Down
8 changes: 8 additions & 0 deletions src/utils/AstUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ export module AstUtils {
/* tslint:enable:no-bitwise */
}

export function hasComputedName(node: ts.Node & { name?: ts.PropertyName }): boolean {
if (!node.name) {
return false;
}

return ts.isComputedPropertyName(node.name);
}

function isBindingPattern(node: ts.Node): node is ts.BindingPattern {
return node != null && (node.kind === ts.SyntaxKind.ArrayBindingPattern ||
node.kind === ts.SyntaxKind.ObjectBindingPattern);
Expand Down

0 comments on commit c32dec7

Please sign in to comment.