logger

package
v0.24.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 20, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const SupportsColorEscapes = true

Variables

View Source
var TerminalColors = Colors{
	Reset:     "\033[0m",
	Bold:      "\033[1m",
	Dim:       "\033[37m",
	Underline: "\033[4m",

	Red:   "\033[31m",
	Green: "\033[32m",
	Blue:  "\033[34m",

	Cyan:    "\033[36m",
	Magenta: "\033[35m",
	Yellow:  "\033[33m",

	RedBgRed:     "\033[41;31m",
	RedBgWhite:   "\033[41;97m",
	GreenBgGreen: "\033[42;32m",
	GreenBgWhite: "\033[42;97m",
	BlueBgBlue:   "\033[44;34m",
	BlueBgWhite:  "\033[44;97m",

	CyanBgCyan:       "\033[46;36m",
	CyanBgBlack:      "\033[46;30m",
	MagentaBgMagenta: "\033[45;35m",
	MagentaBgBlack:   "\033[45;30m",
	YellowBgYellow:   "\033[43;33m",
	YellowBgBlack:    "\033[43;30m",
}

Functions

func MsgIDToString added in v0.14.43

func MsgIDToString(id MsgID) string

func PlatformIndependentPathDirBaseExt added in v0.8.22

func PlatformIndependentPathDirBaseExt(path string) (dir string, base string, ext string)

This has a custom implementation instead of using "filepath.Dir/Base/Ext" because it should work the same on Unix and Windows. These names end up in the generated output and the generated output should not depend on the OS.

func PrintErrorToStderr

func PrintErrorToStderr(osArgs []string, text string)

func PrintErrorWithNoteToStderr added in v0.16.13

func PrintErrorWithNoteToStderr(osArgs []string, text string, note string)

func PrintMessageToStderr added in v0.7.0

func PrintMessageToStderr(osArgs []string, msg Msg)

func PrintSummary added in v0.8.28

func PrintSummary(useColor UseColor, table SummaryTable, start *time.Time)

func PrintText added in v0.8.21

func PrintText(file *os.File, level LogLevel, osArgs []string, callback func(Colors) string)

func PrintTextWithColor added in v0.8.45

func PrintTextWithColor(file *os.File, useColor UseColor, callback func(Colors) string)

func StringToMsgIDs added in v0.14.43

func StringToMsgIDs(str string, logLevel LogLevel, overrides map[MsgID]LogLevel)

Types

type APIKind added in v0.11.13

type APIKind uint8
const (
	GoAPI APIKind = iota
	CLIAPI
	JSAPI
)
var API APIKind

This can be used to customize error messages for the current API kind

type Colors added in v0.8.12

type Colors struct {
	Reset     string
	Bold      string
	Dim       string
	Underline string

	Red   string
	Green string
	Blue  string

	Cyan    string
	Magenta string
	Yellow  string

	RedBgRed     string
	RedBgWhite   string
	GreenBgGreen string
	GreenBgWhite string
	BlueBgBlue   string
	BlueBgWhite  string

	CyanBgCyan       string
	CyanBgBlack      string
	MagentaBgMagenta string
	MagentaBgBlack   string
	YellowBgYellow   string
	YellowBgBlack    string
}

type DeferLogKind added in v0.11.17

type DeferLogKind uint8
const (
	DeferLogAll DeferLogKind = iota
	DeferLogNoVerboseOrDebug
)

type ImportAttribute added in v0.19.7

type ImportAttribute struct {
	Key   string
	Value string
}

type ImportAttributes added in v0.19.7

type ImportAttributes struct {
	// contains filtered or unexported fields
}

We rely on paths as map keys. Go doesn't support custom hash codes and only implements hash codes for certain types. In particular, hash codes are implemented for strings but not for arrays of strings. So we have to pack these import attributes into a string.

func EncodeImportAttributes added in v0.19.7

func EncodeImportAttributes(value map[string]string) ImportAttributes

func (ImportAttributes) DecodeIntoArray added in v0.21.4

func (attrs ImportAttributes) DecodeIntoArray() (result []ImportAttribute)

This returns a sorted array instead of a map to make determinism easier

func (ImportAttributes) DecodeIntoMap added in v0.21.4

func (attrs ImportAttributes) DecodeIntoMap() (result map[string]string)

type LineColumnTracker added in v0.11.19

type LineColumnTracker struct {
	// contains filtered or unexported fields
}

It's not common for large files to have many warnings. But when it happens, we want to make sure that it's not too slow. Source code locations are represented as byte offsets for compactness but transforming these to line/column locations for warning messages requires scanning through the file. A naive approach for this would cause O(n^2) scanning time for n warnings distributed throughout the file.

