Skip to content

Commit

Permalink
feat: Add support for parsing and loading an object diff representation
Browse files Browse the repository at this point in the history
Signed-off-by: Yoriyasu Yano <[email protected]>
  • Loading branch information
yorinasub17 committed Oct 25, 2023
1 parent 6703289 commit 67d1c28
Show file tree
Hide file tree
Showing 6 changed files with 369 additions and 74 deletions.
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"bugs": {
"url": "https://github.com/fensak-io/reng/issues"
},
"type": "module",
"source": "src/index.ts",
"main": "./dist/index.cjs",
"module": "./dist/module.mjs",
Expand Down Expand Up @@ -44,10 +45,15 @@
"@babel/preset-typescript": "^7.23.2",
"@fensak-io/front-matter": "^1.0.0",
"@octokit/rest": "^20.0.2",
"babel-preset-minify": "^0.5.2"
"babel-preset-minify": "^0.5.2",
"json5": "^2.2.3",
"microdiff": "^1.3.2",
"toml": "^3.0.0",
"yaml": "^2.3.3"
},
"devDependencies": {
"@jest/globals": "^29.7.0",
"@octokit/types": "^12.1.0",
"@parcel/config-default": "2.9.3",
"@parcel/packager-ts": "2.9.3",
"@parcel/transformer-typescript-types": "2.9.3",
Expand Down
47 changes: 33 additions & 14 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions src/engine/interpreter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ test("sanity check", async () => {
additions: 0,
deletions: 0,
diff: [],
objectDiff: null,
},
],
nullMeta,
Expand All @@ -51,6 +52,7 @@ test("sanity check old version", async () => {
additions: 0,
deletions: 0,
diff: [],
objectDiff: null,
},
],
nullMeta,
Expand All @@ -74,6 +76,7 @@ test("ES5 minify", async () => {
additions: 0,
deletions: 0,
diff: [],
objectDiff: null,
},
],
nullMeta,
Expand All @@ -99,6 +102,7 @@ test("ES6 support", async () => {
additions: 0,
deletions: 0,
diff: [],
objectDiff: null,
},
],
nullMeta,
Expand Down Expand Up @@ -129,6 +133,7 @@ function main(inp: IPatch[], metadata: IChangeSetMetadata) {
additions: 0,
deletions: 0,
diff: [],
objectDiff: null,
},
],
nullMeta,
Expand Down Expand Up @@ -281,6 +286,7 @@ test("XMLHTTPRequest not supported", async () => {
additions: 0,
deletions: 0,
diff: [],
objectDiff: null,
},
],
nullMeta,
Expand All @@ -307,6 +313,7 @@ test("fetch is not supported", async () => {
additions: 0,
deletions: 0,
diff: [],
objectDiff: null,
},
],
nullMeta,
Expand All @@ -331,6 +338,7 @@ test("process is not supported", async () => {
additions: 0,
deletions: 0,
diff: [],
objectDiff: null,
},
],
nullMeta,
Expand All @@ -355,6 +363,7 @@ test("Deno is not supported", async () => {
additions: 0,
deletions: 0,
diff: [],
objectDiff: null,
},
],
nullMeta,
Expand Down
23 changes: 23 additions & 0 deletions src/engine/patch_types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Fensak, LLC.
// SPDX-License-Identifier: AGPL-3.0-or-later OR BUSL-1.1

import type { Difference } from "microdiff";

/**
* The operation on a line in a hunk of a patch.
* @property Unknown Unknown operation.
Expand Down Expand Up @@ -66,7 +68,10 @@ export enum PatchOp {
* <SOURCE_PLATFORM>:<CONTENTS_URL_HASH>.
* @property path The relative path (from the root of the repo) to the file that was updated in the patch.
* @property op The operation that was done on the file in the patch.
* @property additions The number of lines that were added in this patch.
* @property deletions The number of lines that were removed in this patch.
* @property diff The list of diffs, organized into hunks.
* @property objectDiff If the file represents a parsable data file (e.g., json, yaml, toml), this will contain the object level diff.
*/
export interface IPatch {
contentsID: string;
Expand All @@ -75,6 +80,24 @@ export interface IPatch {
additions: number;
deletions: number;
diff: IHunk[];
objectDiff: IObjectDiff | null;
}

/**
* Represents a diff of the object representation of a file. The specific diff returns a list of object patches that
* contains the keys that were added, removed, or updated. Note that the difference is only populated for updated
* objects - if the file was inserted or deleted, then the diff will be empty.
* @property previous The object representation of the data in the file before the change.
* @property current The object representation of the data in the file after the change.
* @property diff The difference across the two objects.
*/
export interface IObjectDiff {
// eslint-disable-next-line no-var,@typescript-eslint/no-explicit-any
previous: any;
// eslint-disable-next-line no-var,@typescript-eslint/no-explicit-any
current: any;
// eslint-disable-next-line no-var,@typescript-eslint/no-explicit-any
diff: Difference[];
}

/**
Expand Down
Loading

0 comments on commit 67d1c28

Please sign in to comment.