-
Notifications
You must be signed in to change notification settings - Fork 30k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enables strictNullChecks to breadcrumbs.ts, outlineModel.ts, breadcrumbsModel.ts #65062
Conversation
// | ||
this.children = this._groups; | ||
} else { | ||
let group = first(this._groups); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is count
gone?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch -- I think I misread the intent of the code and though I could get away with checking if first(this._groups)
is null, but that changes functionality. I'm appending a commit to this PR to reintroduce count
@@ -90,10 +90,18 @@ export abstract class BreadcrumbsConfig<T> { | |||
readonly name = name; | |||
readonly onDidChange = onDidChange.event; | |||
getValue(overrides?: IConfigurationOverrides): T { | |||
return service.getValue(name, overrides); | |||
if (overrides) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sandy081 is that really needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
service.getValue(name, overrides)
this should be good enough. Does the TS complains when using this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I get
src/vs/workbench/browser/parts/editor/breadcrumbs.ts:93:37 - error TS2345: Argument of type 'IConfigurationOverrides | undefined' is not assignable to parameter of type 'IConfigurationOverrides'.
Type 'undefined' is not assignable to type 'IConfigurationOverrides'.
93 return service.getValue(name, overrides);
~~~~~~~~~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see.. looks like the API is strict here to not to accept undefined
value for overrides. I can relax it if needed or you can go with above if/else branch.
@jrieken up to you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please relax @sandy081
@@ -90,10 +90,18 @@ export abstract class BreadcrumbsConfig<T> { | |||
readonly name = name; | |||
readonly onDidChange = onDidChange.event; | |||
getValue(overrides?: IConfigurationOverrides): T { | |||
return service.getValue(name, overrides); | |||
if (overrides) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
service.getValue(name, overrides)
this should be good enough. Does the TS complains when using this?
|
||
constructor( | ||
readonly id: string, | ||
public parent: OutlineModel | OutlineGroup | OutlineElement, | ||
public parent: TreeElement | undefined, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please refrain from making changes aren't needed for strict null checks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was surprised and spent quite a while trying to understand why, but this use of inheritance did cause errors under strict null checks. You can see them by checking out commit 9bbe60f
(I separated the 'normal' null error fixes from the inheritance-related ones in separate commits).
src/vs/editor/contrib/documentSymbols/outlineModel.ts:90:2 - error TS2416: Property 'children' in type 'OutlineElement' is not assignable to the same property in base type 'TreeElement'.
Type '{ [id: string]: OutlineElement; }' is not assignable to type '{ [id: string]: TreeElement; }'.
Index signatures are incompatible.
Type 'OutlineElement' is not assignable to type 'TreeElement'.
Types of property 'parent' are incompatible.
Type 'OutlineElement | OutlineModel | OutlineGroup' is not assignable to type 'TreeElement'.
Type 'OutlineModel' is not assignable to type 'TreeElement'.
Types of property 'children' are incompatible.
Type '{ [id: string]: OutlineElement | OutlineGroup; }' is not assignable to type '{ [id: string]: TreeElement; }'.
Index signatures are incompatible.
Type 'OutlineElement | OutlineGroup' is not assignable to type 'TreeElement'.
Type 'OutlineGroup' is not assignable to type 'TreeElement'.
Types of property 'adopt' are incompatible.
Type '(parent: OutlineModel) => OutlineGroup' is not assignable to type '(newParent: TreeElement) => TreeElement'.
Types of parameters 'parent' and 'newParent' are incompatible.
Type 'TreeElement' is missing the following properties from type 'OutlineModel': _groups, textModel, _compact, merge, and 5 more.
90 children: { [id: string]: OutlineElement; } = Object.create(null);
I'm not 100% confident in my understand of this error but I think the error prevents a hypothetical error that could be make like this:
class SomeNewTypeOfTreeElement {
...
}
const tree: TreeElement = new OutlineElement(...)
tree.parent = new SomeNewTypeOfTreeElement(...) // this works because TreeElement doesn't know about OutlineElement
tree.doSomethingWithParentThatOutlineElementDoesNotExpect()
There's no such code right now, of course, but the type system doesn't care. Happy to learn if there's a better way to silence these errors though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, intersting. Again, in doubt try to use the !
operator. My challenge is that it's hard to reason about these things but that I know that the code in its current shape runs. So, I'd prefer to not really change it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand, but this just changes the type annotation from OutlineModel | OutlineGroup | OutlineElement
to their parent TreeElement
, so I think it's safe? See 38d4a54
|
||
for (; start < markers.length && Range.areIntersecting(item.symbol.range, markers[start]); start++) { | ||
// remove markers intersecting with this outline element | ||
// and store them in a 'private' array. | ||
let marker = markers[start]; | ||
myMarkers.push(marker); | ||
markers[start] = undefined; | ||
filteredMarkers[start] = undefined; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's important to modify markers
, don't create another another "name" for it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue is that markers
is of type Array<IMarker>
which can't accept undefined values. By the end of the function, those undefined values are removed through coalesceInPlace
, but in the meantime we need to let the type system know about the temporary undefineds.
I tried changing the type of markers
to Array<IMarker | undefined>
instead, but then more errors get throw for uses of binarySearch
, Range.areIntersecting
and others in that function so I went for a new alias as the solution that requires minimal code changes, though I agree it feels like a pretty hacky solution. Alternatives could include rewriting some of the logic to be a little it more functional (e.g. use filter) so that the array doesn't contain temporary undefined values, but that requires even more logic changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when in doubt always use the !
operator. that makes it a lot easier for me/us to reason about these changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right that makes perfect sense, I'm just not sure how to apply it in this case because the !
can't operate in a templated type (turn Array<T | undefined>
into Array<T>
). In this case, markers
is actually both Array<T | undefined>
and Array<T>
at different points in the function due to mutation.
What do you think of this other idea? Use type casting: (markers as Array<IMarker | undefined>)[start] = undefined;
this.children = this._groups; | ||
} else { | ||
let group = first(this._groups); | ||
if (group && count === 1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please leave the code as it was before
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 going to use !
instead
@@ -145,7 +146,12 @@ export class EditorBreadcrumbsModel { | |||
this._updateOutlineElements([]); | |||
} | |||
|
|||
const buffer = this._editor.getModel(); | |||
const editor = this._editor; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use !
instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
@jrieken Any thoughts on the comments above? |
Sounds reasonable. I am in favour of adding casts et al because it make it simpler to reason about the changes for now. Please rebase and then I can merge |
@jrieken It's done -- apologies for the delay! |
Thanks. Merging! |
This PR enables strictNullChecks to
breadcrumbs.ts
,outlineModel.ts
andbreadcrumbsModel.ts
as per #60565Most of it consisted of straightforward
| undefined
orif (...)
additions. The one tricky bit is that subclasses ofTreeElement
were being clever in restricting their allowed parent classes, but that could technically be incorrect (e.g. if we later added code to reparentTreeElement
subtrees).