Warnings are typically generated sequentially as the file is scanned. So one way of optimizing this is to just start scanning from where we left off last time instead of always starting from the beginning of the file. That's what this object does.

Another option could be to eagerly populate an array of line/column offsets and then use binary search for each query. This might slow down the common case of a file with only at most a few warnings though, so think before optimizing too much. Performance in the zero or one warning case is by far the most important.

func MakeLineColumnTracker added in v0.11.19

func MakeLineColumnTracker(source *Source) LineColumnTracker

func (*LineColumnTracker) MsgData added in v0.14.1

func (tracker *LineColumnTracker) MsgData(r Range, text string) MsgData

func (*LineColumnTracker) MsgLocationOrNil added in v0.14.1

func (tracker *LineColumnTracker) MsgLocationOrNil(r Range) *MsgLocation

type Loc

type Loc struct {
	// This is the 0-based index of this location from the start of the file, in bytes
	Start int32
}

func RemapStringInJSLoc added in v0.15.11

func RemapStringInJSLoc(table []StringInJSTableEntry, innerLoc Loc) Loc

type Log

type Log struct {
	AddMsg    func(Msg)
	HasErrors func() bool
	Peek      func() []Msg

	Done func() []Msg

	Level     LogLevel
	Overrides map[MsgID]LogLevel
}

func NewDeferLog

func NewDeferLog(kind DeferLogKind, overrides map[MsgID]LogLevel) Log

func NewStderrLog

func NewStderrLog(options OutputOptions) Log

func NewStringInJSLog added in v0.15.0

func NewStringInJSLog(log Log, outerTracker *LineColumnTracker, table []StringInJSTableEntry) Log

func (Log) AddError

func (log Log) AddError(tracker *LineColumnTracker, r Range, text string)

func (Log) AddErrorWithNotes added in v0.8.8

func (log Log) AddErrorWithNotes(tracker *LineColumnTracker, r Range, text string, notes []MsgData)

func (Log) AddID added in v0.14.42

func (log Log) AddID(id MsgID, kind MsgKind, tracker *LineColumnTracker, r Range, text string)

func (Log) AddIDWithNotes added in v0.14.42

func (log Log) AddIDWithNotes(id MsgID, kind MsgKind, tracker *LineColumnTracker, r Range, text string, notes []MsgData)

func (Log) AddMsgID added in v0.14.43

func (log Log) AddMsgID(id MsgID, msg Msg)

type LogLevel

type LogLevel int8
const (
	LevelNone LogLevel = iota
	LevelVerbose
	LevelDebug
	LevelInfo
	LevelWarning
	LevelError
	LevelSilent
)

type Msg

type Msg struct {
	Notes      []MsgData
	PluginName string
	Data       MsgData
	Kind       MsgKind
	ID         MsgID
}

func (Msg) String

func (msg Msg) String(options OutputOptions, terminalInfo TerminalInfo) string

type MsgData added in v0.8.8

type MsgData struct {
	// Optional user-specified data that is passed through unmodified
	UserDetail interface{}

	Location *MsgLocation
	Text     string

	DisableMaximumWidth bool
}

type MsgDetail

type MsgDetail struct {
	SourceBefore string
	SourceMarked string
	SourceAfter  string

	Indent     string
	Marker     string
	Suggestion string

	ContentAfter string

	Path   string
	Line   int
	Column int
}

type MsgID added in v0.14.42

type MsgID = uint8

Most non-error log messages are given a message ID that can be used to set the log level for that message. Errors do not get a message ID because you cannot turn errors into non-errors (otherwise the build would incorrectly succeed). Some internal log messages do not get a message ID because they are part of verbose and/or internal debugging output. These messages use "MsgID_None" instead.

