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

Fix #459: function-name rule should ignore computed names #505

Merged
merged 1 commit into from
Oct 2, 2018
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
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