Skip to content
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

Update VFile #19583

Merged
merged 16 commits into from Sep 7, 2017
Merged

Update VFile #19583

merged 16 commits into from Sep 7, 2017

Conversation

Rokt33r
Copy link
Contributor

@Rokt33r Rokt33r commented Sep 6, 2017

Please fill in this template.

  • Use a meaningful title for the pull request. Include the name of the package modified.
  • Test the change in your own code. (Compile and run.)
  • Follow the advice from the readme.
  • Avoid common mistakes.
  • Run npm run lint package-name (or tsc if no tslint.json is present).

Select one of these and delete the others:

If adding a new definition:

  • The package does not provide its own types, and you can not add them.
  • If this is for an NPM package, match the name. If not, do not conflict with the name of an NPM package.
  • Create it with dts-gen --dt, not by basing it on an existing project.
  • tslint.json should be present, and tsconfig.json should have noImplicitAny, noImplicitThis, and strictNullChecks set to true.

@dt-bot
Copy link
Member

dt-bot commented Sep 6, 2017

types/vfile/index.d.ts

Checklist

@Rokt33r
Copy link
Contributor Author

Rokt33r commented Sep 6, 2017

@wooorm (Author of VFile) Please check this.

declare namespace vfile {
type Contents = string | Buffer;

interface Position {
Copy link
Contributor

Choose a reason for hiding this comment

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

Use Point as a name for this interface.

column: number;
}

interface Location {
Copy link
Contributor

Choose a reason for hiding this comment

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

Use Position as a name for this interface.

}

interface VFileError {
ruleId: string;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can be null.

line: number;
column: number;
location: Location;
source: string;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can be null.

name: string;
file: string;
reason: string;
line: number;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can be null.

file: string;
reason: string;
line: number;
column: number;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can be null.

column: number;
location: Location;
source: string;
fatal: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can be null or undefined for informational messages.

history: string[];
data: {};
messages: VFileError[];
contents: string;
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn’t this be Contents?

dirname: string;
basename: string;
stem: string;
extname: string;
Copy link
Contributor

Choose a reason for hiding this comment

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

path, basename, stem, extname, dirname can all be undefined if no path is set yet.


type Fail = (reason?: string, position?: Point | Position, ruleId?: string) => void;

type Info = (reason?: string, position?: Point | Position, ruleId?: string) => void;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@wooorm I've also found, position can be Point or Position. Do I understand right?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes! Or a Node!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed!

It also can be Node.
So, I've extracted type for this argument as StringifiablePosition, which is `Point | Position | Node`.
Original properties must be kept!
@Rokt33r
Copy link
Contributor Author

Rokt33r commented Sep 6, 2017

@wooorm

I found vfile instance and its property, data, are extensible. So, I've added 2 generics to customize their typings. Now, type assertions against the below example are working strictly.

import vfile = require("vfile");

const file = vfile({
    custom: 'Custom tango',
    data: {
        custom: 12345
    }
});

// Typings of the custom data can be resolved from the parameters above
const custom: string = file.custom; // 'Custom tango'
const dataCustom: number = file.data.custom; // 12345

// Typings of original properties must be kept
const fileWithWrongParams = vfile({ path: 1234 }); // $ExpectError

Could you check it again? This should be the last review request.

Copy link
Contributor

@wooorm wooorm 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! I don’t use typings, so I’m not entirely sure though!

interface Node {
type: string;
data?: {};
position?: Position;
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this have things like children, value, or basically anything else too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, it should be better to have them.

children?: Node[];
value?: string;

It should be fine, right?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think so! Nodes are allowed to have any property though. HAST includes some other props actually in use.

Copy link
Contributor Author

@Rokt33r Rokt33r Sep 6, 2017

Choose a reason for hiding this comment

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

I see. Let's just keep only children and value for now.

To implement typings for the extra properties of HAST, we can extends from the interface

Copy link
Contributor

Choose a reason for hiding this comment

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

vfile doesn’t care about any other properties. Just that it’s an object with a position, and it’ll use that:

https://github.com/vfile/vfile/blob/86d0c4a17460f3788be7994b182ae8a3bc4207b5/index.js#L169-L171

So you could also allow any other properties!

Copy link
Contributor Author

@Rokt33r Rokt33r Sep 6, 2017

Choose a reason for hiding this comment

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

I see. The message method doesn't seem to care other values except the same properties of Position.
So, I think we should delete Node interface and exploit generics again.

<P extends Position>(reason: string, position?: Point | P, ruleId?: string) => VFileError;

Now the type checking works as our intention. :)

with invalid start
screen shot 2017-09-07 at 2 00 31 am

with Extra value
screen shot 2017-09-07 at 2 00 51 am


type Message = (reason: string, position?: StringifiablePosition, ruleId?: string) => VFileError;

type Fail = (reason?: string, position?: StringifiablePosition, ruleId?: string) => void;
Copy link
Contributor

Choose a reason for hiding this comment

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

fail throws the error btw!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As long as I know, we cannot define function types that always throw an error.

But, it should be better to use never for return type rather than void.
https://basarat.gitbooks.io/typescript/docs/types/never.html

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes!

A function always throws (e.g. in function `foo(){throw new Error('Not Implemented')}` the return type of `foo` is never)

@wooorm
Copy link
Contributor

wooorm commented Sep 6, 2017

👍👍👍

Position is not supposed to be extensible, but Node is.
Testing cases for this are also added.
@Rokt33r
Copy link
Contributor Author

Rokt33r commented Sep 6, 2017

@wooorm Sorry, I misunderstood! In the previous implementation, I made Position extensible, not Node.

So, I bring back Node and add definition test of it. It should be fine now.

3dd0a90#diff-e479b3678f226bc2d6568e43964a5c74

screen shot 2017-09-07 at 3 06 31 am

@sandersn
I mistakenly created another PR for VFile. And the previous one already has been accepted without reviewing any others. Could you give me some instruction how should I deal with it?

One of case must use point
@sandersn
Copy link
Contributor

sandersn commented Sep 6, 2017

Sorry, I didn't see this PR because @typescript-bot hasn't labelled it yet. I think the best thing is to merge from master and then undo the changes you don't want to keep from the previous PR.

@Rokt33r
Copy link
Contributor Author

Rokt33r commented Sep 6, 2017

@sandersn Okay, I'll. Thanks for the instruction. 💯

@wooorm There seems another PR for this library, and already merged into master.
Our one should be better because you reviewed, but the previous one also has lots of things good to have.(Detailed comment, Usage of Unist typings) 👯

@bizen241 wooorm and I are also working typings for this. I want you to review this PR after I fix the conflict. I'll ping you later when ready. 🥇

Should test with Node type with extra properties.
@Rokt33r
Copy link
Contributor Author

Rokt33r commented Sep 6, 2017

@bizen241, @wooorm I've finished. 💃
Let me know if there are still something to be fixed. I'll deal with it after I wake up.

@Rokt33r Rokt33r changed the title Add VFile Update VFile Sep 6, 2017
}
};
// Accept Point
file.message('test', position);
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn’t this one be startPoint?

