Skip to content

Fix #20873: Enforce strictNullChecks for RHS of empty destructuring assignment#21110

Merged
sandersn merged 4 commits into
microsoft:masterfrom
jack-williams:strict-null-empty-destructuring
Jan 18, 2018
Merged

Fix #20873: Enforce strictNullChecks for RHS of empty destructuring assignment#21110
sandersn merged 4 commits into
microsoft:masterfrom
jack-williams:strict-null-empty-destructuring

Conversation

@jack-williams
Copy link
Copy Markdown
Collaborator

When strictNullChecks is on, check the RHS of the following destructuring assignments for possible null or undefined:

const {} = ...
const [] = ...
let {} = ...
let [] = ...
({} = ...)

Fixes #20873

When strictNullChecks is on, check the RHS of the following
destructuring assignments for possible null or undefined:

const {} = ...
const [] = ...
let {} = ...
let [] = ...
({} = ...)
@msftclas
Copy link
Copy Markdown

msftclas commented Jan 10, 2018

CLA assistant check
All CLA requirements met.

@mhegazy mhegazy requested a review from sandersn January 10, 2018 18:28
@mhegazy
Copy link
Copy Markdown
Contributor

mhegazy commented Jan 10, 2018

@sandersn can you please review this change.

Copy link
Copy Markdown
Member

@sandersn sandersn left a comment

Choose a reason for hiding this comment

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

1 question and 1 refactoring request.

Comment thread src/compiler/checker.ts Outdated
if (strictNullChecks && node.name.elements.length === 0) {
initializerType = checkNonNullType(initializerType, node);
}
checkTypeAssignableTo(initializerType, getWidenedTypeForVariableLikeDeclaration(node), node, /*headMessage*/ undefined);
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.

Is checkTypeAssignableTo needed if there aren't any elements? Seems like it could in an else, and then initializerType wouldn't need to be updated in the if block.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Good point. I did consider whether it would be possible to just return (or use an else) immediately after the null check. I was just unsure if checkTypeAssignableTo performs some side-effects (like caching in checkTypeRelatedTo) that might give different results if skipped.

Making that change doesn't break any tests so from what I can tell I think this would be ok. I'm still learning the details of the internals so I may be missing something.

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.

You're right, the cache might be filled earlier this way, but there's no point in doing work that isn't needed.

You can't return because the code still needs to checkParameterInitalizer, although maybe that's redundant with the new call to checkExpressionCached.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I don't think checkParameterInitalizer gets called via checkExpressionCached, so unless it does some equivalent work, I need to move the call after the if-then-else, rather than in the else branch. My most recent commit got this wrong - will fix now.

Comment thread src/compiler/checker.ts Outdated
target = (<BinaryExpression>target).left;
}
if (target.kind === SyntaxKind.ObjectLiteralExpression) {
if (strictNullChecks && (<ObjectLiteralExpression>target).properties.length === 0) {
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 would move this code into checkObjectLiteralAssignment.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done!

Move object destructuring assignment to checkObjectLiteralAssignment

Only check assignability of types in checkVariableLikeDeclaration for
object/array destructuring when there are properties present in the
pattern.
@jack-williams
Copy link
Copy Markdown
Collaborator Author

Just pinging @sandersn to say that I've addressed the suggested changes (I hope). I appreciate this is low priority so just look over it when you can (I just wanted to indicate that I hadn't forgotten about the PR).

Is there a workflow for marking review comments as addressed/resolved?

@sandersn
Copy link
Copy Markdown
Member

Thanks for the ping. I missed the new commits. We don't have an established workflow for marking comment state. I sometimes reply "done" but I think we are basically waiting for github to add that feature.

Anyway, thanks for the change. Looks good!

@sandersn sandersn merged commit 39fee67 into microsoft:master Jan 18, 2018
@jack-williams
Copy link
Copy Markdown
Collaborator Author

jack-williams commented Jan 18, 2018

No problem! Thanks for reviewing it :)

@jack-williams jack-williams deleted the strict-null-empty-destructuring branch January 19, 2018 15:36
@jack-williams jack-williams restored the strict-null-empty-destructuring branch January 19, 2018 15:37
@jack-williams jack-williams deleted the strict-null-empty-destructuring branch February 12, 2018 12:30
@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants