From dd5ecb17f5efa9814560f49db2075d1be700271e Mon Sep 17 00:00:00 2001 From: JounQin Date: Sun, 25 Sep 2022 10:12:01 +0000 Subject: [PATCH 1/3] fix: add types Parent/MapFunction back close #6 --- index.js | 8 +++++++- test.js | 13 +++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 0b7d96f..2337f6e 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,11 @@ /** * @typedef {import('unist').Node} Node + * @typedef {import('unist').Parent} Parent + */ + +/** + * @template {Node} [T = Node] + * @typedef {import('./complex-types.js').MapFunction} MapFunction */ /** @@ -9,7 +15,7 @@ * Type of input tree. * @param {Tree} tree * Tree to map. - * @param {import('./complex-types').MapFunction} mapFunction + * @param {MapFunction} mapFunction * Function called with a node, its index, and its parent to produce a new * node. * @returns {Tree} diff --git a/test.js b/test.js index 6e059d1..9dcb904 100644 --- a/test.js +++ b/test.js @@ -46,6 +46,12 @@ test('unist-util-map', function (t) { 'should work when passing an empty object' ) + t.deepEqual( + map(u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')]), asIs), + u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')]), + 'should keep node as-is' + ) + t.end() /** @@ -69,4 +75,11 @@ test('unist-util-map', function (t) { function addValue() { return {value: 'test'} } + + /** + * @type {import('./index.js').MapFunction} + */ + function asIs(node) { + return node + } }) From e72814a042784f2a42f6523e0cbcfeab5beb721f Mon Sep 17 00:00:00 2001 From: JounQin Date: Tue, 27 Sep 2022 17:26:19 +0800 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: Titus --- index.js | 7 ++++--- test.js | 11 +++++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 2337f6e..d182655 100644 --- a/index.js +++ b/index.js @@ -1,11 +1,12 @@ /** * @typedef {import('unist').Node} Node - * @typedef {import('unist').Parent} Parent */ /** - * @template {Node} [T = Node] - * @typedef {import('./complex-types.js').MapFunction} MapFunction + * @template {Node} [Tree=Node] + * @typedef {import('./complex-types.js').MapFunction} MapFunction + * Function called with a node, its index, and its parent to produce a new + * node. */ /** diff --git a/test.js b/test.js index 9dcb904..a7e09f6 100644 --- a/test.js +++ b/test.js @@ -46,10 +46,13 @@ test('unist-util-map', function (t) { 'should work when passing an empty object' ) + /** @type {Root} */ + const tree = u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')]) + t.deepEqual( - map(u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')]), asIs), - u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')]), - 'should keep node as-is' + map(tree, asIs), + tree, + 'should support an explicitly typed `MapFunction`' ) t.end() @@ -77,7 +80,7 @@ test('unist-util-map', function (t) { } /** - * @type {import('./index.js').MapFunction} + * @type {import('./index.js').MapFunction} */ function asIs(node) { return node From f05dc06e8ed55ffaba4ff34b14024b725eedf43b Mon Sep 17 00:00:00 2001 From: JounQin Date: Tue, 27 Sep 2022 09:33:22 +0000 Subject: [PATCH 3/3] docs: remove unnecessary mention about complex-types.d.ts --- readme.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/readme.md b/readme.md index ce5e61a..b38ee20 100644 --- a/readme.md +++ b/readme.md @@ -133,8 +133,7 @@ if the original node has children, those are mapped instead. ## Types This package is fully typed with [TypeScript][]. -It exports a type `MapFunction` from -`unist-util-map/complex-types.d.ts` to properly type `MapFunction`s. +It exports a type `MapFunction` to properly type `MapFunction`s. ## Compatibility