Copy link
Contributor Author

@Rokt33r Rokt33r Sep 6, 2017

Choose a reason for hiding this comment

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

Yea, you're right. I've already fixed it since f161e0e.

Sorry for confusing you.

@bizen241
Copy link
Contributor

bizen241 commented Sep 6, 2017

@wooorm @Rokt33r I'm very sorry for my mistakes.
I should not have added typings without check by author.

I think my typing definition had a lots of problems.
But your typings is very good.

I added a definition of Unist in #19472 a few days ago.
I'm sorry but please check it.

Thank you for waiting and please accept my apologies.

for review: 👍

@Rokt33r
Copy link
Contributor Author

Rokt33r commented Sep 7, 2017

@bizen241 I'm okay. I don't think it is your fault. 😄
@sandersn I think it is ready to be shipped. Could you check this?

@sandersn
Copy link
Contributor

sandersn commented Sep 7, 2017

Well, I guess CI is back. It found some lint errors: an unused generic N somewhere in the code. After that I think it will be ready to go.

@Rokt33r
Copy link
Contributor Author

Rokt33r commented Sep 7, 2017

Fixed!

@sandersn sandersn merged commit 19d0354 into DefinitelyTyped:master Sep 7, 2017
@sandersn
Copy link
Contributor

sandersn commented Sep 7, 2017

Thanks @Rokt33r!

@Rokt33r Rokt33r deleted the vfile branch September 8, 2017 00:30
@kachkaev
Copy link
Contributor

Hey @Rokt33r! Would you be keen to update definitions for VFile 2.3?

See https://github.com/vfile/vfile/releases/tag/2.3.0

@Rokt33r
Copy link
Contributor Author

Rokt33r commented Feb 27, 2018

@kachkaev Yes, I would. 👍

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.

None yet

6 participants