noctcore-three/no-deprecated-clock
noctcore-three/no-deprecated-clock
- Package
- three v0.1.0
- Preset
- none opt-in
- Severity
- off
- Autofix
- No
- Suggestions
- No
- Options
- Optional
- Type info
- Not needed
No new Clock() on a three.js release that has deprecated it.
three.js r183 deprecated Clock in favour of Timer (migration guide, 182 to 183). From that
release on, every new Clock() logs “Clock: This module has been deprecated. Please use
THREE.Timer instead.” to the console, and the class is on its way out of the library.
Timer (in the core since r179) is not a drop-in rename. You call timer.update() once per
frame, and getDelta() and getElapsed() then return the same values however often they are
read in that frame, where Clock.getDelta() restarts the measurement on every call. A timer can
also follow the Page Visibility API (timer.connect(document), then timer.dispose()), so a tab
that was in the background does not come back with one huge delta.
// rule options: {"threeVersion":183}import { Clock } from 'three';
const clock = new Clock();
renderer.setAnimationLoop(() => { mixer.update(clock.getDelta()); renderer.render(scene, camera);});// rule options: {"threeVersion":183}import { Timer } from 'three';
const timer = new Timer();timer.connect(document);
renderer.setAnimationLoop((time) => { timer.update(time); mixer.update(timer.getDelta()); renderer.render(scene, camera);});What it flags
Section titled “What it flags”new Clock() and new THREE.Clock(), where Clock is imported from three or three/webgpu
(named, renamed, or through import * as THREE), in a project on three.js r183 or later.
The three.js version is read in this order:
- the
threeVersionoption, when set; - the
threerange in the nearestpackage.jsonthat declares it. The lowest version the range allows counts:^0.183.0is r183, and>=0.170.0is r170 even when r187 is installed, so a library that still supports an older release is not told to drop an API that release needs; - the installed
node_modules/three/package.json, when no manifest declares a range the rule can read (acatalog:orworkspace:protocol, a tag such aslatest, a URL).
The lookup walks from the linted file’s directory up to the workspace root (the directory with a
lockfile or .git). Without a workspace root it stops at the first directory that has a
package.json, and with no package.json above the file either nothing is read and the
version is unknown.
// rule options: {"threeVersion":187}import * as THREE from 'three';import { useFrame } from '@react-three/fiber';import { useMemo, useRef } from 'react';
export function Spin() { const ref = useRef<THREE.Mesh>(null!); const clock = useMemo(() => new THREE.Clock(), []); useFrame(() => { ref.current.rotation.y = clock.getElapsedTime(); }); return <mesh ref={ref} />;}// rule options: {"threeVersion":187}import * as THREE from 'three';import { useFrame } from '@react-three/fiber';import { useRef } from 'react';
export function Spin() { const ref = useRef<THREE.Mesh>(null!); useFrame((state) => { ref.current.rotation.y = state.clock.elapsedTime; }); return <mesh ref={ref} />;}Reads package.json
Section titled “Reads package.json”Without threeVersion, the verdict depends on package.json files and on node_modules. ESLint’s
--cache does not see an edit to either, so after a three.js upgrade a cached file keeps its old
result until it changes; see
rules that read other files.
What it does not flag
Section titled “What it does not flag”- Files that import neither
three(or athree/*subpath),three-stdlib, an@react-three/*package nor one ofextraModules: the rule only runs in three.js code, so another engine’s API with the same names (a Phaser or Babylon camera) is never read as three.js. Only staticimportdeclarations count: a file that loads three.js only throughimport()orrequire()is skipped, which can hide a report but never adds one. - Any project below r183, and any project whose three.js version cannot be read (no option, no
manifest that declares
three, no installed copy): an unknown version reports nothing. - A clock the code did not construct: React Three Fiber’s
state.clockanduseThree((state) => state.clock)are built by the library, which is the one to replace them. - A
Clockthat is not three.js’s: one imported from another module, or a local class, also one that shadows the import where it is constructed. - A class that extends
Clock, and aClockreached without a static import (require('three')).
// rule options: {"threeVersion":182}import { Clock } from 'three';
const clock = new Clock();
renderer.setAnimationLoop(() => { mixer.update(clock.getDelta()); renderer.render(scene, camera);});Options
Section titled “Options”| Option | Type | Default | Meaning |
|---|---|---|---|
threeVersion |
integer |
read from disk | The three.js revision the project uses (183 for r183, published as 0.183.x). Overrides the version read from package.json and node_modules. |
allowIn |
string[] (globs) |
[] |
Files the rule skips, matched against the absolute path and the path relative to the working directory or workspace root. |
extraModules |
string[] |
[] |
More module specifiers that mark a file as three.js code (a project module that re-exports three.js, such as @acme/engine); subpaths match too, and their imports count as three.js classes and R3F hooks. |
'noctcore-three/no-deprecated-clock': ['error', { threeVersion: 183 }]Set threeVersion when the version on disk is not the one that ships: a monorepo whose lint runs
from a directory with no three in reach, or a range kept wide for other reasons.
When not to use it
Section titled “When not to use it”A library that supports three.js releases older than r179, where Timer is not in the core, and
that declares that range in peerDependencies, is already left alone: the lowest version of the
range decides. Leave the rule off if you prefer to move off Clock only when three.js removes it.
It is opt-in because a deprecated API still works, so when to migrate is the project’s call.
Checked against three.js r182 (no deprecation), r183 (the release that added it) and r187 (the latest at the time of writing).
Related
Section titled “Related”explicit-shadow-map-type: the other rule that depends on the installed three.js version.