Skip to content

Enables strictNullChecks to breadcrumbs.ts, outlineModel.ts, breadcrumbsModel.ts - #65062

Merged
Johannes Rieken (jrieken) merged 10 commits into
microsoft:masterfrom
rudi-c:snc-1
Jan 23, 2019
Merged

Enables strictNullChecks to breadcrumbs.ts, outlineModel.ts, breadcrumbsModel.ts#65062
Johannes Rieken (jrieken) merged 10 commits into
microsoft:masterfrom
rudi-c:snc-1

Conversation

@rudi-c

Copy link
Copy Markdown
Contributor

This PR enables strictNullChecks to breadcrumbs.ts, outlineModel.ts and breadcrumbsModel.ts as per #60565

Most of it consisted of straightforward | undefined or if (...) additions. The one tricky bit is that subclasses of TreeElement were being clever in restricting their allowed parent classes, but that could technically be incorrect (e.g. if we later added code to reparent TreeElement subtrees).

@msftclas

Microsoft Contribution License Agreements (msftclas) commented Dec 14, 2018

Copy link
Copy Markdown

CLA assistant check
All CLA requirements met.

@rudi-c Rudi Chen (rudi-c) changed the title Enables strictNullChecks to breadcrumbs.ts, outlineModel.ts, breadcrumbbsModel.ts Enables strictNullChecks to breadcrumbs.ts, outlineModel.ts, breadcrumbsModel.ts Dec 14, 2018
//
this.children = this._groups;
} else {
let group = first(this._groups);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is count gone?

Copy link
Copy Markdown
Contributor Author

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

readonly onDidChange = onDidChange.event;
getValue(overrides?: IConfigurationOverrides): T {
return service.getValue(name, overrides);
if (overrides) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sandeep Somavarapu (@sandy081) is that really needed?

Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
Contributor Author

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);
    					                              ~~~~~~~~~

@sandy081 Sandeep Somavarapu (sandy081) Dec 18, 2018

Copy link
Copy Markdown
Member

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.

Johannes Rieken (@jrieken) up to you.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

readonly onDidChange = onDidChange.event;
getValue(overrides?: IConfigurationOverrides): T {
return service.getValue(name, overrides);
if (overrides) {

Copy link
Copy Markdown
Member

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,

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor Author

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

Comment thread src/vs/editor/contrib/documentSymbols/outlineModel.ts Outdated
let marker = markers[start];
myMarkers.push(marker);
markers[start] = undefined;
filteredMarkers[start] = undefined;

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

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) {

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 going to use ! instead

}

const buffer = this._editor.getModel();
const editor = this._editor;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use ! instead

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@rudi-c

Copy link
Copy Markdown
Contributor Author

Johannes Rieken (@jrieken) Any thoughts on the comments above?

@jrieken Johannes Rieken (jrieken) added the engineering VS Code - Build / issue tracking / etc. label Jan 8, 2019
@jrieken

Copy link
Copy Markdown
Contributor

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

@rudi-c

Copy link
Copy Markdown
Contributor Author

Johannes Rieken (@jrieken) It's done -- apologies for the delay!

@jrieken

Copy link
Copy Markdown
Contributor

Thanks. Merging!

@jrieken
Johannes Rieken (jrieken) merged commit 9526cba into microsoft:master Jan 23, 2019
@rudi-c
Rudi Chen (rudi-c) deleted the snc-1 branch January 26, 2019 07:39
@github-actions github-actions Bot locked and limited conversation to collaborators Mar 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

engineering VS Code - Build / issue tracking / etc.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants