-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add NestedFunctionNames and UnnestedFunctionNames rules. These rules allow configuring naming conventions for nested and unnested function names.
- Loading branch information
1 parent
815efa4
commit 4e9d385
Showing
12 changed files
with
445 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
title: FL0080 | ||
category: how-to | ||
hide_menu: true | ||
--- | ||
|
||
# UnnestedFunctionNames (FL0080) | ||
|
||
*Introduced in `0.21.8`* | ||
|
||
## Cause | ||
|
||
Unnested function naming does not match the specified config. | ||
|
||
## Rationale | ||
|
||
Consistency aides readability. | ||
|
||
## How To Fix | ||
|
||
Update the unnested function names to be consistent with the rules you have specified. | ||
|
||
## Rule Settings | ||
|
||
{ | ||
"UnnestedFunctionNames": { | ||
"enabled": false, | ||
"config": { | ||
"naming": "PascalCase", | ||
"underscores": "None" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
title: FL0081 | ||
category: how-to | ||
hide_menu: true | ||
--- | ||
|
||
# NestedFunctionNames (FL0081) | ||
|
||
*Introduced in `0.21.8`* | ||
|
||
## Cause | ||
|
||
Nested function naming does not match the specified config. | ||
|
||
## Rationale | ||
|
||
Consistency aides readability. | ||
|
||
## How To Fix | ||
|
||
Update the nested function names to be consistent with the rules you have specified. | ||
|
||
## Rule Settings | ||
|
||
{ | ||
"NestedFunctionNames": { | ||
"enabled": false, | ||
"config": { | ||
"naming": "CamelCase", | ||
"underscores": "None" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/FSharpLint.Core/Rules/Conventions/Naming/NestedFunctionNames.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module FSharpLint.Rules.NestedFunctionNames | ||
|
||
open FSharp.Compiler.Syntax | ||
open FSharpLint.Framework.Ast | ||
open FSharpLint.Framework.AstInfo | ||
open FSharpLint.Framework.Rules | ||
open FSharpLint.Rules.Helper.Naming | ||
|
||
let private getIdentifiers (args: AstNodeRuleParams) = | ||
match args.AstNode with | ||
| AstNode.Binding (SynBinding (_, _, _, _, _attributes, _, _, pattern, _, _, _, _)) -> | ||
if isNested args args.NodeIndex then | ||
getPatternIdents AccessControlLevel.Public getFunctionIdents true pattern | ||
else | ||
Array.empty | ||
| _ -> Array.empty | ||
|
||
let rule config = | ||
{ Name = "NestedFunctionNames" | ||
Identifier = Identifiers.NestedFunctionNames | ||
RuleConfig = | ||
{ NamingRuleConfig.Config = config | ||
GetIdentifiersToCheck = getIdentifiers } } | ||
|> toAstNodeRule | ||
|> AstNodeRule |
25 changes: 25 additions & 0 deletions
25
src/FSharpLint.Core/Rules/Conventions/Naming/UnnestedFunctionNames.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module FSharpLint.Rules.UnnestedFunctionNames | ||
|
||
open FSharp.Compiler.Syntax | ||
open FSharpLint.Framework.Ast | ||
open FSharpLint.Framework.AstInfo | ||
open FSharpLint.Framework.Rules | ||
open FSharpLint.Rules.Helper.Naming | ||
|
||
let private getIdentifiers (args: AstNodeRuleParams) = | ||
match args.AstNode with | ||
| AstNode.Binding (SynBinding (_, _, _, _, _attributes, _, _, pattern, _, _, _, _)) -> | ||
if isNested args args.NodeIndex then | ||
Array.empty | ||
else | ||
getPatternIdents AccessControlLevel.Public getFunctionIdents true pattern | ||
| _ -> Array.empty | ||
|
||
let rule config = | ||
{ Name = "UnnestedFunctionNames" | ||
Identifier = Identifiers.UnnestedFunctionNames | ||
RuleConfig = | ||
{ NamingRuleConfig.Config = config | ||
GetIdentifiersToCheck = getIdentifiers } } | ||
|> toAstNodeRule | ||
|> AstNodeRule |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
tests/FSharpLint.Core.Tests/Rules/Conventions/Naming/NestedFunctionNames.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
module FSharpLint.Core.Tests.Rules.Conventions.NestedFunctionNames | ||
|
||
open NUnit.Framework | ||
open FSharpLint.Framework.Rules | ||
open FSharpLint.Rules | ||
|
||
let config = | ||
{ NamingConfig.Naming = Some NamingCase.CamelCase | ||
Underscores = Some NamingUnderscores.None | ||
Prefix = None | ||
Suffix = None } | ||
|
||
[<TestFixture>] | ||
type TestConventionsNestedFunctionNames() = | ||
inherit TestAstNodeRuleBase.TestAstNodeRuleBase(NestedFunctionNames.rule config) | ||
|
||
[<Test>] | ||
member this.UnnestedFunctionNameInPascalCaseMustBeIgnored() = | ||
this.Parse """ | ||
module Program = | ||
let CylinderVolume radius length = | ||
let pi = 3.14159 | ||
length * pi * radius * radius""" | ||
|
||
this.AssertNoWarnings() | ||
|
||
[<Test>] | ||
member this.NestedFunctionNameIsCamelCase() = | ||
this.Parse """ | ||
module Program = | ||
let CylinderVolume radius length = | ||
let nestedFunction arg1 = | ||
arg1 + 1 | ||
let pi = 3.14159 | ||
length * pi * radius * radius""" | ||
|
||
this.AssertNoWarnings() | ||
|
||
[<Test>] | ||
member this.NestedFunctionNameIsPascalCase() = | ||
this.Parse """ | ||
module Program = | ||
let CylinderVolume radius length = | ||
let NestedFunction arg1 = | ||
arg1 + 1 | ||
let pi = 3.14159 | ||
length * pi * radius * radius""" | ||
|
||
Assert.IsTrue(this.ErrorExistsAt(4, 8)) | ||
|
||
[<Test>] | ||
member this.NestedFunctionNameInTypeIsPascalCase() = | ||
this.Parse """ | ||
type Record = | ||
{ Dog: int } | ||
member this.CylinderVolume length radius = | ||
let NestedFunction arg1 = | ||
arg1 + 1 | ||
let pi = 3.14159 | ||
length * pi * radius * radius""" | ||
|
||
Assert.IsTrue(this.ErrorExistsAt(5, 12)) | ||
|
||
[<Test>] | ||
member this.UnnestedFunctionNameIsPascalCase() = | ||
this.Parse """ | ||
module Program = | ||
let CylinderVolume() = | ||
let radius = 1 | ||
let pi = 3.14159 | ||
length * pi * radius * radius | ||
let CylinderVolume2() = | ||
let radius = 1 | ||
let pi = 3.14159 | ||
length * pi * radius * radius""" | ||
|
||
this.AssertNoWarnings() | ||
|
||
[<Test>] | ||
member this.PrivateUnnestedFunctionNameIsPascalCase() = | ||
this.Parse """ | ||
module Program = | ||
let private CylinderVolume() = | ||
let radius = 1 | ||
let pi = 3.14159 | ||
length * pi * radius * radius""" | ||
|
||
this.AssertNoWarnings() | ||
|
||
[<Test>] | ||
member this.PrivateUnnestedFunctionNameIsCamelCase() = | ||
this.Parse """ | ||
module Program = | ||
let private cylinderVolume() = | ||
let radius = 1 | ||
let pi = 3.14159 | ||
length * pi * radius * radius""" | ||
|
||
this.AssertNoWarnings() |
Oops, something went wrong.