const (
	MsgID_None MsgID = iota

	// JavaScript
	MsgID_JS_AssertToWith
	MsgID_JS_AssertTypeJSON
	MsgID_JS_AssignToConstant
	MsgID_JS_AssignToDefine
	MsgID_JS_AssignToImport
	MsgID_JS_CallImportNamespace
	MsgID_JS_ClassNameWillThrow
	MsgID_JS_CommonJSVariableInESM
	MsgID_JS_DeleteSuperProperty
	MsgID_JS_DirectEval
	MsgID_JS_DuplicateCase
	MsgID_JS_DuplicateClassMember
	MsgID_JS_DuplicateObjectKey
	MsgID_JS_EmptyImportMeta
	MsgID_JS_EqualsNaN
	MsgID_JS_EqualsNegativeZero
	MsgID_JS_EqualsNewObject
	MsgID_JS_HTMLCommentInJS
	MsgID_JS_ImpossibleTypeof
	MsgID_JS_IndirectRequire
	MsgID_JS_PrivateNameWillThrow
	MsgID_JS_SemicolonAfterReturn
	MsgID_JS_SuspiciousBooleanNot
	MsgID_JS_SuspiciousDefine
	MsgID_JS_SuspiciousLogicalOperator
	MsgID_JS_SuspiciousNullishCoalescing
	MsgID_JS_ThisIsUndefinedInESM
	MsgID_JS_UnsupportedDynamicImport
	MsgID_JS_UnsupportedJSXComment
	MsgID_JS_UnsupportedRegExp
	MsgID_JS_UnsupportedRequireCall

	// CSS
	MsgID_CSS_CSSSyntaxError
	MsgID_CSS_InvalidAtCharset
	MsgID_CSS_InvalidAtImport
	MsgID_CSS_InvalidAtLayer
	MsgID_CSS_InvalidCalc
	MsgID_CSS_JSCommentInCSS
	MsgID_CSS_UndefinedComposesFrom
	MsgID_CSS_UnsupportedAtCharset
	MsgID_CSS_UnsupportedAtNamespace
	MsgID_CSS_UnsupportedCSSProperty
	MsgID_CSS_UnsupportedCSSNesting

	// Bundler
	MsgID_Bundler_AmbiguousReexport
	MsgID_Bundler_DifferentPathCase
	MsgID_Bundler_EmptyGlob
	MsgID_Bundler_IgnoredBareImport
	MsgID_Bundler_IgnoredDynamicImport
	MsgID_Bundler_ImportIsUndefined
	MsgID_Bundler_RequireResolveNotExternal

	// Source maps
	MsgID_SourceMap_InvalidSourceMappings
	MsgID_SourceMap_SectionsInSourceMap
	MsgID_SourceMap_MissingSourceMap
	MsgID_SourceMap_UnsupportedSourceMapComment

	// package.json
	MsgID_PackageJSON_FIRST // Keep this first
	MsgID_PackageJSON_DeadCondition
	MsgID_PackageJSON_InvalidBrowser
	MsgID_PackageJSON_InvalidImportsOrExports
	MsgID_PackageJSON_InvalidSideEffects
	MsgID_PackageJSON_InvalidType
	MsgID_PackageJSON_LAST // Keep this last

	// tsconfig.json
	MsgID_TSConfigJSON_FIRST // Keep this first
	MsgID_TSConfigJSON_Cycle
	MsgID_TSConfigJSON_InvalidImportsNotUsedAsValues
	MsgID_TSConfigJSON_InvalidJSX
	MsgID_TSConfigJSON_InvalidPaths
	MsgID_TSConfigJSON_InvalidTarget
	MsgID_TSConfigJSON_InvalidTopLevelOption
	MsgID_TSConfigJSON_Missing
	MsgID_TSConfigJSON_LAST // Keep this last

	MsgID_END // Keep this at the end (used only for tests)
)

func StringToMaximumMsgID added in v0.14.45

func StringToMaximumMsgID(id string) MsgID

Some message IDs are more diverse internally than externally (in case we want to expand the set of them later on). So just map these to the largest one arbitrarily since you can't tell the difference externally anyway.

type MsgKind

type MsgKind uint8
const (
	Error MsgKind = iota
	Warning
	Info
	Note
	Debug
	Verbose
)

func (MsgKind) Icon added in v0.14.1

func (kind MsgKind) Icon() string

func (MsgKind) String added in v0.8.8

func (kind MsgKind) String() string

type MsgLocation

type MsgLocation struct {
	File       string
	Namespace  string
	LineText   string
	Suggestion string
	Line       int // 1-based
	Column     int // 0-based, in bytes
	Length     int // in bytes
}

type OutputOptions added in v0.8.28

type OutputOptions struct {
	MessageLimit  int
	IncludeSource bool
	Color         UseColor
	LogLevel      LogLevel
	Overrides     map[MsgID]LogLevel
}

func OutputOptionsForArgs added in v0.8.28

func OutputOptionsForArgs(osArgs []string) OutputOptions

type Path

type Path struct {
	Text      string
	Namespace string

	// This feature was added to support ancient CSS libraries that append things
	// like "?#iefix" and "#icons" to some of their import paths as a hack for IE6.
	// The intent is for these suffix parts to be ignored but passed through to
	// the output. This is supported by other bundlers, so we also support this.
	IgnoredSuffix string

	// Import attributes (the "with" keyword after an import) can affect path
	// resolution. In other words, two paths in the same file that are otherwise
	// equal but that have different import attributes may resolve to different
	// paths.
	ImportAttributes ImportAttributes

	Flags PathFlags
}

This is used to represent both file system paths (Namespace == "file") and abstract module paths (Namespace != "file"). Abstract module paths represent "virtual modules" when used for an input file and "package paths" when used to represent an external module.

