Skip to content

fix for 45006 #45020

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

Merged
merged 6 commits into from
Aug 21, 2021
Merged

fix for 45006 #45020

merged 6 commits into from
Aug 21, 2021

Conversation

softwareCobbler
Copy link
Contributor

Fixes #45006
thanks, typescript is great

@typescript-bot typescript-bot added the For Backlog Bug PRs that fix a backlog bug label Jul 14, 2021
@ghost
Copy link

ghost commented Jul 14, 2021

CLA assistant check
All CLA requirements met.

@@ -1835,6 +1833,8 @@ namespace ts {
case SyntaxKind.ClassStaticBlockDeclaration:
return ContainerFlags.IsContainer | ContainerFlags.IsControlFlowContainer | ContainerFlags.HasLocals | ContainerFlags.IsFunctionLike;

case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
Copy link
Member

Choose a reason for hiding this comment

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

This move doesn't seem right -- get/set aren't function expressions. Do the tests still pass with this change reverted?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

reverting just this change causes the new test to fail. The goal here was (admittedly opaquely) to have bindContainer bind the getter/setter node to the fresh start flow, which returning these container flags succeeded at doing (the desired flow binding occurs if the containerFlags for a given container are either ContainerFlags.IsFunctionExpression or ContainerFlags.IsObjectLiteralOrClassExpressionMethod). Your comments about consolidating isObjectLiteralOrClassExpressionMethod makes it look like moving these switch cases to be with the MethodDeclaration case makes sense.

This made me realize I should have made explicit the intent to attach GetAccessorDeclaration and SetAccessorDeclarations to FlowStart.node; doing so has the possibly unfortunate side effect of changing the shape of the FlowStart interface in 2 publicly exposed interface files. Not sure what the policy is on that.

@@ -3372,7 +3372,7 @@ namespace ts {
emitFlags |= NodeFlags.HasAsyncFunctions;
}

if (currentFlow && isObjectLiteralOrClassExpressionMethod(node)) {
Copy link
Member

Choose a reason for hiding this comment

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

There are only 3 callers of isObjectLiteralOrClassExpressionMethod. Two of them need this addition, and the third is guarded by case SyntaxKind.MethodDeclaration. So I'd like to see the new check moved into that function, and the name should be ... uhh ... isObjectLiteralOrClassExpressionMethodOrAccessor ???

Maybe there's a better name if there's a term that means "method or accessor".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just went with choice 1, isObjectLiteralOrClassExpressionMethodOrAccessor; there was also a ContainerFlags by the same name that looked like it could be part of the same renaming effort.

@@ -37,3 +37,13 @@ interface I1 {
}

var i:I1 = function (n) {return n;}

Copy link
Member

Choose a reason for hiding this comment

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

Please add a // @target: es2020 (or any target newer than es3) at the top of this file. I wish our tests didn't target ES3 by default.

Copy link
Contributor Author

@softwareCobbler softwareCobbler Aug 20, 2021

Choose a reason for hiding this comment

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

yes, no problem

in `getContainerFlags`, move cases for get/set accessors
to fallthrough into the block that currently handles MethodDeclaration;
so get/set accessors and method declarations all get the same container flags,
such that during `bindContainer`, `startFlow.node` is assigned to
getters/accessors
(this changes a public api in tsserverlibrary.d.ts and typescript.d.ts
by adding `GetAccessorDeclaration` and `SetAccessorDeclaration` to the type
of `FlowStart.node`)

consolidate predicates checking whether a node is either a get or set
accessor, into `isObjectLiteralOrClassExpressionMethodOrAccessor`
(formerly `isObjectLiteralOrClassExpressionMethod`)

annotate updated test with `@target: es2020`
Copy link
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.

Looks good except one final tweak in isObjectLiteralOrClassExpressionMethodOrAccessor

export function isObjectLiteralOrClassExpressionMethod(node: Node): node is MethodDeclaration {
return node.kind === SyntaxKind.MethodDeclaration &&
export function isObjectLiteralOrClassExpressionMethodOrAccessor(node: Node): node is MethodDeclaration {
return (node.kind === SyntaxKind.MethodDeclaration &&
Copy link
Member

Choose a reason for hiding this comment

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

The parent of a get/set should also have to be an object literal or class expression; they're the same as method declarations in that regard.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, I see that now.

require that Getter/Setters are parented by an ObjectLiteralExpression or ClassExpression
@sandersn sandersn merged commit d18e82b into microsoft:main Aug 21, 2021
BobobUnicorn pushed a commit to BobobUnicorn/TypeScript that referenced this pull request Oct 24, 2021
* fix for 45006

* treat setters like getters in preceding commit; move test accordingly

* fix test baselines

* changes per code review

in `getContainerFlags`, move cases for get/set accessors
to fallthrough into the block that currently handles MethodDeclaration;
so get/set accessors and method declarations all get the same container flags,
such that during `bindContainer`, `startFlow.node` is assigned to
getters/accessors
(this changes a public api in tsserverlibrary.d.ts and typescript.d.ts
by adding `GetAccessorDeclaration` and `SetAccessorDeclaration` to the type
of `FlowStart.node`)

consolidate predicates checking whether a node is either a get or set
accessor, into `isObjectLiteralOrClassExpressionMethodOrAccessor`
(formerly `isObjectLiteralOrClassExpressionMethod`)

annotate updated test with `@target: es2020`

* fix `isObjectLiteralOrClassExpressionMethodOrAccessor`

require that Getter/Setters are parented by an ObjectLiteralExpression or ClassExpression
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Backlog Bug PRs that fix a backlog bug
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Const typeguard flows into methods but not properties
3 participants