Skip to content

[Fix #9004] Improve error message for extending interface#9180

Merged
mhegazy merged 6 commits into
microsoft:masterfrom
HerringtonDarkholme:interface
Jun 17, 2016
Merged

[Fix #9004] Improve error message for extending interface#9180
mhegazy merged 6 commits into
microsoft:masterfrom
HerringtonDarkholme:interface

Conversation

@HerringtonDarkholme
Copy link
Copy Markdown
Contributor

@HerringtonDarkholme HerringtonDarkholme commented Jun 15, 2016

Fixes #9004

I found it had more to do with checker than diagnostics.json. We have to first check whether it is a valid base class before reporting cannot extending an interface. So I put the error reporting in resolveName and checkPropertyAccessExpressionOrQualifiedName, like checkAndReportErrorForMissingPrefix.

New baseline is accepted, and no new tests are added because current testcases are enough for this change.

@msftclas
Copy link
Copy Markdown

Hi @HerringtonDarkholme, I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution!
You've already signed the contribution license agreement. Thanks!

The agreement was validated by Microsoft and real humans are currently evaluating your PR.

TTYL, MSBOT;

Comment thread src/compiler/checker.ts Outdated

function checkAndReportErrorForExtendingInterface(errorLocation: Node, name: string): boolean {
if (!errorLocation || errorLocation.kind !== SyntaxKind.Identifier ||
!errorLocation.parent || !errorLocation.parent.parent ||
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 could be a PropeortyAccess, so use getAncestor instead.

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.

also add a test for the dotted name case.

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 intention here is that if one wrongly extends interface, then the errorLocation must be exactly at HeritageClause -> ExpressionWithTypeArguments -> Identifier.

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.

consider this:

namespace N {
    export interface I { }
}

class C extends N.I {
}

you still want to show the error on I. the node you get is HeritageClause -> ExpressionWithTypeArguments -> PropertyAccessExpression -> Identifier

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.

Current approach will not report extends interface error for clauses like class Child extends Module.Interface

I know this. So should I support propertyAccess in this pull request too?

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.

Yes please.

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.

For such cases

namespace N {
  export interface I {}
}
class C extends N.I {}

resolveEntityName does not work here because it requires a ExpressionWithTypeArguments node, while N.I is binded as a expression.

Do you have a suggestion?

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.

Oh, I didn't understand your comments until now!
I know how to do it.

Comment thread src/compiler/checker.ts Outdated
}
if (errorLocation.kind === SyntaxKind.Identifier) {
const name = (<Identifier>errorLocation).text;
const interfaceOrModule = resolveName(
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.

Sorry wrong recommendation earlier. you probably should use resolveEntityName it handles both identifiers and propeortyAccess.

Comment thread src/compiler/checker.ts

function checkAndReportErrorForExtendingInterface(errorLocation: Node): boolean {
let parentClassExpression = errorLocation;
while (parentClassExpression) {
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.

getAncestor cannot be used here because it will confuse with class A extends generateClass(Interface) {}.
See the test case classExtendsInterfaceInExpression

@mhegazy
Copy link
Copy Markdown
Contributor

mhegazy commented Jun 17, 2016

thanks!

@mhegazy mhegazy merged commit 2b4378d into microsoft:master Jun 17, 2016
@HerringtonDarkholme
Copy link
Copy Markdown
Contributor Author

Thanks for your helpful advice!

@HerringtonDarkholme HerringtonDarkholme deleted the interface branch June 18, 2016 07:26
@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.

Extend Interface: misleading error message

3 participants