Skip to content

Span length is not optional#18558

Merged
2 commits merged into
masterfrom
length
Sep 25, 2017
Merged

Span length is not optional#18558
2 commits merged into
masterfrom
length

Conversation

@ghost
Copy link
Copy Markdown

@ghost ghost commented Sep 18, 2017

Tested with Debug.assert(span.length !== undefined);, which passed on every test.

@ghost ghost requested a review from amcasey September 18, 2017 20:04
Copy link
Copy Markdown
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.

span.length might not be undefined, but it might be NaN. Last I checked, the code did some hacky subtraction without checking endPosition.

Basically

  1. 100 - undefined -> NaN.
  2. NaN || 0 -> 0.

@ghost
Copy link
Copy Markdown
Author

ghost commented Sep 19, 2017

Is there something we can do to prevent ourselves from creating a TextSpan with NaN in it? Shouldn't all of these have a valid span coming in?
However, I added an updated assertion and the following tests fail due to invalid length:

  • convertFunctionToEs6Class3.ts
  • convertFunctionToEs6Class1.ts
  • convertFunctionToEs6Class2.ts
  • convertFunctionToEs6ClassJsDoc.ts
  • convertFunctionToEs6Class-server.ts
  • convertFunctionToEs6Class_emptySwitchCase.ts
  • convertToEs6Class_emptyCatchClause.ts

I've been using JavaScript for years and never realized NaN was falsy. 🍶

@DanielRosenwasser
Copy link
Copy Markdown
Member

DanielRosenwasser commented Sep 21, 2017

It's done on our side. For example, in getEditsForAction, we literally have the following check

const length = context.endPosition === undefined ? 0 : context.endPosition - context.startPosition;

whereas we don't do the same thing in getAvailableActions. Don't ask me why. Also I'm sorry I didn't fix it first.

@ghost
Copy link
Copy Markdown
Author

ghost commented Sep 21, 2017

OK, I've changed that and tested with Debug.assert(span.length !== NaN && span.length !== undefined);.
EDIT: Tested with !isNaN(length) too...

@ghost
Copy link
Copy Markdown
Author

ghost commented Sep 22, 2017

@DanielRosenwasser Good to go?

@ghost ghost merged commit a4cf79b into master Sep 25, 2017
@ghost ghost deleted the length branch September 25, 2017 16:47
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
This pull request was closed.
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.

2 participants