New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Copy metadata associated with a node in the optimized result #6593
base: main
Are you sure you want to change the base?
Changes from all commits
0c32d66
deadc77
3dd08a8
0b197b0
6107e09
8594f86
5cae5d0
525f449
64ef639
cbeea74
4ccd384
bd10ba5
31fe465
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,6 +111,11 @@ func (a *Annotations) EndLoc() *Location { | |
return a.comments[count-1].Location | ||
} | ||
|
||
// Comments returns the comments associated with this annotation. | ||
func (a *Annotations) Comments() []*Comment { | ||
return a.comments | ||
} | ||
|
||
// Compare returns an integer indicating if a is less than, equal to, or greater | ||
// than other. | ||
func (a *Annotations) Compare(other *Annotations) int { | ||
|
@@ -181,6 +186,26 @@ func (a *Annotations) GetTargetPath() Ref { | |
} | ||
} | ||
|
||
func (a *Annotations) GetPackage() *Package { | ||
switch n := a.node.(type) { | ||
case *Package: | ||
return n | ||
case *Rule: | ||
return n.Module.Package | ||
default: | ||
return nil | ||
} | ||
} | ||
|
||
func (a *Annotations) GetRule() *Rule { | ||
switch n := a.node.(type) { | ||
case *Rule: | ||
return n | ||
default: | ||
return nil | ||
} | ||
} | ||
|
||
func (a *Annotations) setJSONOptions(opts astJSON.Options) { | ||
a.jsonOptions = opts | ||
if a.Location != nil { | ||
|
@@ -509,6 +534,37 @@ func (a *Annotations) toObject() (*Object, *Error) { | |
return &obj, nil | ||
} | ||
|
||
func attachRuleAnnotations(mod *Module) { | ||
// make a copy of the annotations | ||
cpy := make([]*Annotations, len(mod.Annotations)) | ||
for i, a := range mod.Annotations { | ||
cpy[i] = a.Copy(a.node) | ||
} | ||
|
||
for _, rule := range mod.Rules { | ||
var j int | ||
var found bool | ||
for i, a := range cpy { | ||
if rule.Loc().Row > a.Location.Row { | ||
if rule.Ref().Equal(a.GetTargetPath()) { | ||
rule.Annotations = append(rule.Annotations, a) | ||
|
||
if a.Scope == annotationScopeRule { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do I get this right?:
This is probably not an actual issue, as we also match the rule ref and annotation target path, so any straggling document-scoped annotations won't be attached to any succeeding rules with unmatched ref/path. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @anderseknert, is there a regal rule for having multiple rule-scope annotation blocks for a rule? If not, might be a good candidate for a new linting rule, as this is a likely user mistake. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
My thinking here is: We attach a rule-scope annotation block that precedes the rule to that specific rule. That annotation will NOT apply to any other rule. So we remove it from |
||
j = i | ||
found = true | ||
} | ||
} | ||
} else { | ||
break | ||
} | ||
} | ||
|
||
if found && j < len(cpy) { | ||
cpy = append(cpy[:j], cpy[j+1:]...) | ||
} | ||
} | ||
} | ||
|
||
func attachAnnotationsNodes(mod *Module) Errors { | ||
var errs Errors | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there situations where
a == nil
?attachRuleAnnotations()
seems to rely on this fact. Could we simplify that function by simply iterating over all the module's annotations, callGetRule()
on them, and attach the annotation to any non-nil return value?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, never mind. I just realized we need to attach document-scoped annotations to all rules with the same path, and
n
would only represent one of those.I suppose we have a way to make sure that we don't emit the document-scope annotation block for each moved rule when rewriting 馃 . Or maybe that makes little difference, as they'll be identical anyways ..