Skip to content

Give more helpful error when trying to set default values on an interface. #5736

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 8 commits into from
Dec 1, 2015

Conversation

delta-nry
Copy link
Contributor

Fixes #5173 - Give more helpful error when trying to set default values on an interface.

Give more helpful error when trying to set default values on an
interface
@msftclas
Copy link

Hi @delta-nry, I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution!

In order for us to evaluate and accept your PR, we ask that you sign a contribution license agreement. It's all electronic and will take just minutes. I promise there's no faxing. https://cla.microsoft.com.

TTYL, MSBOT;

@DanielRosenwasser DanielRosenwasser changed the title Issue 5173 Give more helpful error when trying to set default values on an interface. Nov 20, 2015
@DanielRosenwasser
Copy link
Member

Hey @delta-nry - thanks for the PR. I notice that there's no test case affected - can you add a few tests as per our contribution guidelines?

@delta-nry
Copy link
Contributor Author

Thanks for the quick reply @DanielRosenwasser. As I am part of a group of students from the University of British Columbia, I am waiting for my partners to implement the test case(s) you requested. Thus, we may get back to you sometime within two weeks. Thank you for your patience on resolving this PR.

@DanielRosenwasser
Copy link
Member

No worries, just keep us posted since GitHub doesn't send updates for new commits.

@msftclas
Copy link

@delta-nry, Thanks for signing the contribution license agreement so quickly! Actual humans will now validate the agreement and then evaluate the PR.

Thanks, MSBOT;

@delta-nry
Copy link
Contributor Author

I have implemented the required tests. In the interest of full disclosure, I am the sole person who implemented these tests and none of the content of these tests are original works of my partners. Thank you for your patience regarding this issue.


// Although object type properties cannot not have initializers, we attempt to parse an initializer
// so we can report that an object type property cannot have an initializer.
if (token === SyntaxKind.EqualsToken && lookAhead(() => parseNonParameterInitializer()) !== undefined) {
Copy link
Member

Choose a reason for hiding this comment

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

You can just pass parseNonParameterInitializer to lookahead. Testing truthiness is fine here, and you'll avoid allocating a closure.

Copy link
Contributor

Choose a reason for hiding this comment

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

I would not do it this way. I would just parse out an initializer for these properties. Then, in the checker, detect that and report a nice error there.

We do not want random errors being produced the parser like this.

The benefit of this is that now our tree is well formed. And you can do things like format the tree nicely and have the rest of our features not barf when this is encountered.

Copy link
Member

Choose a reason for hiding this comment

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

So to summarize @delta-nry, just add an optional initializer onto each PropertySignature and perform a grammar check in checker.ts on each property. Report the error on the initializer or the node itself.

@DanielRosenwasser
Copy link
Member

@CyrusNajmabadi can you take a quick look?


==== tests/cases/compiler/errorOnInitializerInObjectType.ts (2 errors) ====
interface Foo {
bar: number = 5;
Copy link
Member

Choose a reason for hiding this comment

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

I'm surprised that we don't go off the rails here since you're not advancing the parser at all (i.e. you use lookAhead, not tryParse).

Copy link
Member

Choose a reason for hiding this comment

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

Actually, this is because the parser tries to be smart and avoids reporting more errors at the same start position. I'm not sure if that's the right thing to do because instead of gracefully parsing the invalid initializer, the parser can start going haywire down the line. Maybe tryParse would be better? What do you think @CyrusNajmabadi?

Copy link
Contributor

Choose a reason for hiding this comment

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

We should just allow initializers. This is a pretty reasonable thing for a user to type, so the tree shoudl just be flexible enough to represent it. We can hten have a simple grammar check in the type checker that reports that thsi isn't legal.

@@ -16085,6 +16095,18 @@ namespace ts {
}
}

if (node.type) {
Copy link
Member

Choose a reason for hiding this comment

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

You should be performing this check in any type literal, not just those in variable like declarations.

I think you should just move the check into checkGrammarProperty, and discriminate on whether you have a PropertySignature with an initializer.

@DanielRosenwasser
Copy link
Member

@delta-nry, I left some feedback. Basically, you should perform the grammar check in checkGrammarProperty.

!!! error TS1005: '=' expected.
~~~
!!! error TS2304: Cannot find name 'bar'.
~
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'm not sure if we should accept these errors, as the '=' expected message implies that initializers are allowed in this case. Maybe we should spin this off into a new issue. @DanielRosenwasser @CyrusNajmabadi, what are your thoughts on this?

Copy link
Member

Choose a reason for hiding this comment

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

It should be sufficient if you make it so that you only try to parse an initializer if the current token is a =.

@delta-nry
Copy link
Contributor Author

@DanielRosenwasser, @CyrusNajmabadi how do these fixes look?

"category": "Error",
"code": 1246
},
"An object type literal property cannot have an initializer.": {
Copy link
Member

Choose a reason for hiding this comment

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

Sorry I didn't notice it earlier, but this should be "A type literal property"

@DanielRosenwasser
Copy link
Member

I think this is great - just address the last comments I left and we should be good to merge. 😃

@DanielRosenwasser
Copy link
Member

💯

DanielRosenwasser added a commit that referenced this pull request Dec 1, 2015
Give more helpful error when trying to set default values on an interface.
@DanielRosenwasser DanielRosenwasser merged commit 4092de6 into microsoft:master Dec 1, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 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