func (Path) IsDisabled added in v0.8.44

func (p Path) IsDisabled() bool

type PathFlags added in v0.8.44

type PathFlags uint8
const (
	// This corresponds to a value of "false' in the "browser" package.json field
	PathDisabled PathFlags = 1 << iota
)

type Range

type Range struct {
	Loc Loc
	Len int32
}

func (Range) End

func (r Range) End() int32

func (*Range) ExpandBy added in v0.18.19

func (a *Range) ExpandBy(b Range)

type SortableMsgs added in v0.8.8

type SortableMsgs []Msg

This type is just so we can use Go's native sort function

func (SortableMsgs) Len added in v0.8.8

func (a SortableMsgs) Len() int

func (SortableMsgs) Less added in v0.8.8

func (a SortableMsgs) Less(i int, j int) bool

func (SortableMsgs) Swap added in v0.8.8

func (a SortableMsgs) Swap(i int, j int)

type Source

type Source struct {
	// This is used for error messages and the metadata JSON file.
	//
	// This is a mostly platform-independent path. It's relative to the current
	// working directory and always uses standard path separators. Use this for
	// referencing a file in all output data. These paths still use the original
	// case of the path so they may still work differently on file systems that
	// are case-insensitive vs. case-sensitive.
	PrettyPath string

	// An identifier that is mixed in to automatically-generated symbol names to
	// improve readability. For example, if the identifier is "util" then the
	// symbol for an "export default" statement will be called "util_default".
	IdentifierName string

	Contents string

	// This is used as a unique key to identify this source file. It should never
	// be shown to the user (e.g. never print this to the terminal).
	//
	// If it's marked as an absolute path, it's a platform-dependent path that
	// includes environment-specific things such as Windows backslash path
	// separators and potentially the user's home directory. Only use this for
	// passing to syscalls for reading and writing to the file system. Do not
	// include this in any output data.
	//
	// If it's marked as not an absolute path, it's an opaque string that is used
	// to refer to an automatically-generated module.
	KeyPath Path

	Index uint32
}

func (*Source) CommentTextWithoutIndent added in v0.16.14

func (s *Source) CommentTextWithoutIndent(r Range) string

func (*Source) LocBeforeWhitespace added in v0.14.1

func (s *Source) LocBeforeWhitespace(loc Loc) Loc

func (*Source) RangeOfLegacyOctalEscape added in v0.8.48

func (s *Source) RangeOfLegacyOctalEscape(loc Loc) (r Range)

func (*Source) RangeOfNumber

func (s *Source) RangeOfNumber(loc Loc) (r Range)

func (*Source) RangeOfOperatorAfter added in v0.7.2

func (s *Source) RangeOfOperatorAfter(loc Loc, op string) Range

func (*Source) RangeOfOperatorBefore added in v0.7.2

func (s *Source) RangeOfOperatorBefore(loc Loc, op string) Range

func (*Source) RangeOfString

func (s *Source) RangeOfString(loc Loc) Range

func (*Source) TextForRange

func (s *Source) TextForRange(r Range) string

type Span added in v0.12.20

type Span struct {
	Text  string
	Range Range
}

type StringInJSTableEntry added in v0.15.11

type StringInJSTableEntry struct {
	// contains filtered or unexported fields
}

func GenerateStringInJSTable added in v0.15.11

func GenerateStringInJSTable(outerContents string, outerStringLiteralLoc Loc, innerContents string) (table []StringInJSTableEntry)

For Yarn PnP we sometimes parse JSON embedded in a JS string. This generates a table that remaps locations inside the embedded JSON string literal into locations in the actual JS file, which makes them easier to understand.

type SummaryTable added in v0.8.28

type SummaryTable []SummaryTableEntry

This type is just so we can use Go's native sort function

func (SummaryTable) Len added in v0.8.28

func (t SummaryTable) Len() int

func (SummaryTable) Less added in v0.8.28

func (t SummaryTable) Less(i int, j int) bool

func (SummaryTable) Swap added in v0.8.28

func (t SummaryTable) Swap(i int, j int)

type SummaryTableEntry added in v0.8.28

type SummaryTableEntry struct {
	Dir         string
	Base        string
	Size        string
	Bytes       int
	IsSourceMap bool
}

type TerminalInfo

type TerminalInfo struct {
	IsTTY           bool
	UseColorEscapes bool
	Width           int
	Height          int
}

func GetTerminalInfo

func GetTerminalInfo(file *os.File) (info TerminalInfo)

type UseColor added in v0.8.28

type UseColor uint8
const (
	ColorIfTerminal UseColor = iota
	ColorNever
	ColorAlways
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL