enable custom hints per api kind

This commit is contained in:
Evan Wallace
2021-04-22 14:14:34 -07:00
parent 610d49bc96
commit fb48d9bac8
6 changed files with 44 additions and 8 deletions

View File

@@ -117,6 +117,8 @@ var helpText = func(colors logger.Colors) string {
}
func main() {
logger.API = logger.CLIAPI
osArgs := os.Args[1:]
heapFile := ""
traceFile := ""

View File

@@ -47,6 +47,8 @@ type outgoingPacket struct {
}
func runService(sendPings bool) {
logger.API = logger.JSAPI
service := serviceType{
callbacks: make(map[uint32]responseCallback),
rebuilds: make(map[int]rebuildCallback),

View File

@@ -432,7 +432,14 @@ func parseFile(args parseArgs) {
}
if args.options.Platform != config.PlatformNode {
if _, ok := resolver.BuiltInNodeModules[record.Path.Text]; ok {
hint = " (set platform to \"node\" when building for node)"
switch logger.API {
case logger.CLIAPI:
hint = " (use \"--platform=node\" when building for node)"
case logger.JSAPI:
hint = " (use \"platform: 'node'\" when building for node)"
case logger.GoAPI:
hint = " (use \"Platform: api.PlatformNode\" when building for node)"
}
}
}
if absResolveDir == "" && pluginName != "" {
@@ -1912,9 +1919,18 @@ func (b *Bundle) Compile(log logger.Log, options config.Options, timer *helpers.
for _, outputFile := range outputFiles {
lowerAbsPath := lowerCaseAbsPathForWindows(outputFile.AbsPath)
if sourceIndex, ok := sourceAbsPaths[lowerAbsPath]; ok {
hint := ""
switch logger.API {
case logger.CLIAPI:
hint = " (use \"--allow-overwrite\" to allow this)"
case logger.JSAPI:
hint = " (use \"allowOverwrite: true\" to allow this)"
case logger.GoAPI:
hint = " (use \"AllowOverwrite: true\" to allow this)"
}
log.AddError(nil, logger.Loc{},
fmt.Sprintf("Refusing to overwrite input file %q without permission (enable \"allow overwrite\" to proceed)",
b.files[sourceIndex].inputFile.Source.PrettyPath))
fmt.Sprintf("Refusing to overwrite input file %q%s",
b.files[sourceIndex].inputFile.Source.PrettyPath, hint))
}
}
}

View File

@@ -1278,7 +1278,7 @@ func TestRequireFSBrowser(t *testing.T) {
AbsOutputFile: "/out.js",
Platform: config.PlatformBrowser,
},
expectedScanLog: `entry.js: error: Could not resolve "fs" (set platform to "node" when building for node)
expectedScanLog: `entry.js: error: Could not resolve "fs" (use "Platform: api.PlatformNode" when building for node)
`,
})
}
@@ -1335,7 +1335,7 @@ func TestImportFSBrowser(t *testing.T) {
AbsOutputFile: "/out.js",
Platform: config.PlatformBrowser,
},
expectedScanLog: `entry.js: error: Could not resolve "fs" (set platform to "node" when building for node)
expectedScanLog: `entry.js: error: Could not resolve "fs" (use "Platform: api.PlatformNode" when building for node)
`,
})
}
@@ -1396,7 +1396,7 @@ func TestExportFSBrowser(t *testing.T) {
AbsOutputFile: "/out.js",
Platform: config.PlatformBrowser,
},
expectedScanLog: `entry.js: error: Could not resolve "fs" (set platform to "node" when building for node)
expectedScanLog: `entry.js: error: Could not resolve "fs" (use "Platform: api.PlatformNode" when building for node)
`,
})
}
@@ -2646,7 +2646,8 @@ func TestNoOverwriteInputFileError(t *testing.T) {
Mode: config.ModeBundle,
AbsOutputDir: "/",
},
expectedCompileLog: "error: Refusing to overwrite input file: entry.js\n",
expectedCompileLog: `error: Refusing to overwrite input file "entry.js" (use "AllowOverwrite: true" to allow this)
`,
})
}

View File

@@ -349,6 +349,17 @@ func errorAndWarningSummary(errors int, warnings int, shownErrors int, shownWarn
}
}
type APIKind uint8
const (
GoAPI APIKind = iota
CLIAPI
JSAPI
)
// This can be used to customize error messages for the current API kind
var API APIKind
type TerminalInfo struct {
IsTTY bool
UseColorEscapes bool

View File

@@ -418,7 +418,11 @@ func validateDefines(
// The key must be a dot-separated identifier list
for _, part := range strings.Split(key, ".") {
if !js_lexer.IsIdentifier(part) {
log.AddError(nil, logger.Loc{}, fmt.Sprintf("Invalid define key: %q", key))
if part == key {
log.AddError(nil, logger.Loc{}, fmt.Sprintf("The define key %q must be a valid identifier", key))
} else {
log.AddError(nil, logger.Loc{}, fmt.Sprintf("The define key %q contains invalid identifier %q", key, part))
}
continue
}
}