-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathblockESLintPlugin.ts
222 lines (203 loc) · 4.85 KB
/
blockESLintPlugin.ts
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
import { base } from "../base.js";
import { blockCSpell } from "./blockCSpell.js";
import { blockDevelopmentDocs } from "./blockDevelopmentDocs.js";
import { blockESLint } from "./blockESLint.js";
import { blockGitHubActionsCI } from "./blockGitHubActionsCI.js";
import { blockPackageJson } from "./blockPackageJson.js";
import { blockVitest } from "./blockVitest.js";
export const blockESLintPlugin = base.createBlock({
about: {
name: "ESLint Plugin",
},
produce({ options }) {
const configFileName = `.eslint-doc-generatorrc.${options.type === "commonjs" ? "mjs" : "js"}`;
return {
addons: [
blockCSpell({
words: ["eslint-doc-generatorrc"],
}),
blockDevelopmentDocs({
sections: {
Building: {
innerSections: [
{
contents: `
Run [\`eslint-doc-generator\`](https://github.com/bmish/eslint-doc-generator) to generate Markdown files documenting rules.
\`\`\`shell
pnpm build:docs
\`\`\`
`,
heading: "Building Docs",
},
],
},
Linting: {
contents: {
items: [
`- \`pnpm lint:docs\` ([eslint-doc-generator](https://github.com/bmish/eslint-doc-generator)): Generates and validates documentation for ESLint rules`,
],
},
},
},
}),
blockESLint({
extensions: ['eslintPlugin.configs["flat/recommended"]'],
ignores: [configFileName, "docs/rules/*/*.ts"],
imports: [
{
source: {
packageName: "eslint-plugin-eslint-plugin",
version: "6.4.0",
},
specifier: "eslintPlugin",
},
],
}),
blockGitHubActionsCI({
jobs: [
{
name: "Lint Docs",
steps: [
{ run: "pnpm build || exit 0" },
{ run: "pnpm lint:docs" },
],
},
],
}),
blockPackageJson({
properties: {
devDependencies: {
"eslint-doc-generator": "2.1.0",
"eslint-plugin-eslint-plugin": "6.4.0",
},
scripts: {
"build:docs": "eslint-doc-generator",
"lint:docs": "eslint-doc-generator --check",
},
},
}),
blockVitest({
coverage: {
exclude: ["src/index.ts", "src/rules/index.ts"],
},
}),
],
files: {
[configFileName]: `import prettier from "prettier";
/** @type {import('eslint-doc-generator').GenerateOptions} */
const config = {
postprocess: async (content, path) =>
prettier.format(content, {
...(await prettier.resolveConfig(path)),
parser: "markdown",
}),
ruleDocTitleFormat: "prefix-name",
};
export default config;
`,
},
};
},
setup({ options }) {
const pluginName = options.repository.replace("eslint-plugin-", "");
return {
files: {
src: {
"index.ts": `import Module from "node:module";
import { rules } from "./rules/index.js";
const require = Module.createRequire(import.meta.url);
const { name, version } =
// \`import\`ing here would bypass the TSConfig's \`"rootDir": "src"\`
require("../package.json") as typeof import("../package.json");
export const plugin = {
configs: {
get recommended() {
return recommended;
},
},
meta: { name, version },
rules,
};
const recommended = {
plugins: {
"${pluginName}": plugin,
},
rules: Object.fromEntries(
Object.keys(rules).map((rule) => [\`${pluginName}/\${rule}\`, "error"]),
),
};
export { rules };
export default plugin;
`,
rules: {
"example.test.ts": `import { rule } from "./enums.js";
import { ruleTester } from "./ruleTester.js";
ruleTester.run("enums", rule, {
invalid: [
{
code: \`enum Values {}\`,
errors: [
{
column: 1,
endColumn: 15,
endLine: 1,
line: 1,
messageId: "enum",
},
],
},
],
valid: [\`const Values = {};\`, \`const Values = {} as const;\`],
});
`,
"example.ts": `import { createRule } from "../utils.js";
export const rule = createRule({
create(context) {
return {
TSEnumDeclaration(node) {
context.report({
messageId: "enum",
node,
});
},
};
},
defaultOptions: [],
meta: {
docs: {
description: "Avoid using TypeScript's enums.",
},
messages: {
enum: "This enum will not be allowed under TypeScript's --erasableSyntaxOnly.",
},
schema: [],
type: "problem",
},
name: "enums",
});
`,
"index.ts": `import { rule as example } from "./example.js";
export const rules = {
example,
};
`,
"ruleTester.ts": `import { RuleTester } from "@typescript-eslint/rule-tester";
import * as vitest from "vitest";
RuleTester.afterAll = vitest.afterAll;
RuleTester.it = vitest.it;
RuleTester.itOnly = vitest.it.only;
RuleTester.describe = vitest.describe;
export const ruleTester = new RuleTester();
`,
},
"utils.ts": `import { ESLintUtils } from "@typescript-eslint/utils";
export const createRule = ESLintUtils.RuleCreator(
(name) =>
\`https://github.com/${options.owner}/${options.repository}/blob/main/docs/rules/\${name}.md\`,
);
`,
},
},
};
},
});