Skip to content

Commit 0e0e288

Browse files
committed
refactor: fix generateRule.js
Also: - refactor: TypeScript for `checkJs`
1 parent 815eb00 commit 0e0e288

File tree

11 files changed

+179
-44
lines changed

11 files changed

+179
-44
lines changed

.eslintrc.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
"unicorn/no-unsafe-regex": 0,
3838
"unicorn/prefer-array-some": 0,
3939
"unicorn/prevent-abbreviations": 0,
40-
"linebreak-style": 0
40+
"linebreak-style": 0,
41+
"no-inline-comments": 0,
42+
"no-extra-parens": 0
4143
}
4244
}

.husky/pre-push

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/usr/bin/env sh
22
. "$(dirname -- "$0")/_/husky.sh"
33

4-
npm test && npm run build && npm run check-readme
4+
npm test && npm run build && npm run check-docs

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ modify files within the `.README` directory. `.README/README.md` contains the
4242
main README skeleton and details on the project, its global `settings`, etc.,
4343
while the documentation for specific rules (that will be pulled into the
4444
README) ought to be modified within the relevant file within `.README/rules`.
45-
Once these files are modified, you can run `pnpm create-readme` to have
45+
Once these files are modified, you can run `pnpm create-docs` to have
4646
these files integrated into the main `/README.md`. While you should include
4747
the built file in your PR, you will not want to make manual changes
4848
directly to this file, as they will be overwritten.

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,17 @@
2929
"@semantic-release/commit-analyzer": "^9.0.2",
3030
"@semantic-release/github": "^8.0.7",
3131
"@semantic-release/npm": "^10.0.3",
32+
"@types/eslint": "^8.37.0",
33+
"@types/node": "^20.1.1",
3234
"@typescript-eslint/parser": "^5.59.2",
3335
"babel-plugin-add-module-exports": "^1.0.4",
3436
"babel-plugin-istanbul": "^6.1.1",
3537
"camelcase": "^6.3.0",
3638
"chai": "^4.3.7",
3739
"cross-env": "^7.0.3",
3840
"decamelize": "^5.0.1",
39-
"eslint-config-canonical": "~33.0.1",
4041
"eslint": "^8.39.0",
42+
"eslint-config-canonical": "~33.0.1",
4143
"gitdown": "^3.1.5",
4244
"glob": "^10.2.2",
4345
"husky": "^8.0.3",
@@ -107,9 +109,10 @@
107109
"package-lock.json": "npm run install-offline"
108110
},
109111
"scripts": {
112+
"tsc": "tsc",
110113
"build": "rimraf ./dist && cross-env NODE_ENV=production babel ./src --out-dir ./dist --copy-files --source-maps --ignore ./src/bin/*.js --no-copy-ignored",
111-
"check-readme": "babel-node ./src/bin/generateDocs.js --check",
112-
"create-readme": "babel-node ./src/bin/generateDocs.js",
114+
"check-docs": "babel-node ./src/bin/generateDocs.js --check",
115+
"create-docs": "babel-node ./src/bin/generateDocs.js",
113116
"create-rule": "babel-node ./src/bin/generateRule.js",
114117
"install-offline": "pnpm install --prefer-offline --no-audit",
115118
"lint": "npm run lint-arg -- .",

pnpm-lock.yaml

Lines changed: 34 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/WarnSettings.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,28 @@ const WarnSettings = function () {
66
/**
77
* Warn only once for each context and setting
88
*
9-
* @param {object} context
9+
* @param {import('eslint').Rule.RuleContext} context
1010
* @param {string} setting
11+
* @returns {boolean}
1112
*/
1213
hasBeenWarned (context, setting) {
13-
return warnedSettings.has(context) && warnedSettings.get(context).has(setting);
14+
return warnedSettings.has(context) && /** @type {Set<string>} */ (
15+
warnedSettings.get(context)
16+
).has(setting);
1417
},
1518

19+
/**
20+
* @param {import('eslint').Rule.RuleContext} context
21+
* @param {string} setting
22+
* @returns {void}
23+
*/
1624
markSettingAsWarned (context, setting) {
1725
// istanbul ignore else
1826
if (!warnedSettings.has(context)) {
1927
warnedSettings.set(context, new Set());
2028
}
2129

22-
warnedSettings.get(context).add(setting);
30+
/** @type {Set<string>} */ (warnedSettings.get(context)).add(setting);
2331
},
2432
};
2533
};

