-
Notifications
You must be signed in to change notification settings - Fork 1
/
vue-header-check.js
48 lines (41 loc) 路 1.46 KB
/
vue-header-check.js
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
const fs = require("fs");
const path = require("path");
module.exports = {
meta: {
type: "suggestion",
docs: {
description: "Enforce Vue section headers",
category: "Best Practices",
recommended: false,
},
fixable: null // This rule is not auto-fixable
},
create(context) {
return {
Program(node) {
const fileName = context.getFilename();
const fileContent = fs.readFileSync(path.resolve(fileName), "utf8");
// Determine the file extension
const fileExtension = path.extname(fileName);
if (fileExtension === ".vue") {
// Define the headers
const headers = ["TEMPLATE", "SCRIPT", "STYLE"];
// Define the Vue tags
const vueTags = ["template", "script", "style"];
// Check each tag individually
vueTags.forEach((tag, index) => {
const tagRegExp = new RegExp(`<${tag}`);
const headerRegExp = new RegExp(`\\n\\n<!--\\s*\\*{70,73}\\s*\\n\\s{5}${headers[index]}\\s*\\n\\s*\\*{70,73}\\s*-->\\s*\\n\\n<${tag}`);
// If the tag exists without the corresponding header, report an error
if (tagRegExp.test(fileContent) && !headerRegExp.test(fileContent)) {
context.report({
node,
message: `The '<${tag}>' section must be preceded by the '${headers[index]}' header comment.`,
});
}
});
}
}
};
}
};