Fix rollback Node

Position is not supposed to be extensible, but Node is.
Testing cases for this are also added.
This commit is contained in:
Junyoung Choi
2017-09-07 02:59:33 +09:00
parent 798233d7a5
commit 3dd0a90cb7
2 changed files with 31 additions and 3 deletions
+7 -3
View File
@@ -18,6 +18,10 @@ declare namespace vfile {
end: Point;
}
interface Node {
position: Position;
}
interface VFileParamsBase<D> {
data?: D;
contents?: Contents;
@@ -43,11 +47,11 @@ declare namespace vfile {
fatal?: boolean | null;
}
type Message = <P extends Position>(reason: string, position?: Point | P, ruleId?: string) => VFileError;
type Message = <N extends Node>(reason: string, position?: Point | Position | N, ruleId?: string) => VFileError;
type Fail = <P extends Position>(reason: string, position?: Point | P, ruleId?: string) => never;
type Fail = <N extends Node>(reason: string, position?: Point | Position | N, ruleId?: string) => never;
type Info = <P extends Position>(reason: string, position?: Point | P, ruleId?: string) => void;
type Info = <N extends Node>(reason: string, position?: Point | Position | N, ruleId?: string) => void;
type ToString = (encoding?: BufferEncoding) => string;
+24
View File
@@ -1,4 +1,5 @@
import vfile = require("vfile");
import { Point, Position } from "vfile";
const file = vfile({
path: '~/example.txt',
@@ -28,5 +29,28 @@ console.log(file.messages);
const custom: string = file.custom; // 'Custom tango'
const dataCustom: number = file.data.custom; // 12345
// message method accept any Stringifiable Position(e.g. point, position, node with position)
const startPoint: Point = {
line: 1,
column: 1,
};
const position: Position = {
start: startPoint,
end: {
line: 2,
column: 2,
}
};
// Accept Point
file.message('test', position);
// Accept Position
file.message('test', position);
// Accept Node with extra value
file.message('test', {
position
});
// But, it must reject any malformed position
file.message({ start: 'invalid point' }); // $ExpectError
// Typings of original properties must be kept
const fileWithWrongParams = vfile({ path: 1234 }); // $ExpectError