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 }); const res = updateDependency({ fileContent: gomod2, upgrade });
expect(res).not.toEqual(gomod2); 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)); expect(res).toContain(upgrade.newDigest.substring(0, 12));
}); });
it('skips already-updated multiline digest', () => { it('skips already-updated multiline digest', () => {

View file

@ -33,13 +33,15 @@ export function updateDependency({
let updateLineExp: RegExp; let updateLineExp: RegExp;
if (depType === 'replace') { if (depType === 'replace') {
updateLineExp = regEx( updateLineExp = regEx(
/^(replace\s+[^\s]+[\s]+[=][>]+\s+)([^\s]+\s+)([^\s]+)/ /^(?<depPart>replace\s+[^\s]+[\s]+[=][>]+\s+)(?<divider>[^\s]+\s+)[^\s]+/
); );
} else if (depType === 'require') { } else if (depType === 'require') {
if (upgrade.managerData.multiLine) { if (upgrade.managerData.multiLine) {
updateLineExp = regEx(/^(\s+[^\s]+)(\s+)([^\s]+)/); updateLineExp = regEx(/^(?<depPart>\s+[^\s]+)(?<divider>\s+)[^\s]+/);
} else { } else {
updateLineExp = regEx(/^(require\s+[^\s]+)(\s+)([^\s]+)/); updateLineExp = regEx(
/^(?<depPart>require\s+[^\s]+)(?<divider>\s+)[^\s]+/
);
} }
} }
if (updateLineExp && !updateLineExp.test(lineToChange)) { if (updateLineExp && !updateLineExp.test(lineToChange)) {
@ -61,10 +63,13 @@ export function updateDependency({
); );
newLine = lineToChange.replace( newLine = lineToChange.replace(
updateLineExp, updateLineExp,
`$1$2${newDigestRightSized}` `$<depPart>$<divider>${newDigestRightSized}`
); );
} else { } else {
newLine = lineToChange.replace(updateLineExp, `$1$2${upgrade.newValue}`); newLine = lineToChange.replace(
updateLineExp,
`$<depPart>$<divider>${upgrade.newValue}`
);
} }
if (upgrade.updateType === 'major') { if (upgrade.updateType === 'major') {
logger.debug({ depName }, 'gomod: major update'); logger.debug({ depName }, 'gomod: major update');

View file

@ -20,8 +20,8 @@ export function bumpPackageVersion(
} }
logger.debug({ newChartVersion }); logger.debug({ newChartVersion });
bumpedContent = content.replace( bumpedContent = content.replace(
/^(version:\s*).*$/m, // TODO #12070 /^(?<version>version:\s*).*$/m, // TODO #12070
`$1${newChartVersion}` `$<version>${newChartVersion}`
); );
if (bumpedContent === content) { if (bumpedContent === content) {
logger.debug('Version was already bumped'); logger.debug('Version was already bumped');

View file

@ -31,8 +31,8 @@ export function bumpPackageVersion(
} }
logger.debug({ newPjVersion }); logger.debug({ newPjVersion });
bumpedContent = content.replace( bumpedContent = content.replace(
/("version":\s*")[^"]*/, // TODO #12070 /(?<version>"version":\s*")[^"]*/, // TODO #12070
`$1${newPjVersion}` `$<version>${newPjVersion}`
); );
if (bumpedContent === content) { if (bumpedContent === content) {
logger.debug('Version was already bumped'); logger.debug('Version was already bumped');

View file

@ -159,7 +159,7 @@ export function writeLockUpdates(
providerBlockLines.forEach((providerBlockLine, providerBlockIndex) => { providerBlockLines.forEach((providerBlockLine, providerBlockIndex) => {
const versionLine = providerBlockLine.replace( const versionLine = providerBlockLine.replace(
versionLineRegex, versionLineRegex,
`$1${update.newVersion}$3` `$<prefix>${update.newVersion}$<suffix>`
); );
if (versionLine !== providerBlockLine) { if (versionLine !== providerBlockLine) {
newProviderBlockLines.push(versionLine); newProviderBlockLines.push(versionLine);
@ -168,7 +168,7 @@ export function writeLockUpdates(
const constraintLine = providerBlockLine.replace( const constraintLine = providerBlockLine.replace(
constraintLineRegex, constraintLineRegex,
`$1${update.newConstraint}$3` `$<prefix>${update.newConstraint}$<suffix>`
); );
if (constraintLine !== providerBlockLine) { if (constraintLine !== providerBlockLine) {
newProviderBlockLines.push(constraintLine); newProviderBlockLines.push(constraintLine);

View file

@ -40,17 +40,20 @@ function getNewValue({
const testFullVersion = /(~>\s*0\.)(\d+)\.\d$/; const testFullVersion = /(~>\s*0\.)(\d+)\.\d$/;
let replaceValue = ''; let replaceValue = '';
if (testFullVersion.test(currentValue)) { if (testFullVersion.test(currentValue)) {
replaceValue = `$1${npm.getMinor(newVersion)}.0`; replaceValue = `$<prefix>${npm.getMinor(newVersion)}.0`;
} else { } 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 // handle special ~> 1.2 case
if (/(~>\s*)\d+\.\d+$/.test(currentValue)) { if (/(~>\s*)\d+\.\d+$/.test(currentValue)) {
return currentValue.replace( return currentValue.replace(
/(~>\s*)\d+\.\d+$/, /(?<prefix>~>\s*)\d+\.\d+$/,
`$1${npm.getMajor(newVersion)}.0` `$<prefix>${npm.getMajor(newVersion)}.0`
); );
} }
} }

View file

@ -126,8 +126,12 @@ const getNewValue = ({
const delimiter = currentValue[0]; const delimiter = currentValue[0];
return newValue return newValue
.split(',') .split(',')
.map((element) => element.replace(/^(\s*)/, `$1${delimiter}`)) .map((element) =>
.map((element) => element.replace(/(\s*)$/, `${delimiter}$1`)) element.replace(/^(?<whitespace>\s*)/, `$<whitespace>${delimiter}`)
)
.map((element) =>
element.replace(/(?<whitespace>\s*)$/, `${delimiter}$<whitespace>`)
)
.join(','); .join(',');
} }
return newValue; return newValue;