From 1dba641d6740ca05889de8a8cbe4753f73cbad6c Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Tue, 9 Feb 2021 11:43:55 +0100 Subject: [PATCH] Fix exception on tree w/o `children` --- index.js | 3 ++- test.js | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 0055646..6ff76ed 100644 --- a/index.js +++ b/index.js @@ -215,7 +215,8 @@ function messageControl(options) { // Detect gaps in `tree`. function detectGaps(tree, file) { - var lastNode = tree.children[tree.children.length - 1] + var children = tree.children || [] + var lastNode = children[children.length - 1] var offset = 0 var gaps = [] var gap diff --git a/test.js b/test.js index ecef908..aa07dc2 100644 --- a/test.js +++ b/test.js @@ -727,5 +727,21 @@ test('control()', function (t) { ) }) + remark() + .use(function () { + return function (tree, file) { + file.message('Error') + delete tree.children + control({name: 'foo', marker: mdastMarker})(tree, file) + } + }) + .process('', function (error, file) { + t.deepEqual( + [error].concat(file.messages.map(String)), + [null, '1:1: Error'], + 'should not fail when there are messages but no `children` on `tree`' + ) + }) + t.end() })