@naturalcycles/dev-lib 20.75.0 → 20.76.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 +2 -3
- package/cfg/oxlint-plugin-nc.d.ts +1 -0
- package/cfg/oxlint-plugin-nc.js +242 -0
- package/cfg/oxlint.config.js +1 -0
- package/package.json +1 -1
package/cfg/collectReporter.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { _last } from '@naturalcycles/js-lib/array/array.util.js'
|
|
2
|
-
|
|
3
1
|
export class CollectReporter {
|
|
4
2
|
constructor(cfg = {}) {
|
|
5
3
|
this.cfg = cfg
|
|
@@ -7,7 +5,8 @@ export class CollectReporter {
|
|
|
7
5
|
|
|
8
6
|
onTestModuleEnd(testModule) {
|
|
9
7
|
const { threshold = 0 } = this.cfg
|
|
10
|
-
|
|
8
|
+
// oxlint-disable-next-line nc/prefer-last -- no js-lib import in this reporter, for performance
|
|
9
|
+
const shortModuleId = testModule.moduleId.split('/').at(-1)
|
|
11
10
|
|
|
12
11
|
const selfTimeMap = {}
|
|
13
12
|
const totalTimeMap = {}
|
package/cfg/oxlint-plugin-nc.js
CHANGED
|
@@ -140,6 +140,247 @@ function visit(node, fn) {
|
|
|
140
140
|
return false
|
|
141
141
|
}
|
|
142
142
|
|
|
143
|
+
// Report-only, like the rules above: `this` is only the same object while the method is called on
|
|
144
|
+
// the instance, so a method handed over unbound (`router.get(path, service.handle)`) needs a look
|
|
145
|
+
const noSingletonSelfReferenceRule = {
|
|
146
|
+
meta: {
|
|
147
|
+
type: 'suggestion',
|
|
148
|
+
docs: {
|
|
149
|
+
description:
|
|
150
|
+
'Inside a class, use `this` rather than the single instance of that class declared in the same file',
|
|
151
|
+
},
|
|
152
|
+
messages: {
|
|
153
|
+
useThis: 'Use `this` instead of {{name}} inside {{className}}',
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
create(context) {
|
|
157
|
+
const { sourceCode } = context
|
|
158
|
+
const classes = []
|
|
159
|
+
const instantiations = []
|
|
160
|
+
|
|
161
|
+
return {
|
|
162
|
+
ClassDeclaration(node) {
|
|
163
|
+
// `export default class {}` has no name to be instantiated by
|
|
164
|
+
if (node.id && isModuleLevel(node)) classes.push(node)
|
|
165
|
+
},
|
|
166
|
+
NewExpression(node) {
|
|
167
|
+
if (node.callee.type === 'Identifier') instantiations.push(node)
|
|
168
|
+
},
|
|
169
|
+
'Program:exit'() {
|
|
170
|
+
for (const classNode of classes) {
|
|
171
|
+
const className = classNode.id.name
|
|
172
|
+
// Every `new ClassName()` that refers to this class, wherever it is in the file
|
|
173
|
+
const ownInstantiations = instantiations.filter(
|
|
174
|
+
node => node.callee.name === className && !isShadowed(node, className, null),
|
|
175
|
+
)
|
|
176
|
+
// With several instances in the file, the one named is not necessarily `this`
|
|
177
|
+
if (ownInstantiations.length !== 1) continue
|
|
178
|
+
const [instantiation] = ownInstantiations
|
|
179
|
+
const declarator = instantiation.parent
|
|
180
|
+
if (!isModuleLevelConst(declarator, instantiation)) continue
|
|
181
|
+
|
|
182
|
+
const name = declarator.id.name
|
|
183
|
+
const classSource = sourceCode.text.slice(classNode.start, classNode.end)
|
|
184
|
+
if (!new RegExp(String.raw`(?<![\w$])${escapeRegExp(name)}(?![\w$])`).test(classSource)) {
|
|
185
|
+
continue
|
|
186
|
+
}
|
|
187
|
+
forEachValueIdentifier(classNode.body, name, node => {
|
|
188
|
+
if (isInstanceCodeOf(node, classNode) && !isShadowed(node, name, classNode)) {
|
|
189
|
+
context.report({ node, messageId: 'useThis', data: { name, className } })
|
|
190
|
+
}
|
|
191
|
+
})
|
|
192
|
+
}
|
|
193
|
+
},
|
|
194
|
+
}
|
|
195
|
+
},
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function escapeRegExp(text) {
|
|
199
|
+
return text.replaceAll('$', String.raw`\$`)
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// Type annotations, type declarations and labels hold no runtime reference
|
|
203
|
+
const TYPE_ONLY_KEYS = new Set([
|
|
204
|
+
'typeAnnotation',
|
|
205
|
+
'returnType',
|
|
206
|
+
'typeParameters',
|
|
207
|
+
'typeArguments',
|
|
208
|
+
'superTypeArguments',
|
|
209
|
+
'implements',
|
|
210
|
+
'label',
|
|
211
|
+
])
|
|
212
|
+
const TYPE_ONLY_NODES = new Set(['TSInterfaceDeclaration', 'TSTypeAliasDeclaration'])
|
|
213
|
+
|
|
214
|
+
// Calls `fn` for each identifier named `name` below `node` that is a value, not a type or a key
|
|
215
|
+
function forEachValueIdentifier(node, name, fn) {
|
|
216
|
+
visit(node, child => {
|
|
217
|
+
if (TYPE_ONLY_NODES.has(child.type)) return false
|
|
218
|
+
if (child.type === 'Identifier' && child.name === name && !isTypeOnly(child, node)) fn(child)
|
|
219
|
+
return false
|
|
220
|
+
})
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function isTypeOnly(identifier, root) {
|
|
224
|
+
let child = identifier
|
|
225
|
+
for (
|
|
226
|
+
let { parent } = identifier;
|
|
227
|
+
parent && child !== root;
|
|
228
|
+
child = parent, parent = parent.parent
|
|
229
|
+
) {
|
|
230
|
+
if (TYPE_ONLY_NODES.has(parent.type)) return true
|
|
231
|
+
for (const key of TYPE_ONLY_KEYS) {
|
|
232
|
+
if (parent[key] === child) return true
|
|
233
|
+
if (Array.isArray(parent[key]) && parent[key].includes(child)) return true
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return false
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// Whether a declaration between `node` and `boundary` (or the file, when null) binds `name`
|
|
240
|
+
function isShadowed(node, name, boundary) {
|
|
241
|
+
let child = node
|
|
242
|
+
for (
|
|
243
|
+
let { parent } = node;
|
|
244
|
+
parent && parent !== boundary;
|
|
245
|
+
child = parent, parent = parent.parent
|
|
246
|
+
) {
|
|
247
|
+
if (parent.type === 'Program') return false
|
|
248
|
+
// All cases of a switch share one scope; the discriminant is outside it
|
|
249
|
+
if (parent.type === 'SwitchStatement') {
|
|
250
|
+
if (child === parent.discriminant) continue
|
|
251
|
+
const cases = parent.cases.flatMap(switchCase => switchCase.consequent)
|
|
252
|
+
if (cases.some(statement => statementBindsName(statement, name))) return true
|
|
253
|
+
continue
|
|
254
|
+
}
|
|
255
|
+
if (declaresName(parent, name)) return true
|
|
256
|
+
}
|
|
257
|
+
return false
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function declaresName(node, name) {
|
|
261
|
+
switch (node.type) {
|
|
262
|
+
case 'FunctionDeclaration':
|
|
263
|
+
case 'FunctionExpression':
|
|
264
|
+
case 'ArrowFunctionExpression': {
|
|
265
|
+
if (node.type === 'FunctionExpression' && node.id?.name === name) return true
|
|
266
|
+
return node.params.some(param => bindsName(param, name))
|
|
267
|
+
}
|
|
268
|
+
// A class declaration's own name binds in the enclosing block (checked there); a named class
|
|
269
|
+
// expression binds it inside itself
|
|
270
|
+
case 'ClassExpression': {
|
|
271
|
+
return node.id?.name === name
|
|
272
|
+
}
|
|
273
|
+
case 'CatchClause': {
|
|
274
|
+
return !!node.param && bindsName(node.param, name)
|
|
275
|
+
}
|
|
276
|
+
case 'ForStatement':
|
|
277
|
+
case 'ForInStatement':
|
|
278
|
+
case 'ForOfStatement': {
|
|
279
|
+
const head = node.type === 'ForStatement' ? node.init : node.left
|
|
280
|
+
return head?.type === 'VariableDeclaration' && statementBindsName(head, name)
|
|
281
|
+
}
|
|
282
|
+
case 'BlockStatement':
|
|
283
|
+
case 'StaticBlock': {
|
|
284
|
+
return node.body.some(statement => statementBindsName(statement, name))
|
|
285
|
+
}
|
|
286
|
+
default: {
|
|
287
|
+
return false
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function statementBindsName(statement, name) {
|
|
293
|
+
const declaration =
|
|
294
|
+
statement.type === 'ExportNamedDeclaration' ? statement.declaration : statement
|
|
295
|
+
if (!declaration) return false
|
|
296
|
+
if (declaration.type === 'VariableDeclaration') {
|
|
297
|
+
return declaration.declarations.some(declarator => bindsName(declarator.id, name))
|
|
298
|
+
}
|
|
299
|
+
return (
|
|
300
|
+
(declaration.type === 'FunctionDeclaration' || declaration.type === 'ClassDeclaration') &&
|
|
301
|
+
declaration.id?.name === name
|
|
302
|
+
)
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// Whether a parameter or destructuring pattern introduces `name`
|
|
306
|
+
function bindsName(pattern, name) {
|
|
307
|
+
switch (pattern?.type) {
|
|
308
|
+
case 'Identifier': {
|
|
309
|
+
return pattern.name === name
|
|
310
|
+
}
|
|
311
|
+
case 'AssignmentPattern': {
|
|
312
|
+
return bindsName(pattern.left, name)
|
|
313
|
+
}
|
|
314
|
+
case 'RestElement': {
|
|
315
|
+
return bindsName(pattern.argument, name)
|
|
316
|
+
}
|
|
317
|
+
case 'TSParameterProperty': {
|
|
318
|
+
return bindsName(pattern.parameter, name)
|
|
319
|
+
}
|
|
320
|
+
case 'ArrayPattern': {
|
|
321
|
+
return pattern.elements.some(element => bindsName(element, name))
|
|
322
|
+
}
|
|
323
|
+
case 'ObjectPattern': {
|
|
324
|
+
return pattern.properties.some(property =>
|
|
325
|
+
bindsName(property.type === 'RestElement' ? property : property.value, name),
|
|
326
|
+
)
|
|
327
|
+
}
|
|
328
|
+
default: {
|
|
329
|
+
return false
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// `const service = new Service()` or `export const service = new Service()`, not nested in a function
|
|
335
|
+
function isModuleLevelConst(declarator, init) {
|
|
336
|
+
if (declarator.type !== 'VariableDeclarator' || declarator.init !== init) return false
|
|
337
|
+
if (declarator.id.type !== 'Identifier') return false
|
|
338
|
+
const declaration = declarator.parent
|
|
339
|
+
return declaration.kind === 'const' && isModuleLevel(declaration)
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// Directly in the file, exported or not
|
|
343
|
+
function isModuleLevel(node) {
|
|
344
|
+
const { parent } = node
|
|
345
|
+
return (
|
|
346
|
+
parent.type === 'Program' ||
|
|
347
|
+
(parent.type.startsWith('Export') && parent.parent.type === 'Program')
|
|
348
|
+
)
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
// Whether `this` at `node` is an instance of the class: in an instance method, accessor, constructor
|
|
352
|
+
// or field initializer, with nothing in between that has a `this` of its own
|
|
353
|
+
function isInstanceCodeOf(node, classNode) {
|
|
354
|
+
let child = node
|
|
355
|
+
for (let { parent } = node; parent; child = parent, { parent } = parent) {
|
|
356
|
+
switch (parent.type) {
|
|
357
|
+
// `typeof service` in a type
|
|
358
|
+
case 'TSTypeQuery':
|
|
359
|
+
// A decorator runs when the class is defined, also one on a parameter
|
|
360
|
+
case 'Decorator':
|
|
361
|
+
case 'FunctionDeclaration':
|
|
362
|
+
case 'StaticBlock': {
|
|
363
|
+
return false
|
|
364
|
+
}
|
|
365
|
+
case 'FunctionExpression': {
|
|
366
|
+
if (parent.parent.type !== 'MethodDefinition') return false
|
|
367
|
+
break
|
|
368
|
+
}
|
|
369
|
+
case 'MethodDefinition':
|
|
370
|
+
case 'PropertyDefinition':
|
|
371
|
+
case 'AccessorProperty': {
|
|
372
|
+
// Not a static member, and not its decorator or computed key
|
|
373
|
+
if (parent.static || parent.value !== child) return false
|
|
374
|
+
break
|
|
375
|
+
}
|
|
376
|
+
case 'ClassBody': {
|
|
377
|
+
return parent.parent === classNode
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
return false
|
|
382
|
+
}
|
|
383
|
+
|
|
143
384
|
const preferLastRule = {
|
|
144
385
|
meta: {
|
|
145
386
|
type: 'suggestion',
|
|
@@ -319,6 +560,7 @@ const ncPlugin = {
|
|
|
319
560
|
name: 'nc',
|
|
320
561
|
},
|
|
321
562
|
rules: {
|
|
563
|
+
'no-singleton-self-reference': noSingletonSelfReferenceRule,
|
|
322
564
|
'prefer-assert': preferAssertRule,
|
|
323
565
|
'prefer-env-destructuring': preferEnvDestructuringRule,
|
|
324
566
|
'prefer-last': preferLastRule,
|
package/cfg/oxlint.config.js
CHANGED
|
@@ -75,6 +75,7 @@ export const sharedConfig = {
|
|
|
75
75
|
{ blankLine: 'always', prev: '*', next: 'class' },
|
|
76
76
|
],
|
|
77
77
|
'@stylistic/lines-between-class-members': [2, 'always', { exceptAfterSingleLine: true }],
|
|
78
|
+
'nc/no-singleton-self-reference': 2,
|
|
78
79
|
'nc/prefer-assert': 2,
|
|
79
80
|
'nc/prefer-env-destructuring': 2,
|
|
80
81
|
'nc/prefer-last': 2,
|