Skip to content

Commit fb3a13f

Browse files
committed
fix: address PR feedback loop 2
1 parent b5dc657 commit fb3a13f

1 file changed

Lines changed: 38 additions & 62 deletions

File tree

src/changelog-check.ts

Lines changed: 38 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -126,46 +126,35 @@ const getDevDependencyLines = (
126126
const allLines = diffOutput.split('\n');
127127
const devDependencyLines: string[] = [];
128128

129-
const devDepSectionBoundaries: { start: number; end: number }[] = [];
129+
let devDependencySectionStart: number | undefined;
130+
let devDependencySectionEnd: number | undefined;
130131

131132
for (let i = 0; i < allLines.length; i++) {
132-
const line = allLines[i];
133-
134-
if (line?.includes('"devDependencies"') && line.includes(':')) {
135-
const startIndex = i;
136-
let endIndex = allLines.length - 1;
137-
138-
// Find the end of this section (next section or closing brace)
139-
for (let j = i + 1; j < allLines.length; j++) {
140-
const nextLine = allLines[j];
141-
if (
142-
nextLine &&
143-
(nextLine.includes('"dependencies"') ||
144-
nextLine.includes('"peerDependencies"') ||
145-
nextLine.includes('"scripts"') ||
146-
nextLine.includes('"engines"') ||
147-
nextLine.includes('"main"') ||
148-
nextLine.includes('"types"') ||
149-
nextLine.includes('"files"')) &&
150-
nextLine.includes(':')
151-
) {
152-
endIndex = j - 1;
153-
break;
154-
}
155-
}
133+
const line = allLines[i] as string;
156134

157-
devDepSectionBoundaries.push({ start: startIndex, end: endIndex });
135+
if (line.includes('"devDependencies":')) {
136+
devDependencySectionStart = i;
137+
} else if (devDependencySectionStart && /^\s*\}/u.test(line)) {
138+
devDependencySectionEnd = i;
139+
break;
158140
}
159141
}
160142

143+
if (
144+
devDependencySectionStart === undefined ||
145+
devDependencySectionEnd === undefined
146+
) {
147+
return [];
148+
}
149+
161150
// Check which nonVersionLines fall within devDependencies sections
162151
for (const changeLine of nonVersionLines) {
163152
const lineIndex = allLines.findIndex((line) => line === changeLine);
164153
if (lineIndex !== -1) {
165154
// Check if this line falls within any devDependencies section
166-
const isInDevDeps = devDepSectionBoundaries.some(
167-
(section) => lineIndex >= section.start && lineIndex <= section.end,
168-
);
155+
const isInDevDeps =
156+
lineIndex >= devDependencySectionStart &&
157+
lineIndex <= devDependencySectionEnd;
169158

170159
if (isInDevDeps) {
171160
devDependencyLines.push(changeLine);
@@ -208,7 +197,7 @@ async function analyzePackageJsonChanges(
208197
isDevDependencyOnly: boolean;
209198
isVersionAndDevDependencyOnly: boolean;
210199
isVersionDowngrade: boolean;
211-
newVersion: string | null;
200+
newVersion: string | undefined;
212201
}> {
213202
try {
214203
const { stdout } = await execa(
@@ -226,7 +215,7 @@ async function analyzePackageJsonChanges(
226215
isDevDependencyOnly: false,
227216
isVersionAndDevDependencyOnly: false,
228217
isVersionDowngrade: false,
229-
newVersion: null,
218+
newVersion: undefined,
230219
};
231220
}
232221

@@ -243,44 +232,31 @@ async function analyzePackageJsonChanges(
243232
isDevDependencyOnly: false,
244233
isVersionAndDevDependencyOnly: false,
245234
isVersionDowngrade: false,
246-
newVersion: null,
235+
newVersion: undefined,
247236
};
248237
}
249238

250-
const versionLines: string[] = [];
239+
const versionLines: { type: 'added' | 'removed'; version: string }[] = [];
251240
const nonVersionLines: string[] = [];
241+
let oldVersion: string | undefined;
242+
let newVersion: string | undefined;
252243

253244
for (const line of lines) {
254-
if (/^[+-]\s*"version":\s*"[^"]+"\s*,?\s*$/mu.test(line)) {
255-
versionLines.push(line);
245+
const match = line.match(/^([+-])\s*"version":\s*"([^"]+)"\s*,?\s*$/u);
246+
if (match) {
247+
const type = match[1] === '+' ? 'added' : 'removed';
248+
const version = match[2] as string;
249+
versionLines.push({ type, version });
256250
} else {
257251
nonVersionLines.push(line);
258252
}
259253
}
260-
261-
// Check for version changes first
262-
const versionAddedLine = versionLines.find(
263-
(line) => line.startsWith('+') && line.includes('"version":'),
264-
);
265-
const versionAddedMatch = versionAddedLine?.match(
266-
/^\+\s*"version":\s*"([^"]+)"/u,
267-
);
268-
const newVersion = versionAddedMatch?.[1] ?? null;
269-
270-
const versionRemovedLine = versionLines.find(
271-
(line) => line.startsWith('-') && line.includes('"version":'),
272-
);
273-
const versionRemovedMatch = versionRemovedLine?.match(
274-
/^-\s*"version":\s*"([^"]+)"/u,
275-
);
276-
const oldVersion = versionRemovedMatch?.[1] ?? null;
277-
278-
const hasNewVersion = newVersion !== null;
279-
280-
if (!hasNewVersion && oldVersion) {
281-
throw new Error(
282-
`Could not find new version for version change in ${filePath}`,
283-
);
254+
if (
255+
versionLines?.[0]?.type === 'removed' &&
256+
versionLines?.[1]?.type === 'added'
257+
) {
258+
oldVersion = versionLines[0].version;
259+
newVersion = versionLines[1].version;
284260
}
285261

286262
const isDowngrade =
@@ -308,9 +284,9 @@ async function analyzePackageJsonChanges(
308284
return {
309285
hasChanges: true,
310286
isVersionOnly: false,
311-
isDevDependencyOnly: !hasNewVersion && allNonVersionLinesAreDevDeps,
287+
isDevDependencyOnly: !newVersion && allNonVersionLinesAreDevDeps,
312288
isVersionAndDevDependencyOnly:
313-
hasNewVersion && allNonVersionLinesAreDevDeps,
289+
newVersion !== undefined && allNonVersionLinesAreDevDeps,
314290
isVersionDowngrade: isDowngrade,
315291
newVersion,
316292
};
@@ -326,7 +302,7 @@ async function analyzePackageJsonChanges(
326302
isDevDependencyOnly: false,
327303
isVersionAndDevDependencyOnly: false,
328304
isVersionDowngrade: false,
329-
newVersion: null,
305+
newVersion: undefined,
330306
};
331307
}
332308
}

0 commit comments

Comments
 (0)