-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathplugin.html-validate.common.js
More file actions
54 lines (48 loc) · 1.51 KB
/
plugin.html-validate.common.js
File metadata and controls
54 lines (48 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const { Rule } = require('html-validate');
/**
* @typedef { import('html-validate').DOMReadyEvent } DOMReadyEvent
*/
/**
* Lint `html[data-node-env]` attribute.
*/
class CheckNodeEnv extends Rule {
/**
* @param {object} options - plugin options
*/
constructor(options) {
super({
NODE_ENV: 'development',
...options,
});
this.domReady = this.domReady.bind(this);
}
/**
* Setup plugin events.
*/
setup() {
this.on('dom:ready', this.domReady);
}
/**
* Lint html document.
* @param {DOMReadyEvent.document} document - document object
*/
domReady({ document }) {
const currentEnv = this.options.NODE_ENV;
const documentElement = document.querySelector('html');
const documentEnv = documentElement.getAttributeValue('data-node-env');
if (documentEnv !== currentEnv) {
const reportCommand = currentEnv === 'development' ? 'npm run html-validate-prod' : 'npm run html-validate-dev';
const reportError = [
`Linter NODE_ENV=${JSON.stringify(currentEnv)}.`,
`Html document using NODE_ENV=${JSON.stringify(documentEnv)}.`,
`Please run: ${JSON.stringify(reportCommand)}.`,
].join(' ');
this.report(documentElement, reportError);
throw new Error(reportError);
}
}
}
module.exports = { CheckNodeEnv };
module.exports.rules = {
'pitcher/check-node-env': CheckNodeEnv,
};