fix: replace numbered capture group replaces with named capture groups (#12315)

This commit is contained in:
Rhys Arkins 2021-10-25 20:21:21 +02:00 committed by GitHub
parent 8571b3e68d
commit 651d5de3cf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 31 additions and 19 deletions

View file

@ -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', () => {

View file

@ -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)) {
@ -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');

View file

@ -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');

View file

@ -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');

View file

@ -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);
@ -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);

View file

@ -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`
);
}
}

View file

@ -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;