@naturalcycles/dev-lib 20.71.0 → 20.73.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/cfg/collectReporter.js +4 -2
- package/cfg/oxlint-plugin-nc.d.ts +5 -1
- package/cfg/oxlint-plugin-nc.js +194 -14
- package/cfg/oxlint.config.js +4 -0
- package/cfg/summaryReporter.js +2 -1
- package/dist/check.util.js +1 -2
- package/dist/commitlint.command.js +2 -1
- package/dist/release/git.util.js +2 -1
- package/dist/release/release.command.js +2 -1
- package/package.json +1 -1
package/cfg/collectReporter.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { _last } from '@naturalcycles/js-lib/array/array.util.js'
|
|
2
|
+
|
|
1
3
|
export class CollectReporter {
|
|
2
4
|
constructor(cfg = {}) {
|
|
3
5
|
this.cfg = cfg
|
|
@@ -5,7 +7,7 @@ export class CollectReporter {
|
|
|
5
7
|
|
|
6
8
|
onTestModuleEnd(testModule) {
|
|
7
9
|
const { threshold = 0 } = this.cfg
|
|
8
|
-
const shortModuleId = testModule.moduleId.split('/')
|
|
10
|
+
const shortModuleId = _last(testModule.moduleId.split('/'))
|
|
9
11
|
|
|
10
12
|
const selfTimeMap = {}
|
|
11
13
|
const totalTimeMap = {}
|
|
@@ -15,7 +17,7 @@ export class CollectReporter {
|
|
|
15
17
|
Object.entries(testModule.diagnostic().importDurations).forEach(([k, v]) => {
|
|
16
18
|
moduleCount++
|
|
17
19
|
selfSum += v.selfTime
|
|
18
|
-
const short = k.split('/').
|
|
20
|
+
const short = k.split('/').slice(-3).join('/')
|
|
19
21
|
if (Math.round(v.totalTime) < threshold) return // skip < ${threshold} ms imports
|
|
20
22
|
selfTimeMap[short] = Math.round(v.selfTime)
|
|
21
23
|
totalTimeMap[short] = Math.round(v.totalTime)
|
|
@@ -4,7 +4,11 @@ type Rule = Parameters<RuleTester['run']>[1]
|
|
|
4
4
|
|
|
5
5
|
declare const ncPlugin: {
|
|
6
6
|
meta: { name: string }
|
|
7
|
-
rules: {
|
|
7
|
+
rules: {
|
|
8
|
+
'prefer-assert': Rule
|
|
9
|
+
'prefer-env-destructuring': Rule
|
|
10
|
+
'prefer-last': Rule
|
|
11
|
+
}
|
|
8
12
|
}
|
|
9
13
|
|
|
10
14
|
export default ncPlugin
|
package/cfg/oxlint-plugin-nc.js
CHANGED
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
const ASSERTABLE_ERRORS = new Set(['Error', 'AppError', 'AssertionError'])
|
|
2
2
|
const EQUALITY_OPERATORS = new Set(['===', '!==', '==', '!='])
|
|
3
|
-
// A condition that stays readable once negated; `!(a
|
|
3
|
+
// A condition that stays readable once negated; `!(a ? b : c)` does not
|
|
4
4
|
const UNASSERTABLE_TESTS = new Set([
|
|
5
|
-
'LogicalExpression',
|
|
6
5
|
'ConditionalExpression',
|
|
7
6
|
'AssignmentExpression',
|
|
8
7
|
'SequenceExpression',
|
|
9
8
|
])
|
|
10
9
|
// An inline callback in the condition (`items.some(item => ...)`) does not read as an assertion once negated
|
|
11
10
|
const FUNCTION_NODES = new Set(['ArrowFunctionExpression', 'FunctionExpression'])
|
|
12
|
-
// `_assert` builds its message and data before checking, so a call
|
|
13
|
-
//
|
|
14
|
-
//
|
|
11
|
+
// `_assert` builds its message and data before checking, so a call or await there would run on the
|
|
12
|
+
// happy path too, and a value the condition narrows (`if (!res.ok) throw new Error(res.error)`) no
|
|
13
|
+
// longer typechecks. Interpolating plain values into the message is cheap enough to allow
|
|
15
14
|
const EVALUATED_ARGUMENTS = new Set([
|
|
16
15
|
'CallExpression',
|
|
17
16
|
'NewExpression',
|
|
@@ -33,7 +32,7 @@ const preferAssertRule = {
|
|
|
33
32
|
type: 'suggestion',
|
|
34
33
|
docs: {
|
|
35
34
|
description:
|
|
36
|
-
'Prefer _assert over an if whose only statement throws an Error or AppError with a
|
|
35
|
+
'Prefer _assert over an if whose only statement throws an Error or AppError with a message that is cheap to build',
|
|
37
36
|
},
|
|
38
37
|
messages: {
|
|
39
38
|
preferAssert:
|
|
@@ -80,9 +79,19 @@ function maxArguments(errorName) {
|
|
|
80
79
|
}
|
|
81
80
|
|
|
82
81
|
function isAssertableTest(test) {
|
|
82
|
+
return isAssertableCondition(test) && !containsFunction(test)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// `a && b && c` negates to a readable `!a || !b || !c`; mixing `&&` with `||` (or `??`) does not
|
|
86
|
+
function isAssertableCondition(test, operator) {
|
|
83
87
|
if (UNASSERTABLE_TESTS.has(test.type)) return false
|
|
84
|
-
if (test.type === 'BinaryExpression'
|
|
85
|
-
|
|
88
|
+
if (test.type === 'BinaryExpression') return EQUALITY_OPERATORS.has(test.operator)
|
|
89
|
+
if (test.type !== 'LogicalExpression') return true
|
|
90
|
+
if (test.operator === '??' || (operator && test.operator !== operator)) return false
|
|
91
|
+
return (
|
|
92
|
+
isAssertableCondition(test.left, test.operator) &&
|
|
93
|
+
isAssertableCondition(test.right, test.operator)
|
|
94
|
+
)
|
|
86
95
|
}
|
|
87
96
|
|
|
88
97
|
function containsFunction(node) {
|
|
@@ -90,12 +99,7 @@ function containsFunction(node) {
|
|
|
90
99
|
}
|
|
91
100
|
|
|
92
101
|
function containsEvaluation(node) {
|
|
93
|
-
return visit(
|
|
94
|
-
node,
|
|
95
|
-
child =>
|
|
96
|
-
EVALUATED_ARGUMENTS.has(child.type) ||
|
|
97
|
-
(child.type === 'TemplateLiteral' && child.expressions.length > 0),
|
|
98
|
-
)
|
|
102
|
+
return visit(node, child => EVALUATED_ARGUMENTS.has(child.type))
|
|
99
103
|
}
|
|
100
104
|
|
|
101
105
|
function referencedNames(node) {
|
|
@@ -136,12 +140,188 @@ function visit(node, fn) {
|
|
|
136
140
|
return false
|
|
137
141
|
}
|
|
138
142
|
|
|
143
|
+
const preferLastRule = {
|
|
144
|
+
meta: {
|
|
145
|
+
type: 'suggestion',
|
|
146
|
+
docs: {
|
|
147
|
+
description:
|
|
148
|
+
'Prefer _last / _lastOrUndefined over array.at(-1), array[array.length - 1] and array.slice(-1)[0]',
|
|
149
|
+
},
|
|
150
|
+
messages: {
|
|
151
|
+
preferLast:
|
|
152
|
+
'Use _last(array), or _lastOrUndefined(array) when it can be empty, from @naturalcycles/js-lib/array',
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
create(context) {
|
|
156
|
+
return {
|
|
157
|
+
// `array.at(-1)`
|
|
158
|
+
CallExpression(node) {
|
|
159
|
+
if (!isMethodCall(node, 'at') || !isMinusOne(node.arguments[0])) return
|
|
160
|
+
if (isStringLiteral(node.callee.object) || containsOptional(node)) return
|
|
161
|
+
context.report({ node, messageId: 'preferLast' })
|
|
162
|
+
},
|
|
163
|
+
MemberExpression(node) {
|
|
164
|
+
if (!node.computed || isWriteTarget(node) || containsOptional(node)) return
|
|
165
|
+
const { object, property } = node
|
|
166
|
+
// `array[array.length - 1]`
|
|
167
|
+
const lastIndex =
|
|
168
|
+
property.type === 'BinaryExpression' &&
|
|
169
|
+
property.operator === '-' &&
|
|
170
|
+
isNumber(property.right, 1) &&
|
|
171
|
+
isLengthOf(property.left, object)
|
|
172
|
+
// `array.slice(-1)[0]`
|
|
173
|
+
const lastSlice =
|
|
174
|
+
isNumber(property, 0) &&
|
|
175
|
+
isMethodCall(object, 'slice') &&
|
|
176
|
+
isMinusOne(object.arguments[0]) &&
|
|
177
|
+
!isStringLiteral(object.callee.object)
|
|
178
|
+
if (lastIndex || lastSlice) context.report({ node, messageId: 'preferLast' })
|
|
179
|
+
},
|
|
180
|
+
}
|
|
181
|
+
},
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const preferEnvDestructuringRule = {
|
|
185
|
+
meta: {
|
|
186
|
+
type: 'suggestion',
|
|
187
|
+
docs: {
|
|
188
|
+
description:
|
|
189
|
+
'Prefer `const { MY_VAR } = process.env` over `const myVar = process.env.MY_VAR`; a read used directly in an expression is left alone',
|
|
190
|
+
},
|
|
191
|
+
messages: {
|
|
192
|
+
preferEnvDestructuring: 'Destructure instead: const { {{name}} } = process.env',
|
|
193
|
+
},
|
|
194
|
+
},
|
|
195
|
+
create(context) {
|
|
196
|
+
// Tests set, restore and assert on single variables, where the member access reads better
|
|
197
|
+
if (TEST_FILE_PATTERN.test(context.filename)) return {}
|
|
198
|
+
|
|
199
|
+
return {
|
|
200
|
+
MemberExpression(node) {
|
|
201
|
+
if (!isProcessEnv(node.object) || node.optional) return
|
|
202
|
+
// Only a plain alias, `const myVar = process.env.MY_VAR`. `if (process.env.CI)` or
|
|
203
|
+
// `process.env.PORT || 8080` would need a single-use variable, which is no improvement
|
|
204
|
+
const { parent } = node
|
|
205
|
+
if (parent.type !== 'VariableDeclarator' || parent.init !== node) return
|
|
206
|
+
if (parent.id.type !== 'Identifier') return
|
|
207
|
+
const name = staticKey(node)
|
|
208
|
+
// Only a name that can be destructured as is; `process.env[key]`, `npm_config_user-agent`
|
|
209
|
+
// and a reserved word like `default` stay
|
|
210
|
+
if (!name || !IDENTIFIER_PATTERN.test(name) || RESERVED_WORDS.has(name)) return
|
|
211
|
+
context.report({ node, messageId: 'preferEnvDestructuring', data: { name } })
|
|
212
|
+
},
|
|
213
|
+
}
|
|
214
|
+
},
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const IDENTIFIER_PATTERN = /^[A-Za-z_$][\w$]*$/
|
|
218
|
+
// Not valid as `const { name } = process.env`
|
|
219
|
+
const RESERVED_WORDS = new Set(
|
|
220
|
+
'arguments await break case catch class const continue debugger default delete do else enum eval export extends false finally for function if implements import in instanceof interface let new null package private protected public return static super switch this throw true try typeof var void while with yield'.split(
|
|
221
|
+
' ',
|
|
222
|
+
),
|
|
223
|
+
)
|
|
224
|
+
|
|
225
|
+
// Not `process?.env` (nor `process.env?.X`): an optional chain guards code that also runs where
|
|
226
|
+
// `process` does not exist, and a destructuring would throw there
|
|
227
|
+
function isProcessEnv(node) {
|
|
228
|
+
return (
|
|
229
|
+
node.type === 'MemberExpression' &&
|
|
230
|
+
!node.computed &&
|
|
231
|
+
!node.optional &&
|
|
232
|
+
node.object.type === 'Identifier' &&
|
|
233
|
+
node.object.name === 'process' &&
|
|
234
|
+
node.property.name === 'env'
|
|
235
|
+
)
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function staticKey(node) {
|
|
239
|
+
if (!node.computed) return node.property.name
|
|
240
|
+
return typeof node.property.value === 'string' ? node.property.value : undefined
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// Assigned to, updated or deleted: `x[i] = 1`, `x[i]++`, `delete x[i]`, and a target inside a
|
|
244
|
+
// destructuring assignment or a loop head: `[x[i]] = values`, `for (x[i] of values)`
|
|
245
|
+
function isWriteTarget(node) {
|
|
246
|
+
const { parent } = node
|
|
247
|
+
return (
|
|
248
|
+
(WRITE_PARENTS.has(parent.type) && parent.left === node) ||
|
|
249
|
+
parent.type === 'UpdateExpression' ||
|
|
250
|
+
(parent.type === 'UnaryExpression' && parent.operator === 'delete') ||
|
|
251
|
+
parent.type === 'ArrayPattern' ||
|
|
252
|
+
parent.type === 'RestElement' ||
|
|
253
|
+
(parent.type === 'Property' && parent.value === node && parent.parent.type === 'ObjectPattern')
|
|
254
|
+
)
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// `left` is what gets written; a default value (`AssignmentPattern.right`) is still a read
|
|
258
|
+
const WRITE_PARENTS = new Set([
|
|
259
|
+
'AssignmentExpression',
|
|
260
|
+
'AssignmentPattern',
|
|
261
|
+
'ForInStatement',
|
|
262
|
+
'ForOfStatement',
|
|
263
|
+
])
|
|
264
|
+
|
|
265
|
+
function isMethodCall(node, name) {
|
|
266
|
+
return (
|
|
267
|
+
node.type === 'CallExpression' &&
|
|
268
|
+
node.callee.type === 'MemberExpression' &&
|
|
269
|
+
!node.callee.computed &&
|
|
270
|
+
node.callee.property.name === name &&
|
|
271
|
+
node.arguments.length === 1
|
|
272
|
+
)
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function isNumber(node, value) {
|
|
276
|
+
return node?.type === 'Literal' && node.value === value
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function isMinusOne(node) {
|
|
280
|
+
return node?.type === 'UnaryExpression' && node.operator === '-' && isNumber(node.argument, 1)
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function isStringLiteral(node) {
|
|
284
|
+
return (
|
|
285
|
+
node.type === 'TemplateLiteral' || (node.type === 'Literal' && typeof node.value === 'string')
|
|
286
|
+
)
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// `a?.b.at(-1)` is undefined when `a` is, which `_lastOrUndefined(a?.b)` does not accept
|
|
290
|
+
function containsOptional(node) {
|
|
291
|
+
return visit(node, child => child.optional === true)
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function isLengthOf(node, array) {
|
|
295
|
+
return (
|
|
296
|
+
node.type === 'MemberExpression' &&
|
|
297
|
+
!node.computed &&
|
|
298
|
+
node.property.name === 'length' &&
|
|
299
|
+
isSameReference(node.object, array)
|
|
300
|
+
)
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// The same variable or property path (`a`, `this.a`, `a.b.c`) on both sides, nothing that is evaluated
|
|
304
|
+
function isSameReference(a, b) {
|
|
305
|
+
if (a.type !== b.type) return false
|
|
306
|
+
if (a.type === 'Identifier') return a.name === b.name
|
|
307
|
+
if (a.type === 'ThisExpression') return true
|
|
308
|
+
return (
|
|
309
|
+
a.type === 'MemberExpression' &&
|
|
310
|
+
!a.computed &&
|
|
311
|
+
!b.computed &&
|
|
312
|
+
a.property.name === b.property.name &&
|
|
313
|
+
isSameReference(a.object, b.object)
|
|
314
|
+
)
|
|
315
|
+
}
|
|
316
|
+
|
|
139
317
|
const ncPlugin = {
|
|
140
318
|
meta: {
|
|
141
319
|
name: 'nc',
|
|
142
320
|
},
|
|
143
321
|
rules: {
|
|
144
322
|
'prefer-assert': preferAssertRule,
|
|
323
|
+
'prefer-env-destructuring': preferEnvDestructuringRule,
|
|
324
|
+
'prefer-last': preferLastRule,
|
|
145
325
|
},
|
|
146
326
|
}
|
|
147
327
|
|
package/cfg/oxlint.config.js
CHANGED
|
@@ -73,6 +73,8 @@ export const sharedConfig = {
|
|
|
73
73
|
],
|
|
74
74
|
'@stylistic/lines-between-class-members': [2, 'always', { exceptAfterSingleLine: true }],
|
|
75
75
|
'nc/prefer-assert': 2,
|
|
76
|
+
'nc/prefer-env-destructuring': 2,
|
|
77
|
+
'nc/prefer-last': 2,
|
|
76
78
|
complexity: [2, { max: 40 }],
|
|
77
79
|
'logical-assignment-operators': [
|
|
78
80
|
2,
|
|
@@ -154,6 +156,8 @@ export const sharedConfig = {
|
|
|
154
156
|
'no-var': 2,
|
|
155
157
|
'unicode-bom': 2,
|
|
156
158
|
'no-extend-native': 2,
|
|
159
|
+
'no-prototype-builtins': 2,
|
|
160
|
+
'prefer-object-has-own': 2,
|
|
157
161
|
'no-extra-bind': 2,
|
|
158
162
|
'no-duplicate-imports': [
|
|
159
163
|
0, // disabled until auto-fix is there
|
package/cfg/summaryReporter.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { _last } from '@naturalcycles/js-lib/array/array.util.js'
|
|
1
2
|
import { _ms } from '@naturalcycles/js-lib/datetime'
|
|
2
3
|
|
|
3
4
|
export class SummaryReporter {
|
|
@@ -14,7 +15,7 @@ export class SummaryReporter {
|
|
|
14
15
|
// In the monorepo root run testModules span all projects, so prefix with the
|
|
15
16
|
// (short) package name to disambiguate identically-named files across packages.
|
|
16
17
|
const pkg = mod.project?.name?.split('/').at(-1)
|
|
17
|
-
const file = mod.moduleId.split('/')
|
|
18
|
+
const file = _last(mod.moduleId.split('/'))
|
|
18
19
|
const name = pkg ? `${pkg} ${file}` : file
|
|
19
20
|
const diag = mod.diagnostic()
|
|
20
21
|
stats.push({ name, ms: diag.duration })
|
package/dist/check.util.js
CHANGED
|
@@ -4,8 +4,7 @@ import { findPackageJSON } from 'node:module';
|
|
|
4
4
|
import path from 'node:path';
|
|
5
5
|
import { _isTruthy } from '@naturalcycles/js-lib';
|
|
6
6
|
import { _uniq } from '@naturalcycles/js-lib/array';
|
|
7
|
-
import { nowUnixMillis } from '@naturalcycles/js-lib/datetime/
|
|
8
|
-
import { _since } from '@naturalcycles/js-lib/datetime/time.util.js';
|
|
7
|
+
import { _since, nowUnixMillis } from '@naturalcycles/js-lib/datetime/time.util.js';
|
|
9
8
|
import { _assert } from '@naturalcycles/js-lib/error/assert.js';
|
|
10
9
|
import { _filterFalsyValues } from '@naturalcycles/js-lib/object/object.util.js';
|
|
11
10
|
import { semver2 } from '@naturalcycles/js-lib/semver';
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { _lastOrUndefined } from '@naturalcycles/js-lib/array/array.util.js';
|
|
1
2
|
import { _assert } from '@naturalcycles/js-lib/error';
|
|
2
3
|
import { fs2 } from '@naturalcycles/nodejs-lib/fs2';
|
|
3
4
|
import { validateCommitMessage } from './commitlint.util.js';
|
|
@@ -8,7 +9,7 @@ import { readDevLibConfigIfPresent } from './config.js';
|
|
|
8
9
|
*/
|
|
9
10
|
export async function runCommitlint() {
|
|
10
11
|
// || '.git/COMMIT_EDITMSG' // fallback is unnecessary, first argument should be always present
|
|
11
|
-
const arg1 = process.argv
|
|
12
|
+
const arg1 = _lastOrUndefined(process.argv);
|
|
12
13
|
_assert(arg1, 'dev-lib commitlint2 is called with $1 (first argument) missing');
|
|
13
14
|
console.log({ arg1 });
|
|
14
15
|
fs2.requireFileToExist(arg1);
|
package/dist/release/git.util.js
CHANGED
|
@@ -152,7 +152,8 @@ export function createAndPushTag(tag, repo) {
|
|
|
152
152
|
* NEVER log commands containing this url, and sanitize their errors - they carry the token.
|
|
153
153
|
*/
|
|
154
154
|
function getAuthenticatedRemote(repo) {
|
|
155
|
-
const
|
|
155
|
+
const { GITHUB_TOKEN, GH_TOKEN } = process.env;
|
|
156
|
+
const token = GITHUB_TOKEN || GH_TOKEN;
|
|
156
157
|
return token
|
|
157
158
|
? `https://x-access-token:${token}@github.com/${repo.owner}/${repo.repo}.git`
|
|
158
159
|
: 'origin';
|
|
@@ -45,7 +45,8 @@ export async function releaseCommand() {
|
|
|
45
45
|
console.log(`Branch "${branch}" is not configured for release - nothing to do`);
|
|
46
46
|
return;
|
|
47
47
|
}
|
|
48
|
-
const
|
|
48
|
+
const { GITHUB_TOKEN, GH_TOKEN } = process.env;
|
|
49
|
+
const token = GITHUB_TOKEN || GH_TOKEN;
|
|
49
50
|
if (!dryRun) {
|
|
50
51
|
_assert(token, 'GITHUB_TOKEN (or GH_TOKEN) is required to release, or use --dry-run');
|
|
51
52
|
}
|