Skip to content

Commit a14c391

Browse files
committed
refactor: some improvements
1 parent a288a15 commit a14c391

File tree

9 files changed

+25
-33
lines changed

9 files changed

+25
-33
lines changed

bin/commands/generate.mjs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { publicGenerators } from '../../src/generators/index.mjs';
1212
import createNodeReleases from '../../src/releases.mjs';
1313
import { loadAndParse } from '../utils.mjs';
1414
import createLinter from '../../src/linter/index.mjs';
15-
import rules from '../../src/linter/rules/index.mjs';
15+
import { getEnabledRules } from '../../src/linter/utils/rules.mjs';
1616

1717
const availableGenerators = Object.keys(publicGenerators);
1818

@@ -123,13 +123,14 @@ export default {
123123
* @returns {Promise<void>}
124124
*/
125125
async action(opts) {
126+
const rules = getEnabledRules(opts.disableRule);
126127
const linter = opts.skipLint ? undefined : createLinter(rules);
127128

128129
const docs = await loadAndParse(opts.input, opts.ignore, linter);
129130

130-
linter.report();
131+
linter?.report();
131132

132-
if (linter.hasError()) {
133+
if (linter?.hasError()) {
133134
console.error('Lint failed; aborting generation.');
134135
process.exit(1);
135136
}

bin/commands/lint.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export default {
8787

8888
linter.report();
8989

90-
process.exitCode = linter.hasError() ? 1 : 0;
90+
process.exitCode = +linter.hasError();
9191
} catch (error) {
9292
console.error('Error running the linter:', error);
9393
process.exitCode = 1;

src/generators/legacy-json/utils/buildSection.mjs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,7 @@ import { getRemarkRehype } from '../../../utils/remark.mjs';
33
import { transformNodesToString } from '../../../utils/unist.mjs';
44
import { parseList } from './parseList.mjs';
55
import { SECTION_TYPE_PLURALS, UNPROMOTED_KEYS } from '../constants.mjs';
6-
7-
/**
8-
* Converts a value to an array.
9-
* @template T
10-
* @param {T | T[]} val - The value to convert.
11-
* @returns {T[]} The value as an array.
12-
*/
13-
const enforceArray = val => (Array.isArray(val) ? val : [val]);
6+
import { enforceArray } from '../../../utils/array.mjs';
147

158
/**
169
*

src/linter/constants.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
'use strict';
22

3+
export const INTRDOCUED_IN_REGEX = /<!--\s?introduced_in=.*-->/;
4+
5+
export const LLM_DESCRIPTION_REGEX = /<!--\s?llm_description=.*-->/;
6+
37
export const LINT_MESSAGES = {
48
missingIntroducedIn: "Missing 'introduced_in' field in the API doc entry",
59
missingChangeVersion: 'Missing version field in the API doc entry',

src/linter/context.mjs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,9 @@ const createContext = (file, tree) => {
2727
*/
2828
const location = {
2929
path: file.path,
30+
position,
3031
};
3132

32-
if (position) {
33-
location.position = position;
34-
}
35-
3633
issues.push({
3734
level,
3835
message,

src/linter/rules/invalid-change-version.mjs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
normalizeYamlSyntax,
1010
} from '../../utils/parser/index.mjs';
1111
import { LINT_MESSAGES } from '../constants.mjs';
12+
import { enforceArray } from '../../utils/array.mjs';
1213

1314
const NODE_RELEASED_VERSIONS = env.NODE_RELEASED_VERSIONS?.split(',');
1415

@@ -55,13 +56,6 @@ const isInvalid = NODE_RELEASED_VERSIONS
5556
: (version, _, { length }) =>
5657
!(isValidReplaceMe(version, length) || valid(version));
5758

58-
/**
59-
*
60-
* @param version
61-
*/
62-
const normalizeVersion = version =>
63-
Array.isArray(version) ? version : [version];
64-
6559
/**
6660
* Identifies invalid change versions from metadata entries.
6761
*
@@ -85,7 +79,7 @@ export const invalidChangeVersion = context => {
8579
}
8680

8781
changes.forEach(({ version }) =>
88-
normalizeVersion(version)
82+
enforceArray(version)
8983
.filter(isInvalid)
9084
.forEach(version =>
9185
context.report({

src/linter/rules/missing-introduced-in.mjs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
'use strict';
22

3-
import { LINT_MESSAGES } from '../constants.mjs';
3+
import { INTRDOCUED_IN_REGEX, LINT_MESSAGES } from '../constants.mjs';
44
import { findTopLevelEntry } from '../utils/find.mjs';
55

6-
const regex = /<!--\s?introduced_in=.*-->/;
7-
86
/**
97
* Checks if `introduced_in` field is missing in the top-level entry.
108
*
@@ -14,7 +12,7 @@ const regex = /<!--\s?introduced_in=.*-->/;
1412
export const missingIntroducedIn = context => {
1513
const introducedIn = findTopLevelEntry(
1614
context.tree,
17-
node => node.type === 'html' && regex.test(node.value)
15+
node => node.type === 'html' && INTRDOCUED_IN_REGEX.test(node.value)
1816
);
1917

2018
if (introducedIn) {

src/linter/rules/missing-llm-description.mjs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { LINT_MESSAGES } from '../constants.mjs';
1+
import { LINT_MESSAGES, LLM_DESCRIPTION_REGEX } from '../constants.mjs';
22
import { findTopLevelEntry } from '../utils/find.mjs';
33

4-
const regex = /<!--llm_description=.*-->/;
5-
64
/**
75
* Checks if a top-level entry is missing a llm_description field or a paragraph
86
* node.
@@ -13,7 +11,7 @@ const regex = /<!--llm_description=.*-->/;
1311
export const missingLlmDescription = context => {
1412
const llmDescription = findTopLevelEntry(
1513
context.tree,
16-
node => node.type === 'html' && regex.test(node.value)
14+
node => node.type === 'html' && LLM_DESCRIPTION_REGEX.test(node.value)
1715
);
1816

1917
if (llmDescription) {
@@ -28,7 +26,7 @@ export const missingLlmDescription = context => {
2826
);
2927

3028
if (paragraph) {
31-
return false;
29+
return;
3230
}
3331

3432
context.report({

src/utils/array.mjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* Converts a value to an array.
3+
* @template T
4+
* @param {T | T[]} val - The value to convert.
5+
* @returns {T[]} The value as an array.
6+
*/
7+
export const enforceArray = val => (Array.isArray(val) ? val : [val]);

0 commit comments

Comments
 (0)