Skip to content

improve Diagnostics for accidentally calling type-assertion expressions #27020

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

Conversation

Kingwl
Copy link
Contributor

@Kingwl Kingwl commented Sep 11, 2018

Fixes #26991

@Kingwl
Copy link
Contributor Author

Kingwl commented Sep 11, 2018

maybe that could be a related Diagnostics?

Copy link
Member

@DanielRosenwasser DanielRosenwasser 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, and great test cases! Can we make this a related span though?

@@ -19655,6 +19655,13 @@ namespace ts {
error(node, Diagnostics.Value_of_type_0_is_not_callable_Did_you_mean_to_include_new, typeToString(funcType));
}
else {
if (node.arguments.length === 1 && isTypeAssertion(first(node.arguments))) {
const text = getSourceFileOfNode(node).text;
const pos = skipTrivia(text, node.expression.end, /* stepAfterLineBreak */ true) - 1;
Copy link
Member

Choose a reason for hiding this comment

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

step -> stop

Copy link
Member

Choose a reason for hiding this comment

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

Technically this function and isLineBreak seem to have differing definitions of what a line break is...

...so I don't know if that's a problem 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

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

in CRLF or LF, the skipTrivia() - 1 will always correctly,
but if only CR, that seems a problem...

const text = getSourceFileOfNode(node).text;
const pos = skipTrivia(text, node.expression.end, /* stepAfterLineBreak */ true) - 1;
if (isLineBreak(text.charCodeAt(pos))) {
error(node.expression, Diagnostics.It_is_highly_likely_that_you_are_missing_a_semicolon);
Copy link
Member

Choose a reason for hiding this comment

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

I agree! This should definitely be a related span if possible.

@DanielRosenwasser DanielRosenwasser merged commit aa9230f into microsoft:master Sep 11, 2018
@DanielRosenwasser
Copy link
Member

Thanks @Kingwl!

@Kingwl Kingwl deleted the improve_accidentally_calling_type-assertion_expressions branch September 12, 2018 00:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants