Skip to content

Commit

Permalink
fix typos for import errors as well as warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jul 25, 2023
1 parent af2bc58 commit abf1dc2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
7 changes: 4 additions & 3 deletions internal/bundler_tests/bundler_default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -835,18 +835,19 @@ func TestExportMissingES6(t *testing.T) {
console.log(ns)
`,
"/foo.js": `
export {nope} from './bar'
export {buton} from './bar'
`,
"/bar.js": `
export const yep = 123
export const button = 123
`,
},
entryPaths: []string{"/entry.js"},
options: config.Options{
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
},
expectedCompileLog: `foo.js: ERROR: No matching export in "bar.js" for import "nope"
expectedCompileLog: `foo.js: ERROR: No matching export in "bar.js" for import "buton"
bar.js: NOTE: Did you mean to import "button" instead?
`,
})
}
Expand Down
54 changes: 32 additions & 22 deletions internal/linker/linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2544,31 +2544,19 @@ loop:
if helpers.IsInsideNodeModules(trackerFile.InputFile.Source.KeyPath.Text) {
msg.Kind = logger.Debug
}

// Attempt to correct an import name with a typo
repr := nextFile.Repr.(*graph.JSRepr)
if repr.Meta.ResolvedExportTypos == nil {
valid := make([]string, 0, len(repr.Meta.ResolvedExports))
for alias := range repr.Meta.ResolvedExports {
valid = append(valid, alias)
}
sort.Strings(valid)
typos := helpers.MakeTypoDetector(valid)
repr.Meta.ResolvedExportTypos = &typos
}
if corrected, ok := repr.Meta.ResolvedExportTypos.MaybeCorrectTypo(namedImport.Alias); ok {
msg.Data.Location.Suggestion = corrected
export := repr.Meta.ResolvedExports[corrected]
importedFile := &c.graph.Files[export.SourceIndex]
msg.Notes = append(msg.Notes, importedFile.LineColumnTracker().MsgData(
js_lexer.RangeOfIdentifier(importedFile.InputFile.Source, export.NameLoc),
fmt.Sprintf("Did you mean to import %q instead?", corrected)))
}
c.maybeCorrectObviousTypo(nextFile.Repr.(*graph.JSRepr), namedImport.Alias, &msg)
c.log.AddMsgID(logger.MsgID_Bundler_ImportIsUndefined, msg)
}
} else {
c.log.AddError(trackerFile.LineColumnTracker(), r, fmt.Sprintf("No matching export in %q for import %q",
c.graph.Files[nextTracker.sourceIndex].InputFile.Source.PrettyPath, namedImport.Alias))
nextFile := &c.graph.Files[nextTracker.sourceIndex].InputFile
msg := logger.Msg{
Kind: logger.Error,
Data: trackerFile.LineColumnTracker().MsgData(r, fmt.Sprintf(
"No matching export in %q for import %q",
nextFile.Source.PrettyPath, namedImport.Alias)),
}
c.maybeCorrectObviousTypo(nextFile.Repr.(*graph.JSRepr), namedImport.Alias, &msg)
c.log.AddMsg(msg)
}

case importProbablyTypeScriptType:
Expand Down Expand Up @@ -2657,6 +2645,28 @@ loop:
return
}

// Attempt to correct an import name with a typo
func (c *linkerContext) maybeCorrectObviousTypo(repr *graph.JSRepr, name string, msg *logger.Msg) {
if repr.Meta.ResolvedExportTypos == nil {
valid := make([]string, 0, len(repr.Meta.ResolvedExports))
for alias := range repr.Meta.ResolvedExports {
valid = append(valid, alias)
}
sort.Strings(valid)
typos := helpers.MakeTypoDetector(valid)
repr.Meta.ResolvedExportTypos = &typos
}

if corrected, ok := repr.Meta.ResolvedExportTypos.MaybeCorrectTypo(name); ok {
msg.Data.Location.Suggestion = corrected
export := repr.Meta.ResolvedExports[corrected]
importedFile := &c.graph.Files[export.SourceIndex]
msg.Notes = append(msg.Notes, importedFile.LineColumnTracker().MsgData(
js_lexer.RangeOfIdentifier(importedFile.InputFile.Source, export.NameLoc),
fmt.Sprintf("Did you mean to import %q instead?", corrected)))
}
}

func (c *linkerContext) recursivelyWrapDependencies(sourceIndex uint32) {
repr := c.graph.Files[sourceIndex].InputFile.Repr.(*graph.JSRepr)
if repr.Meta.DidWrapDependencies {
Expand Down

0 comments on commit abf1dc2

Please sign in to comment.