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

Commit 0e2bb0d

Browse files
ericcornelissenArcanemagus
authored andcommitted
feat: disable when no .htmlhintrc file is found (#172)
* 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.
1 parent db39e4a commit 0e2bb0d

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

lib/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ let generateRange;
1212
let tinyPromisify;
1313
let stripJSONComments;
1414

15+
// Configuration
16+
let disableWhenNoHtmlhintConfig;
17+
18+
// Internal variables
1519
const phpEmbeddedScope = 'text.html.php';
1620

21+
// Internal functions
1722
const getConfig = async (filePath) => {
1823
const readFile = tinyPromisify(fsReadFile);
1924
const configPath = await findAsync(dirname(filePath), '.htmlhintrc');
@@ -86,6 +91,9 @@ export default {
8691
// Add the current scopes
8792
Array.prototype.push.apply(this.grammarScopes, scopes);
8893
}));
94+
this.subscriptions.add(atom.config.observe('linter-htmlhint.disableWhenNoHtmlhintConfig', (value) => {
95+
disableWhenNoHtmlhintConfig = value;
96+
}));
8997
},
9098

9199
deactivate() {
@@ -123,6 +131,9 @@ export default {
123131
loadDeps();
124132

125133
const ruleset = await getConfig(filePath);
134+
if (!ruleset && disableWhenNoHtmlhintConfig) {
135+
return null;
136+
}
126137

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

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
"items": {
2626
"type": "string"
2727
}
28+
},
29+
"disableWhenNoHtmlhintConfig": {
30+
"title": "Disable when no HTMLHint config is found",
31+
"type": "boolean",
32+
"default": true
2833
}
2934
},
3035
"scripts": {

spec/linter-htmlhint-spec.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,52 @@ import { it, fit, wait, beforeEach, afterEach } from 'jasmine-fix';
77
const { lint } = require('../lib/index.js').provideLinter();
88

99
describe('The htmlhint provider for Linter', () => {
10+
const badFile = path.join(__dirname, 'fixtures', 'bad.html');
11+
const goodFile = path.join(__dirname, 'fixtures', 'good.html');
12+
1013
beforeEach(async () => {
1114
atom.workspace.destroyActivePaneItem();
1215
await atom.packages.activatePackage('linter-htmlhint');
1316
await atom.packages.activatePackage('language-html');
1417
});
1518

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

2123
expect(messages.length).toEqual(1);
2224
expect(messages[0].type).toBe('error');
2325
expect(messages[0].text).toBe('Doctype must be declared first.');
24-
expect(messages[0].filePath).toBe(bad);
26+
expect(messages[0].filePath).toBe(badFile);
2527
expect(messages[0].range).toEqual([[0, 0], [0, 5]]);
2628
});
2729

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

3334
expect(messages.length).toBe(0);
3435
});
36+
37+
describe('The "Disable when no HTMLHint config is found" option', () => {
38+
it('lints files with no config when disabled', async () => {
39+
atom.config.set('linter-htmlhint.disableWhenNoHtmlhintConfig', false);
40+
41+
const editor = await atom.workspace.open(badFile);
42+
spyOn(editor, 'getPath').andReturn(__dirname);
43+
const messages = await lint(editor);
44+
45+
expect(messages.length).toEqual(3);
46+
});
47+
48+
it("doesn't lint files with no config when enabled", async () => {
49+
atom.config.set('linter-htmlhint.disableWhenNoHtmlhintConfig', true);
50+
51+
const editor = await atom.workspace.open(badFile);
52+
spyOn(editor, 'getPath').andReturn(__dirname);
53+
const messages = await lint(editor);
54+
55+
expect(messages).toBe(null);
56+
});
57+
});
3558
});

0 commit comments

Comments
 (0)