Skip to content
This repository has been archived by the owner on Aug 7, 2023. It is now read-only.

Support v2 Linter API #149

Merged
merged 8 commits into from
Feb 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 45 additions & 11 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ import { dirname } from 'path';
let executablePath;
let globalHamlLintYmlFile;

const warning = 'warning';
const regex = /.+?:(\d+) \[(W|E)] (\w+): (.+)/g;
const urlBase = 'https://github.com/brigade/haml-lint/blob/master/lib/haml_lint/linter/README.md';

const showErrorNotification = (text = 'HAML-Lint: Unexpected error', description) => {
atom.notifications.addError(text, { description });
};

export default {
activate() {
require('atom-package-deps').install('linter-haml');
Expand All @@ -38,14 +43,16 @@ export default {
name: 'HAML-Lint',
grammarScopes: ['text.haml'],
scope: 'file',
lintOnFly: false,
lintsOnChange: false,
lint: async (textEditor) => {
const filePath = textEditor.getPath();
const text = textEditor.getText();

const options = {
cwd: dirname(filePath),
stream: 'both',
ignoreExitCode: true,
uniqueKey: `linter-haml::${filePath}`,
};
const parameters = [];

Expand All @@ -60,7 +67,32 @@ export default {
// Add the file to be linted
parameters.push(filePath);

const output = await helpers.exec(executablePath, parameters, options);
let output;
try {
output = await helpers.exec(executablePath, parameters, options);
} catch (e) {
showErrorNotification(e.message);
return null;
}

const messages = [];

if (output.exitCode !== 0) {
Arcanemagus marked this conversation as resolved.
Show resolved Hide resolved
if (output.stderr.toLowerCase().startsWith(warning)) {
messages.push({
severity: warning,
excerpt: `haml-lint: ${output.stderr}`,
location: {
file: filePath,
// first line of the file
position: [[0, 0], [0, Infinity]],
},
});
} else {
showErrorNotification(output.stderr);
Arcanemagus marked this conversation as resolved.
Show resolved Hide resolved
return null;
}
}

if (textEditor.getText() !== text) {
// eslint-disable-next-line no-console
Expand All @@ -71,21 +103,23 @@ export default {
return null;
}

const messages = [];
let match = regex.exec(output);
let match = regex.exec(output.stdout);
vzamanillo marked this conversation as resolved.
Show resolved Hide resolved
while (match !== null) {
const type = match[2] === 'W' ? 'Warning' : 'Error';
const severity = match[2] === 'W' ? warning : 'error';
const line = Number.parseInt(match[1], 10) - 1;
const ruleName = escapeHtml(match[3]);
const message = escapeHtml(match[4]);
const excerpt = match[4];
messages.push({
type,
filePath,
range: helpers.generateRange(textEditor, line),
html: `<a href="${urlBase}#${ruleName.toLowerCase()}">${ruleName}</a>: ${message}`,
url: `${urlBase}#${ruleName.toLowerCase()}`,
severity,
excerpt: `${ruleName}: ${excerpt}`,
location: {
file: filePath,
position: helpers.generateRange(textEditor, line),
},
});

match = regex.exec(output);
match = regex.exec(output.stdout);
}
return messages;
},
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"configSchema": {
"executablePath": {
"default": "haml-lint",
"description": "Path to haml-lint executable",
"description": "This is the absolute path to your `haml-lint` command. You may need to run `which haml-lint` or `rbenv which haml-lint` to find this.",
"type": "string"
},
"globalHamlLintYmlFile": {
Expand All @@ -27,13 +27,13 @@
"escape-html": "^1.0.3"
},
"package-deps": [
"linter",
"linter:2.0.0",
"language-haml"
],
"providedServices": {
"linter": {
"versions": {
"1.0.0": "provideLinter"
"2.0.0": "provideLinter"
}
}
},
Expand Down
40 changes: 29 additions & 11 deletions spec/linter-haml-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,35 @@ describe('The haml-lint provider for Linter', () => {
it('checks a file with issues', async () => {
const editor = await atom.workspace.open(cawsvpath);
const messages = await lint(editor);
const messageText = '<a href="' +
'https://github.com/brigade/haml-lint/blob/master/lib/haml_lint/linter/README.md' +
'#classattributewithstaticvalue">ClassAttributeWithStaticValue</a>: ' +
'Avoid defining `class` in attributes hash for static class names';

expect(messages.length).toBe(1);
expect(messages[0].type).toBe('Warning');
expect(messages[0].text).not.toBeDefined();
expect(messages[0].html).toBe(messageText);
expect(messages[0].filePath).toBe(cawsvpath);
expect(messages[0].range).toEqual([[0, 0], [0, 23]]);
const url = 'https://github.com/brigade/haml-lint/blob/master/lib/haml_lint/linter/README.md' +
'#classattributewithstaticvalue';
const excerpt = 'ClassAttributeWithStaticValue: Avoid defining `class` in attributes hash for static class names';

const normalWarningExpects = (message) => {
expect(message.severity).toBe('warning');
expect(message.excerpt).toBe(excerpt);
expect(message.description).not.toBeDefined();
expect(message.url).toBe(url);
expect(message.location.file).toBe(cawsvpath);
expect(message.location.position).toEqual([[0, 0], [0, 23]]);
};

// no old compilant syntax ruby version
expect(messages.length).not.toBeLessThan(1);
// old compilant syntax ruby version (parser warning + ClassAttributeWithStaticValue warning)
expect(messages.length).not.toBeGreaterThan(2);

if (messages.length > 1 && messages[0].excerpt.startsWith('haml-lint: warning')) {
expect(messages[0].severity).toBe('warning');
expect(messages[0].excerpt).toContain('haml-lint: warning');
expect(messages[0].description).not.toBeDefined();
expect(messages[0].url).not.toBeDefined();
expect(messages[0].location.file).toBe(cawsvpath);
expect(messages[0].location.position).toEqual([[0, 0], [0, Infinity]]);
normalWarningExpects(messages[1]);
} else {
normalWarningExpects(messages[0]);
}
});

it('finds nothing wrong with a valid file', async () => {
Expand Down