Skip to content

Commit

Permalink
refactor IfClosed()
Browse files Browse the repository at this point in the history
  • Loading branch information
slavaleleka committed Nov 7, 2023
1 parent 73deab3 commit 54fff10
Showing 1 changed file with 32 additions and 22 deletions.
54 changes: 32 additions & 22 deletions src/linter/rules/if-closed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,41 @@ export const IfClosed: LinterRule<Storage> = {
return;
}

// Check for "if" and "endif" directives
if (rule.name.value === IF_DIRECTIVE) {
// Collect open "if"
context.storage.openIfs.push(rule);
} else if (rule.name.value === ELSE_DIRECTIVE) {
// Check if there is an open "!#if" before "!#else"
if (context.storage.openIfs.length === 0) {
const directive = rule.name.value;
switch (directive) {
case IF_DIRECTIVE:
// Collect open "if"
context.storage.openIfs.push(rule);
break;
case ELSE_DIRECTIVE:
// Check if there is an open "!#if" before "!#else"
if (context.storage.openIfs.length === 0) {
context.report({
// eslint-disable-next-line max-len
message: `Using an "${ELSE_DIRECTIVE}" directive without an opening "${IF_DIRECTIVE}" directive`,
node: rule,
});
}
// otherwise do nothing
break;
case ENDIF_DIRECTIVE:
if (context.storage.openIfs.length === 0) {
context.report({
// eslint-disable-next-line max-len
message: `Using an "${ENDIF_DIRECTIVE}" directive without an opening "${IF_DIRECTIVE}" directive`,
node: rule,
});
} else {
// Mark "if" as closed (simply delete it from collection)
context.storage.openIfs.pop();
}
break;
default:
context.report({
// eslint-disable-next-line max-len
message: `Using an "${ELSE_DIRECTIVE}" directive without an opening "${IF_DIRECTIVE}" directive`,
message: `Unknown directive: "${directive}"`,
node: rule,
});
}
// otherwise do nothing
} else if (rule.name.value === ENDIF_DIRECTIVE) {
if (context.storage.openIfs.length === 0) {
context.report({
// eslint-disable-next-line max-len
message: `Using an "${ENDIF_DIRECTIVE}" directive without an opening "${IF_DIRECTIVE}" directive`,
node: rule,
});
} else {
// Mark "if" as closed (simply delete it from collection)
context.storage.openIfs.pop();
}
break;
}
},
onEndFilterList: (context): void => {
Expand Down

0 comments on commit 54fff10

Please sign in to comment.