Skip to content

Commit e98d69f

Browse files
committed
refactor: code review
1 parent 7ce79ef commit e98d69f

File tree

2 files changed

+19
-22
lines changed

2 files changed

+19
-22
lines changed

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,11 @@ changes:
6969

7070
strictEqual(context.report.mock.callCount(), 2);
7171

72-
const first_call = context.report.mock.calls[0];
72+
const callArguments = context.report.mock.calls.flatMap(
73+
call => call.arguments
74+
);
7375

74-
deepStrictEqual(first_call.arguments, [
76+
deepStrictEqual(callArguments, [
7577
{
7678
level: 'error',
7779
message: 'Missing version field in the API doc entry',
@@ -80,11 +82,6 @@ changes:
8082
end: { line: 3 },
8183
},
8284
},
83-
]);
84-
85-
const second_call = context.report.mock.calls[1];
86-
87-
deepStrictEqual(second_call.arguments, [
8885
{
8986
level: 'error',
9087
message: 'Missing version field in the API doc entry',

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,33 +46,33 @@ const isIgnoredVersion = version => {
4646
/**
4747
* Determines if a given version is invalid.
4848
*
49-
* @param {Scalar} version - The version to check.
49+
* @param {import('yaml').Scalar} version - The version to check.
5050
* @param {unknown} _ - Unused parameter.
5151
* @param {{ length: number }} context - Array containing the length property.
5252
* @returns {boolean} True if the version is invalid, otherwise false.
5353
*/
5454
const isInvalid = NODE_RELEASED_VERSIONS
55-
? (version, _, { length }) =>
55+
? ({ value }, _, { length }) =>
5656
!(
57-
isValidReplaceMe(version.value, length) ||
58-
isIgnoredVersion(version.value) ||
59-
NODE_RELEASED_VERSIONS.includes(version.value.replace(/^v/, ''))
57+
isValidReplaceMe(value, length) ||
58+
isIgnoredVersion(value) ||
59+
NODE_RELEASED_VERSIONS.includes(value.replace(/^v/, ''))
6060
)
61-
: (version, _, { length }) =>
62-
!(isValidReplaceMe(version.value, length) || valid(version.value));
61+
: ({ value }, _, { length }) =>
62+
!(isValidReplaceMe(value, length) || valid(value));
6363

6464
/**
6565
* Validates and extracts versions of a change node
6666
*
6767
* @param {object} root0
6868
* @param {import('../types.d.ts').LintContext} root0.context
6969
* @param {import('yaml').Node} root0.node
70-
* @param {(message: string, node: import('yaml').Node<unknown>) => import('../types.d.ts').IssueDescriptor} root0.report
70+
* @param {(message: string, node: import('yaml').Node<unknown>) => import('../types.d.ts').IssueDescriptor} root0.createYamlIssue
7171
*/
72-
export const extractVersions = ({ context, node, report }) => {
72+
export const extractVersions = ({ context, node, createYamlIssue }) => {
7373
if (!isMap(node)) {
7474
context.report(
75-
report(
75+
createYamlIssue(
7676
LINT_MESSAGES.invalidChangeProperty.replace('{{type}}', node.type),
7777
node
7878
)
@@ -84,7 +84,7 @@ export const extractVersions = ({ context, node, report }) => {
8484
const versionNode = findPropertyByName(node, 'version');
8585

8686
if (!versionNode) {
87-
context.report(report(LINT_MESSAGES.missingChangeVersion, node));
87+
context.report(createYamlIssue(LINT_MESSAGES.missingChangeVersion, node));
8888

8989
return [];
9090
}
@@ -107,7 +107,7 @@ export const invalidChangeVersion = context => {
107107
const lineCounter = new LineCounter();
108108
const document = parseDocument(normalizedYaml, { lineCounter });
109109

110-
const report = createYamlIssueReporter(node, lineCounter);
110+
const createYamlIssue = createYamlIssueReporter(node, lineCounter);
111111

112112
// Skip if yaml isn't a mapping
113113
if (!isMap(document.contents)) {
@@ -124,7 +124,7 @@ export const invalidChangeVersion = context => {
124124
// Validate changes node is a sequence
125125
if (!isSeq(changesNode.value)) {
126126
return context.report(
127-
report(
127+
createYamlIssue(
128128
LINT_MESSAGES.invalidChangeProperty.replace(
129129
'{{type}}',
130130
changesNode.value.type
@@ -135,12 +135,12 @@ export const invalidChangeVersion = context => {
135135
}
136136

137137
changesNode.value.items.forEach(node => {
138-
extractVersions({ context, node, report })
138+
extractVersions({ context, node, createYamlIssue })
139139
.filter(Boolean) // Filter already reported empt items,
140140
.filter(isInvalid)
141141
.forEach(version =>
142142
context.report(
143-
report(
143+
createYamlIssue(
144144
version?.value
145145
? LINT_MESSAGES.invalidChangeVersion.replace(
146146
'{{version}}',

0 commit comments

Comments
 (0)