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

Commit

Permalink
feat: disable when no .htmlhintrc file is found (#172)
Browse files Browse the repository at this point in the history
* Add config option to disable linter-htmlhint when not .htmlhintrc found

The formattig is taken from the linter-eslint
(https://github.com/AtomLinter/linter-eslint) package.

* Add config watcher for no .htmlhintrc, don't lint without config

Add a configuration watcher for the new disableWhenNoHtmhintConfig
options (same as in linter-eslint). And prevent the linter from doing
anything if no ruleset is found and the disableWhenNoHtmlhintConfig is
set to `true`.

Also added top level divider comments to lib/index.js, similar to
src/main.js over at linter-eslint.

* Add unit tests for disable option

Added two unit tests for the new 'disable when no .htmlhintrc file is
found' options. One to see if a default configuration is used when no
.htmlhintrc file is present and the option IS NOT set, and one to see if
the file is not linted if the option IS set.
  • Loading branch information
ericcornelissen authored and Arcanemagus committed Aug 29, 2018
1 parent db39e4a commit 0e2bb0d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
11 changes: 11 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ let generateRange;
let tinyPromisify;
let stripJSONComments;

// Configuration
let disableWhenNoHtmlhintConfig;

// Internal variables
const phpEmbeddedScope = 'text.html.php';

// Internal functions
const getConfig = async (filePath) => {
const readFile = tinyPromisify(fsReadFile);
const configPath = await findAsync(dirname(filePath), '.htmlhintrc');
Expand Down Expand Up @@ -86,6 +91,9 @@ export default {
// Add the current scopes
Array.prototype.push.apply(this.grammarScopes, scopes);
}));
this.subscriptions.add(atom.config.observe('linter-htmlhint.disableWhenNoHtmlhintConfig', (value) => {
disableWhenNoHtmlhintConfig = value;
}));
},

deactivate() {
Expand Down Expand Up @@ -123,6 +131,9 @@ export default {
loadDeps();

const ruleset = await getConfig(filePath);
if (!ruleset && disableWhenNoHtmlhintConfig) {
return null;
}

const messages = HTMLHint.verify(text, ruleset || undefined);

Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
"items": {
"type": "string"
}
},
"disableWhenNoHtmlhintConfig": {
"title": "Disable when no HTMLHint config is found",
"type": "boolean",
"default": true
}
},
"scripts": {
Expand Down
33 changes: 28 additions & 5 deletions spec/linter-htmlhint-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,52 @@ import { it, fit, wait, beforeEach, afterEach } from 'jasmine-fix';
const { lint } = require('../lib/index.js').provideLinter();

describe('The htmlhint provider for Linter', () => {
const badFile = path.join(__dirname, 'fixtures', 'bad.html');
const goodFile = path.join(__dirname, 'fixtures', 'good.html');

beforeEach(async () => {
atom.workspace.destroyActivePaneItem();
await atom.packages.activatePackage('linter-htmlhint');
await atom.packages.activatePackage('language-html');
});

it('detects invalid coding style in bad.html and report as error', async () => {
const bad = path.join(__dirname, 'fixtures', 'bad.html');
const editor = await atom.workspace.open(bad);
const editor = await atom.workspace.open(badFile);
const messages = await lint(editor);

expect(messages.length).toEqual(1);
expect(messages[0].type).toBe('error');
expect(messages[0].text).toBe('Doctype must be declared first.');
expect(messages[0].filePath).toBe(bad);
expect(messages[0].filePath).toBe(badFile);
expect(messages[0].range).toEqual([[0, 0], [0, 5]]);
});

it('finds nothing wrong with a valid file (good.html)', async () => {
const good = path.join(__dirname, 'fixtures', 'good.html');
const editor = await atom.workspace.open(good);
const editor = await atom.workspace.open(goodFile);
const messages = await lint(editor);

expect(messages.length).toBe(0);
});

describe('The "Disable when no HTMLHint config is found" option', () => {
it('lints files with no config when disabled', async () => {
atom.config.set('linter-htmlhint.disableWhenNoHtmlhintConfig', false);

const editor = await atom.workspace.open(badFile);
spyOn(editor, 'getPath').andReturn(__dirname);
const messages = await lint(editor);

expect(messages.length).toEqual(3);
});

it("doesn't lint files with no config when enabled", async () => {
atom.config.set('linter-htmlhint.disableWhenNoHtmlhintConfig', true);

const editor = await atom.workspace.open(badFile);
spyOn(editor, 'getPath').andReturn(__dirname);
const messages = await lint(editor);

expect(messages).toBe(null);
});
});
});

0 comments on commit 0e2bb0d

Please sign in to comment.