From 7df6879af2f455ba0dd938a9e6375b0751d714fc Mon Sep 17 00:00:00 2001 From: Thomas Allmer Date: Wed, 27 May 2020 17:23:26 +0200 Subject: [PATCH] fix(remark-extend): when throwing show path to used files --- packages/remark-extend/src/remarkExtend.js | 18 ++- .../test-node/remark-extend.test.js | 107 ++++++++++++++---- 2 files changed, 100 insertions(+), 25 deletions(-) diff --git a/packages/remark-extend/src/remarkExtend.js b/packages/remark-extend/src/remarkExtend.js index 29b6a31fb..23fc7b67d 100644 --- a/packages/remark-extend/src/remarkExtend.js +++ b/packages/remark-extend/src/remarkExtend.js @@ -274,7 +274,14 @@ function remarkExtend(options) { const { action, startSelector, endSelector, jsCode, addNodes } = extensionTask; const start = select(extensionTask.startSelector, tree); if (!start) { - throw new Error(`The start selector "${startSelector}" could not find a matching node.`); + const msg = [ + `The start selector "${startSelector}" could not find a matching node.`, + options.filePath ? `Markdown File: ${options.filePath}` : '', + options.overrideFilePath ? `Override File: ${options.overrideFilePath}` : '', + ] + .filter(Boolean) + .join('\n'); + throw new Error(msg); } const startIsNode = { ...start }; delete startIsNode.children; // unified is comparison does not support children @@ -283,7 +290,14 @@ function remarkExtend(options) { if (action === 'replaceBetween' || action === 'removeBetween') { const end = select(endSelector, tree); if (!end) { - throw new Error(`The end selector "${endSelector}" could not find a matching node.`); + const msg = [ + `The end selector "${endSelector}" could not find a matching node.`, + options.filePath ? `Markdown File: ${options.filePath}` : '', + options.overrideFilePath ? `Override File: ${options.overrideFilePath}` : '', + ] + .filter(Boolean) + .join('\n'); + throw new Error(msg); } endIsNode = { ...end }; delete endIsNode.children; // unified is comparison does not support children diff --git a/packages/remark-extend/test-node/remark-extend.test.js b/packages/remark-extend/test-node/remark-extend.test.js index b3398da98..d4813ac90 100644 --- a/packages/remark-extend/test-node/remark-extend.test.js +++ b/packages/remark-extend/test-node/remark-extend.test.js @@ -126,29 +126,6 @@ describe('remarkExtend', () => { expect(result.contents).to.equal(output); }); - it('throws if a start selector is not found', async () => { - const input = [ - // - '### Red', - ].join('\n'); - const extendMd = [ - "```js ::replaceFrom('heading:has([value=More Red])')", - 'module.exports.replaceSection = (node) => {}', - '```', - ].join('\n'); - - const parser = unified() - // - .use(markdown) - .use(remarkExtend, { extendMd }) - .use(mdStringify); - - await expectThrowsAsync( - () => parser.process(input), - 'The start selector "heading:has([value=More Red])" could not find a matching node.', - ); - }); - it('can replace within a range (start point included, endpoint not)', async () => { const input = [ '### Red', @@ -191,6 +168,60 @@ describe('remarkExtend', () => { expect(result.contents).to.equal(output); }); + it('throws if a start selector is not found', async () => { + const input = [ + // + '### Red', + ].join('\n'); + const extendMd = [ + "```js ::replaceFrom('heading:has([value=More Red])')", + 'module.exports.replaceSection = (node) => {}', + '```', + ].join('\n'); + + const parser = unified() + // + .use(markdown) + .use(remarkExtend, { extendMd }) + .use(mdStringify); + + await expectThrowsAsync( + () => parser.process(input), + 'The start selector "heading:has([value=More Red])" could not find a matching node.', + ); + }); + + it('throws with addition info (if provide as filePath, overrideFilePath) if a start selector is not found', async () => { + const input = [ + // + '### Red', + ].join('\n'); + const extendMd = [ + "```js ::replaceFrom('heading:has([value=More Red])')", + 'module.exports.replaceSection = (node) => {}', + '```', + ].join('\n'); + + const parser = unified() + // + .use(markdown) + .use(remarkExtend, { + extendMd, + filePath: '/path/to/input.md', + overrideFilePath: '/path/to/override.md', + }) + .use(mdStringify); + + await expectThrowsAsync( + () => parser.process(input), + [ + 'The start selector "heading:has([value=More Red])" could not find a matching node.', + 'Markdown File: /path/to/input.md', + 'Override File: /path/to/override.md', + ].join('\n'), + ); + }); + it('throws if a end selector is not found', async () => { const input = [ // @@ -210,6 +241,36 @@ describe('remarkExtend', () => { ); }); + it('throws with addition info (if provide as filePath, overrideFilePath) if a end selector is not found', async () => { + const input = [ + // + '### Red', + ].join('\n'); + const extendMd = [ + "```js ::replaceBetween('heading:has([value=Red])', 'heading:has([value=Red2])')", + 'module.exports.replaceSection = (node) => {}', + '```', + ].join('\n'); + + const parser = unified() + .use(markdown) + .use(remarkExtend, { + extendMd, + filePath: '/path/to/input.md', + overrideFilePath: '/path/to/override.md', + }) + .use(mdStringify); + + await expectThrowsAsync( + () => parser.process(input), + [ + 'The end selector "heading:has([value=Red2])" could not find a matching node.', + 'Markdown File: /path/to/input.md', + 'Override File: /path/to/override.md', + ].join('\n'), + ); + }); + it('replaces a single node if replacing between the start and end of the same node', async () => { const input = [ //