build: add ESLint to script/lint and containerize linting (closes #152)
All checks were successful
check / check (push) Successful in 54s
All checks were successful
check / check (push) Successful in 54s
script/lint ran `prettier --check .`, byte for byte what script/fmt-check
runs, so make check checked formatting twice and did no static analysis on
a cryptocurrency wallet. Two used-but-not-imported crashes shipped past it.
ESLint is pinned in package.json with @eslint/js recommended as the base and
a flat config in eslint.config.js. no-undef and no-unused-vars are restated
error-level so a future recommended-set change cannot downgrade them.
Globals are declared per tree rather than globally, because a too-wide set
hides the next unimported identifier: browser for the popup and content
scripts, service worker for src/background/ and src/shared/, browser for the
one documented POPUP ONLY module in src/shared/, jest for tests/, node for
build.js, and both for the e2e harnesses, which carry the callbacks they
ship into the page inline.
Two rules new to the recommended set are narrowed, and both would have cost
something to satisfy. no-useless-assignment is off for approval.js and
confirmTx.js only: it flags the `password = null` and `decryptedSecret =
null` wipes at 9 sites there, which are dead by construction — that is what
a best-effort wipe of decrypted key material is — and the rule's fix is to
delete the wipe. It stays on for the rest of the tree, so an ordinary dead
store elsewhere is still an error. preserve-caught-error is off tree-wide:
it would change what the wallet's error paths throw at 3 sites
(src/shared/balances.js 207 and 215, tests/e2e/firefox/run.js 131), and
adopting `{ cause }` is a decision of its own rather than a side effect of
turning a linter on, so new code is not held to it either pending that
decision.
Every remaining violation is fixed: 41 unused bindings and 53 undefined
identifiers. Unused catch bindings became `catch {`, which the repo already
used; the shared init(ctx) view signature keeps its parameter as _ctx in the
three views that do not read it. src/shared/uniswap.js keeps its unused
V2_SWAP_EXACT_OUT decoder behind a scoped disable, because deleting it would
widen the gap it represents rather than close it (#283). driver.js's waitFor
had a plain dead store in its `last` initializer, which the newly scoped
no-useless-assignment catches; the initializer is dropped.
Linting is containerized. script/lint builds the Dockerfile's new lint stage
so the ESLint deciding whether this repo is green is the pinned one and not
whatever the host has; AUTISTMASK_LINT_NATIVE, set only in that image, is
what makes make check inside the CI build lint in place instead of recursing
into docker, and a value set to anything else is now an error rather than a
silent fall-through to the docker path. The check stage takes a COPY --from=
lint dependency so a lint failure fails the whole build early rather than
racing it.
The lint stage roughly doubles the image build, which exposed script/test's
30s cap as marginal rather than a bound: on the first CI run to rebuild the
base stage cold it killed a healthy suite at 30.6s with nothing asserting
false. The cap is a guard against a hung suite, not a wall-clock budget, and
one a healthy suite can trip teaches "just run it again". It stays at 30s on
a host, where the suite runs in about 8s and REPO_POLICIES' figure holds,
and the Dockerfile raises it to 180s through AUTISTMASK_TEST_TIMEOUT for the
in-image run, which also pays a cold jest cache and shares the runner with
the rest of the build. script/test now names a timeout kill as one instead
of reporting it as a test failure, and skips the verbose rerun in that case,
which would only spend the same wall clock to be killed again.
No --fix anywhere in the lint path: make check remains non-mutating.
The README claim that a used-but-not-imported identifier is invisible to
make check, and the same claim in script/test-e2e, are no longer true and
are corrected.
This commit is contained in:
152
eslint.config.js
Normal file
152
eslint.config.js
Normal file
@@ -0,0 +1,152 @@
|
||||
// ESLint flat config. Static analysis for make check; formatting stays with
|
||||
// prettier (script/fmt-check), so nothing here touches style.
|
||||
//
|
||||
// The sources are CommonJS and are bundled per entrypoint by build.js, so the
|
||||
// globals differ by tree and are declared per tree below. Getting that wrong in
|
||||
// either direction defeats the point: too few globals buries a real no-undef in
|
||||
// false positives, too many hides the next unimported identifier.
|
||||
|
||||
const js = require("@eslint/js");
|
||||
const globals = require("globals");
|
||||
|
||||
// The extension APIs. MV3 Chrome exposes `chrome`; Firefox exposes both, and
|
||||
// the code feature-detects between them.
|
||||
const extensionGlobals = {
|
||||
chrome: "readonly",
|
||||
browser: "readonly",
|
||||
};
|
||||
|
||||
const commonjs = {
|
||||
ecmaVersion: 2024,
|
||||
sourceType: "commonjs",
|
||||
};
|
||||
|
||||
module.exports = [
|
||||
{
|
||||
ignores: ["dist/", "node_modules/"],
|
||||
},
|
||||
|
||||
js.configs.recommended,
|
||||
|
||||
{
|
||||
rules: {
|
||||
// The two rules this config exists for. Both are already
|
||||
// error-level in the recommended set; restated so a future
|
||||
// recommended-set change cannot silently downgrade them.
|
||||
"no-undef": "error",
|
||||
// `_`-prefixed arguments are the deliberate "present for the
|
||||
// interface, unused here" marker: the popup views share one
|
||||
// init(ctx) signature and three of the eight do not read ctx.
|
||||
// An unused catch binding is written `catch {`, which the repo
|
||||
// already does, so caught errors stay checked.
|
||||
"no-unused-vars": ["error", { argsIgnorePattern: "^_" }],
|
||||
|
||||
// Off tree-wide: it requires every rethrow to carry `{ cause }`,
|
||||
// at 3 sites today (src/shared/balances.js 207 and 215,
|
||||
// tests/e2e/firefox/run.js 131). That is a change to what the
|
||||
// wallet's error paths actually throw, and it is a decision of its
|
||||
// own rather than a side effect of turning a linter on — so it is
|
||||
// off everywhere, including for new code, until that decision is
|
||||
// made. Unlike no-useless-assignment below, this is not an
|
||||
// accommodation of particular sites and must not be scoped to
|
||||
// them.
|
||||
"preserve-caught-error": "off",
|
||||
},
|
||||
},
|
||||
|
||||
// no-useless-assignment stays on everywhere except the two files that
|
||||
// wipe decrypted key material: the `password = null` and
|
||||
// `decryptedSecret = null` assignments after use are dead by construction
|
||||
// — that is what a best-effort wipe is — and the rule's fix is to delete
|
||||
// the wipe. 9 sites: approval.js 582, 593, 618, 648, 692, 703, 728, 764
|
||||
// and confirmTx.js 459. Everything else in the tree is still checked, so
|
||||
// an ordinary dead store elsewhere is still an error.
|
||||
{
|
||||
files: ["src/popup/views/approval.js", "src/popup/views/confirmTx.js"],
|
||||
rules: {
|
||||
"no-useless-assignment": "off",
|
||||
},
|
||||
},
|
||||
|
||||
// Popup and content scripts: page/window context.
|
||||
{
|
||||
files: ["src/popup/**/*.js", "src/content/**/*.js"],
|
||||
languageOptions: {
|
||||
...commonjs,
|
||||
globals: { ...globals.browser, ...extensionGlobals },
|
||||
},
|
||||
},
|
||||
|
||||
// MV3 background: a service worker, with no window and no document.
|
||||
{
|
||||
files: ["src/background/**/*.js"],
|
||||
languageOptions: {
|
||||
...commonjs,
|
||||
globals: { ...globals.serviceworker, ...extensionGlobals },
|
||||
},
|
||||
},
|
||||
|
||||
// src/shared is bundled into both, so it may only use what both provide:
|
||||
// the service worker globals are the intersection, plus the extension APIs.
|
||||
{
|
||||
files: ["src/shared/**/*.js"],
|
||||
languageOptions: {
|
||||
...commonjs,
|
||||
globals: { ...globals.serviceworker, ...extensionGlobals },
|
||||
},
|
||||
},
|
||||
|
||||
// src/shared/ens.js is the documented exception to the line above: its own
|
||||
// header says POPUP ONLY, it caches in localStorage, and only popup views
|
||||
// require it. Linting it as a service worker would be wrong about the file.
|
||||
{
|
||||
files: ["src/shared/ens.js"],
|
||||
languageOptions: {
|
||||
...commonjs,
|
||||
globals: { ...globals.browser, ...extensionGlobals },
|
||||
},
|
||||
},
|
||||
|
||||
// Unit tests: jest on node.
|
||||
{
|
||||
files: ["tests/**/*.test.js"],
|
||||
languageOptions: {
|
||||
...commonjs,
|
||||
globals: { ...globals.node, ...globals.jest },
|
||||
},
|
||||
},
|
||||
|
||||
// The build script is a plain node program.
|
||||
{
|
||||
files: ["build.js"],
|
||||
languageOptions: {
|
||||
...commonjs,
|
||||
globals: { ...globals.node },
|
||||
},
|
||||
},
|
||||
|
||||
// The e2e harnesses are node programs that also carry, inline, the
|
||||
// callbacks they ship into the browser via page.evaluate — so both
|
||||
// contexts really are present in the same file and both sets of globals
|
||||
// are in scope somewhere in it.
|
||||
{
|
||||
files: ["tests/e2e/**/*.js"],
|
||||
languageOptions: {
|
||||
...commonjs,
|
||||
globals: {
|
||||
...globals.node,
|
||||
...globals.browser,
|
||||
...extensionGlobals,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
// This config file itself.
|
||||
{
|
||||
files: ["eslint.config.js"],
|
||||
languageOptions: {
|
||||
...commonjs,
|
||||
globals: { ...globals.node },
|
||||
},
|
||||
},
|
||||
];
|
||||
Reference in New Issue
Block a user