forked from Azure/azure-sdk-tools
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRestLevelClientChangelogPostProcessor.ts
More file actions
122 lines (112 loc) · 5.01 KB
/
RestLevelClientChangelogPostProcessor.ts
File metadata and controls
122 lines (112 loc) · 5.01 KB
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
import { InlineDeclarationNameSetMessage, NodeContext } from 'typescript-codegen-breaking-change-detector';
import { Changelog, ChangelogItem } from './changelogGenerator';
export class RestLevelClientChangelogPostProcessor {
private changelog: Changelog;
private message: InlineDeclarationNameSetMessage;
constructor(changelog: Changelog, message: InlineDeclarationNameSetMessage) {
this.changelog = changelog;
this.message = message;
}
public run() {
this.handleChangelogItems(this.changelog.addedOperationGroup);
this.handleChangelogItems(this.changelog.removedOperationGroup);
this.handleChangelogItems(this.changelog.addedOperation);
this.handleChangelogItems(this.changelog.addedInterface);
this.handleChangelogItems(this.changelog.addedClass);
this.handleChangelogItems(this.changelog.addedTypeAlias);
this.handleChangelogItems(this.changelog.interfaceAddOptionalParam);
this.handleChangelogItems(this.changelog.interfaceParamTypeExtended);
this.handleChangelogItems(this.changelog.typeAliasAddInherit);
this.handleChangelogItems(this.changelog.typeAliasAddParam);
this.handleChangelogItems(this.changelog.addedEnum);
this.handleChangelogItems(this.changelog.addedEnumValue);
this.handleChangelogItems(this.changelog.addedFunction);
this.handleChangelogItems(this.changelog.removedOperation);
this.handleChangelogItems(this.changelog.operationSignatureChange);
this.handleChangelogItems(this.changelog.deletedClass);
this.handleChangelogItems(this.changelog.classSignatureChange);
this.handleChangelogItems(this.changelog.interfaceParamDelete);
this.handleChangelogItems(this.changelog.interfaceParamAddRequired);
this.handleChangelogItems(this.changelog.interfaceParamTypeChanged);
this.handleChangelogItems(this.changelog.interfaceParamChangeRequired);
this.handleChangelogItems(this.changelog.classParamDelete);
this.handleChangelogItems(this.changelog.classParamChangeRequired);
this.handleChangelogItems(this.changelog.typeAliasDeleteInherit);
this.handleChangelogItems(this.changelog.typeAliasParamDelete);
this.handleChangelogItems(this.changelog.typeAliasAddRequiredParam);
this.handleChangelogItems(this.changelog.typeAliasParamChangeRequired);
this.handleChangelogItems(this.changelog.removedEnum);
this.handleChangelogItems(this.changelog.removedEnumValue);
this.handleChangelogItems(this.changelog.removedFunction);
}
private getCurrentNodeContext(name: string | undefined): NodeContext | undefined {
if (!name) return undefined;
return this.message.current.get(name);
}
private getBaselineNodeContext(name: string | undefined): NodeContext | undefined {
if (!name) return undefined;
return this.message.baseline.get(name);
}
private findCompatibleNodeContext(
inputContext: NodeContext,
contextMapToFind: Map<string, NodeContext>,
checkAssignableFromInputToFound: boolean
): NodeContext | undefined {
for (const [_, foundContext] of contextMapToFind) {
const isCompatible = checkAssignableFromInputToFound
? inputContext.node.getType().isAssignableTo(foundContext.node.getType())
: foundContext.node.getType().isAssignableTo(inputContext.node.getType());
if (isCompatible) return foundContext;
}
return undefined;
}
private tryIgnoreInlineTypeInChangelogItem(
inputContext: NodeContext,
nodeContextMapToFind: Map<string, NodeContext>,
checkAssignableFromInputToFound: boolean,
item: ChangelogItem
) {
if (!inputContext) return;
const foundContext = this.findCompatibleNodeContext(
inputContext,
nodeContextMapToFind,
checkAssignableFromInputToFound
);
if (foundContext) {
inputContext.used = true;
foundContext.used = true;
item.toDelete = true;
}
return;
}
private handleChangelogItems(items: ChangelogItem[]) {
items.forEach((item) => {
if (!item.oldName && !item.newName) return;
if (item.newName && item.oldName) {
const currentContext = this.getCurrentNodeContext(item.newName);
if (!currentContext) return;
const baselineContext = this.getBaselineNodeContext(item.oldName);
if (!baselineContext) return;
const currentType = currentContext.node.getType();
const baselineType = baselineContext.node.getType();
if (currentType.isAssignableTo(baselineType)) {
item.toDelete = true;
currentContext.used = true;
baselineContext.used = true;
}
return;
}
if (item.newName) {
const inputContext = this.getCurrentNodeContext(item.newName);
if (!inputContext) return;
this.tryIgnoreInlineTypeInChangelogItem(inputContext, this.message.baseline, true, item);
return;
}
// item.baselineName exists
const inputContext = this.getBaselineNodeContext(item.oldName);
if (!inputContext) return;
this.tryIgnoreInlineTypeInChangelogItem(inputContext, this.message.current, false, item);
return;
});
}
}