-
-
Notifications
You must be signed in to change notification settings - Fork 153
/
generateRule.js
228 lines (193 loc) · 5.47 KB
/
generateRule.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* eslint-disable no-console -- CLI */
/**
* @example
*
* ```shell
* npm run create-rule my-new-rule --recommended
* ```
*/
import {
existsSync,
} from 'fs';
import fs from 'fs/promises';
import {
resolve,
} from 'path';
import _ from 'lodash';
import open from 'open-editor';
// Todo: Would ideally have prompts, e.g., to ask for whether type was problem/layout, etc.
const [, , ruleName, ...options] = process.argv;
const recommended = options.includes('--recommended');
(async () => {
if (!ruleName) {
console.error('Please supply a rule name');
return;
}
if ((/[A-Z]/u).test(ruleName)) {
console.error('Please ensure the rule has no capital letters');
return;
}
const ruleNamesPath = './test/rules/ruleNames.json';
const ruleNames = JSON.parse(await fs.readFile(
ruleNamesPath, 'utf8',
));
if (!ruleNames.includes(ruleName)) {
ruleNames.push(ruleName);
ruleNames.sort();
}
await fs.writeFile(ruleNamesPath, JSON.stringify(ruleNames, null, 2) + '\n');
console.log('ruleNames', ruleNames);
const ruleTemplate = `import iterateJsdoc from '../iterateJsdoc';
export default iterateJsdoc(({
context,
utils,
}) => {
// Rule here
}, {
iterateAllJsdocs: true,
meta: {
docs: {
description: '',
url: 'https://github.com/gajus/eslint-plugin-jsdoc#eslint-plugin-jsdoc-rules-${ruleName}',
},
schema: [
{
additionalProperies: false,
properties: {
// Option properties here (or remove the object)
},
type: 'object',
},
],
type: 'suggestion',
},
});
`;
const camelCasedRuleName = _.camelCase(ruleName);
const rulePath = `./src/rules/${camelCasedRuleName}.js`;
if (!existsSync(rulePath)) {
await fs.writeFile(rulePath, ruleTemplate);
}
const ruleTestTemplate = `export default {
invalid: [
{
code: \`
\`,
errors: [{
line: '',
message: '',
}],
},
],
valid: [
{
code: \`
\`,
},
],
};
`;
const ruleTestPath = `./test/rules/assertions/${camelCasedRuleName}.js`;
if (!existsSync(ruleTestPath)) {
await fs.writeFile(ruleTestPath, ruleTestTemplate);
}
const ruleReadmeTemplate = `### \`${ruleName}\`
|||
|---|---|
|Context|everywhere|
|Tags|\`\`|
|Recommended|${recommended ? 'true' : 'false'}|
|Settings||
|Options||
<!-- assertions ${camelCasedRuleName} -->
`;
const ruleReadmePath = `./.README/rules/${ruleName}.md`;
if (!existsSync(ruleReadmePath)) {
await fs.writeFile(ruleReadmePath, ruleReadmeTemplate);
}
const replaceInOrder = async ({path, oldRegex, checkName, newLine, oldIsCamel}) => {
const offsets = [];
let readme = await fs.readFile(path, 'utf8');
readme.replace(
oldRegex,
(matchedLine, n1, offset, str, {oldRule}) => {
offsets.push({
matchedLine,
offset,
oldRule,
});
},
);
offsets.sort(({oldRule}, {oldRule: oldRuleB}) => {
// eslint-disable-next-line no-extra-parens
return oldRule < oldRuleB ? -1 : (oldRule > oldRuleB ? 1 : 0);
});
let alreadyIncluded = false;
const itemIndex = offsets.findIndex(({oldRule}) => {
alreadyIncluded ||= oldIsCamel ? camelCasedRuleName === oldRule : ruleName === oldRule;
return oldIsCamel ? camelCasedRuleName < oldRule : ruleName < oldRule;
});
let item = itemIndex !== undefined && offsets[itemIndex];
if (item && itemIndex === 0 &&
// This property would not always be sufficient but in this case it is.
oldIsCamel
) {
item.offset = 0;
}
if (!item) {
item = offsets.pop();
item.offset += item.matchedLine.length;
}
if (alreadyIncluded) {
console.log(`Rule name is already present in ${checkName}.`);
} else {
readme = readme.slice(0, item.offset) +
(item.offset ? '\n' : '') +
newLine +
(item.offset ? '' : '\n') +
readme.slice(item.offset);
await fs.writeFile(path, readme);
}
};
await replaceInOrder({
checkName: 'README',
newLine: `{"gitdown": "include", "file": "./rules/${ruleName}.md"}`,
oldRegex: /\n\{"gitdown": "include", "file": ".\/rules\/(?<oldRule>[^.]*).md"\}/gu,
path: './.README/README.md',
});
await replaceInOrder({
checkName: 'index import',
newLine: `import ${camelCasedRuleName} from './rules/${camelCasedRuleName}';`,
oldIsCamel: true,
oldRegex: /\nimport (?<oldRule>[^ ]*) from '.\/rules\/\1';/gu,
path: './src/index.js',
});
await replaceInOrder({
checkName: 'index recommended',
newLine: `${' '.repeat(8)}'jsdoc/${ruleName}': '${recommended ? 'warn' : 'off'}',`,
oldRegex: /\n\s{8}'jsdoc\/(?<oldRule>[^']*)': '[^']*',/gu,
path: './src/index.js',
});
await replaceInOrder({
checkName: 'index rules',
newLine: `${' '.repeat(4)}'${ruleName}': ${camelCasedRuleName},`,
oldRegex: /\n\s{4}'(?<oldRule>[^']*)': [^,]*,/gu,
path: './src/index.js',
});
// eslint-disable-next-line import/extensions
await import('./generateReadme.js');
/*
console.log('Paths to open for further editing\n');
console.log(`open ${ruleReadmePath}`);
console.log(`open ${rulePath}`);
console.log(`open ${ruleTestPath}\n`);
*/
// Set chdir as somehow still in operation from other test
process.chdir(resolve(__dirname, '../../'));
await open([
// Could even add editor line column numbers like `${rulePath}:1:1`
ruleReadmePath,
ruleTestPath,
rulePath,
]);
})();