Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,47 @@ node.update({

The `update` method reconciles the new content with the existing document, preserving comments, indentation, and spacing.

To merge two documents while preserving comments, use the `merge` method:

```ts

const destinationCode = `{
// Destination pre-foo comment
"foo": "value",
// Destination post-foo comment
"baz": [1, 2, 3]
}
`;

const sourceCode = `{
/* Source pre-bar comment */
"bar": 123, /* Inline comment */
/* Source post-bar comment */
"baz": true /* Another inline comment */
}
`;

const source = JsonParser.parse(sourceCode, JsonObjectNode);
const destination = JsonParser.parse(destinationCode, JsonObjectNode);

console.log(JsonObjectNode.merge(source, destination).toString());
```

Output:

```json5
{
// Destination pre-foo comment
"foo": "value",
/* Source pre-bar comment */
"bar": 123, /* Inline comment */
/* Source post-bar comment */
"baz": true /* Another inline comment */
}
```

The `merge` method removes any existing destination properties that clash with source, along with their leading/trailing trivia, to avoid duplicate keys.

## Contributing

Contributions are welcome!
Expand Down
41 changes: 41 additions & 0 deletions src/node/objectNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {JsonPrimitiveNode, JsonStringNode} from './primitiveNode';
import {JsonValueFactory} from './factory';
import {JsonIdentifierNode} from './identifierNode';
import {JsonError} from '../error';
import { JsonTokenNode } from './tokenNode';
import { JsonTokenType } from '../token';

export interface JsonObjectDefinition extends JsonCompositeDefinition {
readonly properties: readonly JsonPropertyNode[];
Expand All @@ -33,6 +35,45 @@ export class JsonObjectNode extends JsonStructureNode implements JsonCompositeDe
});
}

public static merge(source: JsonObjectNode, destination: JsonObjectNode): JsonObjectNode {
const isTrivia = (node: JsonNode): boolean => {
if (!(node instanceof JsonTokenNode)) {
return false;
}
return node.isType(JsonTokenType.LINE_COMMENT)
|| node.isType(JsonTokenType.BLOCK_COMMENT)
|| node.isType(JsonTokenType.NEWLINE)
|| node.isType(JsonTokenType.WHITESPACE);
}

for (const property of source.properties) {
const key = property.key.toJSON();
const index = destination.children.findIndex(
(node) => node instanceof JsonPropertyNode && node.key.toJSON() === key,
);

if (index >= 0) {
let startIndex = index;
let endIndex = index;
while (startIndex > 0 && isTrivia(destination.children[startIndex - 1])) {
startIndex--;
}
while (endIndex + 1 < destination.children.length && isTrivia(destination.children[endIndex + 1])) {
endIndex++;
}
destination.children.splice(startIndex, endIndex - startIndex + 1);
destination.delete(key);
}
}

for (const property of source.properties) {
destination.set(property.key, property.value);
}

destination.children.push(...source.children);
return destination;
}

public update(other: JsonValueNode|JsonValue): JsonValueNode {
if (!(other instanceof JsonValueNode)) {
if (typeof other !== 'object' || other === null || Array.isArray(other)) {
Expand Down
56 changes: 56 additions & 0 deletions test/node/objectNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,4 +381,60 @@ describe('ObjectNode', () => {
key: value,
});
});

it.each([
{
sourceCode: `{
/* Source pre-bar comment */
"bar": 123, /* Inline comment */
/* Source post-bar comment */
"baz": true /* Another inline comment */ }`,
destinationCode: `{
// Destination pre-foo comment
"foo": "value",
// Destination post-foo comment
"baz": [1, 2, 3] }`,
expected: `{
// Destination pre-foo comment
"foo": "value",
/* Source pre-bar comment */
"bar": 123, /* Inline comment */
/* Source post-bar comment */
"baz": true /* Another inline comment */ }`,
},
{
sourceCode: `{
/* Source pre-bar comment */
"bar": 123, /* Inline comment */
/* Source post-bar comment */
"baz": true /* Another inline comment */ }`,
destinationCode: `{
// Destination pre-foo comment
"foo": "value",
// Destination post-foo comment
"fizz": 42 }`,
expected: `{
// Destination pre-foo comment
"foo": "value",
// Destination post-foo comment
"fizz": 42,
/* Source pre-bar comment */
"bar": 123, /* Inline comment */
/* Source post-bar comment */
"baz": true /* Another inline comment */ }`,
},
])(
'should merge two JsonObjectNode',
({ sourceCode, destinationCode, expected }) => {
const source = JsonParser.parse(sourceCode, JsonObjectNode);
const destination = JsonParser.parse(destinationCode, JsonObjectNode);

const node = JsonObjectNode.merge(source, destination);

const removeWhiteSpaces = (str: string) => str.replace(/\s+/g, ' ');
expect(removeWhiteSpaces(node.toString())).toStrictEqual(
removeWhiteSpaces(expected)
);
}
);
});