From 6dd53d1f2bde388fe1da550c8657f77948ecd04c Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Wed, 8 Apr 2020 10:17:52 +0200
Subject: [PATCH] refactor(pub): use autoReplace

---
 .../pub/__snapshots__/update.spec.ts.snap     | 37 ----------
 lib/manager/pub/index.ts                      |  3 +-
 lib/manager/pub/update.spec.ts                | 70 ------------------
 lib/manager/pub/update.ts                     | 71 -------------------
 4 files changed, 2 insertions(+), 179 deletions(-)
 delete mode 100644 lib/manager/pub/__snapshots__/update.spec.ts.snap
 delete mode 100644 lib/manager/pub/update.spec.ts
 delete mode 100644 lib/manager/pub/update.ts

diff --git a/lib/manager/pub/__snapshots__/update.spec.ts.snap b/lib/manager/pub/__snapshots__/update.spec.ts.snap
deleted file mode 100644
index 426313103f..0000000000
--- a/lib/manager/pub/__snapshots__/update.spec.ts.snap
+++ /dev/null
@@ -1,37 +0,0 @@
-// Jest Snapshot v1, https://goo.gl/fbAQLP
-
-exports[`manager/pub/update updateDependency replaces nested value 1`] = `
-"dev_dependencies:
-  test: 0.1
-  build:
-    version: 0.1
-
-dependencies:
-  bar:
-    sdk: flatter
-  foo: 1
-  bar:
-    version: 1.2.3
-  baz:
-    non-sense: true
-  qux: false
-"
-`;
-
-exports[`manager/pub/update updateDependency replaces one-line value 1`] = `
-"dev_dependencies:
-  test: 0.1
-  build:
-    version: 0.1
-
-dependencies:
-  bar:
-    sdk: flatter
-  foo: 1.2.3
-  bar:
-    version: 1
-  baz:
-    non-sense: true
-  qux: false
-"
-`;
diff --git a/lib/manager/pub/index.ts b/lib/manager/pub/index.ts
index 3034fc824f..245129a5d6 100644
--- a/lib/manager/pub/index.ts
+++ b/lib/manager/pub/index.ts
@@ -1,7 +1,8 @@
 import * as npmVersioning from '../../versioning/npm';
 
 export { extractPackageFile } from './extract';
-export { updateDependency } from './update';
+
+export const autoReplace = true;
 
 export const defaultConfig = {
   fileMatch: ['(^|/)pubspec\\.ya?ml$'],
diff --git a/lib/manager/pub/update.spec.ts b/lib/manager/pub/update.spec.ts
deleted file mode 100644
index 0949e38da5..0000000000
--- a/lib/manager/pub/update.spec.ts
+++ /dev/null
@@ -1,70 +0,0 @@
-import { readFileSync } from 'fs';
-import { updateDependency } from '.';
-
-const fileContent = readFileSync(
-  'lib/manager/pub/__fixtures__/update.yaml',
-  'utf8'
-);
-
-describe('manager/pub/update', () => {
-  describe('updateDependency', () => {
-    it('returns content untouched if versions are same', () => {
-      const upgrade = {
-        depName: 'foo',
-        currentValue: '1',
-        newValue: '1',
-        depType: 'dependencies',
-      };
-      const res = updateDependency({ fileContent, upgrade });
-      expect(res).toEqual(fileContent);
-    });
-    it('returns null if content was updated', () => {
-      expect(
-        updateDependency({
-          fileContent,
-          upgrade: {
-            depName: 'test',
-            currentValue: '0.0.1',
-            newValue: '1',
-            depType: 'dev_dependencies',
-          },
-        })
-      ).toBe(null);
-      expect(
-        updateDependency({
-          fileContent,
-          upgrade: {
-            depName: 'build',
-            currentValue: '0.0.1',
-            newValue: '1',
-            depType: 'dev_dependencies',
-          },
-        })
-      ).toBe(null);
-    });
-    it('replaces one-line value', () => {
-      const upgrade = {
-        depName: 'foo',
-        currentValue: '1',
-        newValue: '1.2.3',
-        depType: 'dependencies',
-      };
-      const res = updateDependency({ fileContent, upgrade });
-      expect(res).not.toEqual(fileContent);
-      expect(res.includes(upgrade.newValue)).toBe(true);
-      expect(res).toMatchSnapshot();
-    });
-    it('replaces nested value', () => {
-      const upgrade = {
-        depName: 'bar',
-        currentValue: '1',
-        newValue: '1.2.3',
-        depType: 'dependencies',
-      };
-      const res = updateDependency({ fileContent, upgrade });
-      expect(res).not.toEqual(fileContent);
-      expect(res.includes(upgrade.newValue)).toBe(true);
-      expect(res).toMatchSnapshot();
-    });
-  });
-});
diff --git a/lib/manager/pub/update.ts b/lib/manager/pub/update.ts
deleted file mode 100644
index 8732f2fa0a..0000000000
--- a/lib/manager/pub/update.ts
+++ /dev/null
@@ -1,71 +0,0 @@
-import { load } from 'js-yaml';
-import { UpdateDependencyConfig } from '../common';
-
-export function updateDependency({
-  fileContent,
-  upgrade,
-}: UpdateDependencyConfig): string {
-  const { depName, depType, currentValue, newValue } = upgrade;
-
-  if (currentValue === newValue) {
-    return fileContent;
-  }
-
-  const sectionBeginRegExp = new RegExp(`^${depType}:`);
-  const isSectionBegin = (line: string): boolean =>
-    sectionBeginRegExp.test(line);
-  const isSectionEnd = (line: string): boolean => /^[^\s]/.test(line);
-
-  const simpleDepRegExp = new RegExp(`^\\s+${depName}:\\s*[^\\s]+\\s*$`);
-  const isOneLineDep = (line: string): boolean => simpleDepRegExp.test(line);
-
-  const multilineDepRegExp = new RegExp(`^\\s+${depName}:\\s*$`);
-  const isMultilineDepRegExp = (line: string): boolean =>
-    multilineDepRegExp.test(line);
-
-  const versionRegExp = new RegExp('^\\s+version:\\s*[^\\s]+\\s*$');
-  const isVersionLine = (line: string): boolean => versionRegExp.test(line);
-
-  const isValidVersion = (line: string): boolean => {
-    const version = load(line.replace(/^.*:\s*/, '')).toString();
-    return version === currentValue;
-  };
-
-  let isSection = false;
-  let indent = null;
-
-  const lines = fileContent.split('\n');
-  const len = lines.length;
-  for (let idx = 0; idx < len; idx += 1) {
-    const line = lines[idx];
-
-    if (isSectionBegin(line)) {
-      isSection = true;
-    } else if (isSectionEnd(line)) {
-      isSection = false;
-    } else if (isSection) {
-      if (isOneLineDep(line)) {
-        if (!isValidVersion(line)) {
-          return null;
-        }
-        lines[idx] = line.replace(currentValue, newValue);
-        break;
-      } else if (isMultilineDepRegExp(line)) {
-        indent = line.search(/[^\s]/);
-      } else if (indent) {
-        const currentIndent = line.search(/[^\s]/);
-        if (currentIndent <= indent) {
-          indent = null;
-        } else if (isVersionLine(line)) {
-          if (!isValidVersion(line)) {
-            return null;
-          }
-          lines[idx] = line.replace(currentValue, newValue);
-          break;
-        }
-      }
-    }
-  }
-
-  return lines.join('\n');
-}
-- 
GitLab