@nera-static/stan 1.0.1 → 1.1.0
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 +16 -0
- package/README.md +147 -15
- package/canonical/editorconfig +12 -0
- package/canonical/github/FUNDING.yml +13 -0
- package/canonical/github/dependabot.yml +20 -0
- package/canonical/github/workflows/publish.yml +64 -0
- package/canonical/github/workflows/test.yml +37 -0
- package/canonical/husky/pre-push +1 -0
- package/canonical/variants/chokidar-major/github/dependabot.yml +25 -0
- package/canonical/variants/desktop-release/github/workflows/publish.yml +104 -0
- package/canonical/variants/dist-check/github/workflows/test.yml +43 -0
- package/canonical/variants/image-cache/github/workflows/publish.yml +73 -0
- package/canonical/variants/image-cache/github/workflows/test.yml +46 -0
- package/canonical/variants/validate-templates/github/workflows/publish.yml +64 -0
- package/canonical/variants/validate-templates/github/workflows/test.yml +40 -0
- package/canonical/variants/windows-smoke/github/workflows/test.yml +57 -0
- package/canonical/vitest.config.js +3 -0
- package/package.json +24 -12
- package/src/bem.js +35 -0
- package/src/checks/canonical.js +368 -0
- package/src/checks/css.js +131 -0
- package/src/checks/docs.js +405 -0
- package/src/checks/package.js +352 -0
- package/src/checks/pug.js +369 -0
- package/src/checks/tree.js +544 -0
- package/src/cli.js +49 -39
- package/src/eslint/check.js +64 -0
- package/src/eslint/config.js +45 -28
- package/src/eslint/files.js +37 -0
- package/src/markdown.js +60 -0
- package/src/repo-files.js +62 -1
- package/src/settings.js +136 -0
- package/src/stylelint/config.js +122 -0
- package/src/stylelint/index.js +15 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { realpathSync } from 'node:fs'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
|
|
4
|
+
import { isInside } from '@nera-static/plugin-utils/urls'
|
|
5
|
+
import { ESLint } from 'eslint'
|
|
6
|
+
|
|
7
|
+
import { configuredRules, createConfig } from './config.js'
|
|
8
|
+
|
|
9
|
+
/** Rule names for ESLint messages that carry no rule id. */
|
|
10
|
+
export const PARSE_ERROR = 'parse-error'
|
|
11
|
+
export const DIRECTIVE = 'eslint-directive'
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The code check: ESLint with the shipped config; the repo's own `eslint.config.js`
|
|
15
|
+
* is never read (D3). A file whose real path lies outside the repo (a symlink out of
|
|
16
|
+
* it) is neither reported nor fixed, so `fix` never writes outside the repo.
|
|
17
|
+
*/
|
|
18
|
+
export const eslintCheck = Object.freeze({
|
|
19
|
+
name: 'eslint',
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Every rule id this check can report for the repo.
|
|
23
|
+
*
|
|
24
|
+
* @param {{ root: string, pkg: object, settings: object }} context
|
|
25
|
+
* @returns {string[]}
|
|
26
|
+
*/
|
|
27
|
+
rules({ root, pkg, settings }) {
|
|
28
|
+
return [...configuredRules(createConfig({ root, pkg, settings })), PARSE_ERROR, DIRECTIVE]
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Lint the repo.
|
|
33
|
+
*
|
|
34
|
+
* @param {{ root: string, pkg: object, settings: object, fix: boolean }} context
|
|
35
|
+
* @returns {Promise<{ findings: { file: string, line: number, column: number, rule: string,
|
|
36
|
+
* message: string, fixable: boolean }[] }>}
|
|
37
|
+
*/
|
|
38
|
+
async run({ root, pkg, settings, fix }) {
|
|
39
|
+
const eslint = new ESLint({
|
|
40
|
+
cwd: root,
|
|
41
|
+
overrideConfigFile: true,
|
|
42
|
+
overrideConfig: createConfig({ root, pkg, settings }),
|
|
43
|
+
errorOnUnmatchedPattern: false,
|
|
44
|
+
fix,
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
const realRoot = realpathSync(root)
|
|
48
|
+
const results = (await eslint.lintFiles(['.']))
|
|
49
|
+
.filter((result) => isInside(realRoot, realpathSync(result.filePath), { self: false }))
|
|
50
|
+
if (fix) {
|
|
51
|
+
await ESLint.outputFixes(results)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const findings = results.flatMap((result) => result.messages.map((message) => ({
|
|
55
|
+
file: path.relative(root, result.filePath).split(path.sep).join('/'),
|
|
56
|
+
line: message.line ?? 1,
|
|
57
|
+
column: message.column ?? 1,
|
|
58
|
+
rule: message.ruleId ?? (message.fatal ? PARSE_ERROR : DIRECTIVE),
|
|
59
|
+
message: message.message,
|
|
60
|
+
fixable: message.fix !== undefined,
|
|
61
|
+
})))
|
|
62
|
+
return { findings }
|
|
63
|
+
},
|
|
64
|
+
})
|
package/src/eslint/config.js
CHANGED
|
@@ -3,7 +3,6 @@ import path from 'node:path'
|
|
|
3
3
|
|
|
4
4
|
import comments from '@eslint-community/eslint-plugin-eslint-comments'
|
|
5
5
|
import js from '@eslint/js'
|
|
6
|
-
import { isPlainObject } from '@nera-static/plugin-utils/values'
|
|
7
6
|
import stylistic from '@stylistic/eslint-plugin'
|
|
8
7
|
import { includeIgnoreFile } from 'eslint/config'
|
|
9
8
|
import n from 'eslint-plugin-n'
|
|
@@ -13,10 +12,10 @@ import { HELPER_REPOS, helperNames } from '../helper-names.js'
|
|
|
13
12
|
import { kindOf, readPackage } from '../kind.js'
|
|
14
13
|
import { entryFile, publishedPatterns } from '../repo-files.js'
|
|
15
14
|
import nera from '../rules/index.js'
|
|
15
|
+
import { readSettings } from '../settings.js'
|
|
16
16
|
import { TEST_UTILS, testHelperNames } from '../test-helper-names.js'
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
export const CONFIG_KEY = 'nera-stan'
|
|
18
|
+
export { CONFIG_KEY } from '../settings.js'
|
|
20
19
|
|
|
21
20
|
/** Paths nera-stan never checks, in any repo (D4). */
|
|
22
21
|
export const ALWAYS_IGNORED = Object.freeze([
|
|
@@ -33,6 +32,29 @@ const BROWSER_SCRIPTS = '**/assets/**/*.{js,mjs}'
|
|
|
33
32
|
const VIEW_SCRIPTS = ['views/**/*.{js,mjs}']
|
|
34
33
|
const TESTS = ['test/**/*.{js,mjs,cjs}', '**/*.test.{js,mjs,cjs}']
|
|
35
34
|
|
|
35
|
+
/**
|
|
36
|
+
* The flat config objects that leave a file unchecked: `ALWAYS_IGNORED`, the repo's
|
|
37
|
+
* `.gitignore` and `nera-stan.ignores`. Every check that lets ESLint find its files
|
|
38
|
+
* starts from these, so code and templates are ignored alike.
|
|
39
|
+
*
|
|
40
|
+
* @param {object} options
|
|
41
|
+
* @param {string} options.root - The checked repo's root directory.
|
|
42
|
+
* @param {{ ignores: string[] }} options.settings - The repo's `nera-stan` settings.
|
|
43
|
+
* @returns {object[]}
|
|
44
|
+
*/
|
|
45
|
+
export function ignoreConfigs({ root, settings }) {
|
|
46
|
+
const gitignore = path.join(root, '.gitignore')
|
|
47
|
+
return [
|
|
48
|
+
{ name: 'nera-stan/ignores', ignores: [...ALWAYS_IGNORED] },
|
|
49
|
+
...(existsSync(gitignore)
|
|
50
|
+
? [includeIgnoreFile(gitignore, { name: 'nera-stan/gitignore', gitignoreResolution: true })]
|
|
51
|
+
: []),
|
|
52
|
+
...(settings.ignores.length > 0
|
|
53
|
+
? [{ name: 'nera-stan/package-ignores', ignores: [...settings.ignores] }]
|
|
54
|
+
: []),
|
|
55
|
+
]
|
|
56
|
+
}
|
|
57
|
+
|
|
36
58
|
/**
|
|
37
59
|
* Build the flat ESLint config nera-stan checks a repo with (D3, D4).
|
|
38
60
|
*
|
|
@@ -43,24 +65,20 @@ const TESTS = ['test/**/*.{js,mjs,cjs}', '**/*.test.{js,mjs,cjs}']
|
|
|
43
65
|
* specifiers, for `nera/no-local-helper`; collected from nera-stan's and the repo's copies when absent.
|
|
44
66
|
* @param {Record<string, string>} [options.testHelpers] - The test-utils helper names and their import
|
|
45
67
|
* specifiers, for `nera/test-helpers`; the shipped list and the repo's installed copy when absent.
|
|
68
|
+
* @param {{ ignores: string[], skip: Record<string, string> }} [options.settings] - The repo's
|
|
69
|
+
* `nera-stan` settings; read from `pkg` when absent. A skipped ESLint rule is switched off.
|
|
46
70
|
* @returns {object[]} The flat config array.
|
|
47
|
-
* @throws {Error} When `package.json` is missing or invalid, or its `nera-stan` key is
|
|
48
|
-
* an object whose `ignores` is a list of globs.
|
|
71
|
+
* @throws {Error} When `package.json` is missing or invalid, or its `nera-stan` key is invalid.
|
|
49
72
|
*/
|
|
50
73
|
export function createConfig({
|
|
51
74
|
root = process.cwd(),
|
|
52
75
|
pkg = readPackage(root),
|
|
53
76
|
helpers = helperNames({ root }),
|
|
54
77
|
testHelpers = testHelperNames({ root }),
|
|
78
|
+
settings = readSettings(pkg, root),
|
|
55
79
|
} = {}) {
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
return [
|
|
59
|
-
{ name: 'nera-stan/ignores', ignores: [...ALWAYS_IGNORED] },
|
|
60
|
-
...(existsSync(gitignore)
|
|
61
|
-
? [includeIgnoreFile(gitignore, { name: 'nera-stan/gitignore', gitignoreResolution: true })]
|
|
62
|
-
: []),
|
|
63
|
-
...repoIgnores(pkg, root),
|
|
80
|
+
const config = [
|
|
81
|
+
...ignoreConfigs({ root, settings }),
|
|
64
82
|
{ name: 'nera-stan/recommended', files: [SCRIPTS], ...js.configs.recommended },
|
|
65
83
|
{
|
|
66
84
|
name: 'nera-stan/base',
|
|
@@ -139,21 +157,20 @@ export function createConfig({
|
|
|
139
157
|
languageOptions: { globals: { ...globals.node, ...globals.vitest } },
|
|
140
158
|
},
|
|
141
159
|
]
|
|
142
|
-
}
|
|
143
160
|
|
|
144
|
-
|
|
145
|
-
const
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
161
|
+
const rules = configuredRules(config)
|
|
162
|
+
const skipped = Object.keys(settings.skip).filter((id) => rules.has(id))
|
|
163
|
+
return skipped.length > 0
|
|
164
|
+
? [...config, { name: 'nera-stan/skip', rules: Object.fromEntries(skipped.map((id) => [id, 'off'])) }]
|
|
165
|
+
: config
|
|
166
|
+
}
|
|
149
167
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
}
|
|
158
|
-
return ignores.length > 0 ? [{ name: 'nera-stan/package-ignores', ignores: [...ignores] }] : []
|
|
168
|
+
/**
|
|
169
|
+
* The ids of every rule a flat config sets, whatever its severity.
|
|
170
|
+
*
|
|
171
|
+
* @param {object[]} config - A flat config array.
|
|
172
|
+
* @returns {Set<string>}
|
|
173
|
+
*/
|
|
174
|
+
export function configuredRules(config) {
|
|
175
|
+
return new Set(config.flatMap((entry) => Object.keys(entry.rules ?? {})))
|
|
159
176
|
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { realpathSync } from 'node:fs'
|
|
2
|
+
|
|
3
|
+
import { isInside } from '@nera-static/plugin-utils/urls'
|
|
4
|
+
import { ESLint } from 'eslint'
|
|
5
|
+
|
|
6
|
+
import { ignoreConfigs } from './config.js'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Lets ESLint find files without parsing them: no code block, no message.
|
|
10
|
+
* ESLint is used only to list the files, so `.gitignore`, `ALWAYS_IGNORED` and
|
|
11
|
+
* `nera-stan.ignores` mean the same for every check that lists its files here.
|
|
12
|
+
*/
|
|
13
|
+
const LIST_ONLY = Object.freeze({ preprocess: () => [], postprocess: () => [] })
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The repo's files that match `pattern` and are not ignored, as absolute paths. A file
|
|
17
|
+
* whose real path lies outside the repo (a symlink out of it) is left out.
|
|
18
|
+
*
|
|
19
|
+
* @param {{ root: string, settings: { ignores: string[] } }} context - The checked repo and its settings.
|
|
20
|
+
* @param {string} pattern - A glob relative to the repo root, e.g. `**\/*.pug`.
|
|
21
|
+
* @returns {Promise<string[]>}
|
|
22
|
+
*/
|
|
23
|
+
export async function listFiles({ root, settings }, pattern) {
|
|
24
|
+
const eslint = new ESLint({
|
|
25
|
+
cwd: root,
|
|
26
|
+
overrideConfigFile: true,
|
|
27
|
+
overrideConfig: [
|
|
28
|
+
...ignoreConfigs({ root, settings }),
|
|
29
|
+
{ name: 'nera-stan/list-files', files: [pattern], processor: LIST_ONLY },
|
|
30
|
+
],
|
|
31
|
+
errorOnUnmatchedPattern: false,
|
|
32
|
+
})
|
|
33
|
+
const realRoot = realpathSync(root)
|
|
34
|
+
return (await eslint.lintFiles([pattern]))
|
|
35
|
+
.map((result) => result.filePath)
|
|
36
|
+
.filter((file) => isInside(realRoot, realpathSync(file), { self: false }))
|
|
37
|
+
}
|
package/src/markdown.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
const FENCE = /^\s*(`{3,}|~{3,})/
|
|
2
|
+
const HEADING = /^ {0,3}(#{1,6})[ \t]+(.*?)(?:[ \t]+#+)?[ \t]*$/
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The lines of a Markdown text that lie outside fenced code blocks, fence lines excluded.
|
|
6
|
+
* A fence closes on a line of the same character, at least as long, and nothing else; an
|
|
7
|
+
* indented fence (inside a list item) counts too. An unclosed fence runs to the end.
|
|
8
|
+
*
|
|
9
|
+
* @param {string} source - The Markdown text.
|
|
10
|
+
* @returns {{ text: string, line: number }[]} Each line with its 1-based number.
|
|
11
|
+
*/
|
|
12
|
+
export function proseLines(source) {
|
|
13
|
+
const lines = []
|
|
14
|
+
let fence = null
|
|
15
|
+
source.split(/\r?\n/).forEach((text, index) => {
|
|
16
|
+
const match = FENCE.exec(text)
|
|
17
|
+
if (fence === null && match) {
|
|
18
|
+
fence = match[1]
|
|
19
|
+
} else if (fence !== null) {
|
|
20
|
+
if (closes(fence, match, text)) {
|
|
21
|
+
fence = null
|
|
22
|
+
}
|
|
23
|
+
} else {
|
|
24
|
+
lines.push({ text, line: index + 1 })
|
|
25
|
+
}
|
|
26
|
+
})
|
|
27
|
+
return lines
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Whether a line closes the open fence: the same character, at least as many, nothing after them. */
|
|
31
|
+
function closes(fence, match, text) {
|
|
32
|
+
return match !== null &&
|
|
33
|
+
match[1][0] === fence[0] &&
|
|
34
|
+
match[1].length >= fence.length &&
|
|
35
|
+
text.trim() === match[1]
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The ATX headings of a Markdown text outside fenced code blocks.
|
|
40
|
+
*
|
|
41
|
+
* @param {string} source - The Markdown text.
|
|
42
|
+
* @returns {{ level: number, text: string, line: number }[]} `text` without the `#` marks.
|
|
43
|
+
*/
|
|
44
|
+
export function headings(source) {
|
|
45
|
+
return proseLines(source).flatMap(({ text, line }) => {
|
|
46
|
+
const heading = headingOf(text)
|
|
47
|
+
return heading ? [{ ...heading, line }] : []
|
|
48
|
+
})
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The ATX heading one line holds.
|
|
53
|
+
*
|
|
54
|
+
* @param {string} text - One line of Markdown, outside a code block.
|
|
55
|
+
* @returns {{ level: number, text: string } | null} `text` without the `#` marks; `null` for no heading.
|
|
56
|
+
*/
|
|
57
|
+
export function headingOf(text) {
|
|
58
|
+
const match = HEADING.exec(text)
|
|
59
|
+
return match ? { level: match[1].length, text: match[2] } : null
|
|
60
|
+
}
|
package/src/repo-files.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import { lstatSync, mkdirSync, realpathSync, statSync, writeFileSync } from 'node:fs'
|
|
1
2
|
import path from 'node:path'
|
|
2
3
|
|
|
3
4
|
import { escapeRegExp } from '@nera-static/plugin-utils/html'
|
|
5
|
+
import { isInside } from '@nera-static/plugin-utils/urls'
|
|
4
6
|
import { isPlainObject } from '@nera-static/plugin-utils/values'
|
|
5
7
|
|
|
6
8
|
/** Top-level folders that hold no plugin runtime code (D8). */
|
|
@@ -10,6 +12,10 @@ const GLOB = /[*?]/
|
|
|
10
12
|
const GLOB_PARTS = Object.freeze({ '**/': '(?:.*/)?', '**': '.*', '*': '[^/]*', '?': '[^/]' })
|
|
11
13
|
const CONDITIONS = Object.freeze(['import', 'node', 'default'])
|
|
12
14
|
|
|
15
|
+
/** What to do when `writeInside` refused a write. */
|
|
16
|
+
export const OUTSIDE_REPO = 'nera-stan --fix writes only inside the repo, and this path leads out of it through a ' +
|
|
17
|
+
'symlink: replace the symlink with the file'
|
|
18
|
+
|
|
13
19
|
/**
|
|
14
20
|
* The path of `filename` relative to the repo root, with `/` separators.
|
|
15
21
|
*
|
|
@@ -104,7 +110,62 @@ function globToRegExp(pattern) {
|
|
|
104
110
|
return new RegExp(`^${source}(?:/.*)?$`)
|
|
105
111
|
}
|
|
106
112
|
|
|
107
|
-
|
|
113
|
+
/**
|
|
114
|
+
* Whether `<base>/<dir>` is a directory.
|
|
115
|
+
*
|
|
116
|
+
* @param {string} base - A directory.
|
|
117
|
+
* @param {string} dir - A path relative to `base`.
|
|
118
|
+
* @returns {boolean}
|
|
119
|
+
*/
|
|
120
|
+
export function isDirectory(base, dir) {
|
|
121
|
+
try {
|
|
122
|
+
return statSync(path.join(base, dir)).isDirectory()
|
|
123
|
+
} catch {
|
|
124
|
+
return false
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Write `text` to `<root>/<file>` when the target stays inside the repo, also through a
|
|
130
|
+
* symlinked folder or file; a dangling symlink on the way is never written through.
|
|
131
|
+
*
|
|
132
|
+
* @param {string} root - The repo's root directory.
|
|
133
|
+
* @param {string} file - A repo-relative path.
|
|
134
|
+
* @param {string} text - The file's new content.
|
|
135
|
+
* @returns {boolean} Whether the file was written.
|
|
136
|
+
*/
|
|
137
|
+
export function writeInside(root, file, text) {
|
|
138
|
+
const target = path.join(root, file)
|
|
139
|
+
let existing = target
|
|
140
|
+
while (!isEntry(existing)) {
|
|
141
|
+
existing = path.dirname(existing)
|
|
142
|
+
}
|
|
143
|
+
let real
|
|
144
|
+
try {
|
|
145
|
+
real = realpathSync(existing)
|
|
146
|
+
} catch {
|
|
147
|
+
return false
|
|
148
|
+
}
|
|
149
|
+
if (!isInside(realpathSync(root), real, { self: existing === root })) {
|
|
150
|
+
return false
|
|
151
|
+
}
|
|
152
|
+
mkdirSync(path.dirname(target), { recursive: true })
|
|
153
|
+
writeFileSync(target, text)
|
|
154
|
+
return true
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Whether a path exists as a file, folder or symlink (a dangling one included). */
|
|
158
|
+
function isEntry(target) {
|
|
159
|
+
try {
|
|
160
|
+
lstatSync(target)
|
|
161
|
+
return true
|
|
162
|
+
} catch {
|
|
163
|
+
return false
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** A `files`/`bin` path without its leading `./`. */
|
|
168
|
+
export function normalize(entry) {
|
|
108
169
|
return entry.replace(/^\.\//, '')
|
|
109
170
|
}
|
|
110
171
|
|
package/src/settings.js
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
|
|
3
|
+
import { isPlainObject } from '@nera-static/plugin-utils/values'
|
|
4
|
+
|
|
5
|
+
/** The package.json key that holds a repo's exceptions (D4). */
|
|
6
|
+
export const CONFIG_KEY = 'nera-stan'
|
|
7
|
+
|
|
8
|
+
/** The keys the `nera-stan` object may hold. */
|
|
9
|
+
export const SETTINGS_KEYS = Object.freeze(['ignores', 'variants', 'skip'])
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The named variants of the canonical files, as `{ <canonical file>: [<variant>, ...] }`:
|
|
13
|
+
* the deviations the 2026-11-27 inventory names (4-tooling-themes §1.4). A repo picks one
|
|
14
|
+
* through `nera-stan.variants`; the README lists every entry. `image-cache`: the
|
|
15
|
+
* `actions/cache` step for plugin-images' `.nera-cache` (theme-ansitz, theme-umwege and
|
|
16
|
+
* their starters); `windows-smoke`, `desktop-release`: nera-mcp's Windows job and release
|
|
17
|
+
* job; `dist-check`: highlightjs-pug's stale-`dist/` check; `validate-templates`:
|
|
18
|
+
* theme-example, which compiles its templates instead of linting and testing;
|
|
19
|
+
* `chokidar-major`: nera-cli's Dependabot ignore of chokidar majors.
|
|
20
|
+
*/
|
|
21
|
+
export const VARIANTS = Object.freeze({
|
|
22
|
+
'.github/workflows/test.yml': Object.freeze(['image-cache', 'windows-smoke', 'dist-check', 'validate-templates']),
|
|
23
|
+
'.github/workflows/publish.yml': Object.freeze(['image-cache', 'desktop-release', 'validate-templates']),
|
|
24
|
+
'.github/dependabot.yml': Object.freeze(['chokidar-major']),
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Read and validate the `nera-stan` key of a parsed `package.json`. Rule ids under
|
|
29
|
+
* `skip` are checked against the checks' rules by `assertKnownRules`.
|
|
30
|
+
*
|
|
31
|
+
* @param {object} pkg - The repo's parsed `package.json`.
|
|
32
|
+
* @param {string} root - The repo's root directory, for the error messages.
|
|
33
|
+
* @returns {{ ignores: string[], variants: Record<string, string>, skip: Record<string, string> }}
|
|
34
|
+
* @throws {Error} When the key is not an object, holds an unknown key, or one of its values is invalid.
|
|
35
|
+
*/
|
|
36
|
+
export function readSettings(pkg, root) {
|
|
37
|
+
const file = path.join(root, 'package.json')
|
|
38
|
+
const own = pkg[CONFIG_KEY] === undefined ? {} : pkg[CONFIG_KEY]
|
|
39
|
+
if (!isPlainObject(own)) {
|
|
40
|
+
throw invalidIgnores(file)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const unknown = Object.keys(own).filter((key) => !SETTINGS_KEYS.includes(key))
|
|
44
|
+
if (unknown.length > 0) {
|
|
45
|
+
throw new Error(
|
|
46
|
+
`${file}: "${CONFIG_KEY}" allows only the keys ${quoteList(SETTINGS_KEYS)}, ` +
|
|
47
|
+
`not ${quoteList(unknown)}: rename or remove them`,
|
|
48
|
+
)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
ignores: readIgnores(own.ignores, file),
|
|
53
|
+
variants: readVariants(own.variants, file),
|
|
54
|
+
skip: readSkip(own.skip, file),
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Make sure every rule id under `nera-stan.skip` names a rule one of the checks reports.
|
|
60
|
+
*
|
|
61
|
+
* @param {{ skip: Record<string, string> }} settings - The result of `readSettings`.
|
|
62
|
+
* @param {Iterable<string>} ruleIds - Every rule id the checks can report.
|
|
63
|
+
* @param {string} root - The repo's root directory, for the error message.
|
|
64
|
+
* @throws {Error} When `skip` names a rule id no check knows.
|
|
65
|
+
*/
|
|
66
|
+
export function assertKnownRules(settings, ruleIds, root) {
|
|
67
|
+
const known = new Set(ruleIds)
|
|
68
|
+
const unknown = Object.keys(settings.skip).filter((id) => !known.has(id))
|
|
69
|
+
if (unknown.length > 0) {
|
|
70
|
+
throw new Error(
|
|
71
|
+
`${path.join(root, 'package.json')}: "${CONFIG_KEY}.skip" names rule ids no check reports, ` +
|
|
72
|
+
`${quoteList(unknown)}: use a rule id exactly as a nera-stan report line prints it, or remove the entry`,
|
|
73
|
+
)
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function readIgnores(ignores = [], file) {
|
|
78
|
+
const valid = Array.isArray(ignores) && ignores.every((glob) => typeof glob === 'string' && glob !== '')
|
|
79
|
+
if (!valid) {
|
|
80
|
+
throw invalidIgnores(file)
|
|
81
|
+
}
|
|
82
|
+
return [...ignores]
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function readVariants(variants = {}, file) {
|
|
86
|
+
const where = `${file}: "${CONFIG_KEY}.variants"`
|
|
87
|
+
if (!isPlainObject(variants)) {
|
|
88
|
+
throw new Error(
|
|
89
|
+
`${where} must be an object of canonical file → variant name, e.g. { ".editorconfig": "<variant>" }`,
|
|
90
|
+
)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
for (const [canonical, variant] of Object.entries(variants)) {
|
|
94
|
+
const allowed = VARIANTS[canonical]
|
|
95
|
+
if (allowed === undefined) {
|
|
96
|
+
throw new Error(
|
|
97
|
+
`${where} names "${canonical}", which has no variants: remove the entry; ` +
|
|
98
|
+
`files with variants: ${quoteList(Object.keys(VARIANTS))}`,
|
|
99
|
+
)
|
|
100
|
+
}
|
|
101
|
+
if (!allowed.includes(variant)) {
|
|
102
|
+
throw new Error(
|
|
103
|
+
`${where} sets "${canonical}" to ${JSON.stringify(variant)}: ` +
|
|
104
|
+
`use one of ${quoteList(allowed)}`,
|
|
105
|
+
)
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return { ...variants }
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function readSkip(skip = {}, file) {
|
|
112
|
+
const where = `${file}: "${CONFIG_KEY}.skip"`
|
|
113
|
+
if (!isPlainObject(skip)) {
|
|
114
|
+
throw new Error(
|
|
115
|
+
`${where} must be an object of rule id → reason, e.g. { "css/<rule>": "<why this repo differs>" }`,
|
|
116
|
+
)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
for (const [id, reason] of Object.entries(skip)) {
|
|
120
|
+
if (typeof reason !== 'string' || reason.trim() === '') {
|
|
121
|
+
throw new Error(`${where} skips "${id}" without a reason: give the reason as a non-empty string`)
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return { ...skip }
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function invalidIgnores(file) {
|
|
128
|
+
return new Error(
|
|
129
|
+
`${file}: "${CONFIG_KEY}" must be an object whose "ignores" is a list ` +
|
|
130
|
+
`of globs relative to the repo root, e.g. { "${CONFIG_KEY}": { "ignores": ["test/markup/"] } }`,
|
|
131
|
+
)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function quoteList(values) {
|
|
135
|
+
return values.length > 0 ? values.map((value) => `"${value}"`).join(', ') : 'none'
|
|
136
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { fileURLToPath } from 'node:url'
|
|
2
|
+
|
|
3
|
+
import { escapeRegExp } from '@nera-static/plugin-utils/html'
|
|
4
|
+
|
|
5
|
+
import { BEM_HINT, CLASS_NAME } from '../bem.js'
|
|
6
|
+
import { kindOf } from '../kind.js'
|
|
7
|
+
|
|
8
|
+
/** The family prefix of every CSS rule id in a nera-stan report (D4). */
|
|
9
|
+
export const CSS_PREFIX = 'css/'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Report ids for what stylelint reports without a rule of its own, and the config flag that
|
|
13
|
+
* switches each off (`null`: nothing to switch, the runner drops a skipped finding).
|
|
14
|
+
*/
|
|
15
|
+
export const OWN_IDS = Object.freeze({
|
|
16
|
+
'CssSyntaxError': { id: `${CSS_PREFIX}parse-error`, flag: null },
|
|
17
|
+
'--report-needless-disables': { id: `${CSS_PREFIX}needless-disable`, flag: 'reportNeedlessDisables' },
|
|
18
|
+
'--report-descriptionless-disables': {
|
|
19
|
+
id: `${CSS_PREFIX}descriptionless-disable`,
|
|
20
|
+
flag: 'reportDescriptionlessDisables',
|
|
21
|
+
},
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
/** Absolute paths, so the config resolves the same from nera-stan, an editor or a repo's own stylelint. */
|
|
25
|
+
const STANDARD = fileURLToPath(import.meta.resolve('stylelint-config-standard'))
|
|
26
|
+
const STYLISTIC = fileURLToPath(import.meta.resolve('@stylistic/stylelint-plugin'))
|
|
27
|
+
|
|
28
|
+
/** The public tokens a theme writes from `config/theme.yaml`; every theme and plugin may use them. */
|
|
29
|
+
const THEME_TOKENS = 'theme'
|
|
30
|
+
/** A plugin's own tokens are `--nera-<plugin>-*`; a theme may set or read any plugin's. */
|
|
31
|
+
const PLUGIN_TOKENS = 'nera'
|
|
32
|
+
const NAME_SCOPE = /^@[^/]+\//
|
|
33
|
+
const KEBAB_REST = '[a-z0-9]+(?:-[a-z0-9]+)*'
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Rules of `stylelint-config-standard` switched off after the first run on the ten repos with
|
|
37
|
+
* CSS (2026-11-27; the counts are in the spec, `4-tooling-themes.md` #3): matters of taste, plus two whose results
|
|
38
|
+
* cannot be trusted, `no-descending-specificity` (false positives on component CSS) and
|
|
39
|
+
* `property-no-vendor-prefix` (its fix drops `-webkit-` prefixes Safari still needs).
|
|
40
|
+
*/
|
|
41
|
+
export const TASTE_RULES = Object.freeze([
|
|
42
|
+
'alpha-value-notation',
|
|
43
|
+
'color-function-alias-notation',
|
|
44
|
+
'color-function-notation',
|
|
45
|
+
'declaration-block-single-line-max-declarations',
|
|
46
|
+
'media-feature-range-notation',
|
|
47
|
+
'no-descending-specificity',
|
|
48
|
+
'number-max-precision',
|
|
49
|
+
'property-no-vendor-prefix',
|
|
50
|
+
])
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The prefixes a repo's custom properties may start with (spec decision D9 (3)): the public
|
|
54
|
+
* `--theme-*` tokens, plus a theme's own name (`@nera-static/theme-stack` → `stack`) and any
|
|
55
|
+
* plugin's `--nera-<plugin>-*` (a theme styles plugin output through them), or a plugin's own
|
|
56
|
+
* `nera-<name>` (`@nera-static/plugin-consent` → `nera-consent`). Other kinds have no prefix
|
|
57
|
+
* rule: `null`.
|
|
58
|
+
*
|
|
59
|
+
* @param {object} pkg - The repo's parsed `package.json`.
|
|
60
|
+
* @returns {string[] | null}
|
|
61
|
+
*/
|
|
62
|
+
export function customPropertyPrefixes(pkg) {
|
|
63
|
+
const kind = kindOf(pkg)
|
|
64
|
+
const name = typeof pkg.name === 'string' ? pkg.name.replace(NAME_SCOPE, '') : ''
|
|
65
|
+
if (kind === 'theme') {
|
|
66
|
+
const own = name.replace(/^(?:nera-)?theme-/, '')
|
|
67
|
+
return own === '' || own === THEME_TOKENS ? [THEME_TOKENS, PLUGIN_TOKENS] : [THEME_TOKENS, own, PLUGIN_TOKENS]
|
|
68
|
+
}
|
|
69
|
+
if (kind === 'plugin') {
|
|
70
|
+
return [THEME_TOKENS, `nera-${name.replace(/^(?:nera-)?plugin-/, '')}`]
|
|
71
|
+
}
|
|
72
|
+
return null
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Build the stylelint config nera-stan checks a repo's CSS with: `stylelint-config-standard`,
|
|
77
|
+
* `@stylistic` with indent 4 and single quotes, BEM class names (D5, highlight.js's own classes
|
|
78
|
+
* allowed), the `TASTE_RULES` off, and the prefixes of `customPropertyPrefixes`. A rule id
|
|
79
|
+
* under `nera-stan.skip` (`css/<rule>`) is switched off.
|
|
80
|
+
*
|
|
81
|
+
* @param {object} options
|
|
82
|
+
* @param {object} options.pkg - The repo's parsed `package.json`.
|
|
83
|
+
* @param {{ skip: Record<string, string> }} [options.settings] - The repo's `nera-stan` settings.
|
|
84
|
+
* @returns {object} A stylelint config object.
|
|
85
|
+
*/
|
|
86
|
+
export function createStylelintConfig({ pkg, settings = { skip: {} } }) {
|
|
87
|
+
const prefixes = customPropertyPrefixes(pkg)
|
|
88
|
+
const rules = {
|
|
89
|
+
'@stylistic/indentation': 4,
|
|
90
|
+
'@stylistic/string-quotes': 'single',
|
|
91
|
+
'@stylistic/no-eol-whitespace': true,
|
|
92
|
+
'@stylistic/no-missing-end-of-source-newline': true,
|
|
93
|
+
'selector-class-pattern': [CLASS_NAME, {
|
|
94
|
+
message: (selector) => `Expected class "${selector}" to be BEM, ${BEM_HINT}: rename the class ` +
|
|
95
|
+
'(renaming a published class is a major release)',
|
|
96
|
+
}],
|
|
97
|
+
...(prefixes ? { 'custom-property-pattern': customPropertyRule(prefixes) } : {}),
|
|
98
|
+
...Object.fromEntries(TASTE_RULES.map((rule) => [rule, null])),
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const flags = { reportNeedlessDisables: true, reportDescriptionlessDisables: true }
|
|
102
|
+
const own = Object.values(OWN_IDS)
|
|
103
|
+
for (const id of Object.keys(settings.skip).filter((key) => key.startsWith(CSS_PREFIX))) {
|
|
104
|
+
const special = own.find((entry) => entry.id === id)
|
|
105
|
+
if (special === undefined) {
|
|
106
|
+
rules[id.slice(CSS_PREFIX.length)] = null
|
|
107
|
+
} else if (special.flag !== null) {
|
|
108
|
+
flags[special.flag] = false
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return { extends: [STANDARD], plugins: [STYLISTIC], ...flags, rules }
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function customPropertyRule(prefixes) {
|
|
116
|
+
const pattern = new RegExp(`^(?:${prefixes.map(escapeRegExp).join('|')})-${KEBAB_REST}$`)
|
|
117
|
+
const allowed = prefixes.map((prefix) => `"--${prefix}-*"`).join(' or ')
|
|
118
|
+
return [pattern, {
|
|
119
|
+
message: (property) => `Expected custom property "${property}" to start with ${allowed}, ` +
|
|
120
|
+
'lower case, words joined by "-": rename it, or move a value meant for sites into "--theme-*"',
|
|
121
|
+
}]
|
|
122
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { readPackage } from '../kind.js'
|
|
2
|
+
import { readSettings } from '../settings.js'
|
|
3
|
+
import { createStylelintConfig } from './config.js'
|
|
4
|
+
|
|
5
|
+
export { CSS_PREFIX, TASTE_RULES, createStylelintConfig, customPropertyPrefixes } from './config.js'
|
|
6
|
+
|
|
7
|
+
const root = process.cwd()
|
|
8
|
+
const pkg = readPackage(root)
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The nera-stan stylelint config for the repo in the current directory, for editors:
|
|
12
|
+
* `export { default } from '@nera-static/stan/stylelint'` in the repo's `stylelint.config.js`.
|
|
13
|
+
* The `nera-stan` bin never reads that file.
|
|
14
|
+
*/
|
|
15
|
+
export default createStylelintConfig({ pkg, settings: readSettings(pkg, root) })
|