From 140938816e297a4acc153f3b1481b21f83e6f7ac Mon Sep 17 00:00:00 2001 From: Nicolas Chang <25302138+NicolasCwy@users.noreply.github.com> Date: Sat, 23 Mar 2024 15:31:38 +0800 Subject: [PATCH 1/4] Rename files to TS --- .../mermaid/src/diagrams/git/{gitGraphAst.js => gitGraphAst.ts} | 0 .../git/{gitGraphParser.spec.js => gitGraphParser.spec.ts} | 0 .../git/{gitGraphParserV2.spec.js => gitGraphParserV2.spec.ts} | 0 .../git/{gitGraphRenderer-old.js => gitGraphRenderer-old.ts} | 0 .../src/diagrams/git/{gitGraphRenderer.js => gitGraphRenderer.ts} | 0 packages/mermaid/src/diagrams/git/{layout.js => layout.ts} | 0 packages/mermaid/src/diagrams/git/{mockDb.js => mockDb.ts} | 0 packages/mermaid/src/diagrams/git/{styles.js => styles.ts} | 0 8 files changed, 0 insertions(+), 0 deletions(-) rename packages/mermaid/src/diagrams/git/{gitGraphAst.js => gitGraphAst.ts} (100%) rename packages/mermaid/src/diagrams/git/{gitGraphParser.spec.js => gitGraphParser.spec.ts} (100%) rename packages/mermaid/src/diagrams/git/{gitGraphParserV2.spec.js => gitGraphParserV2.spec.ts} (100%) rename packages/mermaid/src/diagrams/git/{gitGraphRenderer-old.js => gitGraphRenderer-old.ts} (100%) rename packages/mermaid/src/diagrams/git/{gitGraphRenderer.js => gitGraphRenderer.ts} (100%) rename packages/mermaid/src/diagrams/git/{layout.js => layout.ts} (100%) rename packages/mermaid/src/diagrams/git/{mockDb.js => mockDb.ts} (100%) rename packages/mermaid/src/diagrams/git/{styles.js => styles.ts} (100%) diff --git a/packages/mermaid/src/diagrams/git/gitGraphAst.js b/packages/mermaid/src/diagrams/git/gitGraphAst.ts similarity index 100% rename from packages/mermaid/src/diagrams/git/gitGraphAst.js rename to packages/mermaid/src/diagrams/git/gitGraphAst.ts diff --git a/packages/mermaid/src/diagrams/git/gitGraphParser.spec.js b/packages/mermaid/src/diagrams/git/gitGraphParser.spec.ts similarity index 100% rename from packages/mermaid/src/diagrams/git/gitGraphParser.spec.js rename to packages/mermaid/src/diagrams/git/gitGraphParser.spec.ts diff --git a/packages/mermaid/src/diagrams/git/gitGraphParserV2.spec.js b/packages/mermaid/src/diagrams/git/gitGraphParserV2.spec.ts similarity index 100% rename from packages/mermaid/src/diagrams/git/gitGraphParserV2.spec.js rename to packages/mermaid/src/diagrams/git/gitGraphParserV2.spec.ts diff --git a/packages/mermaid/src/diagrams/git/gitGraphRenderer-old.js b/packages/mermaid/src/diagrams/git/gitGraphRenderer-old.ts similarity index 100% rename from packages/mermaid/src/diagrams/git/gitGraphRenderer-old.js rename to packages/mermaid/src/diagrams/git/gitGraphRenderer-old.ts diff --git a/packages/mermaid/src/diagrams/git/gitGraphRenderer.js b/packages/mermaid/src/diagrams/git/gitGraphRenderer.ts similarity index 100% rename from packages/mermaid/src/diagrams/git/gitGraphRenderer.js rename to packages/mermaid/src/diagrams/git/gitGraphRenderer.ts diff --git a/packages/mermaid/src/diagrams/git/layout.js b/packages/mermaid/src/diagrams/git/layout.ts similarity index 100% rename from packages/mermaid/src/diagrams/git/layout.js rename to packages/mermaid/src/diagrams/git/layout.ts diff --git a/packages/mermaid/src/diagrams/git/mockDb.js b/packages/mermaid/src/diagrams/git/mockDb.ts similarity index 100% rename from packages/mermaid/src/diagrams/git/mockDb.js rename to packages/mermaid/src/diagrams/git/mockDb.ts diff --git a/packages/mermaid/src/diagrams/git/styles.js b/packages/mermaid/src/diagrams/git/styles.ts similarity index 100% rename from packages/mermaid/src/diagrams/git/styles.js rename to packages/mermaid/src/diagrams/git/styles.ts From 04815b0a3b02c44b9ed4b6b62366d81a0dd38206 Mon Sep 17 00:00:00 2001 From: Nicolas Chang <25302138+NicolasCwy@users.noreply.github.com> Date: Sat, 23 Mar 2024 15:36:14 +0800 Subject: [PATCH 2/4] Fix gitGraphParser.spec.ts --- packages/mermaid/src/diagrams/git/gitGraphParser.spec.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/mermaid/src/diagrams/git/gitGraphParser.spec.ts b/packages/mermaid/src/diagrams/git/gitGraphParser.spec.ts index 446f067393..6889bfd119 100644 --- a/packages/mermaid/src/diagrams/git/gitGraphParser.spec.ts +++ b/packages/mermaid/src/diagrams/git/gitGraphParser.spec.ts @@ -1,4 +1,5 @@ import gitGraphAst from './gitGraphAst.js'; +// @ts-expect-error No types available for JISON import { parser } from './parser/gitGraph.jison'; describe('when parsing a gitGraph', function () { From 7e2a27bce14ab610b975dab9ca356398089a4b38 Mon Sep 17 00:00:00 2001 From: Nicolas Chang <25302138+NicolasCwy@users.noreply.github.com> Date: Sat, 23 Mar 2024 15:36:31 +0800 Subject: [PATCH 3/4] Fix gitGraphParserV2.spec.ts --- .../src/diagrams/git/gitGraphParserV2.spec.ts | 462 ++++++++---------- 1 file changed, 213 insertions(+), 249 deletions(-) diff --git a/packages/mermaid/src/diagrams/git/gitGraphParserV2.spec.ts b/packages/mermaid/src/diagrams/git/gitGraphParserV2.spec.ts index 09a9cdb259..682472481c 100644 --- a/packages/mermaid/src/diagrams/git/gitGraphParserV2.spec.ts +++ b/packages/mermaid/src/diagrams/git/gitGraphParserV2.spec.ts @@ -1,22 +1,23 @@ import gitGraphAst from './gitGraphAst.js'; -import { parser } from './parser/gitGraph.jison'; +// @ts-expect-error No types available for JISON +import { gitGraph as gitGraph } from './gitGraph/gitGraph.jison'; describe('when parsing a gitGraph', function () { beforeEach(function () { - parser.yy = gitGraphAst; - parser.yy.clear(); + gitGraph.yy = gitGraphAst; + gitGraph.yy.clear(); }); it('should handle a gitGraph commit with NO pararms, get auto-generated reandom ID', function () { const str = `gitGraph: commit `; - parser.parse(str); - const commits = parser.yy.getCommits(); + gitGraph.parse(str); + const commits = gitGraph.yy.getCommits(); //console.info(commits); expect(Object.keys(commits).length).toBe(1); - expect(parser.yy.getCurrentBranch()).toBe('main'); - expect(parser.yy.getDirection()).toBe('LR'); - expect(Object.keys(parser.yy.getBranches()).length).toBe(1); + expect(gitGraph.yy.getCurrentBranch()).toBe('main'); + expect(gitGraph.yy.getDirection()).toBe('LR'); + expect(Object.keys(gitGraph.yy.getBranches()).length).toBe(1); const key = Object.keys(commits)[0]; expect(commits[key].message).toBe(''); expect(commits[key].id).not.toBeNull(); @@ -28,12 +29,12 @@ describe('when parsing a gitGraph', function () { const str = `gitGraph: commit id:"1111" `; - parser.parse(str); - const commits = parser.yy.getCommits(); + gitGraph.parse(str); + const commits = gitGraph.yy.getCommits(); expect(Object.keys(commits).length).toBe(1); - expect(parser.yy.getCurrentBranch()).toBe('main'); - expect(parser.yy.getDirection()).toBe('LR'); - expect(Object.keys(parser.yy.getBranches()).length).toBe(1); + expect(gitGraph.yy.getCurrentBranch()).toBe('main'); + expect(gitGraph.yy.getDirection()).toBe('LR'); + expect(Object.keys(gitGraph.yy.getBranches()).length).toBe(1); const key = Object.keys(commits)[0]; expect(commits[key].message).toBe(''); expect(commits[key].id).toBe('1111'); @@ -46,12 +47,12 @@ describe('when parsing a gitGraph', function () { commit tag:"test" `; - parser.parse(str); - const commits = parser.yy.getCommits(); + gitGraph.parse(str); + const commits = gitGraph.yy.getCommits(); expect(Object.keys(commits).length).toBe(1); - expect(parser.yy.getCurrentBranch()).toBe('main'); - expect(parser.yy.getDirection()).toBe('LR'); - expect(Object.keys(parser.yy.getBranches()).length).toBe(1); + expect(gitGraph.yy.getCurrentBranch()).toBe('main'); + expect(gitGraph.yy.getDirection()).toBe('LR'); + expect(Object.keys(gitGraph.yy.getBranches()).length).toBe(1); const key = Object.keys(commits)[0]; expect(commits[key].message).toBe(''); expect(commits[key].id).not.toBeNull(); @@ -64,12 +65,12 @@ describe('when parsing a gitGraph', function () { commit type: HIGHLIGHT `; - parser.parse(str); - const commits = parser.yy.getCommits(); + gitGraph.parse(str); + const commits = gitGraph.yy.getCommits(); expect(Object.keys(commits).length).toBe(1); - expect(parser.yy.getCurrentBranch()).toBe('main'); - expect(parser.yy.getDirection()).toBe('LR'); - expect(Object.keys(parser.yy.getBranches()).length).toBe(1); + expect(gitGraph.yy.getCurrentBranch()).toBe('main'); + expect(gitGraph.yy.getDirection()).toBe('LR'); + expect(Object.keys(gitGraph.yy.getBranches()).length).toBe(1); const key = Object.keys(commits)[0]; expect(commits[key].message).toBe(''); expect(commits[key].id).not.toBeNull(); @@ -82,12 +83,12 @@ describe('when parsing a gitGraph', function () { commit type: REVERSE `; - parser.parse(str); - const commits = parser.yy.getCommits(); + gitGraph.parse(str); + const commits = gitGraph.yy.getCommits(); expect(Object.keys(commits).length).toBe(1); - expect(parser.yy.getCurrentBranch()).toBe('main'); - expect(parser.yy.getDirection()).toBe('LR'); - expect(Object.keys(parser.yy.getBranches()).length).toBe(1); + expect(gitGraph.yy.getCurrentBranch()).toBe('main'); + expect(gitGraph.yy.getDirection()).toBe('LR'); + expect(Object.keys(gitGraph.yy.getBranches()).length).toBe(1); const key = Object.keys(commits)[0]; expect(commits[key].message).toBe(''); expect(commits[key].id).not.toBeNull(); @@ -100,12 +101,12 @@ describe('when parsing a gitGraph', function () { commit type: NORMAL `; - parser.parse(str); - const commits = parser.yy.getCommits(); + gitGraph.parse(str); + const commits = gitGraph.yy.getCommits(); expect(Object.keys(commits).length).toBe(1); - expect(parser.yy.getCurrentBranch()).toBe('main'); - expect(parser.yy.getDirection()).toBe('LR'); - expect(Object.keys(parser.yy.getBranches()).length).toBe(1); + expect(gitGraph.yy.getCurrentBranch()).toBe('main'); + expect(gitGraph.yy.getDirection()).toBe('LR'); + expect(Object.keys(gitGraph.yy.getBranches()).length).toBe(1); const key = Object.keys(commits)[0]; expect(commits[key].message).toBe(''); expect(commits[key].id).not.toBeNull(); @@ -118,12 +119,12 @@ describe('when parsing a gitGraph', function () { commit "test commit" `; - parser.parse(str); - const commits = parser.yy.getCommits(); + gitGraph.parse(str); + const commits = gitGraph.yy.getCommits(); expect(Object.keys(commits).length).toBe(1); - expect(parser.yy.getCurrentBranch()).toBe('main'); - expect(parser.yy.getDirection()).toBe('LR'); - expect(Object.keys(parser.yy.getBranches()).length).toBe(1); + expect(gitGraph.yy.getCurrentBranch()).toBe('main'); + expect(gitGraph.yy.getDirection()).toBe('LR'); + expect(Object.keys(gitGraph.yy.getBranches()).length).toBe(1); const key = Object.keys(commits)[0]; expect(commits[key].message).toBe('test commit'); expect(commits[key].id).not.toBeNull(); @@ -136,12 +137,12 @@ describe('when parsing a gitGraph', function () { commit msg: "test commit" `; - parser.parse(str); - const commits = parser.yy.getCommits(); + gitGraph.parse(str); + const commits = gitGraph.yy.getCommits(); expect(Object.keys(commits).length).toBe(1); - expect(parser.yy.getCurrentBranch()).toBe('main'); - expect(parser.yy.getDirection()).toBe('LR'); - expect(Object.keys(parser.yy.getBranches()).length).toBe(1); + expect(gitGraph.yy.getCurrentBranch()).toBe('main'); + expect(gitGraph.yy.getDirection()).toBe('LR'); + expect(Object.keys(gitGraph.yy.getBranches()).length).toBe(1); const key = Object.keys(commits)[0]; expect(commits[key].message).toBe('test commit'); expect(commits[key].id).not.toBeNull(); @@ -154,12 +155,12 @@ describe('when parsing a gitGraph', function () { commit id:"1111" tag: "test tag" `; - parser.parse(str); - const commits = parser.yy.getCommits(); + gitGraph.parse(str); + const commits = gitGraph.yy.getCommits(); expect(Object.keys(commits).length).toBe(1); - expect(parser.yy.getCurrentBranch()).toBe('main'); - expect(parser.yy.getDirection()).toBe('LR'); - expect(Object.keys(parser.yy.getBranches()).length).toBe(1); + expect(gitGraph.yy.getCurrentBranch()).toBe('main'); + expect(gitGraph.yy.getDirection()).toBe('LR'); + expect(Object.keys(gitGraph.yy.getBranches()).length).toBe(1); const key = Object.keys(commits)[0]; expect(commits[key].message).toBe(''); expect(commits[key].id).toBe('1111'); @@ -172,12 +173,12 @@ describe('when parsing a gitGraph', function () { commit type:HIGHLIGHT tag: "test tag" `; - parser.parse(str); - const commits = parser.yy.getCommits(); + gitGraph.parse(str); + const commits = gitGraph.yy.getCommits(); expect(Object.keys(commits).length).toBe(1); - expect(parser.yy.getCurrentBranch()).toBe('main'); - expect(parser.yy.getDirection()).toBe('LR'); - expect(Object.keys(parser.yy.getBranches()).length).toBe(1); + expect(gitGraph.yy.getCurrentBranch()).toBe('main'); + expect(gitGraph.yy.getDirection()).toBe('LR'); + expect(Object.keys(gitGraph.yy.getBranches()).length).toBe(1); const key = Object.keys(commits)[0]; expect(commits[key].message).toBe(''); expect(commits[key].id).not.toBeNull(); @@ -190,12 +191,12 @@ describe('when parsing a gitGraph', function () { commit tag: "test tag" type:HIGHLIGHT `; - parser.parse(str); - const commits = parser.yy.getCommits(); + gitGraph.parse(str); + const commits = gitGraph.yy.getCommits(); expect(Object.keys(commits).length).toBe(1); - expect(parser.yy.getCurrentBranch()).toBe('main'); - expect(parser.yy.getDirection()).toBe('LR'); - expect(Object.keys(parser.yy.getBranches()).length).toBe(1); + expect(gitGraph.yy.getCurrentBranch()).toBe('main'); + expect(gitGraph.yy.getDirection()).toBe('LR'); + expect(Object.keys(gitGraph.yy.getBranches()).length).toBe(1); const key = Object.keys(commits)[0]; expect(commits[key].message).toBe(''); expect(commits[key].id).not.toBeNull(); @@ -208,12 +209,12 @@ describe('when parsing a gitGraph', function () { commit id:"1111" type:REVERSE tag: "test tag" `; - parser.parse(str); - const commits = parser.yy.getCommits(); + gitGraph.parse(str); + const commits = gitGraph.yy.getCommits(); expect(Object.keys(commits).length).toBe(1); - expect(parser.yy.getCurrentBranch()).toBe('main'); - expect(parser.yy.getDirection()).toBe('LR'); - expect(Object.keys(parser.yy.getBranches()).length).toBe(1); + expect(gitGraph.yy.getCurrentBranch()).toBe('main'); + expect(gitGraph.yy.getDirection()).toBe('LR'); + expect(Object.keys(gitGraph.yy.getBranches()).length).toBe(1); const key = Object.keys(commits)[0]; expect(commits[key].message).toBe(''); expect(commits[key].id).toBe('1111'); @@ -226,12 +227,12 @@ describe('when parsing a gitGraph', function () { commit id:"1111" type:REVERSE tag: "test tag" msg:"test msg" `; - parser.parse(str); - const commits = parser.yy.getCommits(); + gitGraph.parse(str); + const commits = gitGraph.yy.getCommits(); expect(Object.keys(commits).length).toBe(1); - expect(parser.yy.getCurrentBranch()).toBe('main'); - expect(parser.yy.getDirection()).toBe('LR'); - expect(Object.keys(parser.yy.getBranches()).length).toBe(1); + expect(gitGraph.yy.getCurrentBranch()).toBe('main'); + expect(gitGraph.yy.getDirection()).toBe('LR'); + expect(Object.keys(gitGraph.yy.getBranches()).length).toBe(1); const key = Object.keys(commits)[0]; expect(commits[key].message).toBe('test msg'); expect(commits[key].id).toBe('1111'); @@ -245,12 +246,12 @@ describe('when parsing a gitGraph', function () { `; - parser.parse(str); - const commits = parser.yy.getCommits(); + gitGraph.parse(str); + const commits = gitGraph.yy.getCommits(); expect(Object.keys(commits).length).toBe(1); - expect(parser.yy.getCurrentBranch()).toBe('main'); - expect(parser.yy.getDirection()).toBe('LR'); - expect(Object.keys(parser.yy.getBranches()).length).toBe(1); + expect(gitGraph.yy.getCurrentBranch()).toBe('main'); + expect(gitGraph.yy.getDirection()).toBe('LR'); + expect(Object.keys(gitGraph.yy.getBranches()).length).toBe(1); const key = Object.keys(commits)[0]; expect(commits[key].message).toBe('test msg'); expect(commits[key].id).toBe('1111'); @@ -263,12 +264,12 @@ describe('when parsing a gitGraph', function () { commit tag: "test tag" msg:"test msg" id:"1111" type:REVERSE `; - parser.parse(str); - const commits = parser.yy.getCommits(); + gitGraph.parse(str); + const commits = gitGraph.yy.getCommits(); expect(Object.keys(commits).length).toBe(1); - expect(parser.yy.getCurrentBranch()).toBe('main'); - expect(parser.yy.getDirection()).toBe('LR'); - expect(Object.keys(parser.yy.getBranches()).length).toBe(1); + expect(gitGraph.yy.getCurrentBranch()).toBe('main'); + expect(gitGraph.yy.getDirection()).toBe('LR'); + expect(Object.keys(gitGraph.yy.getBranches()).length).toBe(1); const key = Object.keys(commits)[0]; expect(commits[key].message).toBe('test msg'); expect(commits[key].id).toBe('1111'); @@ -281,12 +282,12 @@ describe('when parsing a gitGraph', function () { commit msg:"test msg" id:"1111" type:REVERSE tag: "test tag" `; - parser.parse(str); - const commits = parser.yy.getCommits(); + gitGraph.parse(str); + const commits = gitGraph.yy.getCommits(); expect(Object.keys(commits).length).toBe(1); - expect(parser.yy.getCurrentBranch()).toBe('main'); - expect(parser.yy.getDirection()).toBe('LR'); - expect(Object.keys(parser.yy.getBranches()).length).toBe(1); + expect(gitGraph.yy.getCurrentBranch()).toBe('main'); + expect(gitGraph.yy.getDirection()).toBe('LR'); + expect(Object.keys(gitGraph.yy.getBranches()).length).toBe(1); const key = Object.keys(commits)[0]; expect(commits[key].message).toBe('test msg'); expect(commits[key].id).toBe('1111'); @@ -301,12 +302,12 @@ describe('when parsing a gitGraph', function () { commit `; - parser.parse(str); - const commits = parser.yy.getCommits(); + gitGraph.parse(str); + const commits = gitGraph.yy.getCommits(); expect(Object.keys(commits).length).toBe(3); - expect(parser.yy.getCurrentBranch()).toBe('main'); - expect(parser.yy.getDirection()).toBe('LR'); - expect(Object.keys(parser.yy.getBranches()).length).toBe(1); + expect(gitGraph.yy.getCurrentBranch()).toBe('main'); + expect(gitGraph.yy.getDirection()).toBe('LR'); + expect(Object.keys(gitGraph.yy.getBranches()).length).toBe(1); }); it('should handle new branch creation', function () { @@ -315,12 +316,12 @@ describe('when parsing a gitGraph', function () { branch testBranch `; - parser.parse(str); - const commits = parser.yy.getCommits(); + gitGraph.parse(str); + const commits = gitGraph.yy.getCommits(); expect(Object.keys(commits).length).toBe(1); - expect(parser.yy.getCurrentBranch()).toBe('testBranch'); - expect(parser.yy.getDirection()).toBe('LR'); - expect(Object.keys(parser.yy.getBranches()).length).toBe(2); + expect(gitGraph.yy.getCurrentBranch()).toBe('testBranch'); + expect(gitGraph.yy.getDirection()).toBe('LR'); + expect(Object.keys(gitGraph.yy.getBranches()).length).toBe(2); }); it('should allow quoted branch names', function () { @@ -333,19 +334,22 @@ describe('when parsing a gitGraph', function () { merge "branch" `; - parser.parse(str); - const commits = parser.yy.getCommits(); + gitGraph.parse(str); + const commits = gitGraph.yy.getCommits(); expect(Object.keys(commits).length).toBe(3); - expect(parser.yy.getCurrentBranch()).toBe('main'); - expect(parser.yy.getDirection()).toBe('LR'); - expect(Object.keys(parser.yy.getBranches()).length).toBe(2); + expect(gitGraph.yy.getCurrentBranch()).toBe('main'); + expect(gitGraph.yy.getDirection()).toBe('LR'); + expect(Object.keys(gitGraph.yy.getBranches()).length).toBe(2); const commit1 = Object.keys(commits)[0]; const commit2 = Object.keys(commits)[1]; const commit3 = Object.keys(commits)[2]; expect(commits[commit1].branch).toBe('main'); expect(commits[commit2].branch).toBe('branch'); expect(commits[commit3].branch).toBe('main'); - expect(parser.yy.getBranchesAsObjArray()).toStrictEqual([{ name: 'main' }, { name: 'branch' }]); + expect(gitGraph.yy.getBranchesAsObjArray()).toStrictEqual([ + { name: 'main' }, + { name: 'branch' }, + ]); }); it('should allow _-./ characters in branch names', function () { @@ -354,12 +358,12 @@ describe('when parsing a gitGraph', function () { branch azAZ_-./test `; - parser.parse(str); - const commits = parser.yy.getCommits(); + gitGraph.parse(str); + const commits = gitGraph.yy.getCommits(); expect(Object.keys(commits).length).toBe(1); - expect(parser.yy.getCurrentBranch()).toBe('azAZ_-./test'); - expect(parser.yy.getDirection()).toBe('LR'); - expect(Object.keys(parser.yy.getBranches()).length).toBe(2); + expect(gitGraph.yy.getCurrentBranch()).toBe('azAZ_-./test'); + expect(gitGraph.yy.getDirection()).toBe('LR'); + expect(Object.keys(gitGraph.yy.getBranches()).length).toBe(2); }); it('should allow branch names starting with numbers', function () { @@ -369,12 +373,12 @@ describe('when parsing a gitGraph', function () { branch 1.0.1 `; - parser.parse(str); - const commits = parser.yy.getCommits(); + gitGraph.parse(str); + const commits = gitGraph.yy.getCommits(); expect(Object.keys(commits).length).toBe(1); - expect(parser.yy.getCurrentBranch()).toBe('1.0.1'); - expect(parser.yy.getDirection()).toBe('LR'); - expect(Object.keys(parser.yy.getBranches()).length).toBe(2); + expect(gitGraph.yy.getCurrentBranch()).toBe('1.0.1'); + expect(gitGraph.yy.getDirection()).toBe('LR'); + expect(Object.keys(gitGraph.yy.getBranches()).length).toBe(2); }); it('should allow branch names starting with unusual prefixes', function () { @@ -390,13 +394,13 @@ describe('when parsing a gitGraph', function () { branch A `; - parser.parse(str); - const commits = parser.yy.getCommits(); + gitGraph.parse(str); + const commits = gitGraph.yy.getCommits(); expect(Object.keys(commits).length).toBe(1); - expect(parser.yy.getCurrentBranch()).toBe('A'); - expect(parser.yy.getDirection()).toBe('LR'); - expect(Object.keys(parser.yy.getBranches()).length).toBe(7); - expect(Object.keys(parser.yy.getBranches())).toEqual( + expect(gitGraph.yy.getCurrentBranch()).toBe('A'); + expect(gitGraph.yy.getDirection()).toBe('LR'); + expect(Object.keys(gitGraph.yy.getBranches()).length).toBe(7); + expect(Object.keys(gitGraph.yy.getBranches())).toEqual( expect.arrayContaining([ 'branch01', 'checkout02', @@ -415,12 +419,12 @@ describe('when parsing a gitGraph', function () { checkout testBranch `; - parser.parse(str); - const commits = parser.yy.getCommits(); + gitGraph.parse(str); + const commits = gitGraph.yy.getCommits(); expect(Object.keys(commits).length).toBe(1); - expect(parser.yy.getCurrentBranch()).toBe('testBranch'); - expect(parser.yy.getDirection()).toBe('LR'); - expect(Object.keys(parser.yy.getBranches()).length).toBe(2); + expect(gitGraph.yy.getCurrentBranch()).toBe('testBranch'); + expect(gitGraph.yy.getDirection()).toBe('LR'); + expect(Object.keys(gitGraph.yy.getBranches()).length).toBe(2); }); it('should handle new branch checkout with order', function () { const str = `gitGraph: @@ -430,12 +434,12 @@ describe('when parsing a gitGraph', function () { branch test3 order: 1 `; - parser.parse(str); - const commits = parser.yy.getCommits(); + gitGraph.parse(str); + const commits = gitGraph.yy.getCommits(); expect(Object.keys(commits).length).toBe(1); - expect(parser.yy.getCurrentBranch()).toBe('test3'); - expect(Object.keys(parser.yy.getBranches()).length).toBe(4); - expect(parser.yy.getBranchesAsObjArray()).toStrictEqual([ + expect(gitGraph.yy.getCurrentBranch()).toBe('test3'); + expect(Object.keys(gitGraph.yy.getBranches()).length).toBe(4); + expect(gitGraph.yy.getBranchesAsObjArray()).toStrictEqual([ { name: 'main' }, { name: 'test3' }, { name: 'test2' }, @@ -450,12 +454,12 @@ describe('when parsing a gitGraph', function () { branch test3 `; - parser.parse(str); - const commits = parser.yy.getCommits(); + gitGraph.parse(str); + const commits = gitGraph.yy.getCommits(); expect(Object.keys(commits).length).toBe(1); - expect(parser.yy.getCurrentBranch()).toBe('test3'); - expect(Object.keys(parser.yy.getBranches()).length).toBe(4); - expect(parser.yy.getBranchesAsObjArray()).toStrictEqual([ + expect(gitGraph.yy.getCurrentBranch()).toBe('test3'); + expect(Object.keys(gitGraph.yy.getBranches()).length).toBe(4); + expect(gitGraph.yy.getBranchesAsObjArray()).toStrictEqual([ { name: 'main' }, { name: 'test2' }, { name: 'test3' }, @@ -471,12 +475,12 @@ describe('when parsing a gitGraph', function () { commit `; - parser.parse(str); - const commits = parser.yy.getCommits(); + gitGraph.parse(str); + const commits = gitGraph.yy.getCommits(); expect(Object.keys(commits).length).toBe(2); - expect(parser.yy.getCurrentBranch()).toBe('testBranch'); - expect(parser.yy.getDirection()).toBe('LR'); - expect(Object.keys(parser.yy.getBranches()).length).toBe(2); + expect(gitGraph.yy.getCurrentBranch()).toBe('testBranch'); + expect(gitGraph.yy.getDirection()).toBe('LR'); + expect(Object.keys(gitGraph.yy.getBranches()).length).toBe(2); const commit1 = Object.keys(commits)[0]; const commit2 = Object.keys(commits)[1]; expect(commits[commit1].branch).toBe('main'); @@ -496,12 +500,12 @@ describe('when parsing a gitGraph', function () { merge testBranch `; - parser.parse(str); - const commits = parser.yy.getCommits(); + gitGraph.parse(str); + const commits = gitGraph.yy.getCommits(); expect(Object.keys(commits).length).toBe(4); - expect(parser.yy.getCurrentBranch()).toBe('main'); - expect(parser.yy.getDirection()).toBe('LR'); - expect(Object.keys(parser.yy.getBranches()).length).toBe(2); + expect(gitGraph.yy.getCurrentBranch()).toBe('main'); + expect(gitGraph.yy.getDirection()).toBe('LR'); + expect(Object.keys(gitGraph.yy.getBranches()).length).toBe(2); const commit1 = Object.keys(commits)[0]; const commit2 = Object.keys(commits)[1]; const commit3 = Object.keys(commits)[2]; @@ -514,7 +518,7 @@ describe('when parsing a gitGraph', function () { expect(commits[commit3].parents).toStrictEqual([commits[commit2].id]); expect(commits[commit4].branch).toBe('main'); expect(commits[commit4].parents).toStrictEqual([commits[commit1].id, commits[commit3].id]); - expect(parser.yy.getBranchesAsObjArray()).toStrictEqual([ + expect(gitGraph.yy.getBranchesAsObjArray()).toStrictEqual([ { name: 'main' }, { name: 'testBranch' }, ]); @@ -530,12 +534,12 @@ describe('when parsing a gitGraph', function () { merge testBranch tag: "merge-tag" `; - parser.parse(str); - const commits = parser.yy.getCommits(); + gitGraph.parse(str); + const commits = gitGraph.yy.getCommits(); expect(Object.keys(commits).length).toBe(3); - expect(parser.yy.getCurrentBranch()).toBe('main'); - expect(parser.yy.getDirection()).toBe('LR'); - expect(Object.keys(parser.yy.getBranches()).length).toBe(2); + expect(gitGraph.yy.getCurrentBranch()).toBe('main'); + expect(gitGraph.yy.getDirection()).toBe('LR'); + expect(Object.keys(gitGraph.yy.getBranches()).length).toBe(2); const commit1 = Object.keys(commits)[0]; const commit2 = Object.keys(commits)[1]; const commit3 = Object.keys(commits)[2]; @@ -549,7 +553,7 @@ describe('when parsing a gitGraph', function () { expect(commits[commit3].branch).toBe('main'); expect(commits[commit3].parents).toStrictEqual([commits[commit1].id, commits[commit2].id]); expect(commits[commit3].tag).toBe('merge-tag'); - expect(parser.yy.getBranchesAsObjArray()).toStrictEqual([ + expect(gitGraph.yy.getBranchesAsObjArray()).toStrictEqual([ { name: 'main' }, { name: 'testBranch' }, ]); @@ -578,11 +582,11 @@ describe('when parsing a gitGraph', function () { merge testBranch3 id: "6-666" `; - parser.parse(str); - const commits = parser.yy.getCommits(); + gitGraph.parse(str); + const commits = gitGraph.yy.getCommits(); expect(Object.keys(commits).length).toBe(7); - expect(parser.yy.getCurrentBranch()).toBe('main'); - expect(parser.yy.getDirection()).toBe('LR'); + expect(gitGraph.yy.getCurrentBranch()).toBe('main'); + expect(gitGraph.yy.getDirection()).toBe('LR'); // The order of these commits is in alphabetical order of IDs const [ @@ -617,7 +621,7 @@ describe('when parsing a gitGraph', function () { expect(testBranch3Merge.parents).toStrictEqual([testBranch2Merge.id, testBranch3Commit.id]); expect(testBranch3Merge.id).toBe('6-666'); - expect(parser.yy.getBranchesAsObjArray()).toStrictEqual([ + expect(gitGraph.yy.getBranchesAsObjArray()).toStrictEqual([ { name: 'main' }, { name: 'testBranch' }, { name: 'testBranch2' }, @@ -634,8 +638,8 @@ describe('when parsing a gitGraph', function () { cherry-pick id:"A" `; - parser.parse(str); - const commits = parser.yy.getCommits(); + gitGraph.parse(str); + const commits = gitGraph.yy.getCommits(); const cherryPickCommitID = Object.keys(commits)[2]; expect(commits[cherryPickCommitID].tag).toBe('cherry-pick:A'); expect(commits[cherryPickCommitID].branch).toBe('main'); @@ -650,8 +654,8 @@ describe('when parsing a gitGraph', function () { cherry-pick id:"A" tag:"MyTag" `; - parser.parse(str); - const commits = parser.yy.getCommits(); + gitGraph.parse(str); + const commits = gitGraph.yy.getCommits(); const cherryPickCommitID = Object.keys(commits)[2]; expect(commits[cherryPickCommitID].tag).toBe('MyTag'); expect(commits[cherryPickCommitID].branch).toBe('main'); @@ -666,8 +670,8 @@ describe('when parsing a gitGraph', function () { cherry-pick id:"A" tag:"" `; - parser.parse(str); - const commits = parser.yy.getCommits(); + gitGraph.parse(str); + const commits = gitGraph.yy.getCommits(); const cherryPickCommitID = Object.keys(commits)[2]; expect(commits[cherryPickCommitID].tag).toBe(''); expect(commits[cherryPickCommitID].branch).toBe('main'); @@ -687,8 +691,8 @@ describe('when parsing a gitGraph', function () { cherry-pick id: "M" parent:"B" `; - parser.parse(str); - const commits = parser.yy.getCommits(); + gitGraph.parse(str); + const commits = gitGraph.yy.getCommits(); const cherryPickCommitID = Object.keys(commits)[4]; expect(commits[cherryPickCommitID].tag).toBe('cherry-pick:M|parent:B'); expect(commits[cherryPickCommitID].branch).toBe('release'); @@ -708,8 +712,8 @@ describe('when parsing a gitGraph', function () { cherry-pick id: "M" parent:"ZERO" tag: "v1.0" `; - parser.parse(str); - const commits = parser.yy.getCommits(); + gitGraph.parse(str); + const commits = gitGraph.yy.getCommits(); const cherryPickCommitID = Object.keys(commits)[4]; expect(commits[cherryPickCommitID].tag).toBe('v1.0'); expect(commits[cherryPickCommitID].branch).toBe('release'); @@ -731,8 +735,8 @@ describe('when parsing a gitGraph', function () { commit id: "D" `; - parser.parse(str); - const commits = parser.yy.getCommits(); + gitGraph.parse(str); + const commits = gitGraph.yy.getCommits(); const cherryPickCommitID = Object.keys(commits)[5]; expect(commits[cherryPickCommitID].tag).toBe('v2.1:ZERO'); expect(commits[cherryPickCommitID].branch).toBe('release'); @@ -755,8 +759,8 @@ describe('when parsing a gitGraph', function () { cherry-pick id:"M" tag:"" parent: "B" `; - parser.parse(str); - const commits = parser.yy.getCommits(); + gitGraph.parse(str); + const commits = gitGraph.yy.getCommits(); const cherryPickCommitID = Object.keys(commits)[5]; const cherryPickCommitID2 = Object.keys(commits)[7]; expect(commits[cherryPickCommitID].tag).toBe(''); @@ -766,7 +770,7 @@ describe('when parsing a gitGraph', function () { it('should fail cherry-picking of merge commits if the parent of merge commits is not specified', function () { expect(() => - parser + gitGraph .parse( `gitGraph commit id: "ZERO" @@ -790,7 +794,7 @@ describe('when parsing a gitGraph', function () { it('should fail cherry-picking of merge commits when the parent provided is not an immediate parent of cherry picked commit', function () { expect(() => - parser + gitGraph .parse( `gitGraph commit id: "ZERO" @@ -823,16 +827,11 @@ describe('when parsing a gitGraph', function () { merge testBranch `; - try { - parser.parse(str); - // Fail test if above expression doesn't throw anything. - expect(true).toBe(false); - } catch (e) { - expect(e.message).toBe( - 'Trying to create an existing branch. (Help: Either use a new name if you want create a new branch or try using "checkout main")' - ); - } + expect(() => gitGraph.parse(str)).toThrow( + 'Trying to create an existing branch. (Help: Either use a new name if you want create a new branch or try using "checkout main")' + ); }); + it('should throw error when try to branch existing branch: testBranch', function () { const str = `gitGraph commit @@ -844,16 +843,11 @@ describe('when parsing a gitGraph', function () { merge testBranch `; - try { - parser.parse(str); - // Fail test if above expression doesn't throw anything. - expect(true).toBe(false); - } catch (e) { - expect(e.message).toBe( - 'Trying to create an existing branch. (Help: Either use a new name if you want create a new branch or try using "checkout testBranch")' - ); - } + expect(() => gitGraph.parse(str)).toThrow( + 'Trying to create an existing branch. (Help: Either use a new name if you want create a new branch or try using "checkout testBranch")' + ); }); + it('should throw error when try to checkout unknown branch: testBranch', function () { const str = `gitGraph commit @@ -865,16 +859,11 @@ describe('when parsing a gitGraph', function () { merge testBranch `; - try { - parser.parse(str); - // Fail test if above expression doesn't throw anything. - expect(true).toBe(false); - } catch (e) { - expect(e.message).toBe( - 'Trying to checkout branch which is not yet created. (Help try using "branch testBranch")' - ); - } + expect(() => gitGraph.parse(str)).toThrow( + 'Trying to checkout branch which is not yet created. (Help try using "branch testBranch")' + ); }); + it('should throw error when trying to merge, when current branch has no commits', function () { const str = `gitGraph merge testBranch @@ -887,14 +876,11 @@ describe('when parsing a gitGraph', function () { merge testBranch `; - try { - parser.parse(str); - // Fail test if above expression doesn't throw anything. - expect(true).toBe(false); - } catch (e) { - expect(e.message).toBe('Incorrect usage of "merge". Current branch (main)has no commits'); - } + expect(() => gitGraph.parse(str)).toThrow( + 'Incorrect usage of "merge". Current branch (main)has no commits' + ); }); + it('should throw error when trying to merge unknown branch', function () { const str = `gitGraph commit @@ -908,16 +894,11 @@ describe('when parsing a gitGraph', function () { merge testBranch `; - try { - parser.parse(str); - // Fail test if above expression doesn't throw anything. - expect(true).toBe(false); - } catch (e) { - expect(e.message).toBe( - 'Incorrect usage of "merge". Branch to be merged (testBranch) does not exist' - ); - } + expect(() => gitGraph.parse(str)).toThrow( + 'Incorrect usage of "merge". Branch to be merged (testBranch) does not exist' + ); }); + it('should throw error when trying to merge branch to itself', function () { const str = `gitGraph commit @@ -925,13 +906,9 @@ describe('when parsing a gitGraph', function () { merge testBranch `; - try { - parser.parse(str); - // Fail test if above expression doesn't throw anything. - expect(true).toBe(false); - } catch (e) { - expect(e.message).toBe('Incorrect usage of "merge". Cannot merge a branch to itself'); - } + expect(() => gitGraph.parse(str)).toThrow( + 'Incorrect usage of "merge". Cannot merge a branch to itself' + ); }); it('should throw error when using existing id as merge ID', function () { @@ -944,16 +921,11 @@ describe('when parsing a gitGraph', function () { merge testBranch id: "1-111" `; - try { - parser.parse(str); - // Fail test if above expression doesn't throw anything. - expect(true).toBe(false); - } catch (e) { - expect(e.message).toBe( - 'Incorrect usage of "merge". Commit with id:1-111 already exists, use different custom Id' - ); - } + expect(() => gitGraph.parse(str)).toThrow( + 'Incorrect usage of "merge". Commit with id:1-111 already exists, use different custom Id' + ); }); + it('should throw error when trying to merge branches having same heads', function () { const str = `gitGraph commit @@ -962,14 +934,11 @@ describe('when parsing a gitGraph', function () { merge testBranch `; - try { - parser.parse(str); - // Fail test if above expression doesn't throw anything. - expect(true).toBe(false); - } catch (e) { - expect(e.message).toBe('Incorrect usage of "merge". Both branches have same head'); - } + expect(() => gitGraph.parse(str)).toThrow( + 'Incorrect usage of "merge". Both branches have same head' + ); }); + it('should throw error when trying to merge branch which has no commits', function () { const str = `gitGraph branch test1 @@ -979,16 +948,11 @@ describe('when parsing a gitGraph', function () { merge test1 `; - try { - parser.parse(str); - // Fail test if above expression doesn't throw anything. - expect(true).toBe(false); - } catch (e) { - expect(e.message).toBe( - 'Incorrect usage of "merge". Branch to be merged (test1) has no commits' - ); - } + expect(() => gitGraph.parse(str)).toThrow( + 'Incorrect usage of "merge". Branch to be merged (test1) has no commits' + ); }); + describe('accessibility', () => { it('should handle a title and a description (accDescr)', () => { const str = `gitGraph: @@ -996,9 +960,9 @@ describe('when parsing a gitGraph', function () { accDescr: This is a description commit `; - parser.parse(str); - expect(parser.yy.getAccTitle()).toBe('This is a title'); - expect(parser.yy.getAccDescription()).toBe('This is a description'); + gitGraph.parse(str); + expect(gitGraph.yy.getAccTitle()).toBe('This is a title'); + expect(gitGraph.yy.getAccDescription()).toBe('This is a description'); }); it('should handle a title and a multiline description (accDescr)', () => { const str = `gitGraph: @@ -1009,9 +973,9 @@ describe('when parsing a gitGraph', function () { } commit `; - parser.parse(str); - expect(parser.yy.getAccTitle()).toBe('This is a title'); - expect(parser.yy.getAccDescription()).toBe('This is a description\nusing multiple lines'); + gitGraph.parse(str); + expect(gitGraph.yy.getAccTitle()).toBe('This is a title'); + expect(gitGraph.yy.getAccDescription()).toBe('This is a description\nusing multiple lines'); }); }); }); From d464a6b413b9a421fd337972411730ec2f261309 Mon Sep 17 00:00:00 2001 From: Nicolas Chang <25302138+NicolasCwy@users.noreply.github.com> Date: Sat, 23 Mar 2024 19:07:15 +0800 Subject: [PATCH 4/4] Partial migrate for gitGraph AST --- .../mermaid/src/diagrams/git/gitGraphAst.ts | 274 +++++++++++------- 1 file changed, 166 insertions(+), 108 deletions(-) diff --git a/packages/mermaid/src/diagrams/git/gitGraphAst.ts b/packages/mermaid/src/diagrams/git/gitGraphAst.ts index 0997f65b1b..6e7d8e70a7 100644 --- a/packages/mermaid/src/diagrams/git/gitGraphAst.ts +++ b/packages/mermaid/src/diagrams/git/gitGraphAst.ts @@ -11,17 +11,43 @@ import { setDiagramTitle, getDiagramTitle, } from '../common/commonDb.js'; +import defaultConfig from '../../defaultConfig.js'; +import { CommitType } from './gitGraphTypes.js'; -let mainBranchName = getConfig().gitGraph.mainBranchName; -let mainBranchOrder = getConfig().gitGraph.mainBranchOrder; -let commits = {}; -let head = null; -let branchesConfig = {}; +// TODO +type CommitParentGraph = { + // First element is null, rest is parent +}; +type BranchConfig = { + name: string; + order: number; +}; +type DiagramOrientation = 'LR' | 'TB'; +type Commit = { + id: string; + message: string; + seq: number; + type: CommitType; + tag: string; + parents: (string | null)[]; + branch: string; + customType?: CommitType; + customId?: boolean; +}; + +const mainBranchName = + getConfig()?.gitGraph?.mainBranchName ?? defaultConfig.gitGraph.mainBranchName; + +const mainBranchOrder = + getConfig()?.gitGraph?.mainBranchOrder ?? defaultConfig.gitGraph.mainBranchOrder; +let commits: Record = {}; +let head: Commit | null = null; +let branchesConfig: Record = {}; branchesConfig[mainBranchName] = { name: mainBranchName, order: mainBranchOrder }; -let branches = {}; +let branches: Record = {}; // branch name to current commit id branches[mainBranchName] = head; -let curBranch = mainBranchName; -let direction = 'LR'; +let currBranchName = mainBranchName; +let direction: DiagramOrientation = 'LR'; let seq = 0; /** @@ -57,10 +83,10 @@ function getId() { // return currentCommit.id === otherCommit.id; // } -/** - * @param currentCommit - * @param otherCommit - */ +// /** +// * @param currentCommit +// * @param otherCommit +// */ // function isReachableFrom(currentCommit, otherCommit) { // const currentSeq = currentCommit.seq; // const otherSeq = otherCommit.seq; @@ -68,11 +94,7 @@ function getId() { // return false; // } -/** - * @param list - * @param fn - */ -function uniqBy(list, fn) { +function uniqBy(list: any[], fn: (item: any) => any): any[] { const recordMap = Object.create(null); return list.reduce((out, item) => { const key = fn(item); @@ -84,18 +106,23 @@ function uniqBy(list, fn) { }, []); } -export const setDirection = function (dir) { +export const setDirection = function (dir: DiagramOrientation) { direction = dir; }; + let options = {}; -export const setOptions = function (rawOptString) { +export const setOptions = function (rawOptString: string) { log.debug('options str', rawOptString); rawOptString = rawOptString && rawOptString.trim(); rawOptString = rawOptString || '{}'; try { options = JSON.parse(rawOptString); } catch (e) { - log.error('error while parsing gitGraph options', e.message); + if (e instanceof Error) { + log.error('error while parsing gitGraph options', e.message); + } else { + throw e; + } } }; @@ -103,35 +130,48 @@ export const getOptions = function () { return options; }; -export const commit = function (msg, id, type, tag) { +export const commit = function (msg: string, id: string, type: any, tag: string) { log.debug('Entering commit:', msg, id, type, tag); id = common.sanitizeText(id, getConfig()); msg = common.sanitizeText(msg, getConfig()); tag = common.sanitizeText(tag, getConfig()); - const commit = { + const commit: Commit = { id: id ? id : seq + '-' + getId(), message: msg, seq: seq++, - type: type ? type : commitType.NORMAL, + type: type ? type : CommitType.NORMAL, // TODO tag: tag ? tag : '', parents: head == null ? [] : [head.id], - branch: curBranch, + branch: currBranchName, }; head = commit; commits[commit.id] = commit; - branches[curBranch] = commit.id; + branches[currBranchName] = commit.id; log.debug('in pushCommit ' + commit.id); }; -export const branch = function (name, order) { +export const branch = function (name: string, order: string) { name = common.sanitizeText(name, getConfig()); if (branches[name] === undefined) { + const orderInt = parseInt(order, 10); + if (isNaN(orderInt)) { + throw new Error('Invalid order provided'); + // TODO - what is this hash? + // error.hash = { + // text: 'branch ' + name, + // token: 'branch ' + name, + // line: '1', + // loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 }, + // expected: ['Invalid order provided'], + // }; + // throw error; + } branches[name] = head != null ? head.id : null; - branchesConfig[name] = { name, order: order ? parseInt(order, 10) : null }; + branchesConfig[name] = { name, order: orderInt }; checkout(name); log.debug('in createBranch'); } else { - let error = new Error( + const error = new Error( 'Trying to create an existing branch. (Help: Either use a new name if you want create a new branch or try using "checkout ' + name + '")' @@ -147,81 +187,105 @@ export const branch = function (name, order) { } }; -export const merge = function (otherBranch, custom_id, override_type, custom_tag) { - otherBranch = common.sanitizeText(otherBranch, getConfig()); +export const merge = function ( + otherBranchName: string, + custom_id: string, + override_type: CommitType, + custom_tag: string +) { + otherBranchName = common.sanitizeText(otherBranchName, getConfig()); custom_id = common.sanitizeText(custom_id, getConfig()); - const currentCommit = commits[branches[curBranch]]; - const otherCommit = commits[branches[otherBranch]]; - if (curBranch === otherBranch) { - let error = new Error('Incorrect usage of "merge". Cannot merge a branch to itself'); + const currBranch = branches[currBranchName]; + const otherBranch = branches[otherBranchName]; + + let currentCommit; + let otherCommit; + + if (currBranch != null) { + currentCommit = commits[currBranch]; + } + + if (otherBranch != null) { + otherCommit = commits[otherBranch]; + } + + if (currBranchName === otherBranchName) { + const error = new Error('Incorrect usage of "merge". Cannot merge a branch to itself'); error.hash = { - text: 'merge ' + otherBranch, - token: 'merge ' + otherBranch, + text: 'merge ' + otherBranchName, + token: 'merge ' + otherBranchName, line: '1', loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 }, expected: ['branch abc'], }; throw error; } else if (currentCommit === undefined || !currentCommit) { - let error = new Error( - 'Incorrect usage of "merge". Current branch (' + curBranch + ')has no commits' + const error = new Error( + 'Incorrect usage of "merge". Current branch (' + currBranchName + ')has no commits' ); error.hash = { - text: 'merge ' + otherBranch, - token: 'merge ' + otherBranch, + text: 'merge ' + otherBranchName, + token: 'merge ' + otherBranchName, line: '1', loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 }, expected: ['commit'], }; throw error; - } else if (branches[otherBranch] === undefined) { - let error = new Error( - 'Incorrect usage of "merge". Branch to be merged (' + otherBranch + ') does not exist' + } else if (branches[otherBranchName] === undefined) { + const error = new Error( + 'Incorrect usage of "merge". Branch to be merged (' + otherBranchName + ') does not exist' ); error.hash = { - text: 'merge ' + otherBranch, - token: 'merge ' + otherBranch, + text: 'merge ' + otherBranchName, + token: 'merge ' + otherBranchName, line: '1', loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 }, - expected: ['branch ' + otherBranch], + expected: ['branch ' + otherBranchName], }; throw error; } else if (otherCommit === undefined || !otherCommit) { - let error = new Error( - 'Incorrect usage of "merge". Branch to be merged (' + otherBranch + ') has no commits' + const error = new Error( + 'Incorrect usage of "merge". Branch to be merged (' + otherBranchName + ') has no commits' ); error.hash = { - text: 'merge ' + otherBranch, - token: 'merge ' + otherBranch, + text: 'merge ' + otherBranchName, + token: 'merge ' + otherBranchName, line: '1', loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 }, expected: ['"commit"'], }; throw error; } else if (currentCommit === otherCommit) { - let error = new Error('Incorrect usage of "merge". Both branches have same head'); + const error = new Error('Incorrect usage of "merge". Both branches have same head'); error.hash = { - text: 'merge ' + otherBranch, - token: 'merge ' + otherBranch, + text: 'merge ' + otherBranchName, + token: 'merge ' + otherBranchName, line: '1', loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 }, expected: ['branch abc'], }; throw error; } else if (custom_id && commits[custom_id] !== undefined) { - let error = new Error( + const error = new Error( 'Incorrect usage of "merge". Commit with id:' + custom_id + ' already exists, use different custom Id' ); error.hash = { - text: 'merge ' + otherBranch + custom_id + override_type + custom_tag, - token: 'merge ' + otherBranch + custom_id + override_type + custom_tag, + text: 'merge ' + otherBranchName + custom_id + override_type + custom_tag, + token: 'merge ' + otherBranchName + custom_id + override_type + custom_tag, line: '1', loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 }, expected: [ - 'merge ' + otherBranch + ' ' + custom_id + '_UNIQUE ' + override_type + ' ' + custom_tag, + 'merge ' + + otherBranchName + + ' ' + + custom_id + + '_UNIQUE ' + + override_type + + ' ' + + custom_tag, ], }; @@ -236,26 +300,26 @@ export const merge = function (otherBranch, custom_id, override_type, custom_tag // head = commits[branches[curBranch]]; // } else { // create merge commit - const commit = { + const commit: Commit = { id: custom_id ? custom_id : seq + '-' + getId(), - message: 'merged branch ' + otherBranch + ' into ' + curBranch, + message: 'merged branch ' + otherBranchName + ' into ' + currBranchName, seq: seq++, - parents: [head == null ? null : head.id, branches[otherBranch]], - branch: curBranch, - type: commitType.MERGE, + parents: [head == null ? null : head.id, branches[otherBranchName]], + branch: currBranchName, + type: CommitType.MERGE, customType: override_type, customId: custom_id ? true : false, tag: custom_tag ? custom_tag : '', }; head = commit; commits[commit.id] = commit; - branches[curBranch] = commit.id; + branches[currBranchName] = commit.id; // } log.debug(branches); log.debug('in mergeBranch'); }; -export const cherryPick = function (sourceId, targetId, tag, parentCommitId) { +export const cherryPick = function (sourceId: string, targetId, tag, parentCommitId) { log.debug('Entering cherryPick:', sourceId, targetId, tag); sourceId = common.sanitizeText(sourceId, getConfig()); targetId = common.sanitizeText(targetId, getConfig()); @@ -263,7 +327,7 @@ export const cherryPick = function (sourceId, targetId, tag, parentCommitId) { parentCommitId = common.sanitizeText(parentCommitId, getConfig()); if (!sourceId || commits[sourceId] === undefined) { - let error = new Error( + const error = new Error( 'Incorrect usage of "cherryPick". Source commit id should exist and provided' ); error.hash = { @@ -275,19 +339,19 @@ export const cherryPick = function (sourceId, targetId, tag, parentCommitId) { }; throw error; } - let sourceCommit = commits[sourceId]; - let sourceCommitBranch = sourceCommit.branch; + const sourceCommit = commits[sourceId]; + const sourceCommitBranch = sourceCommit.branch; if ( parentCommitId && !(Array.isArray(sourceCommit.parents) && sourceCommit.parents.includes(parentCommitId)) ) { - let error = new Error( + const error = new Error( 'Invalid operation: The specified parent commit is not an immediate parent of the cherry-picked commit.' ); throw error; } - if (sourceCommit.type === commitType.MERGE && !parentCommitId) { - let error = new Error( + if (sourceCommit.type === CommitType.MERGE && !parentCommitId) { + const error = new Error( 'Incorrect usage of cherry-pick: If the source commit is a merge commit, an immediate parent commit must be specified.' ); throw error; @@ -295,8 +359,8 @@ export const cherryPick = function (sourceId, targetId, tag, parentCommitId) { if (!targetId || commits[targetId] === undefined) { // cherry-pick source commit to current branch - if (sourceCommitBranch === curBranch) { - let error = new Error( + if (sourceCommitBranch === currBranchName) { + const error = new Error( 'Incorrect usage of "cherryPick". Source commit is already on current branch' ); error.hash = { @@ -308,10 +372,18 @@ export const cherryPick = function (sourceId, targetId, tag, parentCommitId) { }; throw error; } - const currentCommit = commits[branches[curBranch]]; + + const currBranch = branches[currBranchName]; + + let currentCommit; + + if (currBranch != null) { + currentCommit = commits[currBranch]; + } + if (currentCommit === undefined || !currentCommit) { - let error = new Error( - 'Incorrect usage of "cherry-pick". Current branch (' + curBranch + ')has no commits' + const error = new Error( + 'Incorrect usage of "cherry-pick". Current branch (' + currBranchName + ')has no commits' ); error.hash = { text: 'cherryPick ' + sourceId + ' ' + targetId, @@ -324,28 +396,28 @@ export const cherryPick = function (sourceId, targetId, tag, parentCommitId) { } const commit = { id: seq + '-' + getId(), - message: 'cherry-picked ' + sourceCommit + ' into ' + curBranch, + message: 'cherry-picked ' + sourceCommit + ' into ' + currBranchName, seq: seq++, parents: [head == null ? null : head.id, sourceCommit.id], - branch: curBranch, - type: commitType.CHERRY_PICK, + branch: currBranchName, + type: CommitType.CHERRY_PICK, tag: tag ?? `cherry-pick:${sourceCommit.id}${ - sourceCommit.type === commitType.MERGE ? `|parent:${parentCommitId}` : '' + sourceCommit.type === CommitType.MERGE ? `|parent:${parentCommitId}` : '' }`, }; head = commit; commits[commit.id] = commit; - branches[curBranch] = commit.id; + branches[currBranchName] = commit.id; log.debug(branches); log.debug('in cherryPick'); } }; -export const checkout = function (branch) { +export const checkout = function (branch: string) { branch = common.sanitizeText(branch, getConfig()); - if (branches[branch] === undefined) { - let error = new Error( + if (branches[branch] === undefined || ) { + const error = new Error( 'Trying to checkout branch which is not yet created. (Help try using "branch ' + branch + '")' ); error.hash = { @@ -359,8 +431,8 @@ export const checkout = function (branch) { //branches[branch] = head != null ? head.id : null; //log.debug('in createBranch'); } else { - curBranch = branch; - const id = branches[curBranch]; + currBranchName = branch; + const id = branches[currBranchName]; head = commits[id]; } }; @@ -384,12 +456,7 @@ export const checkout = function (branch) { // branches[curBranch] = commit.id; // }; -/** - * @param arr - * @param key - * @param newVal - */ -function upsert(arr, key, newVal) { +function upsert(arr: any[], key: any, newVal: any) { const index = arr.indexOf(key); if (index === -1) { arr.push(newVal); @@ -398,8 +465,7 @@ function upsert(arr, key, newVal) { } } -/** @param commitArr */ -function prettyPrintCommitHistory(commitArr) { +function prettyPrintCommitHistory(commitArr: Commit[]) { const commit = commitArr.reduce((out, commit) => { if (out.seq > commit.seq) { return out; @@ -415,7 +481,7 @@ function prettyPrintCommitHistory(commitArr) { } }); const label = [line, commit.id, commit.seq]; - for (let branch in branches) { + for (const branch in branches) { if (branches[branch] === commit.id) { label.push(branch); } @@ -444,13 +510,13 @@ export const prettyPrint = function () { export const clear = function () { commits = {}; head = null; - let mainBranch = getConfig().gitGraph.mainBranchName; - let mainBranchOrder = getConfig().gitGraph.mainBranchOrder; + const mainBranch = getConfig().gitGraph?.mainBranchName ?? defaultConfig.gitGraph.mainBranchName; + const mainBranchOrder = getConfig().gitGraph?.mainBranchOrder ?? defaultConfig.gitGraph.mainBranchOrder; branches = {}; branches[mainBranch] = null; branchesConfig = {}; branchesConfig[mainBranch] = { name: mainBranch, order: mainBranchOrder }; - curBranch = mainBranch; + currBranchName = mainBranch; seq = 0; commonClear(); }; @@ -463,7 +529,7 @@ export const getBranchesAsObjArray = function () { } return { ...branchConfig, - order: parseFloat(`0.${i}`, 10), + order: parseFloat(`0.${i}`), }; }) .sort((a, b) => a.order - b.order) @@ -489,7 +555,7 @@ export const getCommitsArray = function () { return commitArr; }; export const getCurrentBranch = function () { - return curBranch; + return currBranchName; }; export const getDirection = function () { return direction; @@ -498,14 +564,6 @@ export const getHead = function () { return head; }; -export const commitType = { - NORMAL: 0, - REVERSE: 1, - HIGHLIGHT: 2, - MERGE: 3, - CHERRY_PICK: 4, -}; - export default { getConfig: () => getConfig().gitGraph, setDirection, @@ -532,5 +590,5 @@ export default { setAccDescription, setDiagramTitle, getDiagramTitle, - commitType, + commitType: CommitType, };