1// Copyright 2023 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package analysisflags
6
7import (
8	"fmt"
9	"net/url"
10
11	"golang.org/x/tools/go/analysis"
12)
13
14// ResolveURL resolves the URL field for a Diagnostic from an Analyzer
15// and returns the URL. See Diagnostic.URL for details.
16func ResolveURL(a *analysis.Analyzer, d analysis.Diagnostic) (string, error) {
17	if d.URL == "" && d.Category == "" && a.URL == "" {
18		return "", nil // do nothing
19	}
20	raw := d.URL
21	if d.URL == "" && d.Category != "" {
22		raw = "#" + d.Category
23	}
24	u, err := url.Parse(raw)
25	if err != nil {
26		return "", fmt.Errorf("invalid Diagnostic.URL %q: %s", raw, err)
27	}
28	base, err := url.Parse(a.URL)
29	if err != nil {
30		return "", fmt.Errorf("invalid Analyzer.URL %q: %s", a.URL, err)
31	}
32	return base.ResolveReference(u).String(), nil
33}
34