diff --git a/AGENTS.md b/AGENTS.md index f9ab489..b8b4534 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -59,3 +59,7 @@ Run `npm run lint && npm run check && npm run build && npm test` in this directo ## Compliance carve-outs - **Hardcoded colors (computed):** `src/explore/view/potentialColor.ts` generates RGB/RGBA CSS by blending `ElectricFieldMapperColors` profile values; this is not an independent hardcoded palette. + +## Accessibility + +Follow [Baton/ACCESSIBILITY.md](https://github.com/OpenLyceum/Baton/blob/main/ACCESSIBILITY.md): PDOM names, per-screen `*ScreenSummaryContent`, and keyboard-help content. Document any intentional deviations under Compliance carve-outs. diff --git a/biome-plugins/no-math-round.grit b/biome-plugins/no-math-round.grit new file mode 100644 index 0000000..c00d4d3 --- /dev/null +++ b/biome-plugins/no-math-round.grit @@ -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: +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" + ) +} diff --git a/biome-plugins/no-native-timer.grit b/biome-plugins/no-native-timer.grit new file mode 100644 index 0000000..12db2dd --- /dev/null +++ b/biome-plugins/no-native-timer.grit @@ -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: +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" + ) +} diff --git a/biome-plugins/no-window-inner-size.grit b/biome-plugins/no-window-inner-size.grit new file mode 100644 index 0000000..a60a72f --- /dev/null +++ b/biome-plugins/no-window-inner-size.grit @@ -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: +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" + ) +} diff --git a/biome-plugins/require-property-suffix.grit b/biome-plugins/require-property-suffix.grit new file mode 100644 index 0000000..6ab614a --- /dev/null +++ b/biome-plugins/require-property-suffix.grit @@ -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: +// 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" + ) +} diff --git a/biome.json b/biome.json index 60bb205..20c80f1 100644 --- a/biome.json +++ b/biome.json @@ -3,7 +3,8 @@ "assist": { "actions": { "source": { - "organizeImports": "on" + "organizeImports": "on", + "useSortedPackageJson": "on" } } }, @@ -82,7 +83,9 @@ "style": { "noCommonJs": "error", "noDefaultExport": "off", + "noEnum": "error", "noExportedImports": "warn", + "noNamespace": "error", "noParameterAssign": "error", "preset": "recommended", "useBlockStatements": "error", @@ -98,6 +101,7 @@ "strictCase": false } }, + "useAsConstAssertion": "warn", "useForOf": "warn", "useImportType": "error", "useNamingConvention": { @@ -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", diff --git a/package.json b/package.json index a3bcd41..210526c 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,8 @@ { "name": "electric-field-mapper", - "private": true, "version": "0.0.0", - "type": "module", + "private": true, "description": "Map electric fields from movable charges with vector arrows, field lines, and a measuring probe.", - "license": "AGPL-3.0-or-later", - "repository": { - "type": "git", - "url": "https://github.com/OpenLyceum/ElectricFieldMapper.git" - }, "keywords": [ "electric field", "field lines", @@ -20,33 +14,45 @@ "pwa", "interactive" ], + "homepage": "https://openlyceum.github.io/ElectricFieldMapper", + "repository": { + "type": "git", + "url": "https://github.com/OpenLyceum/ElectricFieldMapper.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" @@ -69,11 +75,6 @@ "engines": { "node": ">=26" }, - "overrides": { - "lodash": "~4.18.1", - "three": "~0.125.2", - "brace-expansion": "~5.0.12" - }, "devEngines": { "runtime": { "name": "node", diff --git a/src/explore/model/ExploreModel.ts b/src/explore/model/ExploreModel.ts index 379c400..2e7508a 100644 --- a/src/explore/model/ExploreModel.ts +++ b/src/explore/model/ExploreModel.ts @@ -8,7 +8,7 @@ import { StringProperty, type TReadOnlyProperty, } from "scenerystack/axon"; -import { Bounds2, Vector2, Vector2Property } from "scenerystack/dot"; +import { Bounds2, roundSymmetric, Vector2, Vector2Property } from "scenerystack/dot"; import type { TModel } from "scenerystack/joist"; import { CHARGE_PRESETS, type ChargePreset, isChargePreset } from "../../common/model/ChargePresets.js"; import type { FieldDisplayPreferences } from "../../common/model/FieldDisplayOptions.js"; @@ -216,7 +216,7 @@ export class ExploreModel implements TModel { public snapPosition(point: Point): Vector2 { const snap = (value: number, min: number, max: number) => { - const rounded = Math.round(value / GRID_SPACING_M) * GRID_SPACING_M; + const rounded = roundSymmetric(value / GRID_SPACING_M) * GRID_SPACING_M; // Visible edges need not lie on the grid. Clamp to whole grid points inside the margin. const firstGridPoint = Math.ceil((min + GRID_SPACING_M) / GRID_SPACING_M) * GRID_SPACING_M; const lastGridPoint = Math.floor((max - GRID_SPACING_M) / GRID_SPACING_M) * GRID_SPACING_M; @@ -278,7 +278,7 @@ export class ExploreModel implements TModel { const spacing = GRID_SPACING_M / GRID_MINOR_LINES_PER_MAJOR; const bounds = this.fieldBoundsProperty.value; const snap = (value: number, min: number, max: number): number => { - const rounded = Math.round(Math.round(value / spacing) * spacing * 1e6) / 1e6; + const rounded = roundSymmetric(roundSymmetric(value / spacing) * spacing * 1e6) / 1e6; return Math.max(min, Math.min(max, rounded)); }; return new Vector2(snap(point.x, bounds.minX, bounds.maxX), snap(point.y, bounds.minY, bounds.maxY)); diff --git a/src/explore/model/FieldPhysics.ts b/src/explore/model/FieldPhysics.ts index a407ce1..eeb5f84 100644 --- a/src/explore/model/FieldPhysics.ts +++ b/src/explore/model/FieldPhysics.ts @@ -1,3 +1,4 @@ +import { roundSymmetric } from "scenerystack/dot"; import type { VoltageScale } from "../../common/model/FieldDisplayOptions.js"; import type { Point } from "../../common/model/Point.js"; import { FIELD_PHYSICS_SETTINGS } from "../../ElectricFieldMapperConstants.js"; @@ -264,7 +265,7 @@ function symmetricPairFieldLines(charges: readonly Charge[], bounds: FieldBounds return null; } const lines: Point[][] = []; - const lineCount = Math.round(count * Math.abs(a.q)); + const lineCount = roundSymmetric(count * Math.abs(a.q)); for (const charge of [a, b]) { const orientation = charge === a ? 1 : -1; const inward = { x: (orientation * dx) / separation, y: (orientation * dy) / separation }; @@ -311,7 +312,7 @@ export function automaticFieldLines( const arrivals = sinks.map(() => 0); for (const sourceCharge of charges.filter((charge) => charge.q > 0)) { - const lineCount = Math.round(count * sourceCharge.q); + const lineCount = roundSymmetric(count * sourceCharge.q); for (let i = 0; i < lineCount; i++) { const angle = (2 * Math.PI * i) / lineCount; const seed = { @@ -331,7 +332,7 @@ export function automaticFieldLines( } for (const [sinkIndex, charge] of sinks.entries()) { - const lineCount = Math.round(count * -charge.q); + const lineCount = roundSymmetric(count * -charge.q); const needed = Math.max(0, lineCount - (arrivals[sinkIndex] ?? 0)); if (needed === 0) { continue; diff --git a/src/explore/view/ExploreScreenSummaryContent.ts b/src/explore/view/ExploreScreenSummaryContent.ts index 1837a0d..d55e506 100644 --- a/src/explore/view/ExploreScreenSummaryContent.ts +++ b/src/explore/view/ExploreScreenSummaryContent.ts @@ -36,8 +36,11 @@ export class ExploreScreenSummaryContent extends ScreenSummaryContent { states.valuesShownStringProperty, states.valuesHiddenStringProperty, ); - const fieldLineCount = new DerivedProperty([model.changeCountProperty], () => model.seedPoints.length); - const equipotentialCount = new DerivedProperty([model.changeCountProperty], () => model.equipotentialSeeds.length); + const fieldLineCountProperty = new DerivedProperty([model.changeCountProperty], () => model.seedPoints.length); + const equipotentialCountProperty = new DerivedProperty( + [model.changeCountProperty], + () => model.equipotentialSeeds.length, + ); const details = new PatternStringProperty(a11y.currentDetailsStringProperty, { chargeCount: model.charges.lengthProperty, sensorCount: model.sensors.lengthProperty, @@ -45,8 +48,8 @@ export class ExploreScreenSummaryContent extends ScreenSummaryContent { measuringTapeState, voltageMapState, valuesState, - fieldLineCount, - equipotentialCount, + fieldLineCount: fieldLineCountProperty, + equipotentialCount: equipotentialCountProperty, }); super({ playAreaContent: a11y.screenSummary.playAreaStringProperty, @@ -61,8 +64,8 @@ export class ExploreScreenSummaryContent extends ScreenSummaryContent { measuringTapeState, voltageMapState, valuesState, - fieldLineCount, - equipotentialCount, + fieldLineCountProperty, + equipotentialCountProperty, ]) { property.dispose(); } diff --git a/src/explore/view/FieldLinesInfoDialog.ts b/src/explore/view/FieldLinesInfoDialog.ts index 9ab051f..e29fff8 100644 --- a/src/explore/view/FieldLinesInfoDialog.ts +++ b/src/explore/view/FieldLinesInfoDialog.ts @@ -21,12 +21,14 @@ export class FieldLinesInfoDialog extends Dialog { fill: ElectricFieldMapperColors.textColorProperty, maxWidth: INFO_DIALOG_LINE_WRAP, }); - const spokenBody = new DerivedProperty([ui.infoBodyStringProperty], (markup) => markup.replace(/<\/?b>/g, "")); + const spokenBodyProperty = new DerivedProperty([ui.infoBodyStringProperty], (markup) => + markup.replace(/<\/?b>/g, ""), + ); const body = new RichText(ui.infoBodyStringProperty, { font: new PhetFont(FIELD_LINES_INFO_DIALOG_SETTINGS.BODY_FONT_SIZE), fill: ElectricFieldMapperColors.textColorProperty, lineWrap: INFO_DIALOG_LINE_WRAP, - accessibleParagraph: spokenBody, + accessibleParagraph: spokenBodyProperty, }); super(body, { title, diff --git a/src/explore/view/potentialColor.ts b/src/explore/view/potentialColor.ts index 960d309..b422dbc 100644 --- a/src/explore/view/potentialColor.ts +++ b/src/explore/view/potentialColor.ts @@ -1,3 +1,4 @@ +import { roundSymmetric } from "scenerystack/dot"; import type { Color } from "scenerystack/scenery"; import ElectricFieldMapperColors from "../../ElectricFieldMapperColors.js"; import { POTENTIAL_SATURATION, potentialColorFraction } from "../model/FieldPhysics.js"; @@ -29,5 +30,5 @@ export function potentialCSS(potential: number, alpha = 1, saturation = POTENTIA ElectricFieldMapperColors.potentialNegativeColorProperty.value, saturation, ); - return `rgba(${Math.round(r)},${Math.round(g)},${Math.round(b)},${alpha})`; + return `rgba(${roundSymmetric(r)},${roundSymmetric(g)},${roundSymmetric(b)},${alpha})`; } diff --git a/tests/biome-plugins.node-test.ts b/tests/biome-plugins.node-test.ts index 8238845..137cae6 100644 --- a/tests/biome-plugins.node-test.ts +++ b/tests/biome-plugins.node-test.ts @@ -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": [ @@ -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, []); });