Skip to content

Fix TypeScript regression by moving non-null assertion to the end of the line instead of the beginning#4052

Merged
duailibe merged 1 commit intoprettier:masterfrom
ericanderson:fix-non-null-identifier-in-chain
Feb 26, 2018
Merged

Fix TypeScript regression by moving non-null assertion to the end of the line instead of the beginning#4052
duailibe merged 1 commit intoprettier:masterfrom
ericanderson:fix-non-null-identifier-in-chain

Conversation

@ericanderson
Copy link
Copy Markdown
Contributor

@ericanderson ericanderson commented Feb 26, 2018

Fixes #4051

Tests are failing but its the same tests that are failing on master (see #4053), in typescript conditional code; unrelated to chaining.

@ericanderson
Copy link
Copy Markdown
Contributor Author

This needs to go in 1.11. Without it, non-null declarations on identifiers get split on lines, causing tslint to not be able to parse the line.

@ericanderson
Copy link
Copy Markdown
Contributor Author

@j-f1

@ericanderson ericanderson changed the title Fix(typescript) non-null identifier same line in chain Fix(typescript) regression non-null identifier same line in chain Feb 26, 2018
@j-f1 j-f1 added this to the 1.11 milestone Feb 26, 2018
@j-f1 j-f1 self-requested a review February 26, 2018 17:09
@j-f1 j-f1 changed the title Fix(typescript) regression non-null identifier same line in chain Fix TypeScript regression by moving non-null assertion to the end of the line instead of the beginning Feb 26, 2018
@j-f1 j-f1 requested a review from duailibe February 26, 2018 17:54
@duailibe duailibe merged commit efd5bde into prettier:master Feb 26, 2018
@duailibe
Copy link
Copy Markdown
Collaborator

Thanks for contributing! :)

@vjeux
Copy link
Copy Markdown
Contributor

vjeux commented Feb 26, 2018

I'm curious, is the fact that tslint doesn't parse it the only issue with this? Let me preface it with the fact that I've never seen or written any code with it but the way it was originally printed looked clearer to me.

It feels weird to have the ! hanging at the end of the previous line. !. feels like an operator to me, not two distinct tokens.

@ericanderson
Copy link
Copy Markdown
Contributor Author

It seemed typescript didn't mind it.

I thought this was important because it was a regression.

I also think the ! must go with the preceding thing as thats what it modifies.

@vjeux
Copy link
Copy Markdown
Contributor

vjeux commented Feb 26, 2018

In JavaScript, ?. needs to be as a single token otherwise it's not being parsed correctly.

SyntaxError: Unexpected token (1:5)
> 1 | a ? . b
    |     ^

Can you give what was the error message for tslint? We should definitely raise an issue to the project at least.

@j-f1
Copy link
Copy Markdown
Member

j-f1 commented Feb 26, 2018

I'm curious, is the fact that tslint doesn't parse it the only issue with this?

It’s not just TSLint, it’s invalid TypeScript. TS parses it as:

foo;
!(<identifier with name === "">.bar())

@vjeux
Copy link
Copy Markdown
Contributor

vjeux commented Feb 26, 2018

That sounds like a bug! Could any of you two report it to the typescript repo?

@ericanderson
Copy link
Copy Markdown
Contributor Author

This is the fix

@duailibe
Copy link
Copy Markdown
Collaborator

@vjeux I don't think they should behave the same in this case.. I think ?. should follow the same rules as . they are both "accessing" something in the object

! is just type casting (a! or (a as B)) and doesn't need to be in a chain for that matter

(I think that's it, I don't really use TS or ?. so please correct me if I said something wrong)

@vjeux
Copy link
Copy Markdown
Contributor

vjeux commented Feb 26, 2018

My understanding is that !. means access or throw and ?. means access or return null. I view them as being the same operation.

@duailibe
Copy link
Copy Markdown
Collaborator

From https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html:

A new ! post-fix expression operator may be used to assert that its operand is non-null and non-undefined in contexts where the type checker is unable to conclude that fact. Specifically, the operation x! produces a value of the type of x with null and undefined excluded. Similar to type assertions of the forms <T>x and x as T, the ! non-null assertion operator is simply removed in the emitted JavaScript code.

@vjeux
Copy link
Copy Markdown
Contributor

vjeux commented Feb 26, 2018

Makes sense, thanks for the explanation @duailibe!

I would still like to see an issue reported.

a!
  .b

and

a
  !.b

should likely parse the same way.

@ericanderson
Copy link
Copy Markdown
Contributor Author

I'm perfectly fine with the second form being an error.

@ericanderson ericanderson deleted the fix-non-null-identifier-in-chain branch February 26, 2018 19:25
@vjeux
Copy link
Copy Markdown
Contributor

vjeux commented Feb 26, 2018

Ohhh, okay I re-read @j-f1 explanation and this is because of automatic semi-colon insertion. Because it's a post-fix operator and not an infix one with the dot, then it changes the parsing tree. I doubt that they'll want to change the ASI rules to have ! behave differently. ? is already checked differently because it's used for ?:.

@ericanderson thanks for doing this! I just wanted to make sure that we were doing the right thing.

@lock lock Bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Jan 18, 2019
@lock lock Bot locked as resolved and limited conversation to collaborators Jan 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants