@hero-design/snowflake-guard
Version:
A hero-design bot detecting snowflake usage
41 lines (40 loc) • 1.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDiffLocs = void 0;
const DIFF_LOCS_REGEX = /@@(.*)@@/g;
const getDiffLocs = (filePatch) => {
const locs = [];
const patches = {};
if (!filePatch) {
return locs;
}
// Split file patch into lines
const splitPatch = filePatch.split('\n');
// Get patches
// Example: @@ -1,2 +1,3 @@
let tempPatch = '';
splitPatch.forEach((linePatch) => {
const matchPatch = linePatch.match(DIFF_LOCS_REGEX);
if (matchPatch) {
[tempPatch] = matchPatch[0].split('+')[1].split(' ');
patches[tempPatch] = [];
}
else {
patches[tempPatch].push(linePatch);
}
});
// Get diff locations from patches
Object.entries(patches).forEach(([loc, lines]) => {
const [startLoc, numberOfLines] = loc
.split(',')
.map((l) => parseInt(l, 10));
const endLoc = startLoc + numberOfLines - 1;
const firstDiffLineIdx = lines.findIndex((l) => l.startsWith('+') || l.startsWith('-'));
const lastDiffLineIdx = lines
.reverse()
.findIndex((l) => l.startsWith('+') || l.startsWith('-'));
locs.push([startLoc + firstDiffLineIdx, endLoc - lastDiffLineIdx]);
});
return locs;
};
exports.getDiffLocs = getDiffLocs;