2017-06-26 23:46:34 +02:00
|
|
|
// Copyright 2016-2017, Pulumi Corporation. All rights reserved.
|
2016-11-15 20:30:34 +01:00
|
|
|
|
|
|
|
package diag
|
|
|
|
|
|
|
|
// ID is a unique diagnostics identifier.
|
|
|
|
type ID int
|
|
|
|
|
|
|
|
// Diag is an instance of an error or warning generated by the compiler.
|
|
|
|
type Diag struct {
|
|
|
|
ID ID // a unique identifier for this diagnostic.
|
|
|
|
Message string // a human-friendly message for this diagnostic.
|
2017-11-14 20:26:41 +01:00
|
|
|
Raw bool // true if this diagnostic should not be formatted when displayed.
|
2016-11-16 02:42:22 +01:00
|
|
|
Doc *Document // the document in which this diagnostic occurred.
|
|
|
|
Loc *Location // the document location at which this diagnostic occurred.
|
2016-11-15 20:30:34 +01:00
|
|
|
}
|
|
|
|
|
2017-02-23 03:53:36 +01:00
|
|
|
// Message returns an anonymous diagnostic message without any source or ID information.
|
2017-06-03 03:35:53 +02:00
|
|
|
func Message(msg string) *Diag {
|
|
|
|
return &Diag{Message: msg}
|
2017-02-23 03:53:36 +01:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:26:41 +01:00
|
|
|
// RawMessage returns an anonymous diagnostic message without any source or ID information that will not be rendered with Sprintf.
|
|
|
|
func RawMessage(msg string) *Diag {
|
|
|
|
return &Diag{Message: msg, Raw: true}
|
|
|
|
}
|
|
|
|
|
2016-11-23 16:44:03 +01:00
|
|
|
// Diagable can be used to determine a diagnostic's position.
|
|
|
|
type Diagable interface {
|
|
|
|
Where() (*Document, *Location)
|
2016-11-15 20:30:34 +01:00
|
|
|
}
|
|
|
|
|
2016-11-23 16:44:03 +01:00
|
|
|
// At adds a position to an existing diagnostic, retaining its ID and message.
|
|
|
|
func (diag *Diag) At(d Diagable) *Diag {
|
2017-02-28 19:32:24 +01:00
|
|
|
var doc *Document
|
|
|
|
var loc *Location
|
|
|
|
if d != nil {
|
|
|
|
doc, loc = d.Where()
|
|
|
|
}
|
2016-11-15 20:30:34 +01:00
|
|
|
return &Diag{
|
|
|
|
ID: diag.ID,
|
|
|
|
Message: diag.Message,
|
2016-11-16 02:42:22 +01:00
|
|
|
Doc: doc,
|
2016-11-23 16:44:03 +01:00
|
|
|
Loc: loc,
|
2016-11-16 02:42:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-28 00:42:39 +01:00
|
|
|
// AtFile adds a file to an existing diagnostic, retaining its ID and message.
|
2016-11-23 16:44:03 +01:00
|
|
|
func (diag *Diag) AtFile(file string) *Diag {
|
2016-11-16 02:42:22 +01:00
|
|
|
return &Diag{
|
|
|
|
ID: diag.ID,
|
|
|
|
Message: diag.Message,
|
2016-11-23 16:44:03 +01:00
|
|
|
Doc: NewDocument(file),
|
2016-11-15 20:30:34 +01:00
|
|
|
}
|
|
|
|
}
|