Skip to content

Commit

Permalink
begin improving yaml syntax error message and unit test update
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuchard committed May 1, 2024
1 parent 0ada4b0 commit 50e3395
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export default {
return helpers.exec(atom.config.get('linter-ansible-syntax.ansibleExecutablePath'), args, { cwd: require('path').dirname(file), stream: 'stderr', allowEmptyStderr: true }).then(output => {
// capture the error
const matchesError = /ERROR!\s(.*)/.exec(output);
const matchesYAMLError = /Syntax Error while loading YAML\.\n\s*(.*)\n/;

// check if the output is guessing at which file has the issue; if so, capture the file name with an issue if it is elsewhere
const matchesFile = /The error appears to (?:have been|be) in '(.*)':/.exec(output);
Expand All @@ -119,13 +120,17 @@ export default {
const matchesRange = /line\s(\d+),\scolumn\s(\d+)/.exec(output);
let theRange = [[0, 0], [0, 1]];

// if this is a yaml syntax error, then replace the ansible error message with the yaml syntax error
let excerpt = matchesYAMLError == null ? matchesError[1] : matchesYAMLError[1];

// parse range
if (matchesRange != null) {
theRange = [[Number.parseInt(matchesRange[1], 10) - 1, Number.parseInt(matchesRange[2], 10) - 1], [Number.parseInt(matchesRange[1], 10) - 1, Number.parseInt(matchesRange[2], 10)]];
}

toReturn.push({
severity: 'error',
excerpt: matchesError[1],
excerpt,
location: {
file: theFile,
position: theRange,
Expand Down
2 changes: 1 addition & 1 deletion spec/linter-ansible-syntax-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describe('The Ansible Syntax Check provider for Linter', () => {
expect(messages[0].location.file).toBeDefined();
expect(messages[0].location.file).toMatch(/.+yaml_syntax\.yml$/);
expect(messages[0].location.position).toBeDefined();
expect(messages[0].location.position).toEqual([[9, 0], [9, 1]]);
expect(messages[0].location.position).toEqual([[8, 0], [8, 1]]);
});
});
});
Expand Down

0 comments on commit 50e3395

Please sign in to comment.