Skip to content

refactor(translate): reduce cognitive complexity in export and reposync - #2157

Open
devin-ai-integration[bot] wants to merge 1 commit into
developfrom
refactor/sonar-export
Open

devin-ai-integration[bot] wants to merge 1 commit into
developfrom
refactor/sonar-export

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

What and why

SonarCloud's analysis of develop reports six open findings under internal/translate: five functions over the cognitive-complexity threshold (go:S3776) and one empty loop body (go:S108). This PR fixes each at the root by extracting one or two well-named helpers per concern. No behaviour changes: evaluation order and every early return are kept, no test changes, no suppressions or rule exclusions.

File Rule Fix
internal/translate/export/api_json_derived.go completeDerived S3776 (37) The per-property evaluation (evaluator lookup, the three omission reasons, the single-valued collapse) moved to completedValue, which returns the spelled value or reports the omission and false; the loop keeps only the "already stated" skip and the default-value shortcut.
internal/translate/export/behavior.go behaviorHead S3776 (35) The mSend case body moved to sendHead, which builds the declaration, send, and the payload/via/to clauses; the switch case calls it and wraps the (head, true, err) triple.
internal/translate/export/notes.go tokensIn S108 The scan that lived in the for header is a loop with a body (end++) over the same bound, with the same result.
internal/translate/export/rdf_in.go reservedNames S3776 (34) annotatedForms splits an owner's annotatedElement objects into written names and IRIs; declaredNamesOf lists an element's declared name then short name. statedForm, which spelled the same two-property loop, now calls declaredNamesOf too.
internal/translate/export/rdf_normative.go materializeRelationships S3776 (36) The local pending type is the file-level pendingRelationship; pendingRelationships collects the targets and the two position maps; pendingAt resolves a spelled head item to its pending index (an about item by itself, other clauses by kind and object). The emit order (spelled head items first, then every remaining target) is unchanged.
internal/translate/interop/reposync/diff.go viewOf S3776 (39) subjectRoles indexes library references and unheld subjects; carriedContent maps a content triple through the Carrier (a nil carrier carries all, the error keeps its view.name() / property label wrapping); settleViews sorts property values and derives name, normative, declared and mintable, returning the AmbiguousMetaclassError where the loop did.

The gocognit approximation of Sonar's metric now reports 13, 26, 2, 25, 21 and 28 for the six functions (was 37, 35, –, 34, 36, 39).

How it was verified

All on the pushed head, locally:

  • go build ./..., go vet ./..., gofmt -l . (empty), make lint (staticcheck + gosec): clean.
  • go test ./internal/translate/... ./tests/export/..., go test ./tests/hygiene: pass.
  • make test-quick SHARDS="export export-library export-examples translate": pass.
  • go test ./...: pass (cmd/sysml, tests/codegen and tests/export hit the default 10m per-package timeout under the full-suite load on an 8-core box and passed on a rerun with -timeout 45m, as CI runs them).
  • go run -C tools ./cmd/pilot-apijson-diff -update over all nine roots (training, pilot and spec examples downloaded): the baseline under docs/project/api-json-differential/ is byte-identical to develop (935 files; every count unchanged, the digest included), so no baseline change is in the diff. go test -C tools ./referee/apijson: pass.
  • OPENSYSML_REQUIRE_TRAINING_CORPUS=1 OPENSYSML_REQUIRE_PILOT_CORPORA=1 OPENSYSML_REQUIRE_SPEC_EXAMPLES=1 go test ./tests/corpus -run TestCorpusRoundTrip: pass, no per-file verdict moved.
  • Comment cap (git diff --merge-base origin/develop | awk ...), scripts/check-doc-ids.py, scripts/changelog.py check: clean.
  • Self-review per .agents/skills/reviewing-pull-requests/SKILL.md: each helper was checked against its original inline block for the same evaluation order, early returns and error wrapping; every caller is the one function it was extracted from (declaredNamesOf also replaces the identical loop in statedForm).

Dry merges (git merge-tree --write-tree HEAD pr/N) against the open PRs touching these files: #2143 and #2133 clean; #2142 and #2042 conflict only on files they already conflict with develop on (API-JSON differential baseline files, and for #2042 also annotations.go, derived_census_baseline.json and .golden.ttl.sha256 fixtures), none of them touched here.

Checklist

  • make test and make lint pass locally
  • Tests added or updated for the change (pure refactor; existing tests cover every path)
  • Documentation extended where it already covers the surface (see CONTRIBUTING.md)
  • Changelog entry added as changes/unreleased/<slug>.<section>.md, not as an edit to CHANGELOG.md
  • baselines regenerated and make docs-counts run if a gate count moved (compliance rows need nothing: the census is counted at docs build)
  • No internal work-item labels (waves, slices, F4, K5) in the body, docs, or changelog

Link to Devin session: https://nasa-jpl-demo.devinenterprise.com/sessions/e21f30dd70b84ec387741f788db727ff
Open in Devin Desktop: https://nasa-jpl-demo.devinenterprise.com/desktop/session/e21f30dd70b84ec387741f788db727ff?variant=devin
Requested by: @HuiJun


🟢 Devin Review found nothing to fix · View findings

Devin Review

Extract one helper per concern from the five functions SonarCloud flags over the cognitive-complexity threshold (go:S3776) and give the token scan in notes.go tokensIn a loop body (go:S108). Evaluation order and every early return are unchanged.

- api_json_derived.go: completeDerived -> completedValue
- behavior.go: behaviorHead -> sendHead
- rdf_in.go: reservedNames -> annotatedForms, declaredNamesOf
- rdf_normative.go: materializeRelationships -> pendingRelationships, pendingAt
- reposync/diff.go: viewOf -> subjectRoles, carriedContent, settleViews

Co-Authored-By: jason.han <hanhuijun@gmail.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

This branch has not been deployed

No deployments
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.

1 participant