-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
181 lines (161 loc) · 6.76 KB
/
index.js
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
module.exports = {
rules: {
transaction: {
create(context) {
const codePathExpressionMapStack = []
const codePathSegmentStack = []
return {
onCodePathSegmentStart: segment => codePathSegmentStack.push(segment),
onCodePathSegmentEnd: () => codePathSegmentStack.pop(),
onCodePathStart: () => codePathExpressionMapStack.push(new Map()),
onCodePathEnd(codePath, codePathNode) {
const expressionsMap = codePathExpressionMapStack.pop();
const cyclic = new Set()
function countPathsFromStart(segment, pathHistory) {
const { cache } = countPathsFromStart;
let paths = cache.get(segment.id);
const pathList = new Set(pathHistory);
// If `pathList` includes the current segment then we've found a cycle!
// We need to fill `cyclic` with all segments inside cycle
if (pathList.has(segment.id)) {
const pathArray = [...pathList];
const cyclicSegments = pathArray.slice(
pathArray.indexOf(segment.id) + 1,
);
for (const cyclicSegment of cyclicSegments) {
cyclic.add(cyclicSegment);
}
return BigInt('0');
}
// add the current segment to pathList
pathList.add(segment.id);
// We have a cached `paths`. Return it.
if (paths !== undefined) {
return paths;
}
if (codePath.thrownSegments.includes(segment)) {
paths = BigInt('0');
} else if (segment.prevSegments.length === 0) {
paths = BigInt('1');
} else {
paths = BigInt('0');
for (const prevSegment of segment.prevSegments) {
paths += countPathsFromStart(prevSegment, pathList);
}
}
// If our segment is reachable then there should be at least one path
// to it from the start of our code path.
if (segment.reachable && paths === BigInt('0')) {
cache.delete(segment.id);
} else {
cache.set(segment.id, paths);
}
return paths;
}
countPathsFromStart.cache = new Map();
countPathsToEnd.cache = new Map();
function countPathsToEnd(segment, pathHistory) {
const { cache } = countPathsToEnd;
let paths = cache.get(segment.id);
const pathList = new Set(pathHistory);
// If `pathList` includes the current segment then we've found a cycle!
// We need to fill `cyclic` with all segments inside cycle
if (pathList.has(segment.id)) {
const pathArray = Array.from(pathList);
const cyclicSegments = pathArray.slice(
pathArray.indexOf(segment.id) + 1,
);
for (const cyclicSegment of cyclicSegments) {
cyclic.add(cyclicSegment);
}
return BigInt('0');
}
// add the current segment to pathList
pathList.add(segment.id);
// We have a cached `paths`. Return it.
if (paths !== undefined) {
return paths;
}
if (codePath.thrownSegments.includes(segment)) {
paths = BigInt('0');
} else if (segment.nextSegments.length === 0) {
paths = BigInt('1');
} else {
paths = BigInt('0');
for (const nextSegment of segment.nextSegments) {
paths += countPathsToEnd(nextSegment, pathList);
}
}
cache.set(segment.id, paths);
return paths;
}
const segmentsWithTransaction = []
for (const [segment, expressions] of expressionsMap) {
if (!segment.reachable) {
continue;
}
for (const hook of expressions) {
if (
hook.type === "MemberExpression" &&
(hook.object.name === "sequelize" ||
(hook.object.type === "MemberExpression" &&
hook.object.property.name === "sequelize")) &&
hook.property.name === "transaction"
) {
segment.hook = hook
segmentsWithTransaction.push(segment)
}
}
}
// Get final segments that are whitin a transaction scope
const finalPathsWithinTransactionScope = codePath.finalSegments
.filter(s => {
let n = [s]
while (n.length) {
if (n.some(s => segmentsWithTransaction.some(s1 => s1.id === s.id))) {
return true
}
n = n.map(s => s.prevSegments).flatMap(l => l)
}
return false
})
const idsWithTransactons = segmentsWithTransaction.map(s => s.id)
const endingOperations = ["rollback", "commit"]
for (const seg of finalPathsWithinTransactionScope) {
// The final segmentation must have (or have a parent that) called some ending operation in the transaction
function traverse(seg) {
const expressions = expressionsMap.get(seg)
if (expressions && expressions.some(exp => endingOperations.includes((exp.property || {}).name))) {
return expressions.find(exp => endingOperations.includes((exp.property || {}).name))
}
if (idsWithTransactons.includes(seg.id)) {
return false
}
const s = seg.prevSegments[0]
s.hook = seg.hook
return traverse(s)
}
const callToClose = traverse(seg)
if (!callToClose) {
context.report({ node: codePathNode, message: `Transaction in this context is not closed at some path.`})
}
}
},
CallExpression: (node) => {
const expressionMap = last(codePathExpressionMapStack);
const codePathSegment = last(codePathSegmentStack);
let expressions = expressionMap.get(codePathSegment);
if (!expressions) {
expressions = [];
expressionMap.set(codePathSegment, expressions);
}
expressions.push(node.callee);
},
}
},
},
},
};
function last(array) {
return array[array.length - 1];
}