]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tools/internal-rules/consistent-meta-messages.js
b094c86e8844f48d963af571e68721be57dd7f50
[pve-eslint.git] / eslint / tools / internal-rules / consistent-meta-messages.js
1 /**
2 * @fileoverview A rule to enforce using `meta.messages` property in core rules
3 * @author 薛定谔的猫<hh_2013@foxmail.com>
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Helpers
10 //------------------------------------------------------------------------------
11
12 /**
13 * Gets the property of the Object node passed in that has the name specified.
14 * @param {string} property Name of the property to return.
15 * @param {ASTNode} node The ObjectExpression node.
16 * @returns {ASTNode} The Property node or null if not found.
17 */
18 function getPropertyFromObject(property, node) {
19 const properties = node.properties;
20
21 for (let i = 0; i < properties.length; i++) {
22 if (properties[i].key.name === property) {
23 return properties[i];
24 }
25 }
26
27 return null;
28 }
29
30 /**
31 * Verifies that the meta.messages property is present.
32 * TODO: check it has the correct value
33 * @param {RuleContext} context The ESLint rule context.
34 * @param {ASTNode} exportsNode ObjectExpression node that the rule exports.
35 * @returns {void}
36 */
37 function checkMetaMessages(context, exportsNode) {
38 if (exportsNode.type !== "ObjectExpression") {
39
40 // if the exported node is not the correct format, "internal-no-invalid-meta" will already report this.
41 return;
42 }
43
44 const metaProperty = getPropertyFromObject("meta", exportsNode);
45 const messages = metaProperty && getPropertyFromObject("messages", metaProperty.value);
46
47 if (!messages) {
48 context.report({
49 node: metaProperty,
50 messageId: "expectedMessages"
51 });
52 }
53 }
54
55 //------------------------------------------------------------------------------
56 // Rule Definition
57 //------------------------------------------------------------------------------
58
59 module.exports = {
60 meta: {
61 docs: {
62 description: "enforce using `meta.messages` property in core rules",
63 category: "Internal",
64 recommended: false
65 },
66 schema: [],
67 type: "suggestion",
68 messages: {
69 expectedMessages: "Expected `meta.messages` property."
70 }
71 },
72
73 create(context) {
74 return {
75 "AssignmentExpression[left.object.name='module'][left.property.name='exports']"(node) {
76 checkMetaMessages(context, node.right);
77 }
78 };
79 }
80 };