Skip to content

Commit 4f9181b

Browse files
committed
Use updated diagnostic messages in pnpapi + fix autocomplete tests
1 parent f3582f0 commit 4f9181b

File tree

5 files changed

+37
-42
lines changed

5 files changed

+37
-42
lines changed

internal/diagnostics/diagnostics_generated.go

Lines changed: 6 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/diagnostics/extraDiagnosticMessages.json

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,17 @@
3939
"category": "Error",
4040
"code": 110005
4141
},
42-
"no PnP manifest found": {
43-
"category": "Error",
44-
"code": 110006
45-
},
4642
"no package found for path '{0}'": {
4743
"category": "Error",
48-
"code": 110007
44+
"code": 110006
4945
},
5046
"Empty specifier: '{0}'": {
5147
"category": "Error",
52-
"code": 110008
48+
"code": 110007
5349
},
5450
"Invalid specifier: '{0}'": {
5551
"category": "Error",
56-
"code": 110009
52+
"code": 110008
5753
},
5854
"Non-relative paths are not allowed. Did you forget a leading './'?": {
5955
"category": "Error",

internal/fourslash/tests/pnpAutoImportCompletion_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,23 +162,23 @@ aValue;
162162
// Verify that helperA completion creates an import from 'package-a/subpath'
163163
&lsproto.CompletionItem{
164164
Label: "helperA",
165-
Data: PtrTo(any(&ls.CompletionItemData{
166-
AutoImport: &ls.AutoImportData{
165+
Data: &lsproto.CompletionItemData{
166+
AutoImport: &lsproto.AutoImportData{
167167
ModuleSpecifier: "package-a/subpath",
168168
},
169-
})),
169+
},
170170
Kind: PtrTo(lsproto.CompletionItemKindFunction),
171171
AdditionalTextEdits: fourslash.AnyTextEdits,
172172
SortText: PtrTo(string(ls.SortTextAutoImportSuggestions)),
173173
},
174174
// Verify that workspaceHelper completion creates an import from 'workspace-lib'
175175
&lsproto.CompletionItem{
176176
Label: "workspaceHelper",
177-
Data: PtrTo(any(&ls.CompletionItemData{
178-
AutoImport: &ls.AutoImportData{
177+
Data: &lsproto.CompletionItemData{
178+
AutoImport: &lsproto.AutoImportData{
179179
ModuleSpecifier: "workspace-lib",
180180
},
181-
})),
181+
},
182182
Kind: PtrTo(lsproto.CompletionItemKindFunction),
183183
AdditionalTextEdits: fourslash.AnyTextEdits,
184184
SortText: PtrTo(string(ls.SortTextAutoImportSuggestions)),

internal/module/resolver.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/microsoft/typescript-go/internal/core"
1212
"github.com/microsoft/typescript-go/internal/diagnostics"
1313
"github.com/microsoft/typescript-go/internal/packagejson"
14+
"github.com/microsoft/typescript-go/internal/pnp"
1415
"github.com/microsoft/typescript-go/internal/semver"
1516
"github.com/microsoft/typescript-go/internal/tspath"
1617
)
@@ -995,7 +996,7 @@ func (r *resolutionState) loadModuleFromPnpResolution(ext extensions, moduleName
995996
packageDirectory, err := pnpApi.ResolveToUnqualified(packageName, issuer)
996997
if err != nil {
997998
if r.tracer != nil {
998-
r.tracer.write(err.Error())
999+
r.tracer.write(err.Message, err.Args...)
9991000
}
10001001
return nil
10011002
}
@@ -1806,11 +1807,11 @@ func (r *resolutionState) readPackageJsonPeerDependencies(packageJsonInfo *packa
18061807
var peerDependencyPath string
18071808

18081809
if pnpApi != nil {
1809-
var err error
1810+
var err *pnp.PnpError
18101811
peerDependencyPath, err = pnpApi.ResolveToUnqualified(name, packageDirectory)
18111812
if err != nil {
18121813
if r.tracer != nil {
1813-
r.tracer.write(err.Error())
1814+
r.tracer.write(err.Message, err.Args...)
18141815
}
18151816
continue
18161817
}

internal/pnp/pnpapi.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,12 @@ func (p *PnpApi) RefreshManifest() error {
6464
return nil
6565
}
6666

67-
func (p *PnpApi) ResolveToUnqualified(specifier string, parentPath string) (string, error) {
67+
type PnpError struct {
68+
Message *diagnostics.Message
69+
Args []any
70+
}
71+
72+
func (p *PnpApi) ResolveToUnqualified(specifier string, parentPath string) (string, *PnpError) {
6873
if p.manifest == nil {
6974
panic("ResolveToUnqualified called with no PnP manifest available")
7075
}
@@ -112,13 +117,13 @@ func (p *PnpApi) ResolveToUnqualified(specifier string, parentPath string) (stri
112117
if referenceOrAlias == nil {
113118
if isNodeJSBuiltin(specifier) {
114119
if isDependencyTreeRoot(p.manifest, parentLocator) {
115-
return "", errors.New(diagnostics.Your_application_tried_to_access_0_While_this_module_is_usually_interpreted_as_a_Node_builtin_your_resolver_is_running_inside_a_non_Node_resolution_context_where_such_builtins_are_ignored_Since_0_isn_t_otherwise_declared_in_your_dependencies_this_makes_the_require_call_ambiguous_and_unsound_Required_package_Colon_0_1_Required_by_Colon_2.Format(ident, ident, viaSuffix(specifier, ident), parentPath))
120+
return "", &PnpError{Message: diagnostics.Your_application_tried_to_access_0_While_this_module_is_usually_interpreted_as_a_Node_builtin_your_resolver_is_running_inside_a_non_Node_resolution_context_where_such_builtins_are_ignored_Since_0_isn_t_otherwise_declared_in_your_dependencies_this_makes_the_require_call_ambiguous_and_unsound_Required_package_Colon_0_1_Required_by_Colon_2, Args: []any{ident, ident, viaSuffix(specifier, ident), parentPath}}
116121
}
117-
return "", errors.New(diagnostics.X_0_tried_to_access_1_While_this_module_is_usually_interpreted_as_a_Node_builtin_your_resolver_is_running_inside_a_non_Node_resolution_context_where_such_builtins_are_ignored_Since_1_isn_t_otherwise_declared_in_0_s_dependencies_this_makes_the_require_call_ambiguous_and_unsound_Required_package_Colon_1_2_Required_by_Colon_3.Format(parentLocator.Name, ident, ident, parentLocator.Name, ident, viaSuffix(specifier, ident), parentPath))
122+
return "", &PnpError{Message: diagnostics.X_0_tried_to_access_1_While_this_module_is_usually_interpreted_as_a_Node_builtin_your_resolver_is_running_inside_a_non_Node_resolution_context_where_such_builtins_are_ignored_Since_1_isn_t_otherwise_declared_in_0_s_dependencies_this_makes_the_require_call_ambiguous_and_unsound_Required_package_Colon_1_2_Required_by_Colon_3, Args: []any{parentLocator.Name, ident, ident, parentLocator.Name, ident, viaSuffix(specifier, ident), parentPath}}
118123
}
119124

120125
if isDependencyTreeRoot(p.manifest, parentLocator) {
121-
return "", errors.New(diagnostics.Your_application_tried_to_access_0_but_it_isn_t_declared_in_your_dependencies_this_makes_the_require_call_ambiguous_and_unsound_Required_package_Colon_0_1_Required_by_Colon_2.Format(ident, ident, viaSuffix(specifier, ident), parentPath))
126+
return "", &PnpError{Message: diagnostics.Your_application_tried_to_access_0_but_it_isn_t_declared_in_your_dependencies_this_makes_the_require_call_ambiguous_and_unsound_Required_package_Colon_0_1_Required_by_Colon_2, Args: []any{ident, ident, viaSuffix(specifier, ident), parentPath}}
122127
}
123128

124129
brokenAncestors := findBrokenPeerDependencies(specifier, parentLocator)
@@ -133,9 +138,9 @@ func (p *PnpApi) ResolveToUnqualified(specifier string, parentPath string) (stri
133138
}
134139

135140
if len(brokenAncestors) > 0 && allBrokenAreRoots {
136-
return "", errors.New(diagnostics.Your_application_tried_to_access_0_a_peer_dependency_this_isn_t_allowed_as_there_is_no_ancestor_to_satisfy_the_requirement_Use_a_devDependency_if_needed_Required_package_Colon_0_Required_by_Colon_1.Format(ident, ident, parentPath))
141+
return "", &PnpError{Message: diagnostics.Your_application_tried_to_access_0_a_peer_dependency_this_isn_t_allowed_as_there_is_no_ancestor_to_satisfy_the_requirement_Use_a_devDependency_if_needed_Required_package_Colon_0_Required_by_Colon_1, Args: []any{ident, ident, parentPath}}
137142
} else {
138-
return "", errors.New(diagnostics.X_0_tried_to_access_1_a_peer_dependency_but_it_isn_t_provided_by_its_ancestors_Slashyour_application_this_makes_the_require_call_ambiguous_and_unsound_Required_package_Colon_1_Required_by_Colon_2.Format(parentLocator.Name, ident, ident, parentPath))
143+
return "", &PnpError{Message: diagnostics.X_0_tried_to_access_1_a_peer_dependency_but_it_isn_t_provided_by_its_ancestors_Slashyour_application_this_makes_the_require_call_ambiguous_and_unsound_Required_package_Colon_1_Required_by_Colon_2, Args: []any{parentLocator.Name, ident, ident, parentPath}}
139144
}
140145
}
141146

@@ -159,7 +164,7 @@ func (p *PnpApi) findClosestPnpManifest() (*PnpManifestData, error) {
159164
}
160165

161166
if tspath.IsDiskPathRoot(directoryPath) {
162-
return nil, errors.New(diagnostics.X_no_PnP_manifest_found.Format())
167+
return nil, errors.New("no PnP manifest found")
163168
}
164169

165170
directoryPath = tspath.GetDirectoryPath(directoryPath)
@@ -176,7 +181,7 @@ func (p *PnpApi) GetPackage(locator *Locator) *PackageInfo {
176181
return packageInfo
177182
}
178183

179-
func (p *PnpApi) FindLocator(parentPath string) (*Locator, error) {
184+
func (p *PnpApi) FindLocator(parentPath string) (*Locator, *PnpError) {
180185
if parentPath == "" {
181186
return nil, nil
182187
}
@@ -186,11 +191,8 @@ func (p *PnpApi) FindLocator(parentPath string) (*Locator, error) {
186191

187192
if p.manifest.ignorePatternData != nil {
188193
match, err := p.manifest.ignorePatternData.MatchString(relativePath)
189-
if err != nil {
190-
return nil, err
191-
}
192194

193-
if match {
195+
if err == nil && match {
194196
return nil, nil
195197
}
196198
}
@@ -222,7 +224,7 @@ func (p *PnpApi) FindLocator(parentPath string) (*Locator, error) {
222224
}
223225

224226
if bestLocator == nil {
225-
return nil, errors.New(diagnostics.X_no_package_found_for_path_0.Format(relativePath))
227+
return nil, &PnpError{Message: diagnostics.X_no_package_found_for_path_0, Args: []any{relativePath}}
226228
}
227229

228230
return bestLocator, nil
@@ -252,16 +254,16 @@ func (p *PnpApi) ResolveViaFallback(name string) *PackageDependency {
252254
return nil
253255
}
254256

255-
func (p *PnpApi) ParseBareIdentifier(specifier string) (ident string, modulePath string, err error) {
257+
func (p *PnpApi) ParseBareIdentifier(specifier string) (ident string, modulePath string, err *PnpError) {
256258
if len(specifier) == 0 {
257-
return "", "", errors.New(diagnostics.Empty_specifier_Colon_0.Format(specifier))
259+
return "", "", &PnpError{Message: diagnostics.Empty_specifier_Colon_0, Args: []any{specifier}}
258260
}
259261

260262
firstSlash := strings.Index(specifier, "/")
261263

262264
if specifier[0] == '@' {
263265
if firstSlash == -1 {
264-
return "", "", errors.New(diagnostics.Invalid_specifier_Colon_0.Format(specifier))
266+
return "", "", &PnpError{Message: diagnostics.Invalid_specifier_Colon_0, Args: []any{specifier}}
265267
}
266268

267269
secondSlash := strings.Index(specifier[firstSlash+1:], "/")

0 commit comments

Comments
 (0)