From 00fcc6eca2eaa5ffca41eeec000ff80b349030bf Mon Sep 17 00:00:00 2001 From: Junyoung Choi Date: Tue, 4 Dec 2018 15:23:12 +0900 Subject: [PATCH 1/8] Add vfile-message --- types/vfile-message/index.d.ts | 74 ++++++++++++++++++++++ types/vfile-message/tsconfig.json | 22 +++++++ types/vfile-message/tslint.json | 1 + types/vfile-message/vfile-message-tests.ts | 32 ++++++++++ 4 files changed, 129 insertions(+) create mode 100644 types/vfile-message/index.d.ts create mode 100644 types/vfile-message/tsconfig.json create mode 100644 types/vfile-message/tslint.json create mode 100644 types/vfile-message/vfile-message-tests.ts diff --git a/types/vfile-message/index.d.ts b/types/vfile-message/index.d.ts new file mode 100644 index 0000000000..a39d916a34 --- /dev/null +++ b/types/vfile-message/index.d.ts @@ -0,0 +1,74 @@ +// Type definitions for vfile-message 1.0 +// Project: https://github.com/vfile/vfile-message#readme +// Definitions by: Junyoung Choi +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +import * as Unist from 'unist'; + +declare namespace vfileMessage { + /** + * Create a virtual message. + */ + interface VFileMessage extends Error { + /** + * Constructor of a message for `reason` at `position` from `origin`. + * When an error is passed in as `reason`, copies the `stack`. + * + * @param reason Reason for message (`string` or `Error`). Uses the stack and message of the error if given. + * @param position Place at which the message occurred in a file (`Node`, `Position`, or `Point`, optional). + * @param origin Place in code the message originates from (`string`, optional). + */ + (reason: string | Error, position?: Unist.Node | Unist.Position | Unist.Point, origin?: string): VFileMessage + /** + * Category of message. + */ + ruleId: string | null; + /** + * Reason for message. + */ + reason: string; + /** + * Starting line of error. + */ + line: number | null; + /** + * Starting column of error. + */ + column: number | null; + /** + * Full range information, when available. + * Has start and end properties, both set to an object with line and column, set to number?. + */ + location: Unist.Position; + /** + * Namespace of warning. + */ + source: string | null; + /** + * If true, marks associated file as no longer processable. + */ + fatal?: boolean | null; + /** + * You may add a file property with a path of a file (used throughout the VFile ecosystem). + */ + file?: string; + /** + * You may add a note property with a long form description of the message (supported by vfile-reporter). + */ + note?: string; + /** + * You may add a url property with a link to documentation for the message. + */ + url?: string; + /** + * It’s OK to store custom data directly on the VMessage, some of those are handled by utilities. + */ + [key: string]: unknown; + } +} + +declare const vfileMessage: vfileMessage.VFileMessage; + +export = vfileMessage; diff --git a/types/vfile-message/tsconfig.json b/types/vfile-message/tsconfig.json new file mode 100644 index 0000000000..e740952e79 --- /dev/null +++ b/types/vfile-message/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "vfile-message-tests.ts" + ] +} diff --git a/types/vfile-message/tslint.json b/types/vfile-message/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/vfile-message/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/vfile-message/vfile-message-tests.ts b/types/vfile-message/vfile-message-tests.ts new file mode 100644 index 0000000000..ed9086631f --- /dev/null +++ b/types/vfile-message/vfile-message-tests.ts @@ -0,0 +1,32 @@ +import vfileMessage = require('vfile-message'); + +const message = vfileMessage('Error!'); +vfileMessage(new Error()); +vfileMessage('Error!', { + type: 'random node' +}); +vfileMessage('Error!', { + start: { + line: 1, + column: 1 + }, + end: { + line: 1, + column: 1 + } +}); +vfileMessage('Error!', { + line: 1, + column: 1 +}); +vfileMessage('Error!', undefined, 'test'); + +message.file = ''; +message.name = ''; +message.reason = ''; +message.message = ''; +message.stack = ''; +message.fatal = null; +message.fatal = true; +message.column = 1; +message.line = 1; From b2d0033fff089a6373666d8619449d77cc37c239 Mon Sep 17 00:00:00 2001 From: Junyoung Choi Date: Tue, 4 Dec 2018 15:25:15 +0900 Subject: [PATCH 2/8] Discard local type definitions in vfile --- types/vfile/index.d.ts | 44 +++----------------------------------- types/vfile/vfile-tests.ts | 22 +++---------------- 2 files changed, 6 insertions(+), 60 deletions(-) diff --git a/types/vfile/index.d.ts b/types/vfile/index.d.ts index df729e0a49..d1f3321ab7 100644 --- a/types/vfile/index.d.ts +++ b/types/vfile/index.d.ts @@ -8,6 +8,7 @@ /// import * as Unist from 'unist'; +import * as vfileMessage from 'vfile-message'; declare namespace vfile { type VFileContents = string | Buffer; @@ -29,45 +30,6 @@ declare namespace vfile { [key: string]: any; } - /** - * File-related message describing something at certain position. - */ - interface VFileMessage { - /** - * File-path, when the message was triggered. - */ - file: string; - /** - * Category of message. - */ - ruleId: string | null; - /** - * Reason for message. - */ - reason: string; - /** - * Starting line of error. - */ - line: number | null; - /** - * Starting column of error. - */ - column: number | null; - /** - * Full range information, when available. - * Has start and end properties, both set to an object with line and column, set to number?. - */ - location: Unist.Position; - /** - * Namespace of warning. - */ - source: string | null; - /** - * If true, marks associated file as no longer processable. - */ - fatal?: boolean | null; - } - /** * Associates a message with the file for `reason` at `position`. * When an error is passed in as `reason`, copies the stack. @@ -76,7 +38,7 @@ declare namespace vfile { * @param position Place at which the message occurred in `vfile`. * @param ruleId Category of message. */ - type Message = (reason: string, position?: Unist.Point | Unist.Position | NodeWithPosition, ruleId?: string) => VFileMessage; + type Message = (reason: string, position?: Unist.Point | Unist.Position | NodeWithPosition, ruleId?: string) => vfileMessage.VFileMessage; /** * Associates a fatal message with the file, then immediately throws it. * Note: fatal errors mean a file is no longer processable. @@ -121,7 +83,7 @@ declare namespace vfile { /** * List of messages associated with the file. */ - messages: VFileMessage[]; + messages: vfileMessage.VFileMessage[]; /** * Raw value. */ diff --git a/types/vfile/vfile-tests.ts b/types/vfile/vfile-tests.ts index 5c14c8502b..36c0ac163e 100644 --- a/types/vfile/vfile-tests.ts +++ b/types/vfile/vfile-tests.ts @@ -1,5 +1,6 @@ import vfile = require('vfile'); import * as Unist from 'unist'; +import vfileMessage = require('vfile-message'); // Instantiation const file: vfile.VFile = vfile(); @@ -15,24 +16,7 @@ file.dirname = '~'; file.extname = '.md'; file.basename = 'test.text'; file.history = ['~/test.txt']; // => ['~/example.txt', '~/example.md', '~/index.text'] -file.messages = [{ - file: '~/test.txt', - ruleId: '', - reason: '', - line: 1, - column: 1, - location: { - start: { - line: 1, - column: 1, - }, - end: { - line: 1, - column: 1, - } - }, - source: '' -}]; +file.messages = [vfileMessage('random error')]; const startPoint: Unist.Point = { line: 1, @@ -46,7 +30,7 @@ const position: Unist.Position = { }, }; -file.message('test', startPoint); +const message: vfileMessage.VFileMessage = file.message('test', startPoint, 'test origin'); file.message('test', position); file.message('test', { type: 'ramdom node', From 10f75ca776a86365428d98762e361c95beed0afc Mon Sep 17 00:00:00 2001 From: Junyoung Choi Date: Tue, 4 Dec 2018 15:27:09 +0900 Subject: [PATCH 3/8] Fix type definition of info method --- types/vfile/index.d.ts | 2 +- types/vfile/vfile-tests.ts | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/types/vfile/index.d.ts b/types/vfile/index.d.ts index d1f3321ab7..1d0b9f76d9 100644 --- a/types/vfile/index.d.ts +++ b/types/vfile/index.d.ts @@ -55,7 +55,7 @@ declare namespace vfile { * @param position Place at which the message occurred in `vfile`. * @param ruleId Category of message. */ - type Info = (reason: string, position?: Unist.Point | Unist.Position | NodeWithPosition, ruleId?: string) => void; + type Info = (reason: string, position?: Unist.Point | Unist.Position | NodeWithPosition, ruleId?: string) => vfileMessage.VFileMessage; /** * Convert contents of `vfile` to string. diff --git a/types/vfile/vfile-tests.ts b/types/vfile/vfile-tests.ts index 36c0ac163e..b89df6db23 100644 --- a/types/vfile/vfile-tests.ts +++ b/types/vfile/vfile-tests.ts @@ -40,6 +40,10 @@ file.message('test', { file.message('test', { start: 'invalid point' }); // $ExpectError +file.fail('test'); + +const infoMessage: vfileMessage.VFileMessage = file.info('test'); + // Using `data` prop interface CustomData { message: string; From cad3854ae499b17e4ec6d21686806be042f5199e Mon Sep 17 00:00:00 2001 From: Junyoung Choi Date: Tue, 4 Dec 2018 15:34:21 +0900 Subject: [PATCH 4/8] Clean up methods of vfile --- types/vfile/index.d.ts | 68 +++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 37 deletions(-) diff --git a/types/vfile/index.d.ts b/types/vfile/index.d.ts index 1d0b9f76d9..70ca5b8cd9 100644 --- a/types/vfile/index.d.ts +++ b/types/vfile/index.d.ts @@ -30,47 +30,11 @@ declare namespace vfile { [key: string]: any; } - /** - * Associates a message with the file for `reason` at `position`. - * When an error is passed in as `reason`, copies the stack. - * Each message has a `fatal` property which by default is set to `false` (ie. `warning`). - * @param reason Reason for message. Uses the stack and message of the error if given. - * @param position Place at which the message occurred in `vfile`. - * @param ruleId Category of message. - */ - type Message = (reason: string, position?: Unist.Point | Unist.Position | NodeWithPosition, ruleId?: string) => vfileMessage.VFileMessage; - /** - * Associates a fatal message with the file, then immediately throws it. - * Note: fatal errors mean a file is no longer processable. - * Calls `message()` internally. - * @param reason Reason for message. Uses the stack and message of the error if given. - * @param position Place at which the message occurred in `vfile`. - * @param ruleId Category of message. - */ - type Fail = (reason: string, position?: Unist.Point | Unist.Position | NodeWithPosition, ruleId?: string) => never; - /** - * Associates an informational message with the file, where `fatal` is set to `null`. - * Calls `message()` internally. - * @param reason Reason for message. Uses the stack and message of the error if given. - * @param position Place at which the message occurred in `vfile`. - * @param ruleId Category of message. - */ - type Info = (reason: string, position?: Unist.Point | Unist.Position | NodeWithPosition, ruleId?: string) => vfileMessage.VFileMessage; - - /** - * Convert contents of `vfile` to string. - * @param encoding If `contents` is a buffer, `encoding` is used to stringify buffers (default: `'utf8'`). - */ - type ToString = (encoding?: BufferEncoding) => string; - interface VFile { /** * @param options If `options` is `string` or `Buffer`, treats it as `{contents: options}`. If `options` is a `VFile`, returns it. All other options are set on the newly created `vfile`. */ (input?: VFileContents | F | VFileOptions): F; - message: Message; - fail: Fail; - info: Info; /** * List of file-paths the file moved between. */ @@ -119,7 +83,37 @@ declare namespace vfile { * Defaults to `process.cwd()`. */ cwd: string; - toString: ToString; + /** + * Convert contents of `vfile` to string. + * @param encoding If `contents` is a buffer, `encoding` is used to stringify buffers (default: `'utf8'`). + */ + toString: (encoding?: BufferEncoding) => string; + /** + * Associates a message with the file for `reason` at `position`. + * When an error is passed in as `reason`, copies the stack. + * Each message has a `fatal` property which by default is set to `false` (ie. `warning`). + * @param reason Reason for message. Uses the stack and message of the error if given. + * @param position Place at which the message occurred in `vfile`. + * @param ruleId Category of message. + */ + message: (reason: string, position?: Unist.Point | Unist.Position | NodeWithPosition, ruleId?: string) => vfileMessage.VFileMessage; + /** + * Associates a fatal message with the file, then immediately throws it. + * Note: fatal errors mean a file is no longer processable. + * Calls `message()` internally. + * @param reason Reason for message. Uses the stack and message of the error if given. + * @param position Place at which the message occurred in `vfile`. + * @param ruleId Category of message. + */ + fail: (reason: string, position?: Unist.Point | Unist.Position | NodeWithPosition, ruleId?: string) => never; + /** + * Associates an informational message with the file, where `fatal` is set to `null`. + * Calls `message()` internally. + * @param reason Reason for message. Uses the stack and message of the error if given. + * @param position Place at which the message occurred in `vfile`. + * @param ruleId Category of message. + */ + info: (reason: string, position?: Unist.Point | Unist.Position | NodeWithPosition, ruleId?: string) => vfileMessage.VFileMessage; } } From 3844e52c09e513a8edc6d4bdfb4b7d8216697aa0 Mon Sep 17 00:00:00 2001 From: Junyoung Choi Date: Tue, 4 Dec 2018 15:45:09 +0900 Subject: [PATCH 5/8] Fix lint errors of vfile-message --- types/vfile-message/index.d.ts | 3 ++- types/vfile-message/tsconfig.json | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/types/vfile-message/index.d.ts b/types/vfile-message/index.d.ts index a39d916a34..5f4467e007 100644 --- a/types/vfile-message/index.d.ts +++ b/types/vfile-message/index.d.ts @@ -2,6 +2,7 @@ // Project: https://github.com/vfile/vfile-message#readme // Definitions by: Junyoung Choi // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 3.0 /// @@ -20,7 +21,7 @@ declare namespace vfileMessage { * @param position Place at which the message occurred in a file (`Node`, `Position`, or `Point`, optional). * @param origin Place in code the message originates from (`string`, optional). */ - (reason: string | Error, position?: Unist.Node | Unist.Position | Unist.Point, origin?: string): VFileMessage + (reason: string | Error, position?: Unist.Node | Unist.Position | Unist.Point, origin?: string): VFileMessage; /** * Category of message. */ diff --git a/types/vfile-message/tsconfig.json b/types/vfile-message/tsconfig.json index e740952e79..9e35438c08 100644 --- a/types/vfile-message/tsconfig.json +++ b/types/vfile-message/tsconfig.json @@ -7,6 +7,7 @@ "noImplicitAny": true, "noImplicitThis": true, "strictNullChecks": true, + "strictFunctionTypes": true, "baseUrl": "../", "typeRoots": [ "../" From 0383646a99e5f7021f782b07ee7675176b016356 Mon Sep 17 00:00:00 2001 From: Junyoung Choi Date: Tue, 4 Dec 2018 15:49:05 +0900 Subject: [PATCH 6/8] Update and move vfile constructor description so it can be seen in vscode hint --- types/vfile/index.d.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/types/vfile/index.d.ts b/types/vfile/index.d.ts index 70ca5b8cd9..459223d4b9 100644 --- a/types/vfile/index.d.ts +++ b/types/vfile/index.d.ts @@ -32,6 +32,13 @@ declare namespace vfile { interface VFile { /** + * Create a new virtual file. If `options` is `string` or `Buffer`, treats it as `{contents: options}`. + * If `options` is a `VFile`, returns it. All other options are set on the newly created `vfile`. + * + * Path related properties are set in the following order (least specific to most specific): `history`, `path`, `basename`, `stem`, `extname`, `dirname`. + * + * It’s not possible to set either `dirname` or `extname` without setting either `history`, `path`, `basename`, or `stem` as well. + * * @param options If `options` is `string` or `Buffer`, treats it as `{contents: options}`. If `options` is a `VFile`, returns it. All other options are set on the newly created `vfile`. */ (input?: VFileContents | F | VFileOptions): F; @@ -117,11 +124,6 @@ declare namespace vfile { } } -/** - * Create a new virtual file. - * Path related properties are set in the following order (least specific to most specific): `history`, `path`, `basename`, `stem`, `extname`, `dirname`. - * It’s not possible to set either `dirname` or `extname` without setting either `history`, `path`, `basename`, or `stem` as well. - */ declare const vfile: vfile.VFile; export = vfile; From 8144af436c041ca46587678cf907e81456fc4bf3 Mon Sep 17 00:00:00 2001 From: Junyoung Choi Date: Tue, 4 Dec 2018 16:27:52 +0900 Subject: [PATCH 7/8] Implement AnyNode type so position can accept any nodes --- types/vfile-message/index.d.ts | 6 +++++- types/vfile/index.d.ts | 11 +++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/types/vfile-message/index.d.ts b/types/vfile-message/index.d.ts index 5f4467e007..0ee47ee365 100644 --- a/types/vfile-message/index.d.ts +++ b/types/vfile-message/index.d.ts @@ -9,6 +9,10 @@ import * as Unist from 'unist'; declare namespace vfileMessage { + interface AnyNode extends Unist.Node{ + [key: string]: unknown + } + /** * Create a virtual message. */ @@ -21,7 +25,7 @@ declare namespace vfileMessage { * @param position Place at which the message occurred in a file (`Node`, `Position`, or `Point`, optional). * @param origin Place in code the message originates from (`string`, optional). */ - (reason: string | Error, position?: Unist.Node | Unist.Position | Unist.Point, origin?: string): VFileMessage; + (reason: string | Error, position?: AnyNode | Unist.Position | Unist.Point, origin?: string): VFileMessage; /** * Category of message. */ diff --git a/types/vfile/index.d.ts b/types/vfile/index.d.ts index 459223d4b9..efb07e288d 100644 --- a/types/vfile/index.d.ts +++ b/types/vfile/index.d.ts @@ -13,11 +13,6 @@ import * as vfileMessage from 'vfile-message'; declare namespace vfile { type VFileContents = string | Buffer; - interface NodeWithPosition extends Unist.Node { - position: Unist.Position; - [key: string]: any; - } - interface VFileOptions { contents?: VFileContents; path?: string; @@ -103,7 +98,7 @@ declare namespace vfile { * @param position Place at which the message occurred in `vfile`. * @param ruleId Category of message. */ - message: (reason: string, position?: Unist.Point | Unist.Position | NodeWithPosition, ruleId?: string) => vfileMessage.VFileMessage; + message: (reason: string, position?: Unist.Point | Unist.Position | vfileMessage.AnyNode, ruleId?: string) => vfileMessage.VFileMessage; /** * Associates a fatal message with the file, then immediately throws it. * Note: fatal errors mean a file is no longer processable. @@ -112,7 +107,7 @@ declare namespace vfile { * @param position Place at which the message occurred in `vfile`. * @param ruleId Category of message. */ - fail: (reason: string, position?: Unist.Point | Unist.Position | NodeWithPosition, ruleId?: string) => never; + fail: (reason: string, position?: Unist.Point | Unist.Position | vfileMessage.AnyNode, ruleId?: string) => never; /** * Associates an informational message with the file, where `fatal` is set to `null`. * Calls `message()` internally. @@ -120,7 +115,7 @@ declare namespace vfile { * @param position Place at which the message occurred in `vfile`. * @param ruleId Category of message. */ - info: (reason: string, position?: Unist.Point | Unist.Position | NodeWithPosition, ruleId?: string) => vfileMessage.VFileMessage; + info: (reason: string, position?: Unist.Point | Unist.Position | vfileMessage.AnyNode, ruleId?: string) => vfileMessage.VFileMessage; } } From 123806795f7fb7ba4ece861fc2975f14d3bd96f5 Mon Sep 17 00:00:00 2001 From: Junyoung Choi Date: Tue, 4 Dec 2018 16:28:43 +0900 Subject: [PATCH 8/8] Fix lint errors in vfile-message --- types/vfile-message/index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types/vfile-message/index.d.ts b/types/vfile-message/index.d.ts index 0ee47ee365..908f72027b 100644 --- a/types/vfile-message/index.d.ts +++ b/types/vfile-message/index.d.ts @@ -9,8 +9,8 @@ import * as Unist from 'unist'; declare namespace vfileMessage { - interface AnyNode extends Unist.Node{ - [key: string]: unknown + interface AnyNode extends Unist.Node { + [key: string]: unknown; } /**