Repository navigation
feat(AC0005,AC0006): add using directives for codeunit variables in namespaced files - #609
Merged
Merged
Conversation
Move the using-directive insertion and the codeunit-variable helpers from the Page Management CodeFix into ALCops.Common so every CodeFix that declares a codeunit variable can reuse them. The namespace of the referenced codeunit is now resolved from the compilation instead of a hardcoded constant; when it cannot be resolved, no using is added. The using placement mirrors the SDK's own add-using action with public API only, so it also runs on the netstandard2.1 binary. It skips the file's own namespace, dedupes case-insensitively, sorts with the AL name comparer and appends after the last using when a preprocessor directive is present. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…d files The CodeFix declared TranslationHelper: Codeunit "Translation Helper" without importing its namespace, which left AL0185 behind in files that declare a namespace. It now uses the shared Common helpers: the codeunit is looked up in the compilation and a using for its namespace is added when the file declares a different namespace and does not import it yet. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Skip the namespace lookup when the fix reuses an existing variable or the file declares no namespace, keep the type-reference builder private, and record the same-name codeunit clash as an accepted limitation. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
A directive on the line after the last using belongs to the next token, so appending the new using there could place it inside an #if block. The fallback now inserts before the first using in that case. Adds fixtures for a reused variable, the assignment form and a codeunit in the global namespace, drops a single-caller public pass-through and derives the duplicate-using check from the imported-namespace enumeration. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
CodeFixes that declare a
Codeunit "X"variable now make the type resolvable in files that declare an ALnamespaceby addingusing <namespace>;. The logic that AC0006 carried privately (net8-only) moved toALCops.Commonand AC0005 uses it too.Closes #271
Changes
CompilationUnitSyntaxExtensions.AddUsingDirectiveIfNeeded(plusGetFileNamespace,GetImportedNamespaces,HasUsingDirective),MethodOrTriggerDeclarationSyntaxExtensions(find / add a codeunit variable),VariableDeclarationBaseSyntaxExtensions.IsCodeunitVariable,CodeunitVariableFactory,SyntaxFactoryHelper.NamespaceName, and a by-name object lookup inCompilationHelper/CompilationExtensions.GetApplicationObjectNamespaceWithReflection.Translation Helper, namespaceSystem.IO): adds the using in namespaced files.Page Management): refactored onto the shared helpers; behaviour changes listed below./*EOD*/multi-document fixtures, so the target codeunit can live in a second file with its own namespace.NotBlankRequiredOnPrimaryKeyFieldtoTestCodeFix; replaced with the rule's own descriptor.Design decisions
SyntaxFactory.ParseQualifiedName) is replaced by composingQualifiedNamenodes.NamespaceActionUtilitieswith public API, with three refinements: skip when the file is in the target namespace, case-insensitive duplicate check and sort (SemanticFacts.NameComparer, matching CodeCop AA0477), and append after the last using when any using carries a preprocessor directive.Out of scope
Verification
dotnet build ALCops.sln,dotnet test ALCops.sln(all 2117 tests pass)-p:ContinuousIntegrationBuild=true) for Common and ApplicationCop on all three TFMsGITHUB_ACTIONS=true dotnet test ... -p:NavTargetFramework=net8.0for AC0005/AC0006dotnet format ALCops.sln --verify-no-changes,Validate-Rules.ps1Companion docs PR: ALCops/alcops.dev#218
🤖 Generated with Claude Code