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
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": "mol-scope",
"private": true,
"version": "0.0.0",
"type": "module",
"private": true,
"description": "A browser molecular viewer in the spirit of Jmol: explore small molecules, proteins, DNA and crystals in 3D, measure bonds and angles, and drive it with Jmol-style scripts.",
"license": "AGPL-3.0-or-later",
"repository": {
"type": "git",
"url": "https://github.com/OpenLyceum/MolScope.git"
},
"keywords": [
"simulation",
"SceneryStack",
Expand All @@ -22,33 +16,45 @@
"education",
"pwa"
],
"homepage": "https://openlyceum.github.io/MolScope",
"repository": {
"type": "git",
"url": "https://github.com/OpenLyceum/MolScope.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
7 changes: 4 additions & 3 deletions src/common/model/ColorSchemes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Colours are 0xRRGGBB integers so this module stays free of Scenery.
*/

import { roundSymmetric } from "scenerystack/dot";
import type { Atom, Residue, Structure } from "./Structure.js";
import { ResidueKind, SecondaryStructure } from "./Structure.js";

Expand Down Expand Up @@ -142,9 +143,9 @@ export function parseColor(text: string): number | null {

/** Linear blend between two 0xRRGGBB colours. */
function mixColors(c1: number, c2: number, t: number): number {
const r = Math.round(((c1 >> 16) & 255) * (1 - t) + ((c2 >> 16) & 255) * t);
const g = Math.round(((c1 >> 8) & 255) * (1 - t) + ((c2 >> 8) & 255) * t);
const b = Math.round((c1 & 255) * (1 - t) + (c2 & 255) * t);
const r = roundSymmetric(((c1 >> 16) & 255) * (1 - t) + ((c2 >> 16) & 255) * t);
const g = roundSymmetric(((c1 >> 8) & 255) * (1 - t) + ((c2 >> 8) & 255) * t);
const b = roundSymmetric((c1 & 255) * (1 - t) + (c2 & 255) * t);
return (r << 16) | (g << 8) | b;
}

Expand Down
61 changes: 36 additions & 25 deletions src/common/model/ScriptEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,28 @@ export const ScriptMessageKey = {
} as const;
type ScriptMessageKeyValue = (typeof ScriptMessageKey)[keyof typeof ScriptMessageKey];

export type ScriptMessage = {
readonly key: ScriptMessageKeyValue;
readonly isError: boolean;
/** Substitution values; the view decides the wording. */
readonly args: Readonly<Record<string, string | number>>;
type ScriptMessageArgs = {
selected: { count: number };
loaded: { title: string; count: number };
loading: Record<string, never>;
measured: { value: number; count: number };
echo: { text: string };
help: { commands: string };
unknownCommand: { command: string };
badSelection: { token: string };
badArgument: { argument: string };
noStructure: Record<string, never>;
loadFailed: { reason: string };
};

export type ScriptMessage = {
[Key in ScriptMessageKeyValue]: {
readonly key: Key;
readonly isError: boolean;
readonly args: Readonly<ScriptMessageArgs[Key]>;
};
}[ScriptMessageKeyValue];

/** Superseded work is neither a successful load nor a user-facing script error. */
export class ScriptCancelledError extends Error {}

Expand Down Expand Up @@ -222,8 +237,12 @@ export class ScriptEngine {
}
messages.push(
error instanceof SelectionSyntaxError
? message(ScriptMessageKey.BAD_SELECTION, true, { token: error.token })
: message(ScriptMessageKey.LOAD_FAILED, true, { reason: error instanceof Error ? error.message : "" }),
? { key: ScriptMessageKey.BAD_SELECTION, isError: true, args: { token: error.token } }
: {
key: ScriptMessageKey.LOAD_FAILED,
isError: true,
args: { reason: error instanceof Error ? error.message : "" },
},
);
break;
}
Expand All @@ -239,10 +258,10 @@ export class ScriptEngine {
const first = args[0] ?? "";

if (verb === "help") {
return [message(ScriptMessageKey.HELP, false, { commands: COMMANDS.join(", ") })];
return [{ key: ScriptMessageKey.HELP, isError: false, args: { commands: COMMANDS.join(", ") } }];
}
if (verb === "echo") {
return [message(ScriptMessageKey.ECHO, false, { text: rest.replace(/^["']|["']$/g, "") })];
return [{ key: ScriptMessageKey.ECHO, isError: false, args: { text: rest.replace(/^["']|["']$/g, "") } }];
}
if (verb === "load") {
return this.load(rest);
Expand All @@ -263,8 +282,8 @@ export class ScriptEngine {
const context = this.target.selectionContext();
if (!(state && context)) {
return COMMANDS.includes(verb)
? [message(ScriptMessageKey.NO_STRUCTURE, true, {})]
: [message(ScriptMessageKey.UNKNOWN_COMMAND, true, { command: head })];
? [{ key: ScriptMessageKey.NO_STRUCTURE, isError: true, args: {} }]
: [{ key: ScriptMessageKey.UNKNOWN_COMMAND, isError: true, args: { command: head } }];
}

return this.executeStructureCommand(verb, rest, args, first, head, state, context);
Expand Down Expand Up @@ -342,7 +361,7 @@ export class ScriptEngine {
this.target.centerOn(rest ? evaluateSelection(rest, context) : null);
return [];
default:
return [message(ScriptMessageKey.UNKNOWN_COMMAND, true, { command: head })];
return [{ key: ScriptMessageKey.UNKNOWN_COMMAND, isError: true, args: { command: head } }];
}
}

Expand Down Expand Up @@ -405,7 +424,7 @@ export class ScriptEngine {
private cmdSelect(state: DisplayState, rest: string, context: SelectionContext): ScriptMessage[] {
state.setSelection(evaluateSelection(rest, context));
this.target.displayChanged();
return [message(ScriptMessageKey.SELECTED, false, { count: state.selectedCount() })];
return [{ key: ScriptMessageKey.SELECTED, isError: false, args: { count: state.selectedCount() } }];
}

private cmdRestrict(state: DisplayState, rest: string, context: SelectionContext): ScriptMessage[] {
Expand All @@ -415,7 +434,7 @@ export class ScriptEngine {
state.setSelection(mask);
state.setHidden(false);
this.target.displayChanged();
return [message(ScriptMessageKey.SELECTED, false, { count: state.selectedCount() })];
return [{ key: ScriptMessageKey.SELECTED, isError: false, args: { count: state.selectedCount() } }];
}

private cmdDisplayHide(state: DisplayState, rest: string, context: SelectionContext, hide: boolean): ScriptMessage[] {
Expand Down Expand Up @@ -485,7 +504,7 @@ export class ScriptEngine {
request = id ? { source: "library", id, range } : { source: "pubchem", name };
}
const result = await this.target.load(request, true);
return [message(ScriptMessageKey.LOADED, false, { title: result.title, count: result.atomCount })];
return [{ key: ScriptMessageKey.LOADED, isError: false, args: { title: result.title, count: result.atomCount } }];
}

private background(arg: string): ScriptMessage[] {
Expand Down Expand Up @@ -573,7 +592,7 @@ export class ScriptEngine {
const value = this.target.addMeasurement(indices);
return value === null
? [badArgument(joined)]
: [message(ScriptMessageKey.MEASURED, false, { value, count: indices.length })];
: [{ key: ScriptMessageKey.MEASURED, isError: false, args: { value, count: indices.length } }];
}

private set(args: readonly string[]): ScriptMessage[] {
Expand Down Expand Up @@ -629,14 +648,6 @@ export class ScriptEngine {
}
}

function message(
key: ScriptMessageKeyValue,
isError: boolean,
args: Readonly<Record<string, string | number>>,
): ScriptMessage {
return { key, isError, args };
}

function badArgument(argument: string): ScriptMessage {
return message(ScriptMessageKey.BAD_ARGUMENT, true, { argument });
return { key: ScriptMessageKey.BAD_ARGUMENT, isError: true, args: { argument } };
}
Loading
Loading