Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,7 @@ decisions: [`doc/implementation-notes.md`](doc/implementation-notes.md).
| `tests/memory-leak.test.ts` | Disposal of nodes created and removed at runtime |
| `tests/browser/keyboard.spec.ts` | Keyboard-only Playwright passes (`npx playwright test tests/browser`) |
| `tests/fuzz/fuzz.spec.ts` | Template-owned pointer + keyboard fuzz |

## Compliance carve-outs

None.
12 changes: 12 additions & 0 deletions biome-plugins/no-math-round.grit
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Flags Math.round(). Prefer roundSymmetric from scenerystack/dot so positive and
// negative values round the same way (see phetsims/dot#35).
// Suppress a deliberate use with: // biome-ignore lint/plugin/no-math-round: <reason>
language js

`Math.round($value)` as $call where {
register_diagnostic(
span = $call,
message = "Use roundSymmetric(value) from scenerystack/dot instead of Math.round(), which rounds positive and negative numbers differently.",
severity = "warn"
)
}
18 changes: 18 additions & 0 deletions biome-plugins/no-native-timer.grit
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Flags bare setTimeout / setInterval (and window.* forms). Sim code should step
// from the model/view step(dt) chain, or use stepTimer from scenerystack/axon when a
// delayed callback is truly needed. Method calls like stepTimer.setTimeout are allowed.
// Suppress a deliberate use with: // biome-ignore lint/plugin/no-native-timer: <reason>
language js

or {
`setTimeout($...)`,
`setInterval($...)`,
`window.setTimeout($...)`,
`window.setInterval($...)`
} as $call where {
register_diagnostic(
span = $call,
message = "Do not call setTimeout/setInterval in sim code; step from model/view step(dt), or use stepTimer from scenerystack/axon for a delayed callback.",
severity = "warn"
)
}
13 changes: 13 additions & 0 deletions biome-plugins/no-window-inner-size.grit
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Flags window.innerWidth / window.innerHeight. Prefer Sim.dimensionProperty (or the
// screen's layoutBounds / visibleBounds) so layout tracks the sim display, not the
// browser chrome.
// Suppress a deliberate use with: // biome-ignore lint/plugin/no-window-inner-size: <reason>
language js

or { `window.innerWidth`, `window.innerHeight` } as $hit where {
register_diagnostic(
span = $hit,
message = "Use Sim.dimensionProperty (or layoutBounds/visibleBounds) instead of window.innerWidth/innerHeight.",
severity = "warn"
)
}
35 changes: 35 additions & 0 deletions biome-plugins/require-property-suffix.grit
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Flags axon Property instances whose binding name does not end in "Property".
// Covers same-line const/let/var and this.field for the common *Property constructors.
// Class fields (`public readonly foo = new NumberProperty(...)`, bare `foo = ...`) are
// not reliably matched by Biome GritQL — rename those in review or with a codemod.
// Type-only annotations are out of scope (PhET's rule is type-checker based).
// Suppress a deliberate use with: // biome-ignore lint/plugin/require-property-suffix: <reason>
// GritQL regexes here must use non-capturing groups (?:...); a plain (...) silently never matches.
language js

or {
`const $name = new $ctor($...)`,
`let $name = new $ctor($...)`,
`var $name = new $ctor($...)`,
`this.$name = new $ctor($...)`
} as $stmt where {
$ctor <: or {
`Property`,
`BooleanProperty`,
`NumberProperty`,
`StringProperty`,
`DerivedProperty`,
`DerivedStringProperty`,
`DynamicProperty`,
`TinyProperty`,
`EnumerationProperty`,
`StringUnionProperty`,
`ProfileColorProperty`
},
not $name <: r"(?s)(?:.*(?:Property|PROPERTY)|property|_property)$",
register_diagnostic(
span = $name,
message = "Axon Property bindings must end in \"Property\" (e.g. massProperty, not mass).",
severity = "warn"
)
}
10 changes: 9 additions & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"assist": {
"actions": {
"source": {
"organizeImports": "on"
"organizeImports": "on",
"useSortedPackageJson": "on"
}
}
},
Expand Down Expand Up @@ -82,7 +83,9 @@
"style": {
"noCommonJs": "error",
"noDefaultExport": "off",
"noEnum": "error",
"noExportedImports": "warn",
"noNamespace": "error",
"noParameterAssign": "error",
"preset": "recommended",
"useBlockStatements": "error",
Expand All @@ -98,6 +101,7 @@
"strictCase": false
}
},
"useAsConstAssertion": "warn",
"useForOf": "warn",
"useImportType": "error",
"useNamingConvention": {
Expand Down Expand Up @@ -170,6 +174,10 @@
"plugins": [
"./biome-plugins/no-native-tofixed.grit",
"./biome-plugins/no-math-random.grit",
"./biome-plugins/no-math-round.grit",
"./biome-plugins/no-native-timer.grit",
"./biome-plugins/no-window-inner-size.grit",
"./biome-plugins/require-property-suffix.grit",
"./biome-plugins/no-hardcoded-text.grit",
{
"path": "./biome-plugins/no-inline-color.grit",
Expand Down
57 changes: 29 additions & 28 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
{
"name": "lewis-structures",
"private": true,
"version": "0.0.0",
"type": "module",
"private": true,
"description": "Lewis structures for General Chemistry: count valence electrons, draw bonds and lone pairs with live formal-charge and octet checks, compare resonance forms and their hybrid, practise in a three-level challenge, and see VSEPR shapes in 3D.",
"license": "AGPL-3.0-or-later",
"repository": {
"type": "git",
"url": "https://github.com/OpenLyceum/LewisStructures.git"
},
"keywords": [
"simulation",
"SceneryStack",
Expand All @@ -22,33 +16,45 @@
"education",
"pwa"
],
"homepage": "https://openlyceum.github.io/LewisStructures",
"repository": {
"type": "git",
"url": "https://github.com/OpenLyceum/LewisStructures.git"
},
"license": "AGPL-3.0-or-later",
"type": "module",
"scripts": {
"start": "vite",
"dev": "vite",
"build": "tsc && vite build",
"build:single": "tsc && vite build --mode single",
"preview": "vite preview",
"lint": "biome check --error-on-warnings .",
"format": "biome format --write .",
"fix": "biome check --write .",
"check": "tsc --noEmit && tsc -p tsconfig.scripts.json --noEmit && tsc -p tsconfig.test.json --noEmit",
"check:unused": "knip",
"check:architecture": "depcruise src",
"check:unused": "knip",
"clean": "rm -rf dist",
"dev": "vite",
"fix": "biome check --write .",
"format": "biome format --write .",
"icons": "node scripts/generate-icons.ts",
"knip": "knip",
"lint": "biome check --error-on-warnings .",
"prepare": "test \"$(git rev-parse --show-toplevel 2>/dev/null)\" = \"$PWD\" && git config core.hooksPath .githooks || true",
"preview": "vite preview",
"release": "npm run check && npm run lint && npm run check:architecture && npm run build && npm test && npm version patch && git push && git push --tags",
"watch": "tsc --watch",
"start": "vite",
"test": "vitest run",
"test:watch": "vitest",
"test:architecture": "node --test tests/*.node-test.ts",
"test:fuzz": "node scripts/test-fuzz.ts",
"test:fuzz:quick": "node scripts/test-fuzz.ts 10",
"test:fuzz:long": "node scripts/test-fuzz.ts 300",
"test:fuzz:quick": "node scripts/test-fuzz.ts 10",
"test:soak": "npm run test:soak:headless && npm run test:soak:browser",
"test:soak:headless": "vitest run --config vitest.soak.config.ts",
"test:soak:browser": "npx playwright test tests/soak/browser.spec.ts",
"icons": "node scripts/generate-icons.ts",
"clean": "rm -rf dist",
"prepare": "test \"$(git rev-parse --show-toplevel 2>/dev/null)\" = \"$PWD\" && git config core.hooksPath .githooks || true",
"knip": "knip",
"test:architecture": "node --test tests/*.node-test.ts"
"test:soak:headless": "vitest run --config vitest.soak.config.ts",
"test:watch": "vitest",
"watch": "tsc --watch"
},
"overrides": {
"brace-expansion": "~5.0.12",
"lodash": "~4.18.1",
"three": "~0.125.2"
},
"dependencies": {
"scenerystack": "^3.0.0"
Expand All @@ -71,11 +77,6 @@
"engines": {
"node": ">=26"
},
"overrides": {
"lodash": "~4.18.1",
"three": "~0.125.2",
"brace-expansion": "~5.0.12"
},
"devEngines": {
"runtime": {
"name": "node",
Expand Down
3 changes: 2 additions & 1 deletion src/common/model/SpeciesCatalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* structures. Display names are localised under `species.<id>` in the string files.
*/

import { roundSymmetric } from "scenerystack/dot";
import type { ElementSymbol } from "./ElementData.js";
import type { StructureData } from "./StructureData.js";

Expand Down Expand Up @@ -123,7 +124,7 @@ function bent(left: ElementSymbol, center: ElementSymbol, right: ElementSymbol,
}

function round(value: number): number {
return Math.round(value * 1000) / 1000;
return roundSymmetric(value * 1000) / 1000;
}

function benzene(): StructureData {
Expand Down
4 changes: 2 additions & 2 deletions src/common/view/StructureNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { STRUCTURE_NODE_SETTINGS } from "../../LewisStructuresConstants.js";
* `lonePairPoint` and `bondPoint` give view positions for the curved arrows drawn over it.
*/

import { Vector2 } from "scenerystack/dot";
import { roundSymmetric, Vector2 } from "scenerystack/dot";
import { Shape } from "scenerystack/kite";
import { Node, Path, Text } from "scenerystack/scenery";
import { PhetFont } from "scenerystack/scenery-phet";
Expand Down Expand Up @@ -75,7 +75,7 @@ export class StructureNode extends Node {
if (hybrid) {
const order = hybrid.bondOrders[i] ?? bond.order;
bondLayer.addChild(hybridBond(a, b, order));
if (Math.abs(order - Math.round(order)) > 1e-6) {
if (Math.abs(order - roundSymmetric(order)) > 1e-6) {
const normal = b.minus(a).normalized().perpendicular;
labelLayer.addChild(
new Text(fractionText(order), {
Expand Down
4 changes: 2 additions & 2 deletions src/common/view/formatValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* superscripts ("2−"), and element-count lists ("2 O, 1 N").
*/

import { toFixed } from "scenerystack/dot";
import { roundSymmetric, toFixed } from "scenerystack/dot";
import type { ElementSymbol } from "../model/ElementData.js";

const MINUS = "−";
Expand Down Expand Up @@ -55,7 +55,7 @@ export function fractionText(value: number, signed = false): string {
const sign = value < 0 ? MINUS : signed && value > 0 ? "+" : "";
const magnitude = Math.abs(value);
for (let denominator = 1; denominator <= 6; denominator++) {
const numerator = Math.round(magnitude * denominator);
const numerator = roundSymmetric(magnitude * denominator);
if (Math.abs(numerator / denominator - magnitude) < 1e-6) {
const whole = Math.floor(numerator / denominator);
const rest = numerator - whole * denominator;
Expand Down
53 changes: 52 additions & 1 deletion tests/biome-plugins.node-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,54 @@ test("no-math-random flags Math.random() and leaves dotRandom alone", () => {
assert.deepEqual(lines(hits, "dotRandom"), [1]);
});

test("no-math-round flags Math.round() and leaves roundSymmetric alone", () => {
const hits = lint({
"src/a.ts": "export const a = Math.round(1.5);\nexport const b = roundSymmetric(1.5);\n",
});
assert.deepEqual(lines(hits, "roundSymmetric"), [1]);
});

test("no-native-timer flags bare and window timers, allows stepTimer", () => {
const hits = lint({
"src/a.ts": [
"setTimeout(() => {}, 0);",
"setInterval(() => {}, 0);",
"window.setTimeout(() => {}, 0);",
"window.setInterval(() => {}, 0);",
"stepTimer.setTimeout(() => {}, 0);",
"stepTimer.setInterval(() => {}, 0);",
"",
].join("\n"),
});
assert.deepEqual(lines(hits, "stepTimer"), [1, 2, 3, 4]);
});

test("no-window-inner-size flags window.innerWidth/Height", () => {
const hits = lint({
"src/a.ts": "const w = window.innerWidth;\nconst h = window.innerHeight;\nconst d = dimensionProperty;\n",
});
assert.deepEqual(lines(hits, "dimensionProperty"), [1, 2]);
});

test("require-property-suffix flags Property bindings without Property suffix", () => {
const hits = lint({
"src/a.ts": [
"const mass = new NumberProperty(1);",
"const massProperty = new NumberProperty(1);",
"const details = new DerivedProperty([x], (v) => v);",
"const detailsProperty = new DerivedProperty([x], (v) => v);",
"this.visible = new BooleanProperty(true);",
"this.visibleProperty = new BooleanProperty(true);",
// Class fields are not reliably matched by Biome GritQL yet.
"class D {",
" public readonly ignored = new NumberProperty(0);",
"}",
"",
].join("\n"),
});
assert.deepEqual(lines(hits, "Property"), [1, 3, 5]);
});

test("no-hardcoded-text flags literal display words only", () => {
const hits = lint({
"src/a.ts": [
Expand Down Expand Up @@ -116,6 +164,9 @@ test("no-inline-color flags hardcoded colors outside *Colors.ts", () => {
});

test("plugins only run under src/", () => {
const hits = lint({ "scripts/a.ts": 'Math.random(); new Text("Mass"); new Color(1, 2, 3);\n' });
const hits = lint({
"scripts/a.ts":
'Math.random(); Math.round(1); setTimeout(() => {}, 0); window.innerWidth; const mass = new NumberProperty(1); new Text("Mass"); new Color(1, 2, 3);\n',
});
assert.deepEqual(hits, []);
});
Loading