Skip to content

Commit b7e8597

Browse files
committed
refactor: some improvements
1 parent 762adeb commit b7e8597

File tree

3 files changed

+50
-35
lines changed

3 files changed

+50
-35
lines changed

bin/cli.mjs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import generators from '../src/generators/index.mjs';
1212
import createMarkdownLoader from '../src/loaders/markdown.mjs';
1313
import createMarkdownParser from '../src/parsers/markdown.mjs';
1414
import createNodeReleases from '../src/releases.mjs';
15-
import { Linter } from '../src/linter/index.mjs';
15+
import createLinter from '../src/linter/index.mjs';
1616
import reporters from '../src/linter/reporters/index.mjs';
1717

1818
const availableGenerators = Object.keys(generators);
@@ -52,7 +52,9 @@ program
5252
'Set the processing target modes'
5353
).choices(availableGenerators)
5454
)
55-
.addOption(new Option('--skip-linting', 'Skip linting').default(false))
55+
.addOption(
56+
new Option('--lint-dry-run', 'Run linter in dry-run mode').default(false)
57+
)
5658
.addOption(
5759
new Option('-r, --reporter [reporter]', 'Specify the linter reporter')
5860
.choices(Object.keys(reporters))
@@ -68,9 +70,9 @@ program
6870
* @property {string} output Specifies the directory where output files will be saved.
6971
* @property {Target[]} target Specifies the generator target mode.
7072
* @property {string} version Specifies the target Node.js version.
71-
* @property {string} changelog Specifies the path to the Node.js CHANGELOG.md file
72-
* @property {boolean} skipLinting Specifies whether to skip linting
73-
* @property {keyof reporters} reporter Specifies the linter reporter
73+
* @property {string} changelog Specifies the path to the Node.js CHANGELOG.md file.
74+
* @property {boolean} lintDryRun Specifies whether the linter should run in dry-run mode.
75+
* @property {keyof reporters} reporter Specifies the linter reporter.
7476
*
7577
* @name ProgramOptions
7678
* @type {Options}
@@ -82,11 +84,11 @@ const {
8284
target = [],
8385
version,
8486
changelog,
85-
skipLinting,
87+
lintDryRun,
8688
reporter,
8789
} = program.opts();
8890

89-
const linter = skipLinting ? undefined : new Linter();
91+
const linter = createLinter(lintDryRun);
9092

9193
const { loadFiles } = createMarkdownLoader();
9294
const { parseApiDocs } = createMarkdownParser();
@@ -100,7 +102,7 @@ const { runGenerators } = createGenerator(parsedApiDocs);
100102
// Retrieves Node.js release metadata from a given Node.js version and CHANGELOG.md file
101103
const { getAllMajors } = createNodeReleases(changelog);
102104

103-
linter?.lintAll(parsedApiDocs);
105+
linter.lintAll(parsedApiDocs);
104106

105107
await runGenerators({
106108
// A list of target modes for the API docs parser
@@ -115,10 +117,8 @@ await runGenerators({
115117
releases: await getAllMajors(),
116118
});
117119

118-
if (linter) {
119-
linter.report(reporter);
120+
linter.report(reporter);
120121

121-
if (linter.hasError) {
122-
exit(1);
123-
}
122+
if (linter.hasError()) {
123+
exit(1);
124124
}

src/generators/types.d.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { SemVer } from 'semver';
22
import type availableGenerators from './index.mjs';
33
import type { ApiDocReleaseEntry } from '../types';
4-
import type { Linter } from '../linter/index.mjs';
54

65
declare global {
76
// All available generators as an inferable type, to allow Generator interfaces
@@ -31,8 +30,6 @@ declare global {
3130

3231
// A list of all Node.js major versions and their respective release information
3332
releases: Array<ApiDocReleaseEntry>;
34-
35-
linter: Linter | undefined;
3633
}
3734

3835
export interface GeneratorMetadata<I extends any, O extends any> {

src/linter/index.mjs

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,59 +7,77 @@ import { missingIntroducedIn } from './rules/missing-introduced-in.mjs';
77

88
/**
99
* Lint issues in ApiDocMetadataEntry entries
10+
*
11+
* @param {boolean} dryRun Whether to run the linter in dry-run mode
1012
*/
11-
export class Linter {
13+
const createLinter = dryRun => {
1214
/**
1315
* @type {Array<import('./types.d.ts').LintIssue>}
1416
*/
15-
#issues = [];
17+
const issues = [];
1618

1719
/**
1820
* @type {Array<import('./types.d.ts').LintRule>}
1921
*/
20-
#rules = [missingIntroducedIn, missingChangeVersion, invalidChangeVersion];
22+
const rules = [
23+
missingIntroducedIn,
24+
missingChangeVersion,
25+
invalidChangeVersion,
26+
];
2127

2228
/**
2329
* @param {ApiDocMetadataEntry} entry
2430
* @returns {void}
2531
*/
26-
lint(entry) {
27-
for (const rule of this.#rules) {
28-
const issues = rule(entry);
32+
const lint = entry => {
33+
for (const rule of rules) {
34+
const ruleIssues = rule(entry);
2935

30-
if (issues.length > 0) {
31-
this.#issues.push(...issues);
36+
if (ruleIssues.length > 0) {
37+
issues.push(...ruleIssues);
3238
}
3339
}
34-
}
40+
};
3541

3642
/**
3743
* @param {ApiDocMetadataEntry[]} entries
3844
* @returns {void}
3945
*/
40-
lintAll(entries) {
46+
const lintAll = entries => {
4147
for (const entry of entries) {
42-
this.lint(entry);
48+
lint(entry);
4349
}
44-
}
50+
};
4551

4652
/**
4753
* @param {keyof reporters} reporterName
4854
*/
49-
report(reporterName) {
55+
const report = reporterName => {
56+
if (dryRun) {
57+
return;
58+
}
59+
5060
const reporter = reporters[reporterName];
5161

52-
for (const issue of this.#issues) {
62+
for (const issue of issues) {
5363
reporter(issue);
5464
}
55-
}
65+
};
5666

5767
/**
5868
* Returns whether there are any issues with a level of 'error'
5969
*
6070
* @returns {boolean}
6171
*/
62-
get hasError() {
63-
return this.#issues.some(issue => issue.level === 'error');
64-
}
65-
}
72+
const hasError = () => {
73+
return issues.some(issue => issue.level === 'error');
74+
};
75+
76+
return {
77+
lintAll,
78+
report,
79+
hasError,
80+
};
81+
};
82+
83+
export default createLinter;

0 commit comments

Comments
 (0)