From 3dd0a90cb738c8bc610aeb4c2745cb4a6469835a Mon Sep 17 00:00:00 2001 From: Junyoung Choi Date: Thu, 7 Sep 2017 02:59:33 +0900 Subject: [PATCH] Fix rollback Node Position is not supposed to be extensible, but Node is. Testing cases for this are also added. --- types/vfile/index.d.ts | 10 +++++++--- types/vfile/vfile-tests.ts | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/types/vfile/index.d.ts b/types/vfile/index.d.ts index 06726ef138..9289ff826e 100644 --- a/types/vfile/index.d.ts +++ b/types/vfile/index.d.ts @@ -18,6 +18,10 @@ declare namespace vfile { end: Point; } + interface Node { + position: Position; + } + interface VFileParamsBase { data?: D; contents?: Contents; @@ -43,11 +47,11 @@ declare namespace vfile { fatal?: boolean | null; } - type Message =

(reason: string, position?: Point | P, ruleId?: string) => VFileError; + type Message = (reason: string, position?: Point | Position | N, ruleId?: string) => VFileError; - type Fail =

(reason: string, position?: Point | P, ruleId?: string) => never; + type Fail = (reason: string, position?: Point | Position | N, ruleId?: string) => never; - type Info =

(reason: string, position?: Point | P, ruleId?: string) => void; + type Info = (reason: string, position?: Point | Position | N, ruleId?: string) => void; type ToString = (encoding?: BufferEncoding) => string; diff --git a/types/vfile/vfile-tests.ts b/types/vfile/vfile-tests.ts index 293d8f15d5..a4b0edac67 100644 --- a/types/vfile/vfile-tests.ts +++ b/types/vfile/vfile-tests.ts @@ -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