Skip to content

Conversation

@Rokt33r
Copy link
Contributor

@Rokt33r Rokt33r commented Nov 26, 2018

I tried to rewrite in typescript. This is not done yet because we need to deal with some issues. And I don't mind if it is not merged immediately.

FYI, you can run the test by:

npm run test-api

Copy link
Contributor Author

@Rokt33r Rokt33r left a comment

Choose a reason for hiding this comment

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

We don't have to worry about invalid values as long as we are using typescript. So I commented out some tests against them.

Should we....

  1. Keep the previous functionality(checking a parameter) and test in Javascript?
  2. Discard the useless code(because typescript ensure them) and test in Typescript?

I prefer 2. although it would be a breaking change.


function stringify(
value: Unist.Node | Unist.Position | Unist.Point,
): string | null {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we choose 2., it can just return string.

value: Unist.Node | Unist.Position | Unist.Point,
): string | null {
/* Nothing. */
if (!value || typeof value !== 'object') {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't need this checking.

}

/* Point. */
if (isPoint(value)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't need to check here neither because we've already check it is not a node or a position.

}

/* ? */
return null
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't need this neither

return null
}

function stringifyPoint(point: Unist.Point) {
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 renamed function name because tslint blames me that the argument name and the previous function name were same.

}

function stringifyPoint(point: Unist.Point) {
if (!point || typeof point !== 'object') {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't need to check this.

}

return (
convertIndexToNumber(point.line) + ':' + convertIndexToNumber(point.column)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It would be nicer to write with template string like

return `${point.line}:${point.column}`

return stringifyPoint(pos.start) + '-' + stringifyPoint(pos.end)
}

function convertIndexToNumber(value: any): number {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't need this.

@Rokt33r Rokt33r requested a review from wooorm November 26, 2018 09:13
@ChristianMurphy
Copy link
Member

the useless code(because typescript ensure them) and test in Typescript?

Many adopters are still using JavaScript. https://github.com/syntax-tree/unist-util-stringify-position/network/dependents
TypeScript does ahead of time checks when the adopter is also using TypeScript, but does not add runtime checks for JavaScript adopters. https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
I'd lean toward keeping the runtime type checks as is, or using something like https://github.com/fabiandev/ts-runtime to generate runtime type checks from the TypeScript definitions.

@Rokt33r
Copy link
Contributor Author

Rokt33r commented Nov 27, 2018

Hmm.. Sounds reasonable. Actually all of adopters still using js because there is no type definitions for ts. I think we need more time.

Then, I'll keep adding type definitions to DefinitelyTyped so lots of people could start using typescript. After lots of adopters using typescript, we might be able to discard runtime check.

@Rokt33r
Copy link
Contributor Author

Rokt33r commented Dec 13, 2018

I'll close this because it won't be merged soon! I'll be back with type definitions.

@Rokt33r Rokt33r closed this Dec 13, 2018
@wooorm wooorm added ☂️ area/types This affects typings 🙅 no/wontfix This is not (enough of) an issue for this project labels Aug 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

☂️ area/types This affects typings 🙅 no/wontfix This is not (enough of) an issue for this project

Development

Successfully merging this pull request may close these issues.

3 participants