Skip to content

Commit

Permalink
refactor(parse/smcat): replaces module global with a proper counter c…
Browse files Browse the repository at this point in the history
…lass (#226)

## Description

- replaces module global for transition id counting with a proper
counter class in the smcat parser

## Motivation and Context

reduce footguns

## How Has This Been Tested?

- [x] green ci

## Types of changes

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] Documentation only change
- [x] Refactor (non-breaking change which fixes an issue without
changing functionality)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
  • Loading branch information
sverweij authored Dec 25, 2024
1 parent 5b4ecf8 commit d265933
Show file tree
Hide file tree
Showing 18 changed files with 133 additions and 142 deletions.
2 changes: 1 addition & 1 deletion dist/parse/index.mjs

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

9 changes: 0 additions & 9 deletions dist/parse/parser-helpers.mjs

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

5 changes: 5 additions & 0 deletions dist/parse/smcat/parse.mjs

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

3 changes: 1 addition & 2 deletions dist/parse/smcat/smcat-parser.mjs

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

2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<meta property="og:image:type" content="image/png" />
<link rel="canonical" href="https://state-machine-cat.js.org">
<script nonce="known-inline-script">let LOG = false;</script>
<script src="smcat-online-interpreter.min.js" type="module" defer integrity="sha512-bNaySAes5WU/PELPbHEbuokRRzTu12F81U8KJz5czxl4jIcXVPw9a4UvkUfMYfpO5hJGDogb38DCTARlJXIC7A=="></script>
<script src="smcat-online-interpreter.min.js" type="module" defer integrity="sha512-Xw/qQBmfkG0tshkmHFO9g7BeSAyms1nBvG1mVpN3+vhE2fZBJObdA8pyYo9MJFlCNNvGrNjCnYTCkDTXkuvi0g=="></script>
<script defer src="https://code.getmdl.io/1.3.0/material.min.js" async></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="theme-color" content="purple">
Expand Down
2 changes: 1 addition & 1 deletion docs/inpage.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
src="state-machine-cat-inpage.min.js"
type="module"
defer
integrity="sha512-hXI4LlpsUxkExo2T9/L4lzdiPM8aHNlke0TEjMvvtOdLveywHUvOTDUjbjh5ExFoNIQq4XJ50x4+JswrvLfP1Q=="
integrity="sha512-Uh16kicsUw3C8DM2WqNgdWLKVj/kWpeaAfNQKjymY42R7+ytLL5YaW9kMGAdfcsct6GPjCR0KckGXCoxFfnYiQ=="
></script>
<style>
body { font-family: sans-serif; margin: 0 auto; max-width: 799px;
Expand Down
84 changes: 42 additions & 42 deletions docs/smcat-online-interpreter.min.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions docs/smcat-online-interpreter.min.js.map

Large diffs are not rendered by default.

110 changes: 55 additions & 55 deletions docs/state-machine-cat-inpage.min.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions docs/state-machine-cat-inpage.min.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/parse/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
IStateMachine,
} from "types/state-machine-cat.mjs";
import options from "../options.mjs";
import { parse as parseSmCat } from "./smcat/smcat-parser.mjs";
import { parse as parseSmCat } from "./smcat/parse.mjs";
import { parse as parseSCXML } from "./scxml/index.mjs";
import $schema from "./smcat-ast.schema.mjs";

Expand Down Expand Up @@ -33,7 +33,7 @@ export default {
let lReturnValue = pScript;

if (options.getOptionValue(pOptions, "inputType") === "smcat") {
lReturnValue = parseSmCat(pScript);
lReturnValue = parseSmCat(pScript as string);
} else if (options.getOptionValue(pOptions, "inputType") === "scxml") {
// @ts-expect-error inputType scxml => it's a string
lReturnValue = parseSCXML(pScript);
Expand Down
12 changes: 0 additions & 12 deletions src/parse/parser-helpers.mts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const TRIGGER_RE_AS_A_STRING =
"^(entry|activity|exit)\\s*/\\s*([^\\n$]*)(\\n|$)";
/* eslint security/detect-non-literal-regexp:0 */
const TRIGGER_RE = new RegExp(TRIGGER_RE_AS_A_STRING);
let gTransitionIdHwm = 0;

function stateExists(pKnownStateNames: string[], pName: string): boolean {
return pKnownStateNames.includes(pName);
Expand Down Expand Up @@ -267,15 +266,6 @@ function extractActions(pString: string): { type: string; body: string }[] {
.map(extractAction);
}

function nextTransitionId(): number {
// eslint-disable-next-line no-plusplus
return ++gTransitionIdHwm;
}

function resetTransitionId(): void {
gTransitionIdHwm = 0;
}

export default {
initState,
extractUndeclaredStates,
Expand All @@ -287,6 +277,4 @@ export default {
extractActions,
setIf,
setIfNotEmpty,
nextTransitionId,
resetTransitionId,
};
7 changes: 7 additions & 0 deletions src/parse/smcat/parse.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { IStateMachine } from "types/state-machine-cat.mjs";
import { Counter } from "../../counter.mjs";
import { parse as pegParse } from "./smcat-parser.mjs";

export function parse(pScript: string): IStateMachine {
return pegParse(pScript, { counter: new Counter() });
}
3 changes: 1 addition & 2 deletions src/parse/smcat/peg/smcat-parser.peggy
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
program
= _ statemachine:statemachine _
{
parserHelpers.resetTransitionId()
statemachine.states = parserHelpers.extractUndeclaredStates(statemachine);
return parserHelpers.classifyForkJoins(statemachine);
}
Expand Down Expand Up @@ -135,7 +134,7 @@ transition "transition"
);
parserHelpers.setIfNotEmpty(trans, 'note', notes);

trans.id=parserHelpers.nextTransitionId();
trans.id=options.counter.next();
return trans;
}

Expand Down
3 changes: 1 addition & 2 deletions src/parse/smcat/smcat-parser.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,6 @@ function peg$parse(input, options) {
var peg$e79 = peg$otherExpectation("comment");

var peg$f0 = function(statemachine) {
parserHelpers.resetTransitionId()
statemachine.states = parserHelpers.extractUndeclaredStates(statemachine);
return parserHelpers.classifyForkJoins(statemachine);
};
Expand Down Expand Up @@ -393,7 +392,7 @@ function peg$parse(input, options) {
);
parserHelpers.setIfNotEmpty(trans, 'note', notes);

trans.id=parserHelpers.nextTransitionId();
trans.id=options.counter.next();
return trans;
};
var peg$f20 = function(from_, to) {
Expand Down
2 changes: 1 addition & 1 deletion test/parse/smcat-parser.spec.mts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { readFileSync } from "node:fs";
import { deepEqual, equal } from "node:assert/strict";
import Ajv from "ajv";
import { createRequireJSON } from "../utl.mjs";
import { parse as parseSmCat } from "#parse/smcat/smcat-parser.mjs";
import { parse as parseSmCat } from "#parse/smcat/parse.mjs";
import $schema from "#parse/smcat-ast.schema.mjs";

const ajv = new Ajv();
Expand Down
5 changes: 3 additions & 2 deletions test/render/json.spec.mts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { readFileSync, readdirSync } from "node:fs";
import { basename, join } from "node:path";
import { deepEqual, equal } from "node:assert/strict";
import Ajv from "ajv";
import { parse as convert } from "#parse/smcat/smcat-parser.mjs";
import { Counter } from "#counter.mjs";
import { parse } from "#parse/smcat/parse.mjs";

import $schema from "#parse/smcat-ast.schema.mjs";

Expand All @@ -18,7 +19,7 @@ const FIXTURE_INPUTS = readdirSync(FIXTURE_DIR)
describe("#render(json) smcat to json - ", () => {
FIXTURE_INPUTS.forEach((pInputFixture) => {
it(`correctly parses ${basename(pInputFixture)} into json`, () => {
const lResult = convert(readFileSync(pInputFixture, "utf8"));
const lResult = parse(readFileSync(pInputFixture, "utf8"));

deepEqual(
lResult,
Expand Down
6 changes: 4 additions & 2 deletions test/render/smcat.spec.mts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { deepEqual } from "node:assert/strict";
import { createRequireJSON } from "../utl.mjs";
import { Counter } from "#counter.mjs";
import render from "#render/smcat.mjs";
import { parse } from "#parse/smcat/smcat-parser.mjs";
import { parse } from "#parse/smcat/parse.mjs";

const requireJSON = createRequireJSON(import.meta.url);

Expand All @@ -22,7 +23,8 @@ describe("#render(smcat) - smcat, happy day ASTs - ", () => {
xit(pPair.title);
} else {
it(pPair.title, () => {
deepEqual(parse(render(pPair.ast)), pPair.ast);
const lRendered = render(pPair.ast);
deepEqual(parse(lRendered), pPair.ast);
});
}
});
Expand Down

0 comments on commit d265933

Please sign in to comment.