-
Notifications
You must be signed in to change notification settings - Fork 685
/
create-metas.js
105 lines (90 loc) · 2.87 KB
/
create-metas.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const { Severity } = require('@hint/utils-types');
const { camelCase } = require('lodash');
const { categoryId, escapeKey, mkdir, rulesIn, writeFile } = require('./utils');
/** @typedef {import('axe-core').RuleMetadata} RuleMeta */
/**
* @param {string} category
* @param {RuleMeta[]} rules
*/
const createMeta = async (category, rules) => {
const id = categoryId(category);
const camelCaseId = camelCase(id);
const rulesIds = rules.map((rule) => {
return `'${rule.ruleId}'`;
});
const properties = rules.map((rule) => {
const severities = Object.keys(Severity).filter((key) => {
return isNaN(parseInt(key));
});
const quotedSeverities = severities.map((severity) => {
return `'${severity}'`;
});
return `
${escapeKey(rule.ruleId)}: { enum: [${quotedSeverities.join(', ')}], type: 'string' }`;
});
const content = `// autogenerated by scripts/create/create-metas.js
import { Category } from '@hint/utils-types';
import { HintScope } from 'hint/dist/src/lib/enums/hint-scope';
import { HintMetadata } from 'hint/dist/src/lib/types';
import { getMessage } from '../i18n.import';
const meta: HintMetadata = {
docs: {
category: Category.accessibility,
description: getMessage('${camelCaseId}_description', 'en'),
name: getMessage('${camelCaseId}_name', 'en')
},
/* istanbul ignore next */
getDescription(language: string) {
return getMessage('${camelCaseId}_description', language);
},
/* istanbul ignore next */
getName(language: string) {
return getMessage('${camelCaseId}_name', language);
},
id: 'axe/${id}',
schema: [
{
additionalProperties: false,
properties: {${properties.join(',')}
}
},
{
items: {
enum: [${rulesIds.join(', ')}],
type: 'string'
},
typeof: 'array',
uniqueItems: true
}
],
scope: HintScope.any
};
export default meta;
`;
await writeFile(`src/meta/${id}.ts`, content);
};
/**
* Generate a `./meta/*.ts` file containing the metadata for each
* sub-hint. Then generate a `meta.ts` file referencing the metadata
* for all sub-hints.
*
* @param {string[]} categories
* @param {RuleMeta[]} rules
*/
const createMetas = async (categories, rules) => {
await mkdir('src/meta');
for (const category of categories) {
createMeta(category, rulesIn(category, rules));
}
const metas = categories.map((category) => {
const id = categoryId(category);
return ` ${escapeKey(id)}: require('./meta/${id}')`;
});
const content = `// autogenerated by scripts/create/create-metas.js
module.exports = {
${metas.join(',\n')}
};
`;
await writeFile('src/meta.ts', content);
};
module.exports = { createMetas };