Skip to content

Commit 07ec6a9

Browse files
authored
fix: private reference diagnostic was displaying incorrect information (#576)
1 parent d68bd0a commit 07ec6a9

File tree

3 files changed

+64
-58
lines changed

3 files changed

+64
-58
lines changed

src/diagnostics.rs

Lines changed: 55 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,21 @@ use std::borrow::Cow;
3333
use std::collections::HashSet;
3434
use std::rc::Rc;
3535

36-
#[derive(Debug, Clone, PartialEq, Eq)]
36+
#[derive(Debug, Clone)]
3737
pub enum DocDiagnosticKind {
3838
MissingJsDoc,
3939
MissingExplicitType,
4040
MissingReturnType,
41-
PrivateTypeRef {
42-
name: String,
43-
reference: String,
44-
/// The location of the reference.
45-
reference_location: Location,
46-
},
41+
PrivateTypeRef(Box<PrivateTypeRefDiagnostic>),
42+
}
43+
44+
#[derive(Debug, Clone)]
45+
pub struct PrivateTypeRefDiagnostic {
46+
pub name: String,
47+
pub reference: String,
48+
pub reference_text_info: SourceTextInfo,
49+
/// The location of the reference.
50+
pub reference_location: Location,
4751
}
4852

4953
#[derive(Clone)]
@@ -89,10 +93,9 @@ impl Diagnostic for DocDiagnostic {
8993
DocDiagnosticKind::MissingReturnType => Cow::Borrowed(
9094
"exported function is missing an explicit return type annotation",
9195
),
92-
DocDiagnosticKind::PrivateTypeRef {
93-
reference, name, ..
94-
} => Cow::Owned(format!(
95-
"public type '{name}' references private type '{reference}'",
96+
DocDiagnosticKind::PrivateTypeRef(diagnostic) => Cow::Owned(format!(
97+
"public type '{}' references private type '{}'",
98+
diagnostic.name, diagnostic.reference,
9699
)),
97100
}
98101
}
@@ -130,23 +133,23 @@ impl Diagnostic for DocDiagnostic {
130133
}
131134
fn snippet_fixed(&self) -> Option<DiagnosticSnippet<'_>> {
132135
match &self.kind {
133-
DocDiagnosticKind::PrivateTypeRef {
134-
reference_location, ..
135-
} => Some(DiagnosticSnippet {
136-
source: Cow::Borrowed(&self.text_info),
137-
highlight: DiagnosticSnippetHighlight {
138-
style: DiagnosticSnippetHighlightStyle::Hint,
139-
range: DiagnosticSourceRange {
140-
start: DiagnosticSourcePos::ByteIndex(
141-
reference_location.byte_index,
142-
),
143-
end: DiagnosticSourcePos::ByteIndex(
144-
reference_location.byte_index + 1,
145-
),
136+
DocDiagnosticKind::PrivateTypeRef(diagnostic) => {
137+
Some(DiagnosticSnippet {
138+
source: Cow::Borrowed(&diagnostic.reference_text_info),
139+
highlight: DiagnosticSnippetHighlight {
140+
style: DiagnosticSnippetHighlightStyle::Hint,
141+
range: DiagnosticSourceRange {
142+
start: DiagnosticSourcePos::ByteIndex(
143+
diagnostic.reference_location.byte_index,
144+
),
145+
end: DiagnosticSourcePos::ByteIndex(
146+
diagnostic.reference_location.byte_index + 1,
147+
),
148+
},
149+
description: Some(Cow::Borrowed("this is the referenced type")),
146150
},
147-
description: Some(Cow::Borrowed("this is the referenced type")),
148-
},
149-
}),
151+
})
152+
}
150153
_ => None,
151154
}
152155
}
@@ -222,28 +225,31 @@ impl<'a> DiagnosticsCollector<'a> {
222225
decl_range.start,
223226
),
224227
text_info: decl_module.text_info().clone(),
225-
kind: DocDiagnosticKind::PrivateTypeRef {
226-
name: decl_name.to_string(),
227-
reference: reference.to_string(),
228-
reference_location: referenced_symbol
229-
.decls()
230-
.iter()
231-
.next()
232-
.map(|d| {
233-
get_text_info_location(
234-
referenced_module.specifier().as_str(),
235-
referenced_module.text_info(),
236-
d.range.start,
237-
)
238-
})
239-
// should never happen, but just in case
240-
.unwrap_or_else(|| Location {
241-
filename: referenced_module.specifier().to_string(),
242-
line: 1,
243-
col: 0,
244-
byte_index: 0,
245-
}),
246-
},
228+
kind: DocDiagnosticKind::PrivateTypeRef(Box::new(
229+
PrivateTypeRefDiagnostic {
230+
name: decl_name.to_string(),
231+
reference: reference.to_string(),
232+
reference_text_info: referenced_module.text_info().clone(),
233+
reference_location: referenced_symbol
234+
.decls()
235+
.iter()
236+
.next()
237+
.map(|d| {
238+
get_text_info_location(
239+
referenced_module.specifier().as_str(),
240+
referenced_module.text_info(),
241+
d.range.start,
242+
)
243+
})
244+
// should never happen, but just in case
245+
.unwrap_or_else(|| Location {
246+
filename: referenced_module.specifier().to_string(),
247+
line: 1,
248+
col: 0,
249+
byte_index: 0,
250+
}),
251+
},
252+
)),
247253
})
248254
}
249255

