Skip to content

Commit

Permalink
Add invalid file path to parse warning
Browse files Browse the repository at this point in the history
  • Loading branch information
ogonkov committed Sep 4, 2020
1 parent 6d4bec5 commit 2fa89e3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/get-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ async function getAbsoluteFileImports(resourcePath, source) {
return [...fileImports].map(toAbsolutePath);
}

/**
* @template {Error} TError
* @param {TError} error
* @param {string} path
* @returns {TError}
*/
function appendPathToParseError(error, path) {
error.message = `${error.message}\nFile: ${path}`;
return error;
}

export async function getDependencies(loaderContext, source) {
const {resourcePath} = loaderContext;
const dependencies = new Set();
Expand All @@ -69,7 +80,7 @@ export async function getDependencies(loaderContext, source) {
source
);
} catch (e) {
loaderContext.emitWarning(e);
loaderContext.emitWarning(appendPathToParseError(e, resourcePath));

continue;
}
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/another-include.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<fest:template xmlns:fest="http://fest.mail.ru" context_name="json">
Simple include 2

<fest:include src="./invalid.xml"/>
</fest:template>
13 changes: 12 additions & 1 deletion test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('dependencies parse', function() {

await result;

expect(context.addDependency).toHaveBeenCalledTimes(5);
expect(context.addDependency).toHaveBeenCalledTimes(6);
expect(context.addDependency).toHaveBeenCalledWith(
expect.stringMatching('nested-dependency.xml')
);
Expand All @@ -62,6 +62,9 @@ describe('dependencies parse', function() {
expect(context.addDependency).toHaveBeenCalledWith(
expect.stringMatching('another-include.xml')
);
expect(context.addDependency).toHaveBeenCalledWith(
expect.stringMatching('invalid.xml')
);
expect(context.addDependency).toHaveBeenCalledWith(
expect.stringMatching('script.js')
);
Expand All @@ -74,4 +77,12 @@ describe('dependencies parse', function() {
expect.stringMatching('Invalid character in entity name')
]));
});

test('should append real file path to error', async function() {
const stats = await compiler('fixtures/another-include.xml');

expect(stats.toJson().warnings).toEqual(expect.arrayContaining([
expect.stringMatching('fixtures/invalid.xml')
]));
});
});

0 comments on commit 2fa89e3

Please sign in to comment.