This repository has been archived by the owner on Jun 22, 2020. It is now read-only.
/
UnifiedFileParser.ts
127 lines (103 loc) · 4.13 KB
/
UnifiedFileParser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* Copyright © 2019 Atomist, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
FileParser,
ProjectFile,
} from "@atomist/automation-client";
import { TreeNode } from "@atomist/tree-path";
import * as _ from "lodash";
import * as unified from "unified";
export abstract class UnifiedFileParser<TN extends UnifiedTreeNode> implements FileParser<TN> {
// TODO type the parser
protected constructor(public readonly rootName: string,
private readonly parser: any) {
}
public async toAst(f: ProjectFile): Promise<TN> {
const parser = unified()
.use(this.parser);
const content = await f.getContent();
const parsed = parser.parse(content);
// console.log(JSON.stringify(parsed));
const n = toUnifiedTreeNode(parsed as UnifiedNode, this.enrich);
const enriched = this.enrich(n, parsed as UnifiedNode);
eatYourSiblings(enriched, this.shouldBeNested);
adjustOffsets(enriched);
populateValues(enriched, content);
attachParents(enriched);
return enriched;
}
protected enrich(tn: TreeNode, from: UnifiedNode): TN {
// Do nothing
return tn as TN;
}
protected shouldBeNested(shouldThisOne: TN, beNestedUnder: TN): boolean {
return false;
}
}
export type UnifiedTreeNode = TreeNode & { $endOffset: number };
function toUnifiedTreeNode<TN extends UnifiedTreeNode>(unifiedNode: UnifiedNode,
enrich: (utn: UnifiedTreeNode, from: UnifiedNode) => TN): TN {
const startOffset = unifiedNode.position.start.offset;
const endOffset = unifiedNode.position.end.offset;
const unifiedTreeNode = {
$name: unifiedNode.type,
$offset: startOffset,
$children: unifiedNode.children ? unifiedNode.children.map(n => toUnifiedTreeNode(n, enrich)) : [],
$endOffset: endOffset,
};
const enriched = enrich(unifiedTreeNode, unifiedNode);
return enriched;
}
function adjustOffsets(utn: UnifiedTreeNode): void {
utn.$children.forEach(adjustOffsets);
utn.$endOffset = Math.max(utn.$endOffset, ...utn.$children.map(c => (c as UnifiedTreeNode).$endOffset));
}
function populateValues(utn: UnifiedTreeNode, fullContent: string): void {
utn.$value = fullContent.slice(utn.$offset, utn.$endOffset);
utn.$children.forEach(c => populateValues(c as UnifiedTreeNode, fullContent));
}
function attachParents(tree: TreeNode): void {
tree.$children.forEach(c => c.$parent = tree);
tree.$children.forEach(attachParents);
}
// mutates the tree
export function eatYourSiblings<T extends TreeNode>(
tree: T,
shouldBeNested: (shouldThisOne: T, beNestedUnder: T) => boolean): void {
let i = 0;
const ammendedChildren = [];
while (i < tree.$children.length) {
const hungryChild = tree.$children[i];
const edibleSiblings = _.takeWhile(tree.$children.slice(i + 1),
sib => shouldBeNested(sib as T, hungryChild as T));
edibleSiblings.forEach(sib => hungryChild.$children.push(sib)); // might be none
ammendedChildren.push(hungryChild);
i = i + 1 + edibleSiblings.length;
}
tree.$children = ammendedChildren;
tree.$children.forEach(c => eatYourSiblings(c as T, shouldBeNested));
}
/**
* Node returned by Unified
*/
// tslint:disable-next-line:no-implicit-dependencies
import * as Unist from "unist";
export interface UnifiedNode extends Unist.Parent {
type: string;
// position: { start: { offset: number, line: number, column: number }, end: { offset: number } };
children: UnifiedNode[];
value: string;
}