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
Update VFile #19583
Conversation
types/vfile/index.d.ts Checklist
|
@wooorm (Author of VFile) Please check this. |
types/vfile/index.d.ts
Outdated
declare namespace vfile { | ||
type Contents = string | Buffer; | ||
|
||
interface Position { |
There was a problem hiding this comment.
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.
types/vfile/index.d.ts
Outdated
column: number; | ||
} | ||
|
||
interface Location { |
There was a problem hiding this comment.
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.
types/vfile/index.d.ts
Outdated
} | ||
|
||
interface VFileError { | ||
ruleId: string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be null
.
types/vfile/index.d.ts
Outdated
line: number; | ||
column: number; | ||
location: Location; | ||
source: string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be null
.
types/vfile/index.d.ts
Outdated
name: string; | ||
file: string; | ||
reason: string; | ||
line: number; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be null
.
types/vfile/index.d.ts
Outdated
file: string; | ||
reason: string; | ||
line: number; | ||
column: number; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be null
.
types/vfile/index.d.ts
Outdated
column: number; | ||
location: Location; | ||
source: string; | ||
fatal: boolean; |
There was a problem hiding this comment.
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.
types/vfile/index.d.ts
Outdated
history: string[]; | ||
data: {}; | ||
messages: VFileError[]; | ||
contents: string; |
There was a problem hiding this comment.
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
?
types/vfile/index.d.ts
Outdated
dirname: string; | ||
basename: string; | ||
stem: string; | ||
extname: string; |
There was a problem hiding this comment.
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.
It can take both Position and Point
types/vfile/index.d.ts
Outdated
|
||
type Fail = (reason?: string, position?: Point | Position, ruleId?: string) => void; | ||
|
||
type Info = (reason?: string, position?: Point | Position, ruleId?: string) => void; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes! Or a Node
!
There was a problem hiding this comment.
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!
I found 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. |
There was a problem hiding this 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!
types/vfile/index.d.ts
Outdated
interface Node { | ||
type: string; | ||
data?: {}; | ||
position?: Position; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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. :)
types/vfile/index.d.ts
Outdated
|
||
type Message = (reason: string, position?: StringifiablePosition, ruleId?: string) => VFileError; | ||
|
||
type Fail = (reason?: string, position?: StringifiablePosition, ruleId?: string) => void; |
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)
Delete `Node` interface because we are needed to check the properties about position only.
👍👍👍 |
Position is not supposed to be extensible, but Node is. Testing cases for this are also added.
@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 @sandersn |
One of case must use point
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. |
@sandersn Okay, I'll. Thanks for the instruction. 💯 @wooorm There seems another PR for this library, and already merged into master. @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.
types/vfile/vfile-tests.ts
Outdated
} | ||
}; | ||
// Accept Point | ||
file.message('test', position); |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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.
@wooorm @Rokt33r I'm very sorry for my mistakes. I think my typing definition had a lots of problems. I added a definition of Thank you for waiting and please accept my apologies. for review: 👍 |
`data` also can be decided by `C`.
Well, I guess CI is back. It found some lint errors: an unused generic |
Fixed! |
Thanks @Rokt33r! |
Hey @Rokt33r! Would you be keen to update definitions for VFile 2.3? |
@kachkaev Yes, I would. 👍 |
Please fill in this template.
npm run lint package-name
(ortsc
if notslint.json
is present).Select one of these and delete the others:
If adding a new definition:
dts-gen --dt
, not by basing it on an existing project.tslint.json
should be present, andtsconfig.json
should havenoImplicitAny
,noImplicitThis
, andstrictNullChecks
set totrue
.