Skip to content

Commit

Permalink
refactor: simplifies parseRule
Browse files Browse the repository at this point in the history
  • Loading branch information
sverweij committed Jun 4, 2024
1 parent 1ace5d2 commit 5591cee
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 60 deletions.
27 changes: 7 additions & 20 deletions dist/virtual-code-owners/parse.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 0 additions & 16 deletions src/team-map/virtual-teams.schema.ts

This file was deleted.

35 changes: 11 additions & 24 deletions src/virtual-code-owners/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ function parseLine(
if (lTrimmedLine.startsWith("#")) {
return { type: "comment", line: pLineNo, raw: pUntreatedLine };
}
if (lTrimmedLine.startsWith("[") || lTrimmedLine.startsWith("^[")) {
return parseSection(pUntreatedLine, pLineNo, pTeamMap);
}
if (lTrimmedLine === "") {
return { type: "empty", line: pLineNo, raw: pUntreatedLine };
}
if (lTrimmedLine.startsWith("[") || lTrimmedLine.startsWith("^[")) {
return parseSection(pUntreatedLine, pLineNo, pTeamMap);
}

return parseRule(pUntreatedLine, pLineNo, pTeamMap);
}
Expand All @@ -65,37 +65,24 @@ function parseRule(
): IVirtualCodeOwnerLine {
const lTrimmedLine = pUntreatedLine.trim();
const lCommentSplitLine = lTrimmedLine.split(/\s*#/);
const lRuleWithoutUsernames = lCommentSplitLine[0]?.match(
/^(?<filesPattern>[^\s]+)(?<spaces>\s*)$/,
);
const lRule = lCommentSplitLine[0]?.match(
/^(?<filesPattern>[^\s]+)(?<spaces>\s+)(?<userNames>.+)$/,
/^(?<filesPattern>[^\s]+)(?<spaces>\s+)?(?<userNames>.+)?$/,
);

if (lRuleWithoutUsernames?.groups && STATE.currentSection) {
return {
type: "rule",
line: pLineNo,
raw: pUntreatedLine,

filesPattern: lRuleWithoutUsernames.groups.filesPattern as string,
spaces: lRuleWithoutUsernames.groups.spaces as string,
users: [],
inheritedUsers: STATE.inheritedUsers,
currentSection: STATE.currentSection,
inlineComment: lCommentSplitLine[1] ?? "",
};
}
const ruleIsValid =
lRule?.groups &&
(lRule.groups.userNames || STATE.inheritedUsers.length > 0);

if (lRule?.groups) {
if (ruleIsValid) {
let lReturnValue: IRuleCSTLine = {
type: "rule",
line: pLineNo,
raw: pUntreatedLine,

// @ts-expect-error 18048 - ruleIsValid ensures that groups is not null
filesPattern: lRule.groups.filesPattern as string,
spaces: lRule.groups.spaces as string,
users: parseUsers(lRule.groups.userNames as string, pTeamMap),
spaces: lRule.groups?.spaces ?? "",
users: parseUsers(lRule.groups?.userNames ?? "", pTeamMap),
inlineComment: lCommentSplitLine[1] ?? "",
};
if (STATE.currentSection) {
Expand Down

0 comments on commit 5591cee

Please sign in to comment.