src/bin/generateDocs.js

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import {
99
glob,
1010
} from 'glob';
1111

12+
/**
13+
* @param {string} code
14+
* @returns {string}
15+
*/
1216
const trimCode = (code) => {
1317
let lines = code.replace(/^\n/u, '').trimEnd().split('\n');
1418

@@ -29,6 +33,11 @@ const trimCode = (code) => {
2933
return lines.join('\n').replaceAll('\r', '\\r');
3034
};
3135

36+
/**
37+
* @param {import('eslint').RuleTester.InvalidTestCase|import('eslint').RuleTester.ValidTestCase} setup
38+
* @param {string} ruleName
39+
* @returns {string}
40+
*/
3241
const formatCodeSnippet = (setup, ruleName) => {
3342
const paragraphs = [];
3443

@@ -42,8 +51,11 @@ const formatCodeSnippet = (setup, ruleName) => {
4251
paragraphs.push(`// "jsdoc/${ruleName}": ["error"|"warn", ${JSON.stringify(setup.options).slice(1)}`);
4352
}
4453

45-
if (setup.errors) {
46-
paragraphs.push(`// Message: ${setup.errors[0].message}`);
54+
if ('errors' in setup) {
55+
paragraphs.push(`// Message: ${
56+
/** @type {Array<import('eslint').RuleTester.TestCaseError>} */ (
57+
setup.errors
58+
)[0].message}`);
4759
}
4860

4961
return paragraphs.join('\n');
@@ -58,9 +70,14 @@ const getAssertions = async () => {
5870
return path.basename(filePath, '.js');
5971
});
6072

61-
const assertionCodes = assertionFiles.map((filePath, idx) => {
62-
// eslint-disable-next-line import/no-dynamic-require
63-
const codes = require(filePath);
73+
const assertionCodes = await Promise.all(assertionFiles.map(async (filePath, idx) => {
74+
/**
75+
* @type {{
76+
* invalid: (import('eslint').RuleTester.InvalidTestCase & {ignoreReadme?: true})[],
77+
* valid: (import('eslint').RuleTester.ValidTestCase & {ignoreReadme?: true})[]
78+
* }}
79+
*/
80+
const codes = await import(filePath);
6481

6582
const ruleName = decamelize(assertionNames[idx], {
6683
separator: '-',
@@ -82,7 +99,7 @@ const getAssertions = async () => {
8299
return formatCodeSnippet(setup, ruleName);
83100
}),
84101
};
85-
});
102+
}));
86103

87104
return {
88105
assertionNames,
@@ -145,7 +162,13 @@ const generateDocs = async () => {
145162
return docContents.map((docContent) => {
146163
return docContent.replace(
147164
/<!-- assertions-(passing|failing) ([a-z]+?) -->/gui,
148-
(assertionsBlock, passingFailing, ruleName) => {
165+
/**
166+
* @param {string} _assertionsBlock
167+
* @param {string} passingFailing
168+
* @param {string} ruleName
169+
* @returns {string}
170+
*/
171+
(_assertionsBlock, passingFailing, ruleName) => {
149172
const ruleAssertions = assertions[ruleName];
150173

151174
if (!ruleAssertions) {
@@ -164,10 +187,13 @@ const generateDocs = async () => {
164187
});
165188
};
166189

190+
/**
191+
* @returns {string[]}
192+
*/
167193
const getDocPaths = () => {
168194
const basePath = path.join(__dirname, '..', '..', '.README');
169195
const writeBasePath = path.join(__dirname, '..', '..', 'docs');
170-
const docPaths = fs.readdirSync(basePath).flatMap((docFile) => {
196+
const docPaths = /** @type {string[]} */ (fs.readdirSync(basePath).flatMap((docFile) => {
171197
if (extraFiles.includes(docFile)) {
172198
// Will get path separately below
173199
return null;
@@ -190,7 +216,7 @@ const getDocPaths = () => {
190216
return null;
191217
}).filter((file) => {
192218
return file;
193-
});
219+
}));
194220

195221
return [
196222
...docPaths,
@@ -232,7 +258,7 @@ const assertDocsAreUpToDate = async () => {
232258
const isUpToDate = fs.readFileSync(docPath, 'utf8') === docContent;
233259

234260
if (!isUpToDate) {
235-
throw new Error('Readme is not up to date, please run `npm run create-readme` to update it.');
261+
throw new Error('Readme is not up to date, please run `npm run create-docs` to update it.');
236262
}
237263
}
238264
};

0 commit comments

Comments
 (0)