tests/specs/private_type_other_module.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ error[private-type-ref]: public type 'MyClass' references private type 'YesDiagn
2323
| ^
2424
= hint: make the referenced type public or remove the reference
2525
|
26-
3 | export class MyClass extends YesDiagnostic2 {
27-
| - this is the referenced type
26+
7 | export class YesDiagnostic2 {}
27+
| - this is the referenced type
2828

2929
info: to ensure documentation is complete all types that are exposed in the public API must be public
3030

@@ -36,8 +36,8 @@ error[private-type-ref]: public type 'MyClass.prototype.prop' references private
3636
| ^
3737
= hint: make the referenced type public or remove the reference
3838
|
39-
1 | import { MyNamespace, YesDiagnostic2 } from "./diagnostic.ts";
40-
| - this is the referenced type
39+
3 | export namespace MyNamespace {
40+
| - this is the referenced type
4141

4242
info: to ensure documentation is complete all types that are exposed in the public API must be public
4343

@@ -49,8 +49,8 @@ error[private-type-ref]: public type 'MyClass.prototype.prop' references private
4949
| ^
5050
= hint: make the referenced type public or remove the reference
5151
|
52-
1 | import { MyNamespace, YesDiagnostic2 } from "./diagnostic.ts";
53-
| - this is the referenced type
52+
4 | export class YesDiagnostic {}
53+
| - this is the referenced type
5454

5555
info: to ensure documentation is complete all types that are exposed in the public API must be public
5656

tests/specs/reexport_deep.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ error[private-type-ref]: public type 'UrlMessageEntity' references private type
2323
| ^
2424
= hint: make the referenced type public or remove the reference
2525
|
26-
1 | import type { MessageEntity } from "./a.ts";
26+
1 | export declare namespace MessageEntity {
2727
| - this is the referenced type
2828

2929
info: to ensure documentation is complete all types that are exposed in the public API must be public
@@ -36,8 +36,8 @@ error[private-type-ref]: public type 'UrlMessageEntity' references private type
3636
| ^
3737
= hint: make the referenced type public or remove the reference
3838
|
39-
1 | import type { MessageEntity } from "./a.ts";
40-
| - this is the referenced type
39+
2 | export interface TextLinkMessageEntity {
40+
| - this is the referenced type
4141

4242
info: to ensure documentation is complete all types that are exposed in the public API must be public
4343

0 commit comments

Comments
 (0)