{"id":22624039,"url":"https://github.com/mykolav/require-named-args-fs","last_synced_at":"2025-04-11T17:41:14.157Z","repository":{"id":52831938,"uuid":"145012593","full_name":"mykolav/require-named-args-fs","owner":"mykolav","description":"Force named arguments: a Roslyn code analyzer and code-fix provider for C#","archived":false,"fork":false,"pushed_at":"2024-01-23T19:02:46.000Z","size":4576,"stargazers_count":24,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-25T13:39:54.047Z","etag":null,"topics":["fsharp","named-arguments","roslyn","roslyn-analyzer"],"latest_commit_sha":null,"homepage":"","language":"F#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mykolav.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2018-08-16T16:19:51.000Z","updated_at":"2024-11-29T05:39:25.000Z","dependencies_parsed_at":"2024-01-19T23:50:33.323Z","dependency_job_id":null,"html_url":"https://github.com/mykolav/require-named-args-fs","commit_stats":{"total_commits":32,"total_committers":3,"mean_commits":"10.666666666666666","dds":0.15625,"last_synced_commit":"4f0a4c090ad837da0ce100bc32c62142eb652e39"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mykolav%2Frequire-named-args-fs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mykolav%2Frequire-named-args-fs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mykolav%2Frequire-named-args-fs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mykolav%2Frequire-named-args-fs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mykolav","download_url":"https://codeload.github.com/mykolav/require-named-args-fs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247943426,"owners_count":21022424,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fsharp","named-arguments","roslyn","roslyn-analyzer"],"created_at":"2024-12-09T00:09:38.002Z","updated_at":"2025-04-11T17:41:14.135Z","avatar_url":"https://github.com/mykolav.png","language":"F#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Require a method to be invoked with named arguments\n\n[![Build status](https://ci.appveyor.com/api/projects/status/uucjd3ti7tjmmn9d?svg=true)](https://ci.appveyor.com/project/mykolav/require-named-args-fs)\n\nThis project contains a Roslyn code analyzer and an accompanying code-fix provider that let you [force named arguments in C#](https://stackoverflow.com/questions/11300645/forcing-named-arguments-in-c-sharp).\n\n![The RequireNamedArgs analyzer in action](./require-named-args-demo.gif)\n\n## How to use it?\n\nInstall the [nuget package](https://www.nuget.org/packages/RequireNamedArgs/).\n\nIntroduce a `RequireNamedArgsAttribute` attribute to your solution. In other words, place the following C# code in an appropriate spot in your solution.\n\n```csharp\n[AttributeUsage(AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Struct)]\nclass RequireNamedArgsAttribute : Attribute {}\n```\n\nApply the `[RequireNamedArgs]` attribute to the methods that should only be called with named arguments. For example:\n\n```csharp\n[RequireNamedArgs]\npublic static void TellPowerLevel(string name, int powerLevel) {}\n\n// Elsewhere in your code:\n// if `TellPowerLevel` method is called with positional arguments,\n// the analyzer will emit an error.\nTellPowerLevel(name: \"Goku\", powerLevel: 9001);\n```\n\n### Supported method kinds\n\nThe analyzer supports requiring named arguments for the following method kinds  \n- Regular instance and static methods\n- Extension methods\n- Regular constructors\n- Attribute constructors\n- Primary constructors \n\nTo mark a record's primary constructor, apply `[RequireNamedArgs]` to the record itself.\n\n```csharp\n[RequireNamedArgs]\nrecord Character(string Name, int PowerLevel) {}\n\n[RequireNamedArgs]\nrecord struct CharacterStruct(string Name, int PowerLevel) {}\n\n// Elsewhere in your code:\n// if the primary constructor of `Character` or `CharacterStruct` is called with positional arguments,\n// the analyzer will emit an error.\nnew Character(Name: \"Goku\", PowerLevel: 9001);\nnew CharacterStruct(Name: \"Goku\", PowerLevel: 9001);\n```\n\nPlease note, in the code above the attribute only applies to the primary constructors of the records. If a record has additional constructors, you can mark them with this attribute individually in a usual way.\n\n## Download and install\n\nInstall the [RequireNamedArgs](https://www.nuget.org/packages/RequireNamedArgs) nuget package.\nFor example, run the following command in the [NuGet Package Manager Console](https://docs.microsoft.com/en-us/nuget/tools/package-manager-console).\n\n```powershell\nInstall-Package RequireNamedArgs\n```\n\nThis will download all the binaries, and add necessary analyzer references to your project.\n\n## Configuration\n\nStarting in Visual Studio 2019 version 16.3, you can [configure the severity of analyzer rules, or diagnostics](https://learn.microsoft.com/en-us/visualstudio/code-quality/use-roslyn-analyzers?view=vs-2022#configure-severity-levels), in an EditorConfig file, from the light bulb menu, and the error list.\n\nYou can add the following to the `[*.cs]` section of your .editorconfig.\n\n```ini\n[*.cs]\ndotnet_diagnostic.RequireNamedArgs.severity = error\n```\n\nThe possible severity values are:\n- `error`\n- `warning`\n- `suggestion`\n- `silent`\n- `none`\n- `default` (in case of this analyzer, it's equal to `error`)\n\nPlease take a look at [the documentation](https://learn.microsoft.com/en-us/visualstudio/code-quality/use-roslyn-analyzers?view=vs-2022#configure-severity-levels) for a detailed description.\n\n## How does it work?\n\n1. This analyzer looks at an invocation expression (e.g., a method call).\n2. It then finds the method's definition.\n3. If the definition is marked with a `[RequireNamedArgs]` attribute,  \n   the analyzer requires every caller to provide names for the invocation's arguments.\n4. If the last parameter is `params`, the analyzer  \n   doesn't emit the diagnostic, as C# doesn't allow named arguments in this case.\n\n## Technical details\n\nThe analyzer, code-fix provider, and tests are implemented in F#\n\n# Thank you!\n\n- [John Koerner](https://github.com/johnkoerner) for [Creating a Code Analyzer using F#](https://johnkoerner.com/code-analysis/creating-a-code-analyzer-using-f/)\n- [Dustin Campbell](https://github.com/DustinCampbell) for [CSharpEssentials](https://github.com/DustinCampbell/CSharpEssentials)\n- [Alireza Habibi](https://github.com/alrz) for [CSharpUseNamedArgumentsCodeRefactoringProvider](https://github.com/dotnet/roslyn/blob/master/src/Features/CSharp/Portable/UseNamedArguments/CSharpUseNamedArgumentsCodeRefactoringProvider.cs) which provided very useful code examples.\n- [Steve Smith](https://ardalis.com/) for his article [Improve Tests with the Builder Pattern for Test Data](https://ardalis.com/improve-tests-with-the-builder-pattern-for-test-data).\n\n# License\n\nThe [RequireNamedArgs](https://github.com/mykolav/require-named-args-fs) analyzer and code-fix provider are licensed under the MIT license.  \nSo they can be used freely in commercial applications.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmykolav%2Frequire-named-args-fs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmykolav%2Frequire-named-args-fs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmykolav%2Frequire-named-args-fs/lists"}