@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
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import { isTestFile, repoFile } from '../repo-files.js'
|
|
2
|
+
import { TEST_UTILS } from '../test-helper-names.js'
|
|
3
|
+
import { globalReferences } from './scope.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The test rules (D8): in test files of every kind (`test/**` and `*.test.js`)
|
|
7
|
+
* and nowhere else, tests neither move nor depend on the cwd, name what the code
|
|
8
|
+
* does, and take their helpers from `@nera-static/test-utils`. None of them fixes
|
|
9
|
+
* (D9): every rewrite needs a choice the rule cannot make.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const PATH_MODULES = Object.freeze(['path', 'node:path'])
|
|
13
|
+
const PROCESS_MODULES = Object.freeze(['process', 'node:process'])
|
|
14
|
+
|
|
15
|
+
/** The functions vitest registers a suite or a test with. */
|
|
16
|
+
const TEST_FUNCTIONS = Object.freeze(['describe', 'suite', 'it', 'test', 'bench'])
|
|
17
|
+
|
|
18
|
+
/** Modifiers that return a new test function instead of taking the title themselves. */
|
|
19
|
+
const TITLE_LATER = Object.freeze(['each', 'for', 'extend', 'skipIf', 'runIf', 'scoped'])
|
|
20
|
+
|
|
21
|
+
const SHOULD = /^\s*should\b/i
|
|
22
|
+
const ABSOLUTE = /^(?:\/|[A-Za-z]:[\\/]|\\\\)/
|
|
23
|
+
const SHOWN_TITLE = 60
|
|
24
|
+
|
|
25
|
+
const FUNCTION = new Set(['ArrowFunctionExpression', 'FunctionExpression'])
|
|
26
|
+
|
|
27
|
+
/** Whether the rule's file is a test of the checked repo. */
|
|
28
|
+
function inTests(context) {
|
|
29
|
+
const { root = context.cwd } = context.settings.nera ?? {}
|
|
30
|
+
return isTestFile(repoFile(root, context.filename))
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const testNoChdir = {
|
|
34
|
+
meta: {
|
|
35
|
+
type: 'problem',
|
|
36
|
+
docs: { description: 'Tests never change the working directory' },
|
|
37
|
+
schema: [],
|
|
38
|
+
messages: {
|
|
39
|
+
chdir: '`process.chdir` in a test moves the cwd of every test in the worker: pass the code under ' +
|
|
40
|
+
"test its root instead, or `import { useSiteCwd } from '{{specifier}}'`, which stubs " +
|
|
41
|
+
'`process.cwd()` and restores it',
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
|
|
45
|
+
create(context) {
|
|
46
|
+
if (!inTests(context)) {
|
|
47
|
+
return {}
|
|
48
|
+
}
|
|
49
|
+
const code = context.sourceCode
|
|
50
|
+
const report = (node) => context.report({ node, messageId: 'chdir', data: { specifier: TEST_UTILS } })
|
|
51
|
+
const reportMembers = (references) => {
|
|
52
|
+
for (const { identifier } of references) {
|
|
53
|
+
if (isMemberOf(identifier, 'chdir')) {
|
|
54
|
+
report(identifier.parent)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
ImportDeclaration(node) {
|
|
61
|
+
if (!PROCESS_MODULES.includes(node.source.value)) {
|
|
62
|
+
return
|
|
63
|
+
}
|
|
64
|
+
for (const specifier of node.specifiers) {
|
|
65
|
+
const [variable] = code.getDeclaredVariables(specifier)
|
|
66
|
+
if (specifier.type !== 'ImportSpecifier') {
|
|
67
|
+
reportMembers(variable.references)
|
|
68
|
+
} else if (importedName(specifier) === 'chdir') {
|
|
69
|
+
variable.references.forEach(({ identifier }) => report(identifier))
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
'Program:exit'(program) {
|
|
74
|
+
reportMembers(globalReferences(code.getScope(program), 'process'))
|
|
75
|
+
},
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const testNoCwdPath = {
|
|
81
|
+
meta: {
|
|
82
|
+
type: 'problem',
|
|
83
|
+
docs: { description: 'Tests anchor paths at their own file or a root they own, never at the cwd' },
|
|
84
|
+
schema: [],
|
|
85
|
+
messages: {
|
|
86
|
+
cwdPath: '`{{call}}` resolves against the cwd, so the test passes only when vitest runs from the ' +
|
|
87
|
+
"repo root: anchor it at the test file with `fileURLToPath(new URL('…', import.meta.url))`, or " +
|
|
88
|
+
'pass a root the test owns as the first argument',
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
|
|
92
|
+
create(context) {
|
|
93
|
+
if (!inTests(context)) {
|
|
94
|
+
return {}
|
|
95
|
+
}
|
|
96
|
+
const code = context.sourceCode
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
CallExpression(node) {
|
|
100
|
+
const [first] = node.arguments
|
|
101
|
+
if (!isRelativePath(first) || !isPathResolve(node.callee, code.getScope(node))) {
|
|
102
|
+
return
|
|
103
|
+
}
|
|
104
|
+
context.report({
|
|
105
|
+
node,
|
|
106
|
+
messageId: 'cwdPath',
|
|
107
|
+
data: { call: `${code.getText(node.callee)}(${code.getText(first)}${node.arguments.length > 1 ? ', …' : ''})` },
|
|
108
|
+
})
|
|
109
|
+
},
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const testName = {
|
|
115
|
+
meta: {
|
|
116
|
+
type: 'suggestion',
|
|
117
|
+
docs: { description: 'Test and suite names state what the code does, without "should"' },
|
|
118
|
+
schema: [],
|
|
119
|
+
messages: {
|
|
120
|
+
should: '`{{fn}}` title "{{title}}" starts with "should": state what the code does ' +
|
|
121
|
+
'("copies the assets", not "should copy the assets")',
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
|
|
125
|
+
create(context) {
|
|
126
|
+
if (!inTests(context)) {
|
|
127
|
+
return {}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return {
|
|
131
|
+
CallExpression(node) {
|
|
132
|
+
const fn = testFunction(node.callee)
|
|
133
|
+
const title = fn && leadingText(node.arguments[0])
|
|
134
|
+
if (title === null || title === undefined || !SHOULD.test(title)) {
|
|
135
|
+
return
|
|
136
|
+
}
|
|
137
|
+
const shown = title.trim()
|
|
138
|
+
context.report({
|
|
139
|
+
node: node.arguments[0],
|
|
140
|
+
messageId: 'should',
|
|
141
|
+
data: { fn, title: shown.length > SHOWN_TITLE ? `${shown.slice(0, SHOWN_TITLE)}…` : shown },
|
|
142
|
+
})
|
|
143
|
+
},
|
|
144
|
+
}
|
|
145
|
+
},
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const testHelpers = {
|
|
149
|
+
meta: {
|
|
150
|
+
type: 'suggestion',
|
|
151
|
+
docs: { description: 'Tests import their helpers from @nera-static/test-utils instead of defining copies' },
|
|
152
|
+
schema: [],
|
|
153
|
+
messages: {
|
|
154
|
+
localHelper: '`{{name}}` in a test redefines a {{package}} helper: delete it and ' +
|
|
155
|
+
"`import { {{name}} } from '{{specifier}}'`",
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
|
|
159
|
+
create(context) {
|
|
160
|
+
const helpers = context.settings.nera?.testHelpers ?? {}
|
|
161
|
+
if (!inTests(context)) {
|
|
162
|
+
return {}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function check(id) {
|
|
166
|
+
if (Object.hasOwn(helpers, id.name)) {
|
|
167
|
+
context.report({
|
|
168
|
+
node: id,
|
|
169
|
+
messageId: 'localHelper',
|
|
170
|
+
data: { name: id.name, package: TEST_UTILS, specifier: helpers[id.name] },
|
|
171
|
+
})
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return {
|
|
176
|
+
FunctionDeclaration(node) {
|
|
177
|
+
if (node.id) {
|
|
178
|
+
check(node.id)
|
|
179
|
+
}
|
|
180
|
+
},
|
|
181
|
+
VariableDeclarator(node) {
|
|
182
|
+
if (node.id.type === 'Identifier' && FUNCTION.has(node.init?.type)) {
|
|
183
|
+
check(node.id)
|
|
184
|
+
}
|
|
185
|
+
},
|
|
186
|
+
}
|
|
187
|
+
},
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export default {
|
|
191
|
+
'test-no-chdir': testNoChdir,
|
|
192
|
+
'test-no-cwd-path': testNoCwdPath,
|
|
193
|
+
'test-name': testName,
|
|
194
|
+
'test-helpers': testHelpers,
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** Whether `identifier` is the object of a `.property` (or `['property']`) access. */
|
|
198
|
+
function isMemberOf(identifier, property) {
|
|
199
|
+
const { parent } = identifier
|
|
200
|
+
return parent.type === 'MemberExpression' && parent.object === identifier && propertyName(parent) === property
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function propertyName(member) {
|
|
204
|
+
if (!member.computed && member.property.type === 'Identifier') {
|
|
205
|
+
return member.property.name
|
|
206
|
+
}
|
|
207
|
+
return member.property.type === 'Literal' ? member.property.value : null
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function importedName(specifier) {
|
|
211
|
+
return specifier.imported.type === 'Identifier' ? specifier.imported.name : specifier.imported.value
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** A string or a template literal whose leading text is a relative path. */
|
|
215
|
+
function isRelativePath(node) {
|
|
216
|
+
const text = leadingText(node)
|
|
217
|
+
if (typeof text !== 'string') {
|
|
218
|
+
return false
|
|
219
|
+
}
|
|
220
|
+
// `''` resolves to the cwd itself; an empty start before `${root}` or `+ root` says nothing.
|
|
221
|
+
const whole = node.type === 'Literal' || (node.type === 'TemplateLiteral' && node.expressions.length === 0)
|
|
222
|
+
return !ABSOLUTE.test(text) && (whole || text !== '')
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* The text a title or path starts with: a string, the first part of a template
|
|
227
|
+
* literal, or the left end of a `+` concatenation; `null` when it starts with
|
|
228
|
+
* something else.
|
|
229
|
+
*/
|
|
230
|
+
function leadingText(node) {
|
|
231
|
+
if (node?.type === 'Literal') {
|
|
232
|
+
return typeof node.value === 'string' ? node.value : null
|
|
233
|
+
}
|
|
234
|
+
if (node?.type === 'TemplateLiteral') {
|
|
235
|
+
return node.quasis[0].value.cooked
|
|
236
|
+
}
|
|
237
|
+
if (node?.type === 'BinaryExpression' && node.operator === '+') {
|
|
238
|
+
return leadingText(node.left)
|
|
239
|
+
}
|
|
240
|
+
return null
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** `path.resolve` (default or namespace import, or `require`) or a `resolve` imported from `node:path`. */
|
|
244
|
+
function isPathResolve(callee, scope) {
|
|
245
|
+
if (callee.type === 'Identifier') {
|
|
246
|
+
const def = definition(scope, callee.name)
|
|
247
|
+
return def?.type === 'ImportBinding' && def.node.type === 'ImportSpecifier' &&
|
|
248
|
+
importedName(def.node) === 'resolve' && PATH_MODULES.includes(def.parent.source.value)
|
|
249
|
+
}
|
|
250
|
+
if (callee.type !== 'MemberExpression' || callee.object.type !== 'Identifier' || propertyName(callee) !== 'resolve') {
|
|
251
|
+
return false
|
|
252
|
+
}
|
|
253
|
+
const def = definition(scope, callee.object.name)
|
|
254
|
+
if (def?.type === 'ImportBinding') {
|
|
255
|
+
return def.node.type !== 'ImportSpecifier' && PATH_MODULES.includes(def.parent.source.value)
|
|
256
|
+
}
|
|
257
|
+
return def?.type === 'Variable' && isRequireOf(def.node.init, PATH_MODULES)
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function isRequireOf(node, modules) {
|
|
261
|
+
return node?.type === 'CallExpression' && node.callee.type === 'Identifier' && node.callee.name === 'require' &&
|
|
262
|
+
node.arguments[0]?.type === 'Literal' && modules.includes(node.arguments[0].value)
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** The first definition of the variable `name` visible from `scope`. */
|
|
266
|
+
function definition(scope, name) {
|
|
267
|
+
for (let current = scope; current; current = current.upper) {
|
|
268
|
+
const variable = current.set.get(name)
|
|
269
|
+
if (variable) {
|
|
270
|
+
return variable.defs[0] ?? null
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
return null
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* The name of the vitest function a call registers a suite or test with, when
|
|
278
|
+
* the call itself takes the title: `it(…)`, `test.skip(…)`, `describe.concurrent.only(…)`
|
|
279
|
+
* and the call `it.each(table)` returns; `it.each(table)` itself does not.
|
|
280
|
+
*/
|
|
281
|
+
function testFunction(callee) {
|
|
282
|
+
let node = callee
|
|
283
|
+
if (node.type === 'MemberExpression' && TITLE_LATER.includes(propertyName(node))) {
|
|
284
|
+
return null
|
|
285
|
+
}
|
|
286
|
+
while (node.type === 'MemberExpression' || node.type === 'CallExpression' ||
|
|
287
|
+
node.type === 'TaggedTemplateExpression') {
|
|
288
|
+
node = node.type === 'MemberExpression' ? node.object : node.type === 'CallExpression' ? node.callee : node.tag
|
|
289
|
+
}
|
|
290
|
+
return node.type === 'Identifier' && TEST_FUNCTIONS.includes(node.name) ? node.name : null
|
|
291
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
|
|
4
|
+
import { packageExports } from './helper-names.js'
|
|
5
|
+
|
|
6
|
+
/** The package whose exports `nera/test-helpers` protects (D7). */
|
|
7
|
+
export const TEST_UTILS = '@nera-static/test-utils'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The helper functions of `@nera-static/test-utils`, as its API lists them
|
|
11
|
+
* (spec `3-tests.md` §4.5), shipped so the rule works before a repo installs it.
|
|
12
|
+
*/
|
|
13
|
+
export const KNOWN_TEST_HELPERS = Object.freeze([
|
|
14
|
+
'tempDir',
|
|
15
|
+
'writeFiles',
|
|
16
|
+
'makeSite',
|
|
17
|
+
'buildSite',
|
|
18
|
+
'useSiteCwd',
|
|
19
|
+
'captureConsole',
|
|
20
|
+
'readPublic',
|
|
21
|
+
'nera',
|
|
22
|
+
'scaffoldStarter',
|
|
23
|
+
'expectCleanBuild',
|
|
24
|
+
'listFiles',
|
|
25
|
+
'parseYaml',
|
|
26
|
+
'loadYaml',
|
|
27
|
+
'frontmatter',
|
|
28
|
+
'jsonLd',
|
|
29
|
+
'countTags',
|
|
30
|
+
'tags',
|
|
31
|
+
'elementByClass',
|
|
32
|
+
'attr',
|
|
33
|
+
'stripTags',
|
|
34
|
+
'contrast',
|
|
35
|
+
'publishTemplateSuite',
|
|
36
|
+
])
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The helper names `nera/test-helpers` knows: the shipped list unioned with the
|
|
40
|
+
* exports of the `@nera-static/test-utils` installed in the checked repo's
|
|
41
|
+
* `node_modules` (D7). Files are parsed, never imported.
|
|
42
|
+
*
|
|
43
|
+
* @param {object} options
|
|
44
|
+
* @param {string} options.root - The checked repo's root directory.
|
|
45
|
+
* @returns {Record<string, string>} Each helper name mapped to the import specifier that
|
|
46
|
+
* provides it; the installed copy wins over the shipped list.
|
|
47
|
+
*/
|
|
48
|
+
export function testHelperNames({ root }) {
|
|
49
|
+
const shipped = Object.fromEntries(KNOWN_TEST_HELPERS.map((name) => [name, TEST_UTILS]))
|
|
50
|
+
const installed = path.join(root, 'node_modules', ...TEST_UTILS.split('/'))
|
|
51
|
+
return existsSync(path.join(installed, 'package.json'))
|
|
52
|
+
? { ...shipped, ...packageExports(installed) }
|
|
53
|
+
: shipped
|
|
54
|
+
}
|