Skip to content

feat(AC0005,AC0006): add using directives for codeunit variables in namespaced files - #609

Merged
Arthurvdv merged 6 commits into
mainfrom
feat/ac0005-ac0006-using-directive
Nov 13, 2026
Merged

Arthurvdv merged 6 commits into
mainfrom
feat/ac0005-ac0006-using-directive

Conversation

@Arthurvdv

@Arthurvdv Arthurvdv commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Summary

CodeFixes that declare a Codeunit "X" variable now make the type resolvable in files that declare an AL namespace by adding using <namespace>;. The logic that AC0006 carried privately (net8-only) moved to ALCops.Common and AC0005 uses it too.

Closes #271

Changes

  • Common: CompilationUnitSyntaxExtensions.AddUsingDirectiveIfNeeded (plus GetFileNamespace, GetImportedNamespaces, HasUsingDirective), MethodOrTriggerDeclarationSyntaxExtensions (find / add a codeunit variable), VariableDeclarationBaseSyntaxExtensions.IsCodeunitVariable, CodeunitVariableFactory, SyntaxFactoryHelper.NamespaceName, and a by-name object lookup in CompilationHelper / CompilationExtensions.GetApplicationObjectNamespaceWithReflection.
  • AC0005 (Translation Helper, namespace System.IO): adds the using in namespaced files.
  • AC0006 (Page Management): refactored onto the shared helpers; behaviour changes listed below.
  • AC0031: unchanged behaviour, calls the moved namespace helpers.
  • Fixtures: first use of RoslynTestKit's /*EOD*/ multi-document fixtures, so the target codeunit can live in a second file with its own namespace.
  • Drive-by: both test classes passed NotBlankRequiredOnPrimaryKeyField to TestCodeFix; replaced with the rule's own descriptor.

Design decisions

  • The namespace is resolved from the compilation (compiling module first, then references) instead of hardcoded. When the codeunit cannot be resolved or lives in the global namespace, the variable is added without a using.
  • Runs on netstandard2.1 too: the only floor-missing API (SyntaxFactory.ParseQualifiedName) is replaced by composing QualifiedName nodes.
  • Placement mirrors the SDK's internal NamespaceActionUtilities with 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.
  • Namespace test cases are gated at AL 12.0 (was 16.0) because the using logic now ships in every binary.

Out of scope

  • AC0004 is listed in the issue but has no CodeFix; a Confirm Management fix would be a separate task.
  • LC0003 and AC0031 handle namespaces by qualifying names and stay as they are.

Verification

  • dotnet build ALCops.sln, dotnet test ALCops.sln (all 2117 tests pass)
  • CI builds (-p:ContinuousIntegrationBuild=true) for Common and ApplicationCop on all three TFMs
  • GITHUB_ACTIONS=true dotnet test ... -p:NavTargetFramework=net8.0 for AC0005/AC0006
  • dotnet format ALCops.sln --verify-no-changes, Validate-Rules.ps1
  • Please watch the netstandard2.1 test leg for the namespace cases: it is the first time the namespace reflection shim runs there from a CodeFix.

Companion docs PR: ALCops/alcops.dev#218

🤖 Generated with Claude Code

Arthurvdv and others added 3 commits November 13, 2026 14:05
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>
Arthurvdv and others added 2 commits November 13, 2026 14:10
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>
@Arthurvdv
Arthurvdv merged commit 7c86384 into main Nov 13, 2026
53 checks passed
@Arthurvdv
Arthurvdv deleted the feat/ac0005-ac0006-using-directive branch November 13, 2026 14:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CodeFixes that add variables should handle namespace resolution (using directives)

1 participant