fix(remark-extend): when throwing show path to used files

This commit is contained in:
Thomas Allmer 2020-05-27 17:23:26 +02:00 committed by Thomas Allmer
parent e99a4b4620
commit 7df6879af2
2 changed files with 100 additions and 25 deletions

View file

@ -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

View file

@ -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 = [
//