@nera-static/stan 0.0.0-stage → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +23 -0
- package/LICENSE +21 -0
- package/README.md +308 -2
- package/bin/nera-stan.js +22 -0
- package/package.json +60 -5
- package/src/cli.js +143 -0
- package/src/eslint/config.js +159 -0
- package/src/helper-names.js +159 -0
- package/src/index.js +10 -0
- package/src/kind.js +102 -0
- package/src/repo-files.js +140 -0
- package/src/rules/hook-shape.js +168 -0
- package/src/rules/index.js +26 -0
- package/src/rules/no-copied-from.js +92 -0
- package/src/rules/no-local-helper.js +231 -0
- package/src/rules/scope.js +14 -0
- package/src/rules/shared-logger.js +57 -0
- package/src/rules/test-rules.js +291 -0
- package/src/test-helper-names.js +54 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [1.0.1] - 2026-11-27
|
|
9
|
+
|
|
10
|
+
### Changed
|
|
11
|
+
|
|
12
|
+
- README: a section on moving an existing repo to nera-stan (the editor export replaces `eslint.config.js`, one `--fix` sweep followed by a second run, exceptions, the sweep as a commit of its own). First release through CI with npm trusted publishing.
|
|
13
|
+
|
|
14
|
+
## [1.0.0] - 2026-11-27
|
|
15
|
+
|
|
16
|
+
### Added
|
|
17
|
+
|
|
18
|
+
- First release: the bin `nera-stan [dir] [--fix]` (plus `--help` and `--version`) checks a Nera repo and prints one finding per line as `<file>:<line> <rule> <message>`, with a summary line on stderr. Exit codes: 0 clean, 1 findings, 2 usage or internal error.
|
|
19
|
+
- Repo-kind detection from `package.json` alone: `core`, `starter`, `theme`, `plugin`, `site` and `tool`.
|
|
20
|
+
- The shipped ESLint flat config (`@eslint/js` recommended, the `globals` presets, `@stylistic` formatting, `prefer-const`, `no-var`, `no-unused-vars`, `n/prefer-node-protocol`, unused `eslint-disable` comments reported, and every `eslint-disable` comment needs a reason after ` -- ` through `@eslint-community/eslint-comments/require-description`). The checked repo's own `eslint.config.js` is not read. `export default [...nera]` re-exports it for editors, and `createConfig({ root })` builds it for another root.
|
|
21
|
+
- Ignores: `node_modules/`, `public/`, `dist/`, `coverage/`, `.nera-cache/`, `test/site/public/`, the repo's `.gitignore`, and the exception key `nera-stan.ignores` in `package.json`.
|
|
22
|
+
- The Nera rules `nera/no-local-helper`, `nera/shared-logger`, `nera/no-copied-from`, `nera/hook-shape`, `nera/test-no-chdir`, `nera/test-no-cwd-path`, `nera/test-name` and `nera/test-helpers`.
|
|
23
|
+
- `--fix` for the `@stylistic` rules, `n/prefer-node-protocol` and the self-fixing ESLint core rules, and for `nera/hook-shape` on `({ app, pagesData })`.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Michael Becker
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,309 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @nera-static/stan
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://github.com/seebaermichi/nera-stan/actions/workflows/test.yml)
|
|
4
|
+
[](https://www.npmjs.com/package/@nera-static/stan)
|
|
5
|
+
|
|
6
|
+
The handwriting check of a [Nera](https://github.com/seebaermichi/nera) repo. `nera-stan` lints a plugin, theme, starter, the core, a tool or a site with one shipped ESLint configuration and the Nera rules. It reports every finding in one line format and ends with one exit code, so `npm run lint` fails in CI and in the `pre-push` hook on anything that breaks the fleet's style.
|
|
7
|
+
|
|
8
|
+
📖 **Documentation:** [nera.js.org](https://nera.js.org), with the style guide at [nera.js.org/docs/style-guide/](https://nera.js.org/docs/style-guide/)
|
|
9
|
+
|
|
10
|
+
## ✨ Features
|
|
11
|
+
|
|
12
|
+
- One ESLint flat config for every Nera repo: `@eslint/js` recommended, the `globals` presets and the fleet's formatting through `@stylistic`
|
|
13
|
+
- Eight Nera rules: no local copies of `@nera-static/plugin-utils` or `@nera-static/test-utils` helpers, the shared logger in plugin runtime code, the canonical hook shape, no "copied from" notes and no roadmap references in published files, and four test rules
|
|
14
|
+
- Detects the repo kind from `package.json`, and each rule applies only to the kinds and files it is meant for
|
|
15
|
+
- One report line per finding, `<file>:<line> <rule> <message>`, where each message names what is wrong and what to do
|
|
16
|
+
- `--fix` applies only the fixes that have one obviously correct result, and only inside the checked repo
|
|
17
|
+
- Ships ESLint and every plugin it uses as its own dependencies, so a repo installs one package
|
|
18
|
+
- Needs nothing but the checked repo: no network, no workspace and no sibling folder
|
|
19
|
+
|
|
20
|
+
CSS, Pug and structure checks per repo kind are planned for a later release.
|
|
21
|
+
|
|
22
|
+
## 🚀 Installation
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install --save-dev @nera-static/stan
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Then point the repo's `lint` script at it:
|
|
29
|
+
|
|
30
|
+
```json
|
|
31
|
+
{
|
|
32
|
+
"scripts": {
|
|
33
|
+
"lint": "nera-stan"
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
`npm run lint` now checks the whole repo. The fleet's husky hooks run it before every commit (`pre-commit`: `npm run lint`) and the tests before every push (`pre-push`: `npx vitest run`). `test.yml` and `publish.yml` run `npm run lint`, so CI refuses what fails.
|
|
39
|
+
|
|
40
|
+
### Moving an existing repo to nera-stan
|
|
41
|
+
|
|
42
|
+
1. Install it and point `lint` at it, as above.
|
|
43
|
+
2. Replace the repo's `eslint.config.js` with the editor export (see [Editor integration](#editor-integration)) and remove the ESLint packages from its own `devDependencies`: nera-stan brings them.
|
|
44
|
+
3. Run `npx nera-stan --fix` once, then run `npx nera-stan` again. A fix can create a new finding: re-indenting a block can push a line past `max-len`.
|
|
45
|
+
4. Fix what is left by hand. Generated or vendored files that are not Nera code go into `nera-stan.ignores` (see [Ignored paths](#ignored-paths)); a single deliberate line gets an `eslint-disable-next-line <rule> -- <reason>` comment.
|
|
46
|
+
5. Run the tests, then commit the sweep on its own, so the formatting changes stay apart from real ones.
|
|
47
|
+
|
|
48
|
+
## 🧩 Usage
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
nera-stan # check the repo in the current directory
|
|
52
|
+
nera-stan ../my-repo # check another repo
|
|
53
|
+
nera-stan --fix # apply the obvious fixes, then report what is left
|
|
54
|
+
nera-stan --help
|
|
55
|
+
nera-stan --version
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
| Option | Meaning |
|
|
59
|
+
|---|---|
|
|
60
|
+
| `[dir]` | The repo to check, default the current directory. At most one. |
|
|
61
|
+
| `--fix` | Write the fixes that have one obviously correct result back to disk, then report the remaining findings. A file whose real path lies outside `dir` (a symlink out of the repo) is neither reported nor fixed. |
|
|
62
|
+
| `--help` | Print the usage text. |
|
|
63
|
+
| `--version` | Print the nera-stan version. |
|
|
64
|
+
|
|
65
|
+
### Report format
|
|
66
|
+
|
|
67
|
+
stdout carries one finding per line, sorted by file and then by line:
|
|
68
|
+
|
|
69
|
+
```text
|
|
70
|
+
index.js:12 nera/hook-shape `getAppData(data)` must destructure its argument with defaults, listing only the keys it uses: `getAppData({ app = {}, pagesData = [] } = {})`
|
|
71
|
+
test/build.test.js:4 nera/test-no-chdir `process.chdir` in a test moves the cwd of every test in the worker: …
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
The file is relative to the checked repo, with `/` separators. The rule is an ESLint rule id (`nera/hook-shape`, `@stylistic/indent`, `no-var`, …), `parse-error` for a file ESLint cannot parse, or `eslint-directive` for an unused `eslint-disable` comment.
|
|
75
|
+
|
|
76
|
+
stderr carries one summary line:
|
|
77
|
+
|
|
78
|
+
```text
|
|
79
|
+
nera-stan: 2 problems (1 fixable with --fix)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Exit codes
|
|
83
|
+
|
|
84
|
+
| Code | Meaning |
|
|
85
|
+
|---|---|
|
|
86
|
+
| `0` | Clean: no finding |
|
|
87
|
+
| `1` | At least one finding |
|
|
88
|
+
| `2` | A usage or internal error, for example an unknown option, a missing or invalid `package.json`, or an invalid `nera-stan` key |
|
|
89
|
+
|
|
90
|
+
Every rule is an error. There is no warnings level.
|
|
91
|
+
|
|
92
|
+
## Repo kinds
|
|
93
|
+
|
|
94
|
+
nera-stan reads only `package.json` to decide what kind of repo it checks. The first match wins:
|
|
95
|
+
|
|
96
|
+
| Kind | Detected by | Examples |
|
|
97
|
+
|---|---|---|
|
|
98
|
+
| `core` | name `@nera-static/core` | `generator` |
|
|
99
|
+
| `starter` | a `nera.starter` field | `nera-starter-blog` |
|
|
100
|
+
| `theme` | name `@nera-static/theme-*`, or a `nera.generator` field | `nera-theme-stack` |
|
|
101
|
+
| `plugin` | name `@nera-static/plugin-*`, except `@nera-static/plugin-utils` | `nera-plugin-sitemap` |
|
|
102
|
+
| `site` | a name outside `@nera-static/` and a dependency on `@nera-static/nera` | `nera-website` |
|
|
103
|
+
| `tool` | everything else | `@nera-static/nera`, `validate`, `plugin-utils`, `mcp`, `highlightjs-pug`, `stan` |
|
|
104
|
+
|
|
105
|
+
A missing or unreadable `package.json` ends the run with exit code 2.
|
|
106
|
+
|
|
107
|
+
## ⚙️ The base config
|
|
108
|
+
|
|
109
|
+
Every `*.js`, `*.mjs` and `*.cjs` file is checked with:
|
|
110
|
+
|
|
111
|
+
- `@eslint/js` recommended
|
|
112
|
+
- `globals.node` everywhere, `globals.browser` for `assets/**` and a plugin's `views/**` scripts (they run in the visitor's browser), and `globals.vitest` in tests. `*.cjs` files are parsed as CommonJS.
|
|
113
|
+
- `@stylistic`: `indent` 4, `semi` never, `quotes` single with `avoidEscape`, `comma-dangle` always-multiline, `arrow-parens` always, `eol-last`, `no-trailing-spaces`, and `max-len` 120 with strings, URLs and comments ignored
|
|
114
|
+
- `object-curly-spacing` always, `prefer-const`, `no-var` and `no-unused-vars`
|
|
115
|
+
- `n/prefer-node-protocol`, so built-ins are imported as `node:fs`, `node:path` and so on
|
|
116
|
+
- `reportUnusedDisableDirectives: 'error'`, so an `eslint-disable` comment that no longer suppresses anything is itself a finding
|
|
117
|
+
- `@eslint-community/eslint-comments/require-description`, so every `eslint-disable`/`eslint-enable` comment says why after ` -- `: `// eslint-disable-next-line no-var -- <reason>`
|
|
118
|
+
|
|
119
|
+
The repo's own `eslint.config.js` is **not** read: nera-stan builds its configuration itself, so the check is the same in every repo.
|
|
120
|
+
|
|
121
|
+
### Ignored paths
|
|
122
|
+
|
|
123
|
+
nera-stan never checks `node_modules/` and `.nera-cache/` at any depth, `public/`, `dist/` and `coverage/` at the repo root, and any `test/site/public/`. It also skips everything the repo's `.gitignore` ignores.
|
|
124
|
+
|
|
125
|
+
The only exception key is `nera-stan.ignores` in `package.json`, a list of globs relative to the repo root:
|
|
126
|
+
|
|
127
|
+
```json
|
|
128
|
+
{
|
|
129
|
+
"nera-stan": {
|
|
130
|
+
"ignores": ["test/markup/", "demo/demo.js"]
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Use it for generated or third-party files that live in the repo, such as built bundles or test fixtures copied from upstream. Anything other than an object with a list of non-empty strings under `ignores` is exit code 2. A single deliberate exception in code stays possible with an inline `// eslint-disable-next-line <rule> -- <reason>`.
|
|
136
|
+
|
|
137
|
+
## Rules
|
|
138
|
+
|
|
139
|
+
| Rule | Kinds | Files | `--fix` |
|
|
140
|
+
|---|---|---|---|
|
|
141
|
+
| [`nera/no-local-helper`](#nerano-local-helper) | all, except in `@nera-static/plugin-utils` and `@nera-static/test-utils` | every script except `assets/**` and `views/**` | no |
|
|
142
|
+
| [`nera/shared-logger`](#nerashared-logger) | `plugin` | runtime code | no |
|
|
143
|
+
| [`nera/no-copied-from`](#nerano-copied-from) | all | comments in every script; roadmap references in published files | no |
|
|
144
|
+
| [`nera/hook-shape`](#nerahook-shape) | `plugin` | the entry module and runtime code | adds the defaults to `({ app, pagesData })`, never `(data)` |
|
|
145
|
+
| [`nera/test-no-chdir`](#neratest-no-chdir) | all | tests | no |
|
|
146
|
+
| [`nera/test-no-cwd-path`](#neratest-no-cwd-path) | all | tests | no |
|
|
147
|
+
| [`nera/test-name`](#neratest-name) | all | tests | no |
|
|
148
|
+
| [`nera/test-helpers`](#neratest-helpers) | all, except in `@nera-static/test-utils` | tests | no |
|
|
149
|
+
|
|
150
|
+
**Tests** are the files under `test/` and every `*.test.js`. **Runtime code** is every file that is not a test and does not lie under `bin/`, `scripts/`, `views/` or `assets/`. A plugin's **entry module** is its `main`, else the root of its `exports`, else `index.js`.
|
|
151
|
+
|
|
152
|
+
The base config's fixes come from ESLint and its plugins: the `@stylistic` rules, `n/prefer-node-protocol`, `prefer-const` and the other core rules that fix themselves.
|
|
153
|
+
|
|
154
|
+
### `nera/no-local-helper`
|
|
155
|
+
|
|
156
|
+
A function must not copy a helper that `@nera-static/plugin-utils` exports. It is found in two ways:
|
|
157
|
+
|
|
158
|
+
- **By name:** a function declaration, or a function assigned to a variable, carries the name of a plugin-utils export. The names are the union of the exports of nera-stan's own plugin-utils and of the copy installed in the checked repo. Both copies are parsed, never imported.
|
|
159
|
+
- **By shape:** a function body that does what an export does: a plain-object check (`isPlainObject`), the page language `meta.lang || app.lang` (`pageLang`), hand-rolled XML or HTML escaping (`escapeXml`, `escapeHtml`) and semver parsing (`satisfiesFloor`). A shape fires only when plugin-utils actually exports the helper it points to.
|
|
160
|
+
|
|
161
|
+
So the rule grows with plugin-utils: the subpath exports such as `/values`, `/log`, `/lang`, `/html` and `/guard` come with plugin-utils 1.7.0 and 1.8.0, and their names and shapes count as soon as one of the two copies has them.
|
|
162
|
+
|
|
163
|
+
```js
|
|
164
|
+
// ✗ `isPlainObject` is a local copy of a @nera-static/plugin-utils helper
|
|
165
|
+
function isPlainObject(value) {
|
|
166
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// ✓
|
|
170
|
+
import { isPlainObject } from '@nera-static/plugin-utils/values'
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
The browser scripts under `assets/` and `views/` are exempt, because they cannot import plugin-utils without a bundler.
|
|
174
|
+
|
|
175
|
+
### `nera/shared-logger`
|
|
176
|
+
|
|
177
|
+
Plugin runtime code logs through the shared logger, never through a bare `console.*` call.
|
|
178
|
+
|
|
179
|
+
```js
|
|
180
|
+
// ✗ `console.warn` in plugin runtime code
|
|
181
|
+
console.warn('[plugin-sitemap] no origin configured')
|
|
182
|
+
|
|
183
|
+
// ✓
|
|
184
|
+
import { createLogger } from '@nera-static/plugin-utils/log'
|
|
185
|
+
|
|
186
|
+
const log = createLogger('plugin-sitemap')
|
|
187
|
+
log.warn('no origin configured')
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### `nera/no-copied-from`
|
|
191
|
+
|
|
192
|
+
No comment may mark code as a copy of another repo's, in any kind and any file. The note counts only when what follows "copied from" names a source of code: a Nera package or repo, a path, a `.js` file, or "the <name> plugin, theme, starter, repo or package". A note wrapped over consecutive `//` lines is read as one. Prose such as "bytes copied from the source" does not count.
|
|
193
|
+
|
|
194
|
+
The rule also reports a `ROADMAP…` reference in a comment or string of a file that `npm publish` ships (`files`, `main`, `bin` and `exports`, or every file that is not a test when `files` is absent). The roadmap specs are local only, so such a reference 404s on npm and GitHub.
|
|
195
|
+
|
|
196
|
+
```js
|
|
197
|
+
// ✗ comment "copied from nera-plugin-sitemap/src/guard.js" marks a copy
|
|
198
|
+
// copied from nera-plugin-sitemap/src/guard.js
|
|
199
|
+
|
|
200
|
+
// ✗ in a published file: `ROADMAP.md` 404s on npm and GitHub
|
|
201
|
+
// See ROADMAP.md D3 for why.
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
To fix it, import the shared code from plugin-utils (or ask for a new export there). If the code is the repo's own, delete the note. For a roadmap reference, state the decision in place.
|
|
205
|
+
|
|
206
|
+
### `nera/hook-shape`
|
|
207
|
+
|
|
208
|
+
In a plugin:
|
|
209
|
+
|
|
210
|
+
- The entry module declares every hook it has (`getAppData`, `getMetaData`, `getAssets`) as `export function` (or `export async function`), never through `export { … }` or `export const`.
|
|
211
|
+
- A hook takes no argument, or one destructured object with defaults: `({ app = {}, pagesData = [] } = {})`. It lists only the keys it uses, since `no-unused-vars` refuses the others, so `({ app = {} } = {})` and `({ pagesData = [] } = {})` are fine too.
|
|
212
|
+
- No runtime file reads its config at module scope (`getConfig`, `loadPluginConfig` or `loadConfig` outside a function), because the hooks must read the current file on every build.
|
|
213
|
+
|
|
214
|
+
```js
|
|
215
|
+
// ✗ `getMetaData(data)` must destructure its argument with defaults
|
|
216
|
+
export function getMetaData(data) { /* … */ }
|
|
217
|
+
|
|
218
|
+
// ✓
|
|
219
|
+
export function getMetaData({ app = {}, pagesData = [] } = {}) { /* … */ }
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
`--fix` rewrites only an argument that destructures nothing but `app` and `pagesData`, for example `({ app, pagesData })` → `({ app = {}, pagesData = [] } = {})`. It never rewrites `(data)`, because every `data.app` in the body would have to change too.
|
|
223
|
+
|
|
224
|
+
### `nera/test-no-chdir`
|
|
225
|
+
|
|
226
|
+
Tests never call `process.chdir` (also not through `import process from 'node:process'` or `import { chdir } from 'node:process'`). It moves the cwd of every test in the same worker. Pass the code under test its root instead, or use `useSiteCwd` from `@nera-static/test-utils`, which stubs `process.cwd()` and restores it.
|
|
227
|
+
|
|
228
|
+
### `nera/test-no-cwd-path`
|
|
229
|
+
|
|
230
|
+
Tests never resolve a relative path against the cwd, because such a test passes only when vitest runs from the repo root:
|
|
231
|
+
|
|
232
|
+
```js
|
|
233
|
+
// ✗
|
|
234
|
+
const view = path.resolve('views/partials/tags.pug')
|
|
235
|
+
|
|
236
|
+
// ✓
|
|
237
|
+
const view = fileURLToPath(new URL('../views/partials/tags.pug', import.meta.url))
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
The rule reports `path.resolve` and a `resolve` imported from `node:path` whose first argument is a relative string or template literal. Passing an absolute root the test owns as the first argument is fine.
|
|
241
|
+
|
|
242
|
+
### `nera/test-name`
|
|
243
|
+
|
|
244
|
+
The titles of `describe`, `suite`, `it`, `test` and `bench` (including `.skip`, `.only` and the call that `.each(table)` returns) do not start with "should". They state what the code does:
|
|
245
|
+
|
|
246
|
+
```js
|
|
247
|
+
// ✗
|
|
248
|
+
it('should copy the assets', () => { /* … */ })
|
|
249
|
+
|
|
250
|
+
// ✓
|
|
251
|
+
it('copies the assets', () => { /* … */ })
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
There is no fix, because dropping "should" also changes the verb form.
|
|
255
|
+
|
|
256
|
+
### `nera/test-helpers`
|
|
257
|
+
|
|
258
|
+
A test must not define its own copy of a `@nera-static/test-utils` helper. The names are the helper list nera-stan ships (`tempDir`, `writeFiles`, `makeSite`, `buildSite`, `useSiteCwd`, `captureConsole`, `readPublic`, `nera`, `scaffoldStarter`, `expectCleanBuild`, `listFiles`, `parseYaml`, `loadYaml`, `frontmatter`, `jsonLd`, `countTags`, `tags`, `elementByClass`, `attr`, `stripTags`, `contrast`, `publishTemplateSuite`), plus the exports of the test-utils copy installed in the checked repo. Once `@nera-static/test-utils` is published, nera-stan depends on it as it does on plugin-utils and takes the names from its exports instead of the shipped list.
|
|
259
|
+
|
|
260
|
+
```js
|
|
261
|
+
// ✗ `makeSite` in a test redefines a @nera-static/test-utils helper
|
|
262
|
+
function makeSite(files) { /* … */ }
|
|
263
|
+
|
|
264
|
+
// ✓
|
|
265
|
+
import { makeSite } from '@nera-static/test-utils'
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
## Editor integration
|
|
269
|
+
|
|
270
|
+
Editors read `eslint.config.js`, so a repo's config is a single line that re-exports nera-stan's:
|
|
271
|
+
|
|
272
|
+
```js
|
|
273
|
+
import nera from '@nera-static/stan'
|
|
274
|
+
|
|
275
|
+
export default [...nera]
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
The default export is the config for the repo in the current directory, which is the repo root when the editor starts ESLint there. To build it for another root, use `createConfig`:
|
|
279
|
+
|
|
280
|
+
```js
|
|
281
|
+
import { createConfig } from '@nera-static/stan'
|
|
282
|
+
|
|
283
|
+
export default createConfig({ root: '/path/to/repo' })
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
The package also exports `detectKind(root)`, `kindOf(pkg)`, `KINDS`, `ALWAYS_IGNORED` and `CONFIG_KEY` (`'nera-stan'`).
|
|
287
|
+
|
|
288
|
+
## 🧩 Compatibility
|
|
289
|
+
|
|
290
|
+
Node.js `^22.13.0 || >=24`, the range ESLint 10 and its plugins declare. ESLint, `@eslint/js`, `globals`, `@stylistic/eslint-plugin`, `eslint-plugin-n` and `espree` come with the package, and `@nera-static/plugin-utils` is a dependency for the helper names.
|
|
291
|
+
|
|
292
|
+
The rule ids, the report format, the exit codes, the flags, the repo kinds, the `nera-stan.ignores` key and the exported config are the public contract. A new rule or a stricter option fails consumers' CI, so it ships as a minor only when the whole fleet is already green under it, otherwise as a major.
|
|
293
|
+
|
|
294
|
+
## 🧪 Development
|
|
295
|
+
|
|
296
|
+
```bash
|
|
297
|
+
npx vitest run
|
|
298
|
+
npm run lint
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
`npm test` runs Vitest in watch mode. `npm run lint` runs `nera-stan` on this repo itself. The tests build throwaway repos in a temp directory.
|
|
302
|
+
|
|
303
|
+
## 🤝 Contributing
|
|
304
|
+
|
|
305
|
+
See the [contributing guide](https://github.com/seebaermichi/nera/blob/main/CONTRIBUTING.md).
|
|
306
|
+
|
|
307
|
+
## 📦 License
|
|
308
|
+
|
|
309
|
+
MIT
|
package/bin/nera-stan.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { EXIT, USAGE, UsageError, formatFinding, formatSummary, lint, parseArgs, version } from '../src/cli.js'
|
|
3
|
+
|
|
4
|
+
try {
|
|
5
|
+
const options = parseArgs(process.argv.slice(2))
|
|
6
|
+
if (options.help) {
|
|
7
|
+
process.stdout.write(`${USAGE}\n`)
|
|
8
|
+
} else if (options.version) {
|
|
9
|
+
process.stdout.write(`${version()}\n`)
|
|
10
|
+
} else {
|
|
11
|
+
const { findings, fixable } = await lint(options.dir, { fix: options.fix })
|
|
12
|
+
for (const finding of findings) {
|
|
13
|
+
process.stdout.write(`${formatFinding(finding)}\n`)
|
|
14
|
+
}
|
|
15
|
+
process.stderr.write(`${formatSummary(findings.length, fixable)}\n`)
|
|
16
|
+
process.exitCode = findings.length > 0 ? EXIT.findings : EXIT.clean
|
|
17
|
+
}
|
|
18
|
+
} catch (error) {
|
|
19
|
+
const hint = error instanceof UsageError ? ' (see nera-stan --help)' : ''
|
|
20
|
+
process.stderr.write(`nera-stan: ${error.message}${hint}\n`)
|
|
21
|
+
process.exitCode = EXIT.error
|
|
22
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,61 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@nera-static/stan",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "The handwriting check of a Nera repo: ESLint with the Nera rules, one report format, one exit code.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"nera-stan": "bin/nera-stan.js"
|
|
8
|
+
},
|
|
9
|
+
"exports": {
|
|
10
|
+
".": "./src/index.js"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"bin",
|
|
14
|
+
"src",
|
|
15
|
+
"CHANGELOG.md"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"lint": "node bin/nera-stan.js",
|
|
19
|
+
"test": "vitest",
|
|
20
|
+
"prepare": "husky"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"nera",
|
|
24
|
+
"eslint",
|
|
25
|
+
"eslint-config",
|
|
26
|
+
"eslintplugin",
|
|
27
|
+
"lint",
|
|
28
|
+
"style"
|
|
29
|
+
],
|
|
30
|
+
"author": "Michael Becker",
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+https://github.com/seebaermichi/nera-stan.git"
|
|
35
|
+
},
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://github.com/seebaermichi/nera-stan/issues"
|
|
38
|
+
},
|
|
39
|
+
"homepage": "https://github.com/seebaermichi/nera-stan#readme",
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
},
|
|
43
|
+
"engines": {
|
|
44
|
+
"node": "^22.13.0 || >=24"
|
|
45
|
+
},
|
|
46
|
+
"sideEffects": false,
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"@eslint-community/eslint-plugin-eslint-comments": "^4.8.1",
|
|
49
|
+
"@eslint/js": "^10.0.1",
|
|
50
|
+
"@nera-static/plugin-utils": "^1.8.0",
|
|
51
|
+
"@stylistic/eslint-plugin": "^6.0.1",
|
|
52
|
+
"eslint": "^10.16.0",
|
|
53
|
+
"eslint-plugin-n": "^18.6.0",
|
|
54
|
+
"espree": "^11.2.0",
|
|
55
|
+
"globals": "^17.14.0"
|
|
56
|
+
},
|
|
57
|
+
"devDependencies": {
|
|
58
|
+
"husky": "^9.1.7",
|
|
59
|
+
"vitest": "^5.1.2"
|
|
60
|
+
}
|
|
61
|
+
}
|
package/src/cli.js
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { realpathSync } from 'node:fs'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import { fileURLToPath } from 'node:url'
|
|
4
|
+
import { parseArgs as parseNodeArgs } from 'node:util'
|
|
5
|
+
|
|
6
|
+
import { isInside } from '@nera-static/plugin-utils/urls'
|
|
7
|
+
import { ESLint } from 'eslint'
|
|
8
|
+
|
|
9
|
+
import { createConfig } from './eslint/config.js'
|
|
10
|
+
import { readPackage } from './kind.js'
|
|
11
|
+
|
|
12
|
+
/** Exit codes of the `nera-stan` bin (D2). */
|
|
13
|
+
export const EXIT = Object.freeze({ clean: 0, findings: 1, error: 2 })
|
|
14
|
+
|
|
15
|
+
/** The `--help` text. */
|
|
16
|
+
export const USAGE = `Usage: nera-stan [dir] [--fix]
|
|
17
|
+
|
|
18
|
+
Check the Nera repo at <dir> (default: the current directory) and print one
|
|
19
|
+
finding per line as "<file>:<line> <rule> <message>".
|
|
20
|
+
|
|
21
|
+
Options:
|
|
22
|
+
--fix apply the fixes that have one obviously correct result
|
|
23
|
+
--help show this text
|
|
24
|
+
--version show the nera-stan version
|
|
25
|
+
|
|
26
|
+
Exit codes: 0 clean, 1 findings, 2 usage or internal error.`
|
|
27
|
+
|
|
28
|
+
/** Rule names for ESLint messages that carry no rule id. */
|
|
29
|
+
const PARSE_ERROR = 'parse-error'
|
|
30
|
+
const DIRECTIVE = 'eslint-directive'
|
|
31
|
+
|
|
32
|
+
/** An error in how nera-stan was called; the bin prints it with the usage hint. */
|
|
33
|
+
export class UsageError extends Error {}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Parse the bin's arguments.
|
|
37
|
+
*
|
|
38
|
+
* @param {string[]} argv - The arguments after `node nera-stan`.
|
|
39
|
+
* @returns {{ dir: string, fix: boolean, help: boolean, version: boolean }}
|
|
40
|
+
* @throws {UsageError} On an unknown option or more than one directory.
|
|
41
|
+
*/
|
|
42
|
+
export function parseArgs(argv) {
|
|
43
|
+
let parsed
|
|
44
|
+
try {
|
|
45
|
+
parsed = parseNodeArgs({
|
|
46
|
+
args: argv,
|
|
47
|
+
allowPositionals: true,
|
|
48
|
+
strict: true,
|
|
49
|
+
options: {
|
|
50
|
+
fix: { type: 'boolean', default: false },
|
|
51
|
+
help: { type: 'boolean', default: false },
|
|
52
|
+
version: { type: 'boolean', default: false },
|
|
53
|
+
},
|
|
54
|
+
})
|
|
55
|
+
} catch (error) {
|
|
56
|
+
throw new UsageError(error.message, { cause: error })
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const { positionals, values } = parsed
|
|
60
|
+
if (positionals.length > 1) {
|
|
61
|
+
throw new UsageError(`expected at most one directory, got ${positionals.length}: ${positionals.join(' ')}`)
|
|
62
|
+
}
|
|
63
|
+
return { dir: positionals[0] ?? '.', ...values }
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The version of this nera-stan package.
|
|
68
|
+
*
|
|
69
|
+
* @returns {string}
|
|
70
|
+
*/
|
|
71
|
+
export function version() {
|
|
72
|
+
return readPackage(fileURLToPath(new URL('..', import.meta.url))).version
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Lint the repo at `dir` with the shipped config; its own `eslint.config.js` is
|
|
77
|
+
* never read (D3). A file whose real path lies outside `dir` (a symlink out of the
|
|
78
|
+
* repo) is neither reported nor fixed, so `fix` never writes outside `dir`.
|
|
79
|
+
*
|
|
80
|
+
* @param {string} dir - The repo's root directory.
|
|
81
|
+
* @param {object} [options]
|
|
82
|
+
* @param {boolean} [options.fix=false] - Write fixes back to disk.
|
|
83
|
+
* @returns {Promise<{ findings: { file: string, line: number, rule: string, message: string }[], fixable: number }>}
|
|
84
|
+
* The remaining findings, sorted by file and line, and how many of them `--fix` would remove.
|
|
85
|
+
* @throws {Error} When the repo's `package.json` or its `nera-stan` key is invalid.
|
|
86
|
+
*/
|
|
87
|
+
export async function lint(dir, { fix = false } = {}) {
|
|
88
|
+
const root = path.resolve(dir)
|
|
89
|
+
const eslint = new ESLint({
|
|
90
|
+
cwd: root,
|
|
91
|
+
overrideConfigFile: true,
|
|
92
|
+
overrideConfig: createConfig({ root }),
|
|
93
|
+
errorOnUnmatchedPattern: false,
|
|
94
|
+
fix,
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
const realRoot = realpathSync(root)
|
|
98
|
+
const results = (await eslint.lintFiles(['.']))
|
|
99
|
+
.filter((result) => isInside(realRoot, realpathSync(result.filePath), { self: false }))
|
|
100
|
+
if (fix) {
|
|
101
|
+
await ESLint.outputFixes(results)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const findings = results.flatMap((result) => result.messages.map((message) => ({
|
|
105
|
+
file: path.relative(root, result.filePath).split(path.sep).join('/'),
|
|
106
|
+
line: message.line ?? 1,
|
|
107
|
+
column: message.column ?? 1,
|
|
108
|
+
rule: message.ruleId ?? (message.fatal ? PARSE_ERROR : DIRECTIVE),
|
|
109
|
+
message: message.message,
|
|
110
|
+
})))
|
|
111
|
+
findings.sort((a, b) => compare(a.file, b.file) || a.line - b.line || a.column - b.column)
|
|
112
|
+
|
|
113
|
+
const fixable = results.reduce((sum, result) => sum + result.fixableErrorCount + result.fixableWarningCount, 0)
|
|
114
|
+
return { findings: findings.map(({ file, line, rule, message }) => ({ file, line, rule, message })), fixable }
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* One report line: `<file>:<line> <rule> <message>`.
|
|
119
|
+
*
|
|
120
|
+
* @param {{ file: string, line: number, rule: string, message: string }} finding
|
|
121
|
+
* @returns {string}
|
|
122
|
+
*/
|
|
123
|
+
export function formatFinding({ file, line, rule, message }) {
|
|
124
|
+
return `${file}:${line} ${rule} ${message}`
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* The summary line printed to stderr.
|
|
129
|
+
*
|
|
130
|
+
* @param {number} problems - The number of findings.
|
|
131
|
+
* @param {number} fixable - How many of them `--fix` would remove.
|
|
132
|
+
* @returns {string}
|
|
133
|
+
*/
|
|
134
|
+
export function formatSummary(problems, fixable) {
|
|
135
|
+
return `nera-stan: ${problems} problems (${fixable} fixable with --fix)`
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function compare(a, b) {
|
|
139
|
+
if (a === b) {
|
|
140
|
+
return 0
|
|
141
|
+
}
|
|
142
|
+
return a < b ? -1 : 1
|
|
143
|
+
}
|