Skip to content

Commit

Permalink
fix: replace numbered capture group replaces with named capture groups (
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed Oct 25, 2021
1 parent 8571b3e commit 651d5de
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 19 deletions.
2 changes: 1 addition & 1 deletion lib/manager/gomod/update.spec.ts
Expand Up @@ -185,7 +185,7 @@ describe('manager/gomod/update', () => {
};
const res = updateDependency({ fileContent: gomod2, upgrade });
expect(res).not.toEqual(gomod2);
expect(res).not.toContain(upgrade.newDigest);
expect(res).toContain('github.com/spf13/jwalterweatherman 123456123456');
expect(res).toContain(upgrade.newDigest.substring(0, 12));
});
it('skips already-updated multiline digest', () => {
Expand Down
15 changes: 10 additions & 5 deletions lib/manager/gomod/update.ts
Expand Up @@ -33,13 +33,15 @@ export function updateDependency({
let updateLineExp: RegExp;
if (depType === 'replace') {
updateLineExp = regEx(
/^(replace\s+[^\s]+[\s]+[=][>]+\s+)([^\s]+\s+)([^\s]+)/
/^(?<depPart>replace\s+[^\s]+[\s]+[=][>]+\s+)(?<divider>[^\s]+\s+)[^\s]+/
);
} else if (depType === 'require') {
if (upgrade.managerData.multiLine) {
updateLineExp = regEx(/^(\s+[^\s]+)(\s+)([^\s]+)/);
updateLineExp = regEx(/^(?<depPart>\s+[^\s]+)(?<divider>\s+)[^\s]+/);
} else {
updateLineExp = regEx(/^(require\s+[^\s]+)(\s+)([^\s]+)/);
updateLineExp = regEx(
/^(?<depPart>require\s+[^\s]+)(?<divider>\s+)[^\s]+/
);
}
}
if (updateLineExp && !updateLineExp.test(lineToChange)) {
Expand All @@ -61,10 +63,13 @@ export function updateDependency({
);
newLine = lineToChange.replace(
updateLineExp,
`$1$2${newDigestRightSized}`
`$<depPart>$<divider>${newDigestRightSized}`
);
} else {
newLine = lineToChange.replace(updateLineExp, `$1$2${upgrade.newValue}`);
newLine = lineToChange.replace(
updateLineExp,
`$<depPart>$<divider>${upgrade.newValue}`
);
}
if (upgrade.updateType === 'major') {
logger.debug({ depName }, 'gomod: major update');
Expand Down
4 changes: 2 additions & 2 deletions lib/manager/helmv3/update.ts
Expand Up @@ -20,8 +20,8 @@ export function bumpPackageVersion(
}
logger.debug({ newChartVersion });
bumpedContent = content.replace(
/^(version:\s*).*$/m, // TODO #12070
`$1${newChartVersion}`
/^(?<version>version:\s*).*$/m, // TODO #12070
`$<version>${newChartVersion}`
);
if (bumpedContent === content) {
logger.debug('Version was already bumped');
Expand Down
4 changes: 2 additions & 2 deletions lib/manager/npm/update/package-version/index.ts
Expand Up @@ -31,8 +31,8 @@ export function bumpPackageVersion(
}
logger.debug({ newPjVersion });
bumpedContent = content.replace(
/("version":\s*")[^"]*/, // TODO #12070
`$1${newPjVersion}`
/(?<version>"version":\s*")[^"]*/, // TODO #12070
`$<version>${newPjVersion}`
);
if (bumpedContent === content) {
logger.debug('Version was already bumped');
Expand Down
4 changes: 2 additions & 2 deletions lib/manager/terraform/lockfile/util.ts
Expand Up @@ -159,7 +159,7 @@ export function writeLockUpdates(
providerBlockLines.forEach((providerBlockLine, providerBlockIndex) => {
const versionLine = providerBlockLine.replace(
versionLineRegex,
`$1${update.newVersion}$3`
`$<prefix>${update.newVersion}$<suffix>`
);
if (versionLine !== providerBlockLine) {
newProviderBlockLines.push(versionLine);
Expand All @@ -168,7 +168,7 @@ export function writeLockUpdates(

const constraintLine = providerBlockLine.replace(
constraintLineRegex,
`$1${update.newConstraint}$3`
`$<prefix>${update.newConstraint}$<suffix>`
);
if (constraintLine !== providerBlockLine) {
newProviderBlockLines.push(constraintLine);
Expand Down
13 changes: 8 additions & 5 deletions lib/versioning/hashicorp/index.ts
Expand Up @@ -40,17 +40,20 @@ function getNewValue({
const testFullVersion = /(~>\s*0\.)(\d+)\.\d$/;
let replaceValue = '';
if (testFullVersion.test(currentValue)) {
replaceValue = `$1${npm.getMinor(newVersion)}.0`;
replaceValue = `$<prefix>${npm.getMinor(newVersion)}.0`;
} else {
replaceValue = `$1${npm.getMinor(newVersion)}$3`;
replaceValue = `$<prefix>${npm.getMinor(newVersion)}$<suffix>`;
}
return currentValue.replace(/(~>\s*0\.)(\d+)(.*)$/, replaceValue);
return currentValue.replace(
/(?<prefix>~>\s*0\.)\d+(?<suffix>.*)$/,
replaceValue
);
}
// handle special ~> 1.2 case
if (/(~>\s*)\d+\.\d+$/.test(currentValue)) {
return currentValue.replace(
/(~>\s*)\d+\.\d+$/,
`$1${npm.getMajor(newVersion)}.0`
/(?<prefix>~>\s*)\d+\.\d+$/,
`$<prefix>${npm.getMajor(newVersion)}.0`
);
}
}
Expand Down
8 changes: 6 additions & 2 deletions lib/versioning/ruby/index.ts
Expand Up @@ -126,8 +126,12 @@ const getNewValue = ({
const delimiter = currentValue[0];
return newValue
.split(',')
.map((element) => element.replace(/^(\s*)/, `$1${delimiter}`))
.map((element) => element.replace(/(\s*)$/, `${delimiter}$1`))
.map((element) =>
element.replace(/^(?<whitespace>\s*)/, `$<whitespace>${delimiter}`)
)
.map((element) =>
element.replace(/(?<whitespace>\s*)$/, `${delimiter}$<whitespace>`)
)
.join(',');
}
return newValue;
Expand Down

0 comments on commit 651d5de

Please sign in to comment.