Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(manager/haskell-cabal): Handle comments in Cabal file #33695

Merged
merged 2 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/modules/manager/haskell-cabal/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@ import {
countPackageNameLength,
countPrecedingIndentation,
extractNamesAndRanges,
findDepends,
findExtents,
splitSingleDependency,
} from './extract';

const commentCabalFile = `build-depends:
-- leading
base,
-- middle
other,
-- trailing
other2`;

describe('modules/manager/haskell-cabal/extract', () => {
describe('countPackageNameLength', () => {
it.each`
Expand Down Expand Up @@ -91,4 +100,14 @@ describe('modules/manager/haskell-cabal/extract', () => {
]);
});
});

describe('findDepends()', () => {
it('strips comments', () => {
const res = findDepends(commentCabalFile + '\na: b');
expect(res).toEqual({
buildDependsContent: '\n base,\n other,\n other2',
lengthProcessed: commentCabalFile.length,
});
});
});
});
24 changes: 20 additions & 4 deletions lib/modules/manager/haskell-cabal/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { regEx } from '../../../util/regex';
const buildDependsRegex = regEx(
/(?<buildDependsFieldName>build-depends[ \t]*:)/i,
);
const commentRegex = regEx(/^[ \t]*--/);
function isNonASCII(str: string): boolean {
for (let i = 0; i < str.length; i++) {
if (str.charCodeAt(i) > 127) {
Expand Down Expand Up @@ -86,6 +87,11 @@ export function findExtents(indent: number, content: string): number {
break;
}
if (thisIndent < indent) {
if (content.slice(blockIdx - 1, blockIdx + 1) === '--') {
// not enough indention, but the line is a comment, so include it
mode = 'finding-newline';
continue;
}
// go back to before the newline
for (;;) {
if (content[blockIdx--] === '\n') {
Expand Down Expand Up @@ -125,7 +131,8 @@ export function countPrecedingIndentation(
*
* @returns {{buildDependsContent: string, lengthProcessed: number}}
* buildDependsContent:
* the contents of the field, excluding the field name and the colon.
* the contents of the field, excluding the field name and the colon,
* and any comments within
*
* lengthProcessed:
* points to after the end of the field. Note that the field does _not_
Expand All @@ -143,10 +150,19 @@ export function findDepends(
const indent = countPrecedingIndentation(content, matchObj.index);
const ourIdx: number =
matchObj.index + matchObj.groups['buildDependsFieldName'].length;
const extent: number = findExtents(indent + 1, content.slice(ourIdx));
const extentLength: number = findExtents(indent + 1, content.slice(ourIdx));
const extent = content.slice(ourIdx, ourIdx + extentLength);
const lines = [];
// Windows-style line breaks are fine because
// carriage returns are before the line feed.
for (const maybeCommentLine of extent.split('\n')) {
if (!commentRegex.test(maybeCommentLine)) {
lines.push(maybeCommentLine);
}
}
return {
buildDependsContent: content.slice(ourIdx, ourIdx + extent),
lengthProcessed: ourIdx + extent,
buildDependsContent: lines.join('\n'),
lengthProcessed: ourIdx + extentLength,
};
}

Expand Down
Loading