Skip to content

Commit

Permalink
fix: make rearrangeRules update the array itself
Browse files Browse the repository at this point in the history
  • Loading branch information
Yokozuna59 committed Nov 4, 2023
1 parent 0c57433 commit cf18803
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions packages/parser/src/language/common/tokenBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,26 @@ export abstract class MermaidTokenBuilder extends DefaultTokenBuilder {
return tokenTypes;
}

private rearrangeRules(rules: GrammarAST.AbstractRule[]): GrammarAST.AbstractRule[] {
const pivotIndex = rules.findIndex((rule) => rule.name === 'TitleAndAccessibilities');
if (pivotIndex === -1) {
return rules;
/**
* Reorders rules using a pivot rule.
*
* We use this function to reorder rules because imported rules are
* inserted at the end of the array.
*
* @param rules - the grammar rules.
*/
private rearrangeRules(rules: GrammarAST.AbstractRule[]): void {
const index = rules.findIndex((rule) => rule.name === 'TitleAndAccessibilities');
if (index === -1) {
return;
}
return [...rules.slice(pivotIndex), ...rules.slice(0, pivotIndex)];

const [item] = rules.splice(index, 1);

rules.unshift(item);

const itemsToMove = rules.splice(1, index);
rules.push(...itemsToMove);
}
}

Expand Down

0 comments on commit cf18803

Please sign in to comment.