@cerios/sparta-lint-rules 0.0.1 → 0.0.2
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/LICENSE +7 -7
- package/README.md +46 -30
- package/dist/index.cjs +2 -2
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +2 -2
- package/dist/oxlint.cjs +1 -1
- package/dist/oxlint.d.cts +1 -1
- package/dist/oxlint.d.mts +1 -1
- package/dist/oxlint.mjs +1 -1
- package/dist/{plugin-types-CgeW0cQl.d.cts → plugin-types-CXFDQLJF.d.cts} +2 -1
- package/dist/{plugin-types-CgeW0cQl.d.mts → plugin-types-CXFDQLJF.d.mts} +2 -1
- package/dist/{rules-BSRYNh4F.mjs → rules-D94N3gGx.mjs} +850 -171
- package/dist/rules-D94N3gGx.mjs.map +1 -0
- package/dist/{rules-COk7kmEU.cjs → rules-T_C5segG.cjs} +850 -171
- package/dist/rules-T_C5segG.cjs.map +1 -0
- package/dist/settings.d.cts +5 -1
- package/dist/settings.d.mts +5 -1
- package/package.json +1 -1
- package/dist/rules-BSRYNh4F.mjs.map +0 -1
- package/dist/rules-COk7kmEU.cjs.map +0 -1
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
1
2
|
import path from "node:path";
|
|
2
3
|
import { existsSync, readFileSync, statSync } from "node:fs";
|
|
3
4
|
import { parseSync } from "oxc-parser";
|
|
@@ -5,12 +6,51 @@ import { parseSync } from "oxc-parser";
|
|
|
5
6
|
const isIdentifier = (node, name) => node?.type === "Identifier" && node.name === name;
|
|
6
7
|
/** The value of a string literal node, or `undefined` for any other node. */
|
|
7
8
|
const stringValue = (node) => node?.type === "Literal" && typeof node.value === "string" ? node.value : void 0;
|
|
9
|
+
/** The value of a string literal or a template literal without expressions (`\`./x\``), or `undefined`. */
|
|
10
|
+
const staticString = (node) => node?.type === "TemplateLiteral" && node.expressions.length === 0 ? node.quasis[0]?.value.cooked ?? void 0 : stringValue(node);
|
|
8
11
|
/** True for `@step` and `@step("...")` decorators, or any of the given decorator `names`. */
|
|
9
12
|
const isStepDecorator = (decorator, names) => {
|
|
10
13
|
const { expression } = decorator;
|
|
11
14
|
const callee = expression.type === "CallExpression" ? expression.callee : expression;
|
|
12
15
|
return callee.type === "Identifier" && names.has(callee.name);
|
|
13
16
|
};
|
|
17
|
+
// src/utils/class-members.ts
|
|
18
|
+
const isMethod = (member) => member.type === "MethodDefinition" || member.type === "TSAbstractMethodDefinition";
|
|
19
|
+
const FUNCTION_VALUES$1 = /* @__PURE__ */ new Set([
|
|
20
|
+
"ArrowFunctionExpression",
|
|
21
|
+
"FunctionExpression",
|
|
22
|
+
"ClassExpression"
|
|
23
|
+
]);
|
|
24
|
+
/**
|
|
25
|
+
* A plain property declaration (`static readonly PATTERN = /.../;`, `private readonly date: Date;`, …).
|
|
26
|
+
* A property holding a function or class is not plain: it is treated like a method.
|
|
27
|
+
*/
|
|
28
|
+
const isPlainProperty = (member) => {
|
|
29
|
+
if (member.type === "TSIndexSignature") return true;
|
|
30
|
+
if (member.type !== "PropertyDefinition" && member.type !== "TSAbstractPropertyDefinition" && member.type !== "AccessorProperty") return false;
|
|
31
|
+
return member.value === null || !FUNCTION_VALUES$1.has(member.value.type);
|
|
32
|
+
};
|
|
33
|
+
/** `private` or an ECMAScript `#private` name. */
|
|
34
|
+
const isPrivate = (member) => "accessibility" in member && member.accessibility === "private" || "key" in member && member.key.type === "PrivateIdentifier";
|
|
35
|
+
const memberName$1 = (member) => {
|
|
36
|
+
const key = "key" in member ? member.key : void 0;
|
|
37
|
+
if (key && "name" in key && typeof key.name === "string") return key.name;
|
|
38
|
+
if (key && "value" in key && typeof key.value === "string") return key.value;
|
|
39
|
+
return "<computed>";
|
|
40
|
+
};
|
|
41
|
+
/** A member that belongs to the class itself: `static` members, static blocks and a `private constructor`. */
|
|
42
|
+
const isStaticMember = (member) => member.type === "StaticBlock" || "static" in member && member.static || member.type === "MethodDefinition" && member.kind === "constructor" && member.accessibility === "private";
|
|
43
|
+
/** A static abstract class: `abstract`, with only static members, so it is used without ever being instantiated. */
|
|
44
|
+
const isStaticClass = (node) => node.abstract === true && node.body.body.every(isStaticMember);
|
|
45
|
+
/** The classes declared at the top level of a program, including exported ones. */
|
|
46
|
+
function topLevelClasses(program) {
|
|
47
|
+
const classes = [];
|
|
48
|
+
for (const statement of program.body) {
|
|
49
|
+
const declaration = statement.type === "ExportNamedDeclaration" || statement.type === "ExportDefaultDeclaration" ? statement.declaration : statement;
|
|
50
|
+
if (declaration?.type === "ClassDeclaration") classes.push(declaration);
|
|
51
|
+
}
|
|
52
|
+
return classes;
|
|
53
|
+
}
|
|
14
54
|
// src/utils/docs.ts
|
|
15
55
|
const REPOSITORY = "https://github.com/CeriosTesting/sparta-lint-rules";
|
|
16
56
|
const docsUrl = (ruleName) => `${REPOSITORY}/blob/main/docs/rules/${ruleName}.md`;
|
|
@@ -63,11 +103,18 @@ const LAYER_FOLDERS = {
|
|
|
63
103
|
steppers: {
|
|
64
104
|
layer: "stepper",
|
|
65
105
|
suffix: "stepper"
|
|
106
|
+
},
|
|
107
|
+
windows: {
|
|
108
|
+
layer: "window",
|
|
109
|
+
suffix: "window"
|
|
66
110
|
}
|
|
67
111
|
};
|
|
112
|
+
/** Layer folders that live directly in an area (`sparta/<area>/builders/`), shared by its groups, not in a group. */
|
|
113
|
+
const AREA_LAYER_FOLDERS = /* @__PURE__ */ new Set(["builders", "directors"]);
|
|
68
114
|
const AREAS = [
|
|
69
115
|
"api",
|
|
70
116
|
"database",
|
|
117
|
+
"desktop",
|
|
71
118
|
"mobile",
|
|
72
119
|
"ui"
|
|
73
120
|
];
|
|
@@ -109,9 +156,9 @@ const suggest = (value, candidates) => {
|
|
|
109
156
|
})).toSorted((a, b) => a.score - b.score);
|
|
110
157
|
return best && best.score <= Math.max(2, Math.floor(best.candidate.length / 3)) ? `; did you mean ${JSON.stringify(best.candidate)}?` : "";
|
|
111
158
|
};
|
|
112
|
-
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
159
|
+
const isRecord$1 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
113
160
|
function record(value, where) {
|
|
114
|
-
if (!isRecord(value)) throw new SettingsError(where, `must be an object, got ${JSON.stringify(value)}.`);
|
|
161
|
+
if (!isRecord$1(value)) throw new SettingsError(where, `must be an object, got ${JSON.stringify(value)}.`);
|
|
115
162
|
return value;
|
|
116
163
|
}
|
|
117
164
|
function checkKeys(value, allowed, where) {
|
|
@@ -200,14 +247,15 @@ function buildPatterns(spartaFolder, testsFolder, areas) {
|
|
|
200
247
|
const sparta = escape(spartaFolder);
|
|
201
248
|
const tests = escape(testsFolder);
|
|
202
249
|
const areaNames = [...areas.keys()].map(escape).join("|");
|
|
250
|
+
const end = "(?=/|$)";
|
|
203
251
|
return {
|
|
204
|
-
area: new RegExp(`/${sparta}/(${areaNames})
|
|
252
|
+
area: new RegExp(`/${sparta}/(${areaNames})${end}(?:/([^/]+))?(?:/([^/]+))?`),
|
|
205
253
|
layer: new RegExp(`/${sparta}/(?:${areaNames})/[^/]+/([^/]+)/`),
|
|
206
|
-
top: new RegExp(`/${sparta}/(${TOP_LEVEL_FOLDERS.join("|")})
|
|
254
|
+
top: new RegExp(`/${sparta}/(${TOP_LEVEL_FOLDERS.join("|")})${end}`),
|
|
207
255
|
checked: new RegExp(`/(?:${sparta}|${tests})/`),
|
|
208
256
|
sparta: new RegExp(`/${sparta}/`),
|
|
209
|
-
tests: new RegExp(`/${tests}
|
|
210
|
-
framework: new RegExp(`/${tests}/framework
|
|
257
|
+
tests: new RegExp(`/${tests}${end}`),
|
|
258
|
+
framework: new RegExp(`/${tests}/framework${end}`)
|
|
211
259
|
};
|
|
212
260
|
}
|
|
213
261
|
const STANDARD_LAYER_FOLDERS = Object.keys(LAYER_FOLDERS);
|
|
@@ -238,6 +286,196 @@ function parseSettings(raw, cwd) {
|
|
|
238
286
|
};
|
|
239
287
|
}
|
|
240
288
|
parseSettings(void 0, process.cwd());
|
|
289
|
+
// src/utils/tsconfig.ts
|
|
290
|
+
const normalize$1 = (value) => value.split("\\").join("/");
|
|
291
|
+
const CONFIG_DIR = "${configDir}";
|
|
292
|
+
/** Where the string or comment that starts at `index` ends (exclusive), or `undefined` when none starts there. */
|
|
293
|
+
function skippedEnd(text, index) {
|
|
294
|
+
const start = text.slice(index, index + 2);
|
|
295
|
+
if (text[index] === "\"") {
|
|
296
|
+
let end = index + 1;
|
|
297
|
+
while (end < text.length && text[end] !== "\"") end += text[end] === "\\" ? 2 : 1;
|
|
298
|
+
return end + 1;
|
|
299
|
+
}
|
|
300
|
+
if (start === "//") {
|
|
301
|
+
const end = text.indexOf("\n", index);
|
|
302
|
+
return end === -1 ? text.length : end;
|
|
303
|
+
}
|
|
304
|
+
if (start === "/*") {
|
|
305
|
+
const end = text.indexOf("*/", index + 2);
|
|
306
|
+
return end === -1 ? text.length : end + 2;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Parses JSON with comments and trailing commas, as `tsconfig.json` allows. Comments are only recognised outside
|
|
311
|
+
* strings, so `"https://..."` stays intact.
|
|
312
|
+
*/
|
|
313
|
+
function parseJsonc(text) {
|
|
314
|
+
let out = "";
|
|
315
|
+
/** Position in `out` of a comma that may still turn out to be a trailing one. */
|
|
316
|
+
let pendingComma = -1;
|
|
317
|
+
let index = text.charCodeAt(0) === 65279 ? 1 : 0;
|
|
318
|
+
while (index < text.length) {
|
|
319
|
+
const char = text[index];
|
|
320
|
+
const end = skippedEnd(text, index);
|
|
321
|
+
if (end !== void 0) {
|
|
322
|
+
if (char === "\"") {
|
|
323
|
+
out += text.slice(index, end);
|
|
324
|
+
pendingComma = -1;
|
|
325
|
+
}
|
|
326
|
+
index = end;
|
|
327
|
+
continue;
|
|
328
|
+
}
|
|
329
|
+
if (char === ",") pendingComma = out.length;
|
|
330
|
+
else if ((char === "}" || char === "]") && pendingComma !== -1) {
|
|
331
|
+
out = `${out.slice(0, pendingComma)} ${out.slice(pendingComma + 1)}`;
|
|
332
|
+
pendingComma = -1;
|
|
333
|
+
} else if (!/\s/.test(char)) pendingComma = -1;
|
|
334
|
+
out += char;
|
|
335
|
+
index++;
|
|
336
|
+
}
|
|
337
|
+
return JSON.parse(out);
|
|
338
|
+
}
|
|
339
|
+
const isFile$1 = (file) => statSync(file, { throwIfNoEntry: false })?.isFile() ?? false;
|
|
340
|
+
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
341
|
+
var TsconfigError = class extends Error {
|
|
342
|
+
constructor(file, message) {
|
|
343
|
+
super(`sparta-lint-rules: cannot read ${normalize$1(file)}: ${message}`);
|
|
344
|
+
this.name = "TsconfigError";
|
|
345
|
+
}
|
|
346
|
+
};
|
|
347
|
+
function readConfig(file) {
|
|
348
|
+
let config;
|
|
349
|
+
try {
|
|
350
|
+
config = parseJsonc(readFileSync(file, "utf8"));
|
|
351
|
+
} catch (error) {
|
|
352
|
+
throw new TsconfigError(file, error instanceof Error ? error.message : String(error));
|
|
353
|
+
}
|
|
354
|
+
if (!isRecord(config)) throw new TsconfigError(file, "the config must be a JSON object.");
|
|
355
|
+
return config;
|
|
356
|
+
}
|
|
357
|
+
/** The file an `extends` entry points to: a path relative to the config, or a package such as `@tsconfig/node22`. */
|
|
358
|
+
function resolveExtends(specifier, from) {
|
|
359
|
+
if (specifier.startsWith(".") || path.isAbsolute(specifier)) {
|
|
360
|
+
const file = path.resolve(path.dirname(from), specifier);
|
|
361
|
+
if (isFile$1(file)) return file;
|
|
362
|
+
if (!file.endsWith(".json") && isFile$1(`${file}.json`)) return `${file}.json`;
|
|
363
|
+
} else {
|
|
364
|
+
const require = createRequire(from);
|
|
365
|
+
const candidates = specifier.endsWith(".json") ? [specifier] : [`${specifier}/tsconfig.json`, specifier];
|
|
366
|
+
for (const candidate of candidates) try {
|
|
367
|
+
return require.resolve(candidate);
|
|
368
|
+
} catch {}
|
|
369
|
+
}
|
|
370
|
+
throw new TsconfigError(from, `extends ${JSON.stringify(specifier)}, which cannot be found.`);
|
|
371
|
+
}
|
|
372
|
+
const substitute = (value, configDir) => value.split(CONFIG_DIR).join(configDir);
|
|
373
|
+
function loadOptions(file, configDir, chain, read) {
|
|
374
|
+
if (chain.includes(file)) throw new TsconfigError(chain[0], `circular extends: ${[...chain, file].map(normalize$1).join(" -> ")}.`);
|
|
375
|
+
const config = readConfig(file);
|
|
376
|
+
const dir = path.dirname(file);
|
|
377
|
+
const parents = typeof config.extends === "string" ? [config.extends] : config.extends;
|
|
378
|
+
let options = {};
|
|
379
|
+
read.push(file);
|
|
380
|
+
if (Array.isArray(parents)) for (const parent of parents) {
|
|
381
|
+
if (typeof parent !== "string") continue;
|
|
382
|
+
const inherited = loadOptions(resolveExtends(parent, file), configDir, [...chain, file], read);
|
|
383
|
+
options = {
|
|
384
|
+
...options,
|
|
385
|
+
...inherited
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
const compilerOptions = isRecord(config.compilerOptions) ? config.compilerOptions : {};
|
|
389
|
+
if (typeof compilerOptions.baseUrl === "string") options.baseUrl = path.resolve(dir, substitute(compilerOptions.baseUrl, configDir));
|
|
390
|
+
if (isRecord(compilerOptions.paths)) options.paths = {
|
|
391
|
+
mapping: compilerOptions.paths,
|
|
392
|
+
base: dir
|
|
393
|
+
};
|
|
394
|
+
return options;
|
|
395
|
+
}
|
|
396
|
+
function toMappings(options, configDir) {
|
|
397
|
+
if (!options.paths) return [];
|
|
398
|
+
const base = options.baseUrl ?? options.paths.base;
|
|
399
|
+
return Object.entries(options.paths.mapping).flatMap(([pattern, value]) => {
|
|
400
|
+
const targets = Array.isArray(value) ? value.filter((target) => typeof target === "string") : [];
|
|
401
|
+
const star = pattern.indexOf("*");
|
|
402
|
+
if (targets.length === 0) return [];
|
|
403
|
+
return [{
|
|
404
|
+
pattern,
|
|
405
|
+
prefix: star === -1 ? pattern : pattern.slice(0, star),
|
|
406
|
+
suffix: star === -1 ? "" : pattern.slice(star + 1),
|
|
407
|
+
wildcard: star !== -1,
|
|
408
|
+
targets: targets.map((target) => normalize$1(path.resolve(base, substitute(target, configDir))))
|
|
409
|
+
}];
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
/** Parsed configs per file, kept for the lifetime of the linter process and refreshed when any file in the chain changes. */
|
|
413
|
+
const configCache = /* @__PURE__ */ new Map();
|
|
414
|
+
const mtime = (file) => statSync(file, { throwIfNoEntry: false })?.mtimeMs;
|
|
415
|
+
/** The `paths` and `baseUrl` of a config file, including what it extends. Throws for unreadable or invalid configs. */
|
|
416
|
+
function loadTsconfig(file) {
|
|
417
|
+
const cached = configCache.get(file);
|
|
418
|
+
if (cached?.read.every((entry) => mtime(entry.file) === entry.mtimeMs)) return cached.value;
|
|
419
|
+
const configDir = path.dirname(file);
|
|
420
|
+
const read = [];
|
|
421
|
+
const options = loadOptions(file, configDir, [], read);
|
|
422
|
+
const value = {
|
|
423
|
+
file: normalize$1(file),
|
|
424
|
+
baseUrl: options.baseUrl === void 0 ? void 0 : normalize$1(options.baseUrl),
|
|
425
|
+
paths: toMappings(options, configDir)
|
|
426
|
+
};
|
|
427
|
+
configCache.set(file, {
|
|
428
|
+
read: read.map((entry) => ({
|
|
429
|
+
file: entry,
|
|
430
|
+
mtimeMs: mtime(entry) ?? -1
|
|
431
|
+
})),
|
|
432
|
+
value
|
|
433
|
+
});
|
|
434
|
+
return value;
|
|
435
|
+
}
|
|
436
|
+
/** Nearest `tsconfig.json` per directory (`null` for none), kept for the lifetime of the linter process. */
|
|
437
|
+
const lookupCache = /* @__PURE__ */ new Map();
|
|
438
|
+
/** The nearest `tsconfig.json`, from the file's folder upwards, or `undefined` when there is none. */
|
|
439
|
+
function findTsconfig(filename) {
|
|
440
|
+
const visited = [];
|
|
441
|
+
let found;
|
|
442
|
+
for (let dir = path.dirname(path.resolve(filename));;) {
|
|
443
|
+
found = lookupCache.get(dir);
|
|
444
|
+
if (found !== void 0) break;
|
|
445
|
+
visited.push(dir);
|
|
446
|
+
const candidate = path.join(dir, "tsconfig.json");
|
|
447
|
+
if (isFile$1(candidate)) {
|
|
448
|
+
found = candidate;
|
|
449
|
+
break;
|
|
450
|
+
}
|
|
451
|
+
const parent = path.dirname(dir);
|
|
452
|
+
if (parent === dir) {
|
|
453
|
+
found = null;
|
|
454
|
+
break;
|
|
455
|
+
}
|
|
456
|
+
dir = parent;
|
|
457
|
+
}
|
|
458
|
+
for (const dir of visited) lookupCache.set(dir, found);
|
|
459
|
+
return found ?? void 0;
|
|
460
|
+
}
|
|
461
|
+
/** The `paths` and `baseUrl` that apply to a file, from its nearest `tsconfig.json`. */
|
|
462
|
+
function tsconfigFor(filename) {
|
|
463
|
+
const file = findTsconfig(filename);
|
|
464
|
+
return file === void 0 ? void 0 : loadTsconfig(file);
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
467
|
+
* The candidate paths a `paths` entry maps a request to, as TypeScript picks them: an exact pattern first,
|
|
468
|
+
* otherwise the wildcard pattern with the longest prefix. `undefined` when no pattern matches.
|
|
469
|
+
*/
|
|
470
|
+
function matchPaths(request, mappings) {
|
|
471
|
+
const exact = mappings.find((mapping) => !mapping.wildcard && mapping.pattern === request);
|
|
472
|
+
if (exact) return exact.targets;
|
|
473
|
+
let best;
|
|
474
|
+
for (const mapping of mappings) if (mapping.wildcard && request.length >= mapping.prefix.length + mapping.suffix.length && request.startsWith(mapping.prefix) && request.endsWith(mapping.suffix) && (best === void 0 || mapping.prefix.length > best.prefix.length)) best = mapping;
|
|
475
|
+
if (best === void 0) return void 0;
|
|
476
|
+
const matched = request.slice(best.prefix.length, request.length - best.suffix.length);
|
|
477
|
+
return best.targets.map((target) => target.replace("*", matched));
|
|
478
|
+
}
|
|
241
479
|
// src/utils/sparta-paths.ts
|
|
242
480
|
const normalize = (value) => value.split("\\").join("/");
|
|
243
481
|
const TOP_LEVEL_KINDS = {
|
|
@@ -246,6 +484,17 @@ const TOP_LEVEL_KINDS = {
|
|
|
246
484
|
};
|
|
247
485
|
/** The standard layer folder that a project's folder name follows, if any. */
|
|
248
486
|
const standardFolder = (conventions, folder) => folder === void 0 ? void 0 : conventions.layerFolders.get(folder);
|
|
487
|
+
/** The layer of a standard layer folder (`stepper` for `steppers`), if it has one. */
|
|
488
|
+
const layerOf = (standard) => standard === void 0 ? void 0 : LAYER_FOLDERS[standard].layer;
|
|
489
|
+
/** The area-level layer folder (`builders/`, `directors/` directly below an area) a project path is in, if any. */
|
|
490
|
+
const areaLayerFolder = (file, conventions) => {
|
|
491
|
+
const name = file.match(conventions.patterns.area)?.[2];
|
|
492
|
+
const standard = standardFolder(conventions, name);
|
|
493
|
+
return name !== void 0 && standard !== void 0 && AREA_LAYER_FOLDERS.has(standard) ? {
|
|
494
|
+
name,
|
|
495
|
+
standard
|
|
496
|
+
} : void 0;
|
|
497
|
+
};
|
|
249
498
|
const caseInsensitivePaths = process.platform === "win32";
|
|
250
499
|
/**
|
|
251
500
|
* The part of `filename` below the project `root`, with a leading `/` (`/sparta/ui/...`), or `undefined` for files
|
|
@@ -267,9 +516,15 @@ function classify(filename, conventions, root) {
|
|
|
267
516
|
const area = file.match(patterns.area);
|
|
268
517
|
if (area) {
|
|
269
518
|
const [, areaName, group, folder] = area;
|
|
519
|
+
const areaLayer = areaLayerFolder(file, conventions);
|
|
520
|
+
if (areaLayer) return {
|
|
521
|
+
kind: "area-layer",
|
|
522
|
+
area: conventions.areas.get(areaName),
|
|
523
|
+
areaFolder: areaName,
|
|
524
|
+
layer: layerOf(areaLayer.standard)
|
|
525
|
+
};
|
|
270
526
|
const base = file.match(/-base-(client|repository|service)(?:\.[cm]?[jt]s)?$/)?.[1];
|
|
271
|
-
const
|
|
272
|
-
const layer = base ? `base-${base}` : standard === void 0 ? void 0 : LAYER_FOLDERS[standard].layer;
|
|
527
|
+
const layer = base ? `base-${base}` : layerOf(standardFolder(conventions, folder));
|
|
273
528
|
return {
|
|
274
529
|
kind: group === "common" ? "area-common" : "group",
|
|
275
530
|
area: conventions.areas.get(areaName),
|
|
@@ -308,15 +563,49 @@ function projectRoot(filename) {
|
|
|
308
563
|
for (const dir of visited) rootCache.set(dir, root);
|
|
309
564
|
return root;
|
|
310
565
|
}
|
|
566
|
+
/** The extensions tried, in order, to find the file behind an extension-less import. */
|
|
567
|
+
const MODULE_EXTENSIONS = [
|
|
568
|
+
"",
|
|
569
|
+
".ts",
|
|
570
|
+
".tsx",
|
|
571
|
+
".mts",
|
|
572
|
+
".cts",
|
|
573
|
+
"/index.ts"
|
|
574
|
+
];
|
|
575
|
+
const isFile = (file) => statSync(file, { throwIfNoEntry: false })?.isFile() ?? false;
|
|
576
|
+
/** The TypeScript file an import path points to (`./x` -> `./x.ts`, `./x.js` -> `./x.ts`), if it exists. */
|
|
577
|
+
function moduleFile(base) {
|
|
578
|
+
const stripped = base.replace(/\.(?:m|c)?js$/, "");
|
|
579
|
+
for (const candidate of [base, ...MODULE_EXTENSIONS.map((extension) => `${stripped}${extension}`)]) if (isFile(candidate)) return normalize(candidate);
|
|
580
|
+
}
|
|
581
|
+
/** True when an import path points to an existing file or folder. */
|
|
582
|
+
const moduleExists = (base) => (statSync(base, { throwIfNoEntry: false })?.isDirectory() ?? false) || moduleFile(base) !== void 0;
|
|
311
583
|
/**
|
|
312
|
-
* Resolves
|
|
313
|
-
*
|
|
584
|
+
* Resolves an import to an absolute, normalized path, the way TypeScript would:
|
|
585
|
+
*
|
|
586
|
+
* 1. relative imports, against the importing file;
|
|
587
|
+
* 2. `compilerOptions.paths` of the nearest `tsconfig.json`, taking the first target that exists;
|
|
588
|
+
* 3. the `@sparta/*` and `@tests/*` aliases and `settings.sparta.aliases`, against `root`;
|
|
589
|
+
* 4. the first `paths` target when none exists (yet);
|
|
590
|
+
* 5. `compilerOptions.baseUrl`, only when the file exists, so package imports are not mistaken for project files.
|
|
591
|
+
*
|
|
592
|
+
* Other imports (packages) resolve to `undefined`.
|
|
314
593
|
*/
|
|
315
594
|
function resolveImport(filename, request, root, conventions) {
|
|
316
|
-
for (const [prefix, target] of conventions.aliases) if (request.startsWith(prefix)) return `${root}/${target === "sparta" ? conventions.spartaFolder : conventions.testsFolder}/${request.slice(prefix.length)}`;
|
|
317
595
|
if (request.startsWith(".")) return normalize(path.resolve(path.dirname(filename), request));
|
|
596
|
+
const tsconfig = tsconfigFor(filename);
|
|
597
|
+
const mapped = tsconfig === void 0 ? void 0 : matchPaths(request, tsconfig.paths);
|
|
598
|
+
const existing = mapped?.find(moduleExists);
|
|
599
|
+
if (existing !== void 0) return existing;
|
|
600
|
+
for (const [prefix, target] of conventions.aliases) if (request.startsWith(prefix)) return `${root}/${target === "sparta" ? conventions.spartaFolder : conventions.testsFolder}/${request.slice(prefix.length)}`;
|
|
601
|
+
if (mapped !== void 0 && mapped.length > 0) return mapped[0];
|
|
602
|
+
const fromBaseUrl = tsconfig?.baseUrl === void 0 ? void 0 : `${tsconfig.baseUrl}/${request}`;
|
|
603
|
+
return fromBaseUrl !== void 0 && moduleExists(fromBaseUrl) ? fromBaseUrl : void 0;
|
|
318
604
|
}
|
|
319
|
-
/**
|
|
605
|
+
/**
|
|
606
|
+
* The layer folder a file lives in (`steppers`, `pages`, ...), with `sparta/scenarios/` counting as `scenarios`
|
|
607
|
+
* and `sparta/<area>/builders/` and `directors/` as themselves.
|
|
608
|
+
*/
|
|
320
609
|
const layerFolder = (filename, conventions, root) => {
|
|
321
610
|
const file = projectPath(filename, root);
|
|
322
611
|
if (file === void 0) return void 0;
|
|
@@ -324,6 +613,8 @@ const layerFolder = (filename, conventions, root) => {
|
|
|
324
613
|
name: "scenarios",
|
|
325
614
|
standard: "scenarios"
|
|
326
615
|
};
|
|
616
|
+
const areaLayer = areaLayerFolder(file, conventions);
|
|
617
|
+
if (areaLayer) return areaLayer;
|
|
327
618
|
const folder = file.match(conventions.patterns.layer)?.[1];
|
|
328
619
|
return folder === void 0 ? void 0 : {
|
|
329
620
|
name: folder,
|
|
@@ -332,6 +623,10 @@ const layerFolder = (filename, conventions, root) => {
|
|
|
332
623
|
};
|
|
333
624
|
/** The file name suffix the files in a layer folder must use (`stepper` for `steppers/`), if it has one. */
|
|
334
625
|
const layerSuffix = (folder) => folder?.standard === void 0 ? void 0 : LAYER_FOLDERS[folder.standard].suffix;
|
|
626
|
+
/** The file name without its TypeScript extension: `orders-stepper` for `orders-stepper.ts` or `.mts`. */
|
|
627
|
+
const moduleName = (filename) => path.basename(filename).replace(/\.[cm]?tsx?$/, "");
|
|
628
|
+
/** `.d.ts`, `.d.mts` and `.d.cts` files, which only declare types. */
|
|
629
|
+
const isDeclarationFile = (filename) => /\.d\.[cm]?ts$/.test(filename);
|
|
335
630
|
/** True for files that follow the layer naming convention (layer folders and fixtures). */
|
|
336
631
|
const isLayerFile = (filename, conventions, root) => layerSuffix(layerFolder(filename, conventions, root)) !== void 0 || projectPath(filename, root)?.match(conventions.patterns.top)?.[1] === "fixtures";
|
|
337
632
|
// src/utils/settings.ts
|
|
@@ -365,22 +660,18 @@ const DATABASE_LAYERS = [
|
|
|
365
660
|
"service",
|
|
366
661
|
"stepper"
|
|
367
662
|
];
|
|
368
|
-
/** The UI layers; `view` is the area's name for a page (`page` for web, `screen` for mobile). */
|
|
663
|
+
/** The UI layers; `view` is the area's name for a page (`page` for web, `screen` for mobile, `window` for desktop). */
|
|
369
664
|
const uiLayers = (view) => ({
|
|
370
665
|
component: /* @__PURE__ */ new Set([
|
|
371
666
|
view,
|
|
372
667
|
"stepper",
|
|
373
668
|
"asserter",
|
|
374
|
-
"scenario"
|
|
375
|
-
"builder",
|
|
376
|
-
"director"
|
|
669
|
+
"scenario"
|
|
377
670
|
]),
|
|
378
671
|
[view]: /* @__PURE__ */ new Set([
|
|
379
672
|
"stepper",
|
|
380
673
|
"asserter",
|
|
381
|
-
"scenario"
|
|
382
|
-
"builder",
|
|
383
|
-
"director"
|
|
674
|
+
"scenario"
|
|
384
675
|
]),
|
|
385
676
|
stepper: /* @__PURE__ */ new Set(["asserter", "scenario"]),
|
|
386
677
|
asserter: /* @__PURE__ */ new Set([
|
|
@@ -388,23 +679,9 @@ const uiLayers = (view) => ({
|
|
|
388
679
|
"asserter",
|
|
389
680
|
"scenario"
|
|
390
681
|
]),
|
|
391
|
-
builder: /* @__PURE__ */ new Set([
|
|
392
|
-
view,
|
|
393
|
-
"component",
|
|
394
|
-
"stepper",
|
|
395
|
-
"asserter",
|
|
396
|
-
"scenario"
|
|
397
|
-
]),
|
|
398
|
-
director: /* @__PURE__ */ new Set([
|
|
399
|
-
view,
|
|
400
|
-
"component",
|
|
401
|
-
"stepper",
|
|
402
|
-
"asserter",
|
|
403
|
-
"scenario"
|
|
404
|
-
]),
|
|
405
682
|
scenario: /* @__PURE__ */ new Set([view, "component"])
|
|
406
683
|
});
|
|
407
|
-
/** Forbidden target layers inside the same area (API -> API, UI -> UI, database -> database,
|
|
684
|
+
/** Forbidden target layers inside the same area (API -> API, UI -> UI, database -> database, and so on). */
|
|
408
685
|
const FORBIDDEN_LAYERS = {
|
|
409
686
|
api: {
|
|
410
687
|
client: /* @__PURE__ */ new Set([
|
|
@@ -507,9 +784,19 @@ const FORBIDDEN_LAYERS = {
|
|
|
507
784
|
"base-repository"
|
|
508
785
|
])
|
|
509
786
|
},
|
|
787
|
+
desktop: uiLayers("window"),
|
|
510
788
|
mobile: uiLayers("screen"),
|
|
511
789
|
ui: uiLayers("page")
|
|
512
790
|
};
|
|
791
|
+
/** The layers of `sparta/<area>/builders/` and `sparta/<area>/directors/`. */
|
|
792
|
+
const BUILDER_LAYERS = /* @__PURE__ */ new Set(["builder", "director"]);
|
|
793
|
+
/** The only group layers that may use their area's builders and directors. */
|
|
794
|
+
const BUILDER_USERS = /* @__PURE__ */ new Set([
|
|
795
|
+
"scenario",
|
|
796
|
+
"stepper",
|
|
797
|
+
"asserter",
|
|
798
|
+
"service"
|
|
799
|
+
]);
|
|
513
800
|
const DATABASE_ACCESS = [
|
|
514
801
|
"repository",
|
|
515
802
|
"base-repository",
|
|
@@ -528,6 +815,7 @@ const FORBIDDEN_FOR_ORCHESTRATION = {
|
|
|
528
815
|
"base-service"
|
|
529
816
|
]),
|
|
530
817
|
database: new Set(DATABASE_ACCESS),
|
|
818
|
+
desktop: /* @__PURE__ */ new Set(["window", "component"]),
|
|
531
819
|
mobile: /* @__PURE__ */ new Set(["screen", "component"]),
|
|
532
820
|
ui: /* @__PURE__ */ new Set(["page", "component"])
|
|
533
821
|
},
|
|
@@ -545,6 +833,12 @@ const FORBIDDEN_FOR_ORCHESTRATION = {
|
|
|
545
833
|
"stepper",
|
|
546
834
|
"asserter"
|
|
547
835
|
]),
|
|
836
|
+
desktop: /* @__PURE__ */ new Set([
|
|
837
|
+
"window",
|
|
838
|
+
"component",
|
|
839
|
+
"stepper",
|
|
840
|
+
"asserter"
|
|
841
|
+
]),
|
|
548
842
|
mobile: /* @__PURE__ */ new Set([
|
|
549
843
|
"screen",
|
|
550
844
|
"component",
|
|
@@ -561,32 +855,45 @@ const FORBIDDEN_FOR_ORCHESTRATION = {
|
|
|
561
855
|
};
|
|
562
856
|
const REEXPORT_LAYERS = /* @__PURE__ */ new Set([
|
|
563
857
|
"asserter",
|
|
858
|
+
"builder",
|
|
564
859
|
"client",
|
|
565
860
|
"base-client",
|
|
566
861
|
"base-repository",
|
|
567
862
|
"component",
|
|
863
|
+
"director",
|
|
568
864
|
"page",
|
|
569
865
|
"repository",
|
|
570
866
|
"scenario",
|
|
571
867
|
"screen",
|
|
572
868
|
"service",
|
|
573
869
|
"base-service",
|
|
574
|
-
"stepper"
|
|
870
|
+
"stepper",
|
|
871
|
+
"window"
|
|
575
872
|
]);
|
|
576
873
|
const describe = (info, { spartaFolder, testsFolder }) => ({
|
|
577
|
-
group: `${info.areaFolder}
|
|
874
|
+
group: `${info.areaFolder}${info.group === void 0 ? "" : `/${info.group}`}${info.layer ? ` ${info.layer}` : ""}`,
|
|
578
875
|
"area-common": `${info.areaFolder}/common`,
|
|
876
|
+
"area-layer": `${info.areaFolder} ${info.layer}`,
|
|
579
877
|
"top-scenario": `${spartaFolder}/scenarios`,
|
|
580
878
|
fixture: `${spartaFolder}/fixtures`,
|
|
581
879
|
shared: `${spartaFolder}/common|support`,
|
|
582
880
|
test: testsFolder
|
|
583
881
|
})[info.kind];
|
|
882
|
+
/** Whether code in an area folder (a group, or the area's builders and directors) may not depend on `target`. */
|
|
883
|
+
function areaIsolationViolation(source, target) {
|
|
884
|
+
if (["fixture", "top-scenario"].includes(target.kind)) return true;
|
|
885
|
+
if (target.areaFolder === void 0) return false;
|
|
886
|
+
if (target.areaFolder !== source.areaFolder) return true;
|
|
887
|
+
if (target.kind !== "group") return false;
|
|
888
|
+
return source.kind === "area-layer" ? target.layer !== "service" : target.group !== source.group;
|
|
889
|
+
}
|
|
584
890
|
/** Isolation compares area folders, not area types: `web/` and `ui/` are separate areas even when both follow `ui`. */
|
|
585
891
|
function isolationViolation(source, target) {
|
|
586
892
|
if (target.kind === "test") return source.kind !== "test";
|
|
587
893
|
if (source.kind === "shared") return [
|
|
588
894
|
"group",
|
|
589
895
|
"area-common",
|
|
896
|
+
"area-layer",
|
|
590
897
|
"fixture",
|
|
591
898
|
"top-scenario"
|
|
592
899
|
].includes(target.kind);
|
|
@@ -594,20 +901,25 @@ function isolationViolation(source, target) {
|
|
|
594
901
|
if ([
|
|
595
902
|
"fixture",
|
|
596
903
|
"top-scenario",
|
|
597
|
-
"group"
|
|
904
|
+
"group",
|
|
905
|
+
"area-layer"
|
|
598
906
|
].includes(target.kind)) return true;
|
|
599
907
|
return target.kind === "area-common" && target.areaFolder !== source.areaFolder;
|
|
600
908
|
}
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
909
|
+
return source.kind === "group" || source.kind === "area-layer" ? areaIsolationViolation(source, target) : false;
|
|
910
|
+
}
|
|
911
|
+
/**
|
|
912
|
+
* Whether `source` may not use the builder or director `target`: builders use no builders or directors, directors
|
|
913
|
+
* only use builders, and in a group only scenarios, steppers, asserters and services may use them.
|
|
914
|
+
*/
|
|
915
|
+
function builderViolation(source, target) {
|
|
916
|
+
if (source.kind === "area-layer") return source.layer === "builder" || target.layer === "director";
|
|
917
|
+
if (source.kind === "group") return source.layer === void 0 || !BUILDER_USERS.has(source.layer);
|
|
608
918
|
}
|
|
609
919
|
function layerViolation(source, target) {
|
|
610
920
|
if (!target.layer || !target.area) return false;
|
|
921
|
+
const builder = BUILDER_LAYERS.has(target.layer) ? builderViolation(source, target) : void 0;
|
|
922
|
+
if (builder !== void 0) return builder;
|
|
611
923
|
if (source.kind === "group" || source.kind === "area-common") {
|
|
612
924
|
if (!source.area || !source.layer) return false;
|
|
613
925
|
return source.areaFolder === target.areaFolder && Boolean(FORBIDDEN_LAYERS[source.area][source.layer]?.has(target.layer));
|
|
@@ -616,7 +928,7 @@ function layerViolation(source, target) {
|
|
|
616
928
|
return false;
|
|
617
929
|
}
|
|
618
930
|
const isForbiddenReexport = (source, target) => target.layer !== void 0 && REEXPORT_LAYERS.has(target.layer) && target.layer !== source.layer && !["fixture", "test"].includes(source.kind);
|
|
619
|
-
const rule$
|
|
931
|
+
const rule$13 = {
|
|
620
932
|
meta: {
|
|
621
933
|
type: "problem",
|
|
622
934
|
docs: {
|
|
@@ -627,7 +939,9 @@ const rule$12 = {
|
|
|
627
939
|
messages: {
|
|
628
940
|
isolation: "{{source}} must not depend on {{target}} ({{request}}). Move shared code to the area's common/ folder or the top-level common/ folder.",
|
|
629
941
|
layer: "{{source}} must not depend on {{target}} ({{request}}).",
|
|
630
|
-
reexport: "Do not re-export {{target}} outside its layer; this bypasses the architecture boundaries."
|
|
942
|
+
reexport: "Do not re-export {{target}} outside its layer; this bypasses the architecture boundaries.",
|
|
943
|
+
staticService: "{{source}} is a static abstract class and must not depend on {{target}} ({{request}}); only a normal class may use services.",
|
|
944
|
+
placement: "Builders and directors belong in '{{expected}}/', shared by the area's groups, not in a group folder."
|
|
631
945
|
}
|
|
632
946
|
},
|
|
633
947
|
create(context) {
|
|
@@ -636,11 +950,15 @@ const rule$12 = {
|
|
|
636
950
|
const root = rootFor(context, conventions);
|
|
637
951
|
const source = classify(filename, conventions, root);
|
|
638
952
|
if (!source) return {};
|
|
953
|
+
/** Whether the file declares a normal (instantiable) class. Set on `Program`, before any import is visited. */
|
|
954
|
+
let isNormalClass = false;
|
|
955
|
+
/** An `index.ts` barrel in `builders/` or `directors/` may import and re-export its own layer. */
|
|
956
|
+
const isOwnBarrel = (target) => source.kind === "area-layer" && moduleName(filename) === "index" && target.kind === "area-layer" && target.areaFolder === source.areaFolder && target.layer === source.layer;
|
|
639
957
|
const check = (node, request, reexport = false) => {
|
|
640
958
|
if (request === void 0) return;
|
|
641
959
|
const resolved = resolveImport(filename, request, root, conventions);
|
|
642
960
|
const target = resolved ? classify(resolved, conventions, root) : void 0;
|
|
643
|
-
if (!target) return;
|
|
961
|
+
if (!target || isOwnBarrel(target)) return;
|
|
644
962
|
const data = {
|
|
645
963
|
source: describe(source, conventions),
|
|
646
964
|
target: describe(target, conventions),
|
|
@@ -656,6 +974,11 @@ const rule$12 = {
|
|
|
656
974
|
messageId: "layer",
|
|
657
975
|
data
|
|
658
976
|
});
|
|
977
|
+
else if (source.kind === "area-layer" && target.layer === "service" && !isNormalClass) context.report({
|
|
978
|
+
node,
|
|
979
|
+
messageId: "staticService",
|
|
980
|
+
data
|
|
981
|
+
});
|
|
659
982
|
else if (reexport && isForbiddenReexport(source, target)) context.report({
|
|
660
983
|
node,
|
|
661
984
|
messageId: "reexport",
|
|
@@ -663,15 +986,28 @@ const rule$12 = {
|
|
|
663
986
|
});
|
|
664
987
|
};
|
|
665
988
|
return {
|
|
989
|
+
Program(node) {
|
|
990
|
+
isNormalClass = topLevelClasses(node).some((declaration) => !isStaticClass(declaration));
|
|
991
|
+
if (source.kind === "group" && source.layer !== void 0 && BUILDER_LAYERS.has(source.layer)) {
|
|
992
|
+
const folder = layerFolder(filename, conventions, root)?.name ?? `${source.layer}s`;
|
|
993
|
+
const expected = `${conventions.spartaFolder}/${source.areaFolder}/${folder}`;
|
|
994
|
+
context.report({
|
|
995
|
+
node,
|
|
996
|
+
messageId: "placement",
|
|
997
|
+
data: { expected }
|
|
998
|
+
});
|
|
999
|
+
}
|
|
1000
|
+
},
|
|
666
1001
|
ImportDeclaration: (node) => check(node, stringValue(node.source)),
|
|
667
1002
|
ExportNamedDeclaration: (node) => check(node, stringValue(node.source), true),
|
|
668
1003
|
ExportAllDeclaration: (node) => check(node, stringValue(node.source), true),
|
|
669
|
-
ImportExpression: (node) => check(node,
|
|
1004
|
+
ImportExpression: (node) => check(node, staticString(node.source)),
|
|
1005
|
+
TSImportType: (node) => check(node, stringValue(node.source)),
|
|
670
1006
|
TSImportEqualsDeclaration(node) {
|
|
671
1007
|
if (node.moduleReference.type === "TSExternalModuleReference") check(node, stringValue(node.moduleReference.expression));
|
|
672
1008
|
},
|
|
673
1009
|
CallExpression(node) {
|
|
674
|
-
if (node.callee.type === "Identifier" && node.callee.name === "require") check(node,
|
|
1010
|
+
if (node.callee.type === "Identifier" && node.callee.name === "require") check(node, staticString(node.arguments[0]));
|
|
675
1011
|
}
|
|
676
1012
|
};
|
|
677
1013
|
}
|
|
@@ -682,7 +1018,7 @@ const classOf = (statement) => {
|
|
|
682
1018
|
const node = (statement.type === "ExportNamedDeclaration" || statement.type === "ExportDefaultDeclaration") && statement.declaration ? statement.declaration : statement;
|
|
683
1019
|
return node.type === "ClassDeclaration" ? node : void 0;
|
|
684
1020
|
};
|
|
685
|
-
const rule$
|
|
1021
|
+
const rule$12 = {
|
|
686
1022
|
meta: {
|
|
687
1023
|
type: "suggestion",
|
|
688
1024
|
docs: {
|
|
@@ -692,15 +1028,29 @@ const rule$11 = {
|
|
|
692
1028
|
schema: [],
|
|
693
1029
|
messages: {
|
|
694
1030
|
name: "Class '{{found}}' must be named '{{expected}}' to match the file name.",
|
|
695
|
-
single: "A layer file contains exactly one class; move '{{found}}' to its own file."
|
|
1031
|
+
single: "A layer file contains exactly one class; move '{{found}}' to its own file.",
|
|
1032
|
+
missing: "A layer file contains exactly one class; expected a class named '{{expected}}'."
|
|
696
1033
|
}
|
|
697
1034
|
},
|
|
698
1035
|
create(context) {
|
|
699
1036
|
const conventions = conventionsFor(context);
|
|
700
|
-
|
|
701
|
-
const
|
|
1037
|
+
const root = rootFor(context, conventions);
|
|
1038
|
+
const { filename } = context;
|
|
1039
|
+
if (isDeclarationFile(filename) || !isLayerFile(filename, conventions, root)) return {};
|
|
1040
|
+
const module = moduleName(filename);
|
|
1041
|
+
const expected = toPascalCase(module);
|
|
1042
|
+
const requiresClass = module !== "index" && layerSuffix(layerFolder(filename, conventions, root)) !== void 0;
|
|
702
1043
|
return { Program(program) {
|
|
703
|
-
program.body.map(classOf).filter((node) => node !== void 0)
|
|
1044
|
+
const classes = program.body.map(classOf).filter((node) => node !== void 0);
|
|
1045
|
+
if (classes.length === 0 && requiresClass) {
|
|
1046
|
+
context.report({
|
|
1047
|
+
node: program,
|
|
1048
|
+
messageId: "missing",
|
|
1049
|
+
data: { expected }
|
|
1050
|
+
});
|
|
1051
|
+
return;
|
|
1052
|
+
}
|
|
1053
|
+
classes.forEach((node, index) => {
|
|
704
1054
|
const found = node.id?.name ?? "(anonymous)";
|
|
705
1055
|
if (index > 0) context.report({
|
|
706
1056
|
node: node.id ?? node,
|
|
@@ -719,6 +1069,68 @@ const rule$11 = {
|
|
|
719
1069
|
} };
|
|
720
1070
|
}
|
|
721
1071
|
};
|
|
1072
|
+
// src/rules/director-composes-builders.ts
|
|
1073
|
+
const rule$11 = {
|
|
1074
|
+
meta: {
|
|
1075
|
+
type: "suggestion",
|
|
1076
|
+
docs: {
|
|
1077
|
+
description: "Require a director to compose several builders; a director using a single builder belongs on that builder.",
|
|
1078
|
+
url: docsUrl("director-composes-builders")
|
|
1079
|
+
},
|
|
1080
|
+
schema: [],
|
|
1081
|
+
messages: { singleBuilder: "This director only uses '{{builder}}'; make it a custom method on '{{builder}}'. A director composes several builders." }
|
|
1082
|
+
},
|
|
1083
|
+
create(context) {
|
|
1084
|
+
const filename = context.filename;
|
|
1085
|
+
const conventions = conventionsFor(context);
|
|
1086
|
+
const root = rootFor(context, conventions);
|
|
1087
|
+
const source = classify(filename, conventions, root);
|
|
1088
|
+
if (source?.kind !== "area-layer" || source.layer !== "director" || moduleName(filename) === "index") return {};
|
|
1089
|
+
const isBuilder = (request) => {
|
|
1090
|
+
if (request === void 0) return false;
|
|
1091
|
+
const resolved = resolveImport(filename, request, root, conventions);
|
|
1092
|
+
return resolved !== void 0 && classify(resolved, conventions, root)?.layer === "builder";
|
|
1093
|
+
};
|
|
1094
|
+
/** The builders the director uses, by imported name, with the import to report. */
|
|
1095
|
+
const builders = /* @__PURE__ */ new Map();
|
|
1096
|
+
/** Set when a builder is imported in a way that does not tell how many builders are used. */
|
|
1097
|
+
let unknown = false;
|
|
1098
|
+
return {
|
|
1099
|
+
ImportDeclaration(node) {
|
|
1100
|
+
if (node.importKind === "type" || typeof node.source.value !== "string" || !isBuilder(node.source.value)) return;
|
|
1101
|
+
for (const specifier of node.specifiers) if (specifier.type === "ImportNamespaceSpecifier") unknown = true;
|
|
1102
|
+
else if (specifier.type === "ImportDefaultSpecifier") builders.set(specifier.local.name, {
|
|
1103
|
+
local: specifier.local.name,
|
|
1104
|
+
node
|
|
1105
|
+
});
|
|
1106
|
+
else if (specifier.importKind !== "type") {
|
|
1107
|
+
const imported = specifier.imported.type === "Identifier" ? specifier.imported.name : String(specifier.imported.value);
|
|
1108
|
+
builders.set(imported, {
|
|
1109
|
+
local: specifier.local.name,
|
|
1110
|
+
node
|
|
1111
|
+
});
|
|
1112
|
+
}
|
|
1113
|
+
},
|
|
1114
|
+
ImportExpression(node) {
|
|
1115
|
+
if (isBuilder(staticString(node.source))) unknown = true;
|
|
1116
|
+
},
|
|
1117
|
+
CallExpression(node) {
|
|
1118
|
+
if (node.callee.type === "Identifier" && node.callee.name === "require") {
|
|
1119
|
+
if (isBuilder(staticString(node.arguments[0]))) unknown = true;
|
|
1120
|
+
}
|
|
1121
|
+
},
|
|
1122
|
+
"Program:exit"() {
|
|
1123
|
+
const [only] = builders.values();
|
|
1124
|
+
if (unknown || builders.size !== 1 || only === void 0) return;
|
|
1125
|
+
context.report({
|
|
1126
|
+
node: only.node,
|
|
1127
|
+
messageId: "singleBuilder",
|
|
1128
|
+
data: { builder: only.local }
|
|
1129
|
+
});
|
|
1130
|
+
}
|
|
1131
|
+
};
|
|
1132
|
+
}
|
|
1133
|
+
};
|
|
722
1134
|
// src/rules/expect-from-support.ts
|
|
723
1135
|
/** Files inside the sparta folder, except its support folder (where `expect` itself is defined). */
|
|
724
1136
|
const appliesTo = (filename, { patterns }, root) => {
|
|
@@ -739,10 +1151,21 @@ const rule$10 = {
|
|
|
739
1151
|
create(context) {
|
|
740
1152
|
const conventions = conventionsFor(context);
|
|
741
1153
|
const { supportModule } = conventions;
|
|
742
|
-
|
|
1154
|
+
const root = rootFor(context, conventions);
|
|
1155
|
+
if (!appliesTo(context.filename, conventions, root)) return {};
|
|
1156
|
+
/**
|
|
1157
|
+
* The support module, a subpath of it, or a relative import into the support folder. Other aliases for that
|
|
1158
|
+
* folder are reported, so the configured module name stays the one that is used.
|
|
1159
|
+
*/
|
|
1160
|
+
const isSupport = (source) => {
|
|
1161
|
+
if (source === supportModule || source.startsWith(`${supportModule}/`)) return true;
|
|
1162
|
+
if (!source.startsWith(".")) return false;
|
|
1163
|
+
const resolved = resolveImport(context.filename, source, root, conventions);
|
|
1164
|
+
return (resolved === void 0 ? void 0 : projectPath(resolved, root))?.match(conventions.patterns.top)?.[1] === "support";
|
|
1165
|
+
};
|
|
743
1166
|
return { ImportDeclaration(node) {
|
|
744
1167
|
const source = node.source.value;
|
|
745
|
-
if (source
|
|
1168
|
+
if (isSupport(String(source))) return;
|
|
746
1169
|
const specifier = node.specifiers.find((item) => item.type === "ImportSpecifier" && item.imported.type === "Identifier" && item.imported.name === "expect");
|
|
747
1170
|
if (specifier === void 0) return;
|
|
748
1171
|
const data = {
|
|
@@ -767,24 +1190,35 @@ const rule$10 = {
|
|
|
767
1190
|
}
|
|
768
1191
|
};
|
|
769
1192
|
// src/rules/file-naming.ts
|
|
1193
|
+
/** Lowercase words of letters and digits, joined by single hyphens: `orders-v2-stepper`. */
|
|
1194
|
+
const KEBAB_CASE = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
1195
|
+
/** `myOrders`, `My_APIOrders` and `checkout--flow` become `my-orders`, `my-api-orders` and `checkout-flow`. */
|
|
1196
|
+
const toKebabCase = (name) => name.replace(/([A-Z]+)([A-Z][a-z])/g, "$1-$2").replace(/([a-z0-9])([A-Z])/g, "$1-$2").replace(/[^A-Za-z0-9]+/g, "-").replace(/^-|-$/g, "").toLowerCase();
|
|
770
1197
|
const rule$9 = {
|
|
771
1198
|
meta: {
|
|
772
1199
|
type: "suggestion",
|
|
773
1200
|
docs: {
|
|
774
|
-
description: "Require files in a layer folder to end with the layer suffix (e.g.
|
|
1201
|
+
description: "Require files in a layer folder to be kebab-case and end with the layer suffix (e.g. `orders-stepper.ts`).",
|
|
775
1202
|
url: docsUrl("file-naming")
|
|
776
1203
|
},
|
|
777
1204
|
schema: [],
|
|
778
|
-
messages: {
|
|
1205
|
+
messages: {
|
|
1206
|
+
suffix: "Files in '{{folder}}/' must be named '*-{{suffix}}.ts' (found '{{name}}').",
|
|
1207
|
+
kebabCase: "File name '{{name}}' must be kebab-case: '{{expected}}'."
|
|
1208
|
+
}
|
|
779
1209
|
},
|
|
780
1210
|
create(context) {
|
|
781
1211
|
const conventions = conventionsFor(context);
|
|
782
1212
|
const folder = layerFolder(context.filename, conventions, rootFor(context, conventions));
|
|
783
1213
|
const suffix = layerSuffix(folder);
|
|
784
1214
|
const name = path.basename(context.filename);
|
|
785
|
-
|
|
1215
|
+
const module = moduleName(context.filename);
|
|
1216
|
+
if (folder === void 0 || suffix === void 0 || isDeclarationFile(name) || module === "index") return {};
|
|
1217
|
+
const hasSuffix = module.endsWith(`-${suffix}`);
|
|
1218
|
+
const isKebabCase = KEBAB_CASE.test(module);
|
|
1219
|
+
if (hasSuffix && isKebabCase) return {};
|
|
786
1220
|
return { Program(node) {
|
|
787
|
-
context.report({
|
|
1221
|
+
if (!hasSuffix) context.report({
|
|
788
1222
|
node,
|
|
789
1223
|
messageId: "suffix",
|
|
790
1224
|
data: {
|
|
@@ -793,30 +1227,61 @@ const rule$9 = {
|
|
|
793
1227
|
name
|
|
794
1228
|
}
|
|
795
1229
|
});
|
|
1230
|
+
if (!isKebabCase) {
|
|
1231
|
+
const expected = `${toKebabCase(module)}${name.slice(module.length)}`;
|
|
1232
|
+
context.report({
|
|
1233
|
+
node,
|
|
1234
|
+
messageId: "kebabCase",
|
|
1235
|
+
data: {
|
|
1236
|
+
name,
|
|
1237
|
+
expected
|
|
1238
|
+
}
|
|
1239
|
+
});
|
|
1240
|
+
}
|
|
796
1241
|
} };
|
|
797
1242
|
}
|
|
798
1243
|
};
|
|
799
1244
|
// src/rules/lines-between-class-members.ts
|
|
800
1245
|
const BLANK_LINE$1 = /\r?\n[\t ]*\r?\n/;
|
|
1246
|
+
const BLANK_LINES$1 = /(\r?\n)(?:[\t ]*\r?\n)+/g;
|
|
1247
|
+
/** An overload signature (or abstract method): a method without a body. */
|
|
1248
|
+
const isSignature = (member) => isMethod(member) && member.value.body === null;
|
|
801
1249
|
const rule$8 = {
|
|
802
1250
|
meta: {
|
|
803
1251
|
type: "layout",
|
|
804
1252
|
docs: {
|
|
805
|
-
description: "Require a blank line after every multi-line class member.",
|
|
1253
|
+
description: "Require a blank line after every multi-line method-like class member, and disallow blank lines between method overloads.",
|
|
806
1254
|
url: docsUrl("lines-between-class-members")
|
|
807
1255
|
},
|
|
808
1256
|
fixable: "whitespace",
|
|
809
1257
|
schema: [],
|
|
810
|
-
messages: {
|
|
1258
|
+
messages: {
|
|
1259
|
+
expectedBlankLine: "Expected a blank line between class members.",
|
|
1260
|
+
unexpectedBlankLine: "Unexpected blank line between overloads of the same method."
|
|
1261
|
+
}
|
|
811
1262
|
},
|
|
812
1263
|
create(context) {
|
|
813
|
-
const {
|
|
1264
|
+
const { sourceCode } = context;
|
|
1265
|
+
const { text } = sourceCode;
|
|
1266
|
+
const keyOf = (method) => `${method.static ? "static " : ""}${method.computed ? "[]" : ""}${sourceCode.getText(method.key)}`;
|
|
1267
|
+
/** `current` continues the overload group that `previous` (a bodyless signature) belongs to. */
|
|
1268
|
+
const isOverloadOf = (previous, current) => isSignature(previous) && isMethod(current) && current.kind === previous.kind && keyOf(current) === keyOf(previous);
|
|
814
1269
|
return { ClassBody(node) {
|
|
815
1270
|
for (let index = 1; index < node.body.length; index++) {
|
|
816
1271
|
const previous = node.body[index - 1];
|
|
817
1272
|
const current = node.body[index];
|
|
818
|
-
if (previous.loc.start.line === previous.loc.end.line) continue;
|
|
819
1273
|
const textBetween = text.slice(previous.range[1], current.range[0]);
|
|
1274
|
+
if (isOverloadOf(previous, current)) {
|
|
1275
|
+
if (!BLANK_LINE$1.test(textBetween)) continue;
|
|
1276
|
+
context.report({
|
|
1277
|
+
node: current,
|
|
1278
|
+
messageId: "unexpectedBlankLine",
|
|
1279
|
+
fix: (fixer) => fixer.replaceTextRange([previous.range[1], current.range[0]], textBetween.replace(BLANK_LINES$1, "$1"))
|
|
1280
|
+
});
|
|
1281
|
+
continue;
|
|
1282
|
+
}
|
|
1283
|
+
if (previous.loc.start.line === previous.loc.end.line) continue;
|
|
1284
|
+
if (isPlainProperty(previous)) continue;
|
|
820
1285
|
if (BLANK_LINE$1.test(textBetween)) continue;
|
|
821
1286
|
context.report({
|
|
822
1287
|
node: current,
|
|
@@ -831,9 +1296,154 @@ const rule$8 = {
|
|
|
831
1296
|
} };
|
|
832
1297
|
}
|
|
833
1298
|
};
|
|
834
|
-
// src/rules/
|
|
835
|
-
const
|
|
1299
|
+
// src/rules/member-ordering.ts
|
|
1300
|
+
const KINDS = [
|
|
1301
|
+
"property",
|
|
1302
|
+
"constructor",
|
|
1303
|
+
"method"
|
|
1304
|
+
];
|
|
1305
|
+
const ACCESSES = [
|
|
1306
|
+
"public",
|
|
1307
|
+
"protected",
|
|
1308
|
+
"private"
|
|
1309
|
+
];
|
|
1310
|
+
/** Properties (incl. index signatures) and methods; a property holding a function or class counts as a method. */
|
|
1311
|
+
const kindOf = (member) => {
|
|
1312
|
+
if (isMethod(member)) return member.kind === "constructor" ? "constructor" : "method";
|
|
1313
|
+
if (member.type === "StaticBlock") return void 0;
|
|
1314
|
+
return isPlainProperty(member) ? "property" : "method";
|
|
1315
|
+
};
|
|
1316
|
+
const accessOf = (member) => {
|
|
1317
|
+
if (isPrivate(member)) return "private";
|
|
1318
|
+
if ("accessibility" in member && member.accessibility === "protected") return "protected";
|
|
1319
|
+
return "public";
|
|
1320
|
+
};
|
|
1321
|
+
/**
|
|
1322
|
+
* The required order: static properties, instance properties, constructor, static methods, instance methods,
|
|
1323
|
+
* each from public to protected to private.
|
|
1324
|
+
*/
|
|
1325
|
+
const groupOf = (member) => {
|
|
1326
|
+
const kind = kindOf(member);
|
|
1327
|
+
if (kind === void 0) return void 0;
|
|
1328
|
+
if (kind === "constructor") return {
|
|
1329
|
+
rank: KINDS.indexOf(kind) * 6,
|
|
1330
|
+
label: "constructor"
|
|
1331
|
+
};
|
|
1332
|
+
const isStatic = "static" in member && member.static;
|
|
1333
|
+
const access = accessOf(member);
|
|
1334
|
+
return {
|
|
1335
|
+
rank: KINDS.indexOf(kind) * 6 + (isStatic ? 0 : 3) + ACCESSES.indexOf(access),
|
|
1336
|
+
label: `${isStatic ? "static " : ""}${access} ${kind}`
|
|
1337
|
+
};
|
|
1338
|
+
};
|
|
1339
|
+
const SKIPPED_KEYS$1 = /* @__PURE__ */ new Set([
|
|
1340
|
+
"parent",
|
|
1341
|
+
"loc",
|
|
1342
|
+
"range",
|
|
1343
|
+
"start",
|
|
1344
|
+
"end",
|
|
1345
|
+
"typeAnnotation"
|
|
1346
|
+
]);
|
|
1347
|
+
const isNode$1 = (value) => typeof value === "object" && value !== null && typeof value.type === "string";
|
|
1348
|
+
const FUNCTIONS = /* @__PURE__ */ new Set([
|
|
1349
|
+
"ArrowFunctionExpression",
|
|
1350
|
+
"FunctionExpression",
|
|
1351
|
+
"ClassExpression"
|
|
1352
|
+
]);
|
|
1353
|
+
/** `x` for `this.x`, `this.#x` and `ClassName.x`. */
|
|
1354
|
+
const ownPropertyRead = (node, className) => {
|
|
1355
|
+
if (node.type !== "MemberExpression" || node.computed) return void 0;
|
|
1356
|
+
const { object, property } = node;
|
|
1357
|
+
return (object.type === "ThisExpression" || className !== void 0 && object.type === "Identifier" && object.name === className) && (property.type === "Identifier" || property.type === "PrivateIdentifier") ? property.name : void 0;
|
|
1358
|
+
};
|
|
1359
|
+
/**
|
|
1360
|
+
* The property names an initializer reads while it runs, as `this.x`, `this.#x` or `ClassName.x`. Code inside
|
|
1361
|
+
* functions is left out: it runs later, when every property is initialized.
|
|
1362
|
+
*/
|
|
1363
|
+
function readsDuringInit(value, className) {
|
|
1364
|
+
const names = /* @__PURE__ */ new Set();
|
|
1365
|
+
const visit = (node) => {
|
|
1366
|
+
if (FUNCTIONS.has(node.type)) return;
|
|
1367
|
+
const read = ownPropertyRead(node, className);
|
|
1368
|
+
if (read !== void 0) names.add(read);
|
|
1369
|
+
for (const [key, child] of Object.entries(node)) {
|
|
1370
|
+
if (SKIPPED_KEYS$1.has(key)) continue;
|
|
1371
|
+
if (Array.isArray(child)) {
|
|
1372
|
+
for (const item of child) if (isNode$1(item)) visit(item);
|
|
1373
|
+
} else if (isNode$1(child)) visit(child);
|
|
1374
|
+
}
|
|
1375
|
+
};
|
|
1376
|
+
visit(value);
|
|
1377
|
+
return names;
|
|
1378
|
+
}
|
|
1379
|
+
/**
|
|
1380
|
+
* Initializers run in source order, so a property that reads a property declared before it must stay below it,
|
|
1381
|
+
* even when the order of the groups says otherwise.
|
|
1382
|
+
*/
|
|
1383
|
+
const dependsOnEarlier = (member, earlier, className) => {
|
|
1384
|
+
if (member.type !== "PropertyDefinition" && member.type !== "AccessorProperty") return false;
|
|
1385
|
+
if (!member.value) return false;
|
|
1386
|
+
return [...readsDuringInit(member.value, className)].some((name) => earlier.has(name));
|
|
1387
|
+
};
|
|
836
1388
|
const rule$7 = {
|
|
1389
|
+
meta: {
|
|
1390
|
+
type: "suggestion",
|
|
1391
|
+
docs: {
|
|
1392
|
+
description: "Require class members in a fixed order: properties, constructor, methods; static before instance; public before protected before private.",
|
|
1393
|
+
url: docsUrl("member-ordering")
|
|
1394
|
+
},
|
|
1395
|
+
schema: [],
|
|
1396
|
+
messages: { memberOrdering: "'{{name}}' ({{group}}) must come before '{{otherName}}' ({{otherGroup}})." }
|
|
1397
|
+
},
|
|
1398
|
+
create(context) {
|
|
1399
|
+
return { ClassBody(node) {
|
|
1400
|
+
let highest;
|
|
1401
|
+
const { parent } = node;
|
|
1402
|
+
const className = (parent.type === "ClassDeclaration" || parent.type === "ClassExpression") && parent.id ? parent.id.name : void 0;
|
|
1403
|
+
/** Names of the properties declared so far. */
|
|
1404
|
+
const earlier = /* @__PURE__ */ new Set();
|
|
1405
|
+
for (const member of node.body) {
|
|
1406
|
+
const group = groupOf(member);
|
|
1407
|
+
const dependent = dependsOnEarlier(member, earlier, className);
|
|
1408
|
+
if (isPlainProperty(member) && member.type !== "TSIndexSignature") earlier.add(memberName$1(member));
|
|
1409
|
+
if (group === void 0) continue;
|
|
1410
|
+
if (highest === void 0 || group.rank >= highest.group.rank) {
|
|
1411
|
+
highest = {
|
|
1412
|
+
member,
|
|
1413
|
+
group
|
|
1414
|
+
};
|
|
1415
|
+
continue;
|
|
1416
|
+
}
|
|
1417
|
+
if (dependent) continue;
|
|
1418
|
+
context.report({
|
|
1419
|
+
node: member,
|
|
1420
|
+
messageId: "memberOrdering",
|
|
1421
|
+
data: {
|
|
1422
|
+
name: memberName$1(member),
|
|
1423
|
+
group: group.label,
|
|
1424
|
+
otherName: memberName$1(highest.member),
|
|
1425
|
+
otherGroup: highest.group.label
|
|
1426
|
+
}
|
|
1427
|
+
});
|
|
1428
|
+
}
|
|
1429
|
+
} };
|
|
1430
|
+
}
|
|
1431
|
+
};
|
|
1432
|
+
// src/rules/no-direct-construction-in-tests.ts
|
|
1433
|
+
/** Names of SPARTA layer classes, for classes whose import does not tell their layer. */
|
|
1434
|
+
const LAYER_CLASS = /(Stepper|Asserter|Scenario|Service|Client|Repository|Page|Screen|Window|Component|Builder|Director|Factory|Provider|Container|Database)$/;
|
|
1435
|
+
/** The class a `new` expression constructs: `X` for `new X()`, `ns` and `X` for `new ns.X()`. */
|
|
1436
|
+
const constructed = (callee) => {
|
|
1437
|
+
if (callee.type === "Identifier") return {
|
|
1438
|
+
binding: callee.name,
|
|
1439
|
+
name: callee.name
|
|
1440
|
+
};
|
|
1441
|
+
if (callee.type === "MemberExpression" && !callee.computed && callee.object.type === "Identifier" && callee.property.type === "Identifier") return {
|
|
1442
|
+
binding: callee.object.name,
|
|
1443
|
+
name: callee.property.name
|
|
1444
|
+
};
|
|
1445
|
+
};
|
|
1446
|
+
const rule$6 = {
|
|
837
1447
|
meta: {
|
|
838
1448
|
type: "problem",
|
|
839
1449
|
docs: {
|
|
@@ -845,15 +1455,36 @@ const rule$7 = {
|
|
|
845
1455
|
},
|
|
846
1456
|
create(context) {
|
|
847
1457
|
const conventions = conventionsFor(context);
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
1458
|
+
const root = rootFor(context, conventions);
|
|
1459
|
+
if (classify(context.filename, conventions, root)?.kind !== "test") return {};
|
|
1460
|
+
/** Local names of imports (named, default and namespace) to their module. */
|
|
1461
|
+
const imports = /* @__PURE__ */ new Map();
|
|
1462
|
+
/**
|
|
1463
|
+
* Whether constructing `name` (bound to the local `binding`) creates a SPARTA layer object. An import decides by
|
|
1464
|
+
* where it points: never for packages, always for layer folders. Otherwise the class name decides.
|
|
1465
|
+
*/
|
|
1466
|
+
const isLayerClass = (binding, name) => {
|
|
1467
|
+
const source = imports.get(binding);
|
|
1468
|
+
if (source === void 0) return LAYER_CLASS.test(name);
|
|
1469
|
+
const resolved = resolveImport(context.filename, source, root, conventions);
|
|
1470
|
+
if (resolved === void 0) return false;
|
|
1471
|
+
const target = classify(resolved, conventions, root);
|
|
1472
|
+
return target?.layer !== void 0 || target?.kind === "top-scenario" || layerSuffix(layerFolder(resolved, conventions, root)) !== void 0 || LAYER_CLASS.test(name);
|
|
1473
|
+
};
|
|
1474
|
+
return {
|
|
1475
|
+
ImportDeclaration(node) {
|
|
1476
|
+
if (typeof node.source.value !== "string") return;
|
|
1477
|
+
for (const specifier of node.specifiers) imports.set(specifier.local.name, node.source.value);
|
|
1478
|
+
},
|
|
1479
|
+
NewExpression(node) {
|
|
1480
|
+
const found = constructed(node.callee);
|
|
1481
|
+
if (found && isLayerClass(found.binding, found.name)) context.report({
|
|
1482
|
+
node,
|
|
1483
|
+
messageId: "useFixture",
|
|
1484
|
+
data: { name: context.sourceCode.getText(node.callee) }
|
|
1485
|
+
});
|
|
1486
|
+
}
|
|
1487
|
+
};
|
|
857
1488
|
}
|
|
858
1489
|
};
|
|
859
1490
|
// src/rules/no-expect-in-tests.ts
|
|
@@ -862,7 +1493,7 @@ const rule$7 = {
|
|
|
862
1493
|
* `expected`, `expecting` and the like are left alone. `settings.sparta.expectNames` adds exact names.
|
|
863
1494
|
*/
|
|
864
1495
|
const EXPECT_NAME = /^expect(?:ly)?(?:[A-Z_$\d]|$)/;
|
|
865
|
-
const rule$
|
|
1496
|
+
const rule$5 = {
|
|
866
1497
|
meta: {
|
|
867
1498
|
type: "problem",
|
|
868
1499
|
docs: {
|
|
@@ -925,7 +1556,15 @@ const resolvesPromise = (callback, resolve) => {
|
|
|
925
1556
|
const call = body.type === "CallExpression" ? body : body.type === "BlockStatement" && body.body.length === 1 && body.body[0].type === "ExpressionStatement" ? body.body[0].expression : void 0;
|
|
926
1557
|
return call?.type === "CallExpression" && isIdentifier(call.callee, resolve);
|
|
927
1558
|
};
|
|
928
|
-
|
|
1559
|
+
/** The name of an import specifier or object pattern key (`setTimeout`, `"setTimeout"`). */
|
|
1560
|
+
const propertyName = (node) => node.type === "Identifier" ? node.name : stringValue(node);
|
|
1561
|
+
/** `require("timers/promises")` or `require("node:timers/promises")`. */
|
|
1562
|
+
const isTimersRequire = (node) => {
|
|
1563
|
+
if (node?.type !== "CallExpression" || !isIdentifier(node.callee, "require")) return false;
|
|
1564
|
+
const source = staticString(node.arguments[0]);
|
|
1565
|
+
return source !== void 0 && TIMER_MODULES.has(source);
|
|
1566
|
+
};
|
|
1567
|
+
const rule$4 = {
|
|
929
1568
|
meta: {
|
|
930
1569
|
type: "problem",
|
|
931
1570
|
docs: {
|
|
@@ -940,17 +1579,47 @@ const rule$5 = {
|
|
|
940
1579
|
const { patterns } = conventions;
|
|
941
1580
|
const file = projectPath(context.filename, rootFor(context, conventions));
|
|
942
1581
|
if (file === void 0 || !patterns.checked.test(file) || patterns.framework.test(file)) return {};
|
|
1582
|
+
/** Local names bound to the `timers/promises` module (`timers`) and to its `scheduler` (`schedulers`). */
|
|
1583
|
+
const timers = /* @__PURE__ */ new Set();
|
|
1584
|
+
const schedulers = /* @__PURE__ */ new Set();
|
|
1585
|
+
/** Records a named binding from `timers/promises`; `setTimeout` itself is reported where it is bound. */
|
|
1586
|
+
const bindNamed = (imported, local, node) => {
|
|
1587
|
+
if (imported === "setTimeout") context.report({
|
|
1588
|
+
node,
|
|
1589
|
+
messageId: "hardWait"
|
|
1590
|
+
});
|
|
1591
|
+
if (imported === "scheduler" && local?.type === "Identifier") schedulers.add(local.name);
|
|
1592
|
+
};
|
|
1593
|
+
/** `timers.setTimeout(...)`, `scheduler.wait(...)` and `timers.scheduler.wait(...)`. */
|
|
1594
|
+
const isTimersSleep = (callee) => {
|
|
1595
|
+
if (callee.type !== "MemberExpression") return false;
|
|
1596
|
+
const { object, property } = callee;
|
|
1597
|
+
if (isIdentifier(property, "setTimeout")) return object.type === "Identifier" && timers.has(object.name);
|
|
1598
|
+
if (!isIdentifier(property, "wait")) return false;
|
|
1599
|
+
if (object.type === "Identifier") return schedulers.has(object.name);
|
|
1600
|
+
return object.type === "MemberExpression" && object.object.type === "Identifier" && timers.has(object.object.name) && isIdentifier(object.property, "scheduler");
|
|
1601
|
+
};
|
|
943
1602
|
return {
|
|
944
1603
|
ImportDeclaration(node) {
|
|
945
1604
|
if (typeof node.source.value !== "string" || !TIMER_MODULES.has(node.source.value)) return;
|
|
946
|
-
for (const specifier of node.specifiers) if (specifier.type === "ImportSpecifier"
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
1605
|
+
for (const specifier of node.specifiers) if (specifier.type === "ImportSpecifier") bindNamed(propertyName(specifier.imported) ?? "", specifier.local, specifier);
|
|
1606
|
+
else timers.add(specifier.local.name);
|
|
1607
|
+
},
|
|
1608
|
+
VariableDeclarator(node) {
|
|
1609
|
+
if (!isTimersRequire(node.init)) return;
|
|
1610
|
+
if (node.id.type === "Identifier") {
|
|
1611
|
+
timers.add(node.id.name);
|
|
1612
|
+
return;
|
|
1613
|
+
}
|
|
1614
|
+
if (node.id.type !== "ObjectPattern") return;
|
|
1615
|
+
for (const property of node.id.properties) {
|
|
1616
|
+
if (property.type !== "Property" || property.computed) continue;
|
|
1617
|
+
bindNamed(propertyName(property.key) ?? "", property.value, property);
|
|
1618
|
+
}
|
|
950
1619
|
},
|
|
951
1620
|
CallExpression(node) {
|
|
952
1621
|
const { callee } = node;
|
|
953
|
-
if (callee.type === "MemberExpression" && isIdentifier(callee.property, "waitForTimeout")) {
|
|
1622
|
+
if (callee.type === "MemberExpression" && isIdentifier(callee.property, "waitForTimeout") || isTimersSleep(callee)) {
|
|
954
1623
|
context.report({
|
|
955
1624
|
node,
|
|
956
1625
|
messageId: "hardWait"
|
|
@@ -978,7 +1647,7 @@ const CALLBACK_ARGUMENT_METHODS = /* @__PURE__ */ new Set(["poll", "pollAndRetur
|
|
|
978
1647
|
/** Methods that retry the callback given to the start of their chain (`expect(cb).toPass()`). */
|
|
979
1648
|
const CALLBACK_SUBJECT_METHODS = /* @__PURE__ */ new Set(["toPass", "toPassAndReturn"]);
|
|
980
1649
|
/** The static name of a member access (`a.b`, `a["b"]`), or `undefined` for computed or private access. */
|
|
981
|
-
const memberName
|
|
1650
|
+
const memberName = (node) => {
|
|
982
1651
|
if (node.type !== "MemberExpression") return void 0;
|
|
983
1652
|
const { property } = node;
|
|
984
1653
|
if (!node.computed && property.type === "Identifier") return property.name;
|
|
@@ -987,7 +1656,7 @@ const memberName$1 = (node) => {
|
|
|
987
1656
|
/** The polling method a call invokes (`toPass` for `expect(cb).toPass()`), or `undefined` for any other node. */
|
|
988
1657
|
const pollingMethod = (node, methods) => {
|
|
989
1658
|
if (node.type !== "CallExpression") return void 0;
|
|
990
|
-
const name = memberName
|
|
1659
|
+
const name = memberName(node.callee);
|
|
991
1660
|
return name !== void 0 && methods.has(name) ? name : void 0;
|
|
992
1661
|
};
|
|
993
1662
|
const isFunction$1 = (node) => node?.type === "ArrowFunctionExpression" || node?.type === "FunctionExpression";
|
|
@@ -1029,14 +1698,6 @@ const SKIPPED_KEYS = /* @__PURE__ */ new Set([
|
|
|
1029
1698
|
"returnType",
|
|
1030
1699
|
"decorators"
|
|
1031
1700
|
]);
|
|
1032
|
-
const MODULE_EXTENSIONS = [
|
|
1033
|
-
"",
|
|
1034
|
-
".ts",
|
|
1035
|
-
".tsx",
|
|
1036
|
-
".mts",
|
|
1037
|
-
".cts",
|
|
1038
|
-
"/index.ts"
|
|
1039
|
-
];
|
|
1040
1701
|
const isNode = (value) => typeof value === "object" && value !== null && typeof value.type === "string";
|
|
1041
1702
|
/** Visits `node` and its descendants depth-first. Returning `false` from `visit` skips the node's children. */
|
|
1042
1703
|
function walk(node, visit) {
|
|
@@ -1071,11 +1732,16 @@ function summarizeClass(name, node) {
|
|
|
1071
1732
|
}
|
|
1072
1733
|
return info;
|
|
1073
1734
|
}
|
|
1074
|
-
/** A typed field (`x: X`)
|
|
1735
|
+
/** A typed field (`x: X`), a field created with `new X()`, or a function-valued field, which acts as a method. */
|
|
1075
1736
|
function summarizeField(info, member) {
|
|
1076
1737
|
const field = member.computed ? void 0 : keyName(member.key);
|
|
1738
|
+
if (field === void 0) return;
|
|
1739
|
+
if (isFunctionLike(member.value)) {
|
|
1740
|
+
info.methods.set(field, member.value);
|
|
1741
|
+
return;
|
|
1742
|
+
}
|
|
1077
1743
|
const type = typeName(member.typeAnnotation) ?? newName(member.value);
|
|
1078
|
-
if (
|
|
1744
|
+
if (type !== void 0) info.fields.set(field, type);
|
|
1079
1745
|
}
|
|
1080
1746
|
/** Methods, getters (whose return type acts as a field type) and the constructor. */
|
|
1081
1747
|
function summarizeMethod(info, member) {
|
|
@@ -1100,7 +1766,7 @@ function summarizeConstructor(info, constructor) {
|
|
|
1100
1766
|
walk(constructor.body, (node) => {
|
|
1101
1767
|
if (node.type !== "AssignmentExpression" || node.left.type !== "MemberExpression") return;
|
|
1102
1768
|
if (node.left.object.type !== "ThisExpression") return;
|
|
1103
|
-
const field = memberName
|
|
1769
|
+
const field = memberName(node.left);
|
|
1104
1770
|
const type = newName(node.right);
|
|
1105
1771
|
if (field !== void 0 && type !== void 0 && !info.fields.has(field)) info.fields.set(field, type);
|
|
1106
1772
|
});
|
|
@@ -1172,13 +1838,10 @@ function summarizeNamedExport(summary, statement) {
|
|
|
1172
1838
|
else summary.exports.set(exported, local);
|
|
1173
1839
|
}
|
|
1174
1840
|
}
|
|
1175
|
-
|
|
1176
|
-
/** Resolves a relative or aliased import to a TypeScript file. Package imports resolve to `undefined`. */
|
|
1841
|
+
/** Resolves a relative, aliased or tsconfig-mapped import to a TypeScript file. Package imports resolve to `undefined`. */
|
|
1177
1842
|
function resolveModule(fromFile, request, root, conventions) {
|
|
1178
1843
|
const base = resolveImport(fromFile, request, root, conventions);
|
|
1179
|
-
|
|
1180
|
-
const stripped = base.replace(/\.(?:m|c)?js$/, "");
|
|
1181
|
-
for (const candidate of [base, ...MODULE_EXTENSIONS.map((extension) => `${stripped}${extension}`)]) if (isFile(candidate)) return normalize(candidate);
|
|
1844
|
+
return base === void 0 ? void 0 : moduleFile(base);
|
|
1182
1845
|
}
|
|
1183
1846
|
/**
|
|
1184
1847
|
* Summaries of files on disk per project root and conventions (imports resolve against them), kept for the lifetime of the
|
|
@@ -1283,7 +1946,7 @@ var ProjectIndex = class {
|
|
|
1283
1946
|
fn
|
|
1284
1947
|
} : void 0;
|
|
1285
1948
|
}
|
|
1286
|
-
const method = memberName
|
|
1949
|
+
const method = memberName(node);
|
|
1287
1950
|
if (method === void 0 || node.type !== "MemberExpression") return void 0;
|
|
1288
1951
|
const receiver = this.receiverClass(node.object, scope);
|
|
1289
1952
|
return receiver ? this.findMethod(receiver, method) : void 0;
|
|
@@ -1313,13 +1976,13 @@ var ProjectIndex = class {
|
|
|
1313
1976
|
case "ThisExpression": return scope.cls;
|
|
1314
1977
|
case "Identifier": {
|
|
1315
1978
|
const type = scope.locals.get(node.name);
|
|
1316
|
-
return
|
|
1979
|
+
return this.resolveClass(scope.file, type ?? node.name);
|
|
1317
1980
|
}
|
|
1318
1981
|
case "NewExpression": return node.callee.type === "Identifier" ? this.resolveClass(scope.file, node.callee.name) : void 0;
|
|
1319
1982
|
case "TSNonNullExpression":
|
|
1320
1983
|
case "ParenthesizedExpression": return this.receiverClass(node.expression, scope, depth + 1);
|
|
1321
1984
|
case "MemberExpression": {
|
|
1322
|
-
const field = memberName
|
|
1985
|
+
const field = memberName(node);
|
|
1323
1986
|
const owner = field === void 0 ? void 0 : this.receiverClass(node.object, scope, depth + 1);
|
|
1324
1987
|
return owner && field !== void 0 ? this.fieldClass(owner, field) : void 0;
|
|
1325
1988
|
}
|
|
@@ -1381,11 +2044,29 @@ var ProjectIndex = class {
|
|
|
1381
2044
|
}
|
|
1382
2045
|
}
|
|
1383
2046
|
};
|
|
1384
|
-
/**
|
|
2047
|
+
/** `x` and `X` for a parameter `x: X` or `x = new X()`. Parameter properties are fields, not locals. */
|
|
2048
|
+
function typedParameter(param) {
|
|
2049
|
+
const binding = param.type === "AssignmentPattern" ? param.left : param;
|
|
2050
|
+
if (binding.type !== "Identifier") return void 0;
|
|
2051
|
+
const type = typeName(binding.typeAnnotation) ?? (param.type === "AssignmentPattern" ? newName(param.right) : void 0);
|
|
2052
|
+
return type === void 0 ? void 0 : [binding.name, type];
|
|
2053
|
+
}
|
|
2054
|
+
/**
|
|
2055
|
+
* Locals whose class is known: typed parameters of `fn` and the functions inside it, `const x = new X()` and
|
|
2056
|
+
* `const x: X = ...`.
|
|
2057
|
+
*/
|
|
1385
2058
|
function collectLocals(fn) {
|
|
1386
2059
|
const locals = /* @__PURE__ */ new Map();
|
|
2060
|
+
const addParameters = (node) => {
|
|
2061
|
+
for (const param of node.params) {
|
|
2062
|
+
const found = typedParameter(param);
|
|
2063
|
+
if (found) locals.set(...found);
|
|
2064
|
+
}
|
|
2065
|
+
};
|
|
2066
|
+
addParameters(fn);
|
|
1387
2067
|
if (!fn.body) return locals;
|
|
1388
2068
|
walk(fn.body, (node) => {
|
|
2069
|
+
if (isFunctionLike(node)) addParameters(node);
|
|
1389
2070
|
if (node.type !== "VariableDeclarator" || node.id.type !== "Identifier") return;
|
|
1390
2071
|
const type = typeName(node.id.typeAnnotation) ?? newName(node.init);
|
|
1391
2072
|
if (type !== void 0) locals.set(node.id.name, type);
|
|
@@ -1406,7 +2087,7 @@ function enclosing(node) {
|
|
|
1406
2087
|
}
|
|
1407
2088
|
return { fn };
|
|
1408
2089
|
}
|
|
1409
|
-
const rule$
|
|
2090
|
+
const rule$3 = {
|
|
1410
2091
|
meta: {
|
|
1411
2092
|
type: "problem",
|
|
1412
2093
|
docs: {
|
|
@@ -1477,6 +2158,7 @@ const rule$4 = {
|
|
|
1477
2158
|
};
|
|
1478
2159
|
// src/rules/padding-lines.ts
|
|
1479
2160
|
const BLANK_LINE = /\r?\n[\t ]*\r?\n/;
|
|
2161
|
+
const BLANK_LINES = /(\r?\n)(?:[\t ]*\r?\n)+/g;
|
|
1480
2162
|
const unwrapExport = (node) => (node.type === "ExportNamedDeclaration" || node.type === "ExportDefaultDeclaration") && node.declaration ? node.declaration : node;
|
|
1481
2163
|
const isFunctionValue = (node) => node?.type === "ArrowFunctionExpression" || node?.type === "FunctionExpression" || node?.type === "TSEmptyBodyFunctionExpression";
|
|
1482
2164
|
const isImport = (node) => node.type === "ImportDeclaration" || node.type === "TSImportEqualsDeclaration" && node.moduleReference.type === "TSExternalModuleReference";
|
|
@@ -1493,12 +2175,16 @@ const isOverloadPair = (previous, current) => {
|
|
|
1493
2175
|
const name = functionStatementName(previous);
|
|
1494
2176
|
return unwrapExport(previous).type === "TSDeclareFunction" && name !== void 0 && name === functionStatementName(current);
|
|
1495
2177
|
};
|
|
1496
|
-
|
|
1497
|
-
|
|
2178
|
+
/**
|
|
2179
|
+
* A multi-line method, getter, setter or property holding a function. Single-line ones need no blank lines,
|
|
2180
|
+
* as in `lines-between-class-members`.
|
|
2181
|
+
*/
|
|
2182
|
+
const isMethodProperty = (property) => property.type === "Property" && property.loc.start.line !== property.loc.end.line && (property.method || property.kind !== "init" || isFunctionValue(property.value));
|
|
2183
|
+
const rule$2 = {
|
|
1498
2184
|
meta: {
|
|
1499
2185
|
type: "layout",
|
|
1500
2186
|
docs: {
|
|
1501
|
-
description: "Require blank lines after imports, around top-level functions and around object methods.",
|
|
2187
|
+
description: "Require blank lines after imports, around top-level functions and around object methods, and disallow blank lines between function overloads.",
|
|
1502
2188
|
url: docsUrl("padding-lines")
|
|
1503
2189
|
},
|
|
1504
2190
|
fixable: "whitespace",
|
|
@@ -1506,7 +2192,8 @@ const rule$3 = {
|
|
|
1506
2192
|
messages: {
|
|
1507
2193
|
afterImports: "Expected a blank line after the import declarations.",
|
|
1508
2194
|
aroundFunctions: "Expected a blank line between a function and the adjacent statement.",
|
|
1509
|
-
betweenObjectMethods: "Expected a blank line around methods in an object literal."
|
|
2195
|
+
betweenObjectMethods: "Expected a blank line around methods in an object literal.",
|
|
2196
|
+
betweenOverloads: "Unexpected blank line between overloads of the same function."
|
|
1510
2197
|
}
|
|
1511
2198
|
},
|
|
1512
2199
|
create(context) {
|
|
@@ -1525,12 +2212,23 @@ const rule$3 = {
|
|
|
1525
2212
|
}
|
|
1526
2213
|
});
|
|
1527
2214
|
};
|
|
2215
|
+
/** Overload signatures and the implementation form one group without blank lines; comments are kept. */
|
|
2216
|
+
const forbidBlankLines = (previous, current) => {
|
|
2217
|
+
const textBetween = text.slice(previous.range[1], current.range[0]);
|
|
2218
|
+
if (!BLANK_LINE.test(textBetween)) return;
|
|
2219
|
+
context.report({
|
|
2220
|
+
node: current,
|
|
2221
|
+
messageId: "betweenOverloads",
|
|
2222
|
+
fix: (fixer) => fixer.replaceTextRange([previous.range[1], current.range[0]], textBetween.replace(BLANK_LINES, "$1"))
|
|
2223
|
+
});
|
|
2224
|
+
};
|
|
1528
2225
|
const checkStatements = (statements) => {
|
|
1529
2226
|
for (let index = 1; index < statements.length; index++) {
|
|
1530
2227
|
const previous = statements[index - 1];
|
|
1531
2228
|
const current = statements[index];
|
|
1532
2229
|
if (isImport(previous) && !isImport(current)) requireBlankLine(previous, current, "afterImports");
|
|
1533
|
-
else if (
|
|
2230
|
+
else if (isOverloadPair(previous, current)) forbidBlankLines(previous, current);
|
|
2231
|
+
else if (isFunctionStatement(previous) || isFunctionStatement(current)) requireBlankLine(previous, current, "aroundFunctions");
|
|
1534
2232
|
}
|
|
1535
2233
|
};
|
|
1536
2234
|
return {
|
|
@@ -1547,51 +2245,12 @@ const rule$3 = {
|
|
|
1547
2245
|
};
|
|
1548
2246
|
}
|
|
1549
2247
|
};
|
|
1550
|
-
// src/rules/private-members-last.ts
|
|
1551
|
-
const isMethodLike = (member) => (member.type === "MethodDefinition" || member.type === "TSAbstractMethodDefinition") && member.kind !== "constructor";
|
|
1552
|
-
const isPrivate = (member) => "accessibility" in member && member.accessibility === "private" || "key" in member && member.key.type === "PrivateIdentifier";
|
|
1553
|
-
const memberName = (member) => {
|
|
1554
|
-
const key = "key" in member ? member.key : void 0;
|
|
1555
|
-
if (key && "name" in key && typeof key.name === "string") return key.name;
|
|
1556
|
-
if (key && "value" in key && typeof key.value === "string") return key.value;
|
|
1557
|
-
return "<computed>";
|
|
1558
|
-
};
|
|
1559
|
-
const rule$2 = {
|
|
1560
|
-
meta: {
|
|
1561
|
-
type: "suggestion",
|
|
1562
|
-
docs: {
|
|
1563
|
-
description: "Require public methods to be placed above private methods.",
|
|
1564
|
-
url: docsUrl("private-members-last")
|
|
1565
|
-
},
|
|
1566
|
-
schema: [],
|
|
1567
|
-
messages: { privateMembersLast: "Public method '{{name}}' must be placed above all private methods (private method '{{privateName}}' comes first)." }
|
|
1568
|
-
},
|
|
1569
|
-
create(context) {
|
|
1570
|
-
return { ClassBody(node) {
|
|
1571
|
-
let firstPrivate;
|
|
1572
|
-
for (const member of node.body) {
|
|
1573
|
-
if (!isMethodLike(member)) continue;
|
|
1574
|
-
if (isPrivate(member)) {
|
|
1575
|
-
firstPrivate ??= member;
|
|
1576
|
-
continue;
|
|
1577
|
-
}
|
|
1578
|
-
if (firstPrivate !== void 0) context.report({
|
|
1579
|
-
node: member,
|
|
1580
|
-
messageId: "privateMembersLast",
|
|
1581
|
-
data: {
|
|
1582
|
-
name: memberName(member),
|
|
1583
|
-
privateName: memberName(firstPrivate)
|
|
1584
|
-
}
|
|
1585
|
-
});
|
|
1586
|
-
}
|
|
1587
|
-
} };
|
|
1588
|
-
}
|
|
1589
|
-
};
|
|
1590
2248
|
// src/rules/step-placeholders.ts
|
|
1591
2249
|
/** The same pattern `@step` uses at runtime: the path is taken as written, so `{{ name }}` looks up " name ". */
|
|
1592
2250
|
const NAMED = /\{\{(.*?)\}\}/g;
|
|
1593
2251
|
const INDEX = /\[\[(\d+)\]\]/g;
|
|
1594
|
-
|
|
2252
|
+
/** A lookbehind instead of a consumed prefix character, so adjacent placeholders (`{a}{b}`) are all found. */
|
|
2253
|
+
const SINGLE_BRACES = /(?<!\{)\{([A-Za-z_$][\w$]*(?:\.[\w$]+)*)\}(?!\})/g;
|
|
1595
2254
|
const findAll = (pattern, text) => {
|
|
1596
2255
|
const regex = new RegExp(pattern.source, "g");
|
|
1597
2256
|
const found = [];
|
|
@@ -1603,13 +2262,18 @@ const paramName = (param) => {
|
|
|
1603
2262
|
const binding = target.type === "AssignmentPattern" ? target.left : target.type === "RestElement" ? target.argument : target;
|
|
1604
2263
|
return binding.type === "Identifier" ? binding.name : void 0;
|
|
1605
2264
|
};
|
|
2265
|
+
/** True when a placeholder path starts at a parameter, or at `this` (a property of the instance). */
|
|
2266
|
+
const isKnownRoot = (path, names) => {
|
|
2267
|
+
const [root] = path.split(".");
|
|
2268
|
+
return root === "this" || names.includes(root);
|
|
2269
|
+
};
|
|
1606
2270
|
/**
|
|
1607
2271
|
* What is wrong with the path of a `{{path}}` placeholder, if anything. `@step` reads the path exactly as written,
|
|
1608
2272
|
* so spaces around it either fail the parameter lookup or read a property that does not exist.
|
|
1609
2273
|
*/
|
|
1610
2274
|
const namedProblem = (path, names) => {
|
|
1611
2275
|
const trimmed = path.trim();
|
|
1612
|
-
const isParameter = (value) =>
|
|
2276
|
+
const isParameter = (value) => isKnownRoot(value, names);
|
|
1613
2277
|
if (trimmed !== path && isParameter(trimmed)) return "whitespace";
|
|
1614
2278
|
return trimmed === path && isParameter(path) ? void 0 : "unknownParameter";
|
|
1615
2279
|
};
|
|
@@ -1691,14 +2355,14 @@ const rule$1 = {
|
|
|
1691
2355
|
});
|
|
1692
2356
|
}
|
|
1693
2357
|
for (const match of findAll(SINGLE_BRACES, raw)) {
|
|
1694
|
-
const placeholder = match[
|
|
1695
|
-
const from = start + match.index
|
|
2358
|
+
const placeholder = match[1];
|
|
2359
|
+
const from = start + match.index;
|
|
1696
2360
|
const range = [from, from + placeholder.length + 2];
|
|
1697
2361
|
const data = {
|
|
1698
2362
|
found: `{${placeholder}}`,
|
|
1699
2363
|
expected: `{{${placeholder}}}`
|
|
1700
2364
|
};
|
|
1701
|
-
if (
|
|
2365
|
+
if (isKnownRoot(placeholder, names)) context.report({
|
|
1702
2366
|
node: description,
|
|
1703
2367
|
messageId: "singleBraces",
|
|
1704
2368
|
data,
|
|
@@ -1719,19 +2383,23 @@ const STEP_LAYERS = /* @__PURE__ */ new Set([
|
|
|
1719
2383
|
"asserter",
|
|
1720
2384
|
"scenario"
|
|
1721
2385
|
]);
|
|
2386
|
+
const FUNCTION_VALUES = /* @__PURE__ */ new Set(["ArrowFunctionExpression", "FunctionExpression"]);
|
|
2387
|
+
/** Public instance members are steps; static members are helpers (such as factories). */
|
|
2388
|
+
const isPublicInstance = (member) => !member.static && member.key.type !== "PrivateIdentifier" && member.accessibility !== "private" && member.accessibility !== "protected";
|
|
1722
2389
|
// src/rules/index.ts
|
|
1723
2390
|
const rules = {
|
|
1724
|
-
"architecture-boundaries": rule$
|
|
1725
|
-
"class-matches-filename": rule$
|
|
2391
|
+
"architecture-boundaries": rule$13,
|
|
2392
|
+
"class-matches-filename": rule$12,
|
|
2393
|
+
"director-composes-builders": rule$11,
|
|
1726
2394
|
"expect-from-support": rule$10,
|
|
1727
2395
|
"file-naming": rule$9,
|
|
1728
2396
|
"lines-between-class-members": rule$8,
|
|
1729
|
-
"
|
|
1730
|
-
"no-
|
|
1731
|
-
"no-
|
|
1732
|
-
"no-
|
|
1733
|
-
"
|
|
1734
|
-
"
|
|
2397
|
+
"member-ordering": rule$7,
|
|
2398
|
+
"no-direct-construction-in-tests": rule$6,
|
|
2399
|
+
"no-expect-in-tests": rule$5,
|
|
2400
|
+
"no-hard-waits": rule$4,
|
|
2401
|
+
"no-nested-polling": rule$3,
|
|
2402
|
+
"padding-lines": rule$2,
|
|
1735
2403
|
"step-placeholders": rule$1,
|
|
1736
2404
|
"step-required": {
|
|
1737
2405
|
meta: {
|
|
@@ -1741,24 +2409,35 @@ const rules = {
|
|
|
1741
2409
|
url: docsUrl("step-required")
|
|
1742
2410
|
},
|
|
1743
2411
|
schema: [],
|
|
1744
|
-
messages: {
|
|
2412
|
+
messages: {
|
|
2413
|
+
missingStep: "Public method '{{method}}' in a {{layer}} needs a @step decorator so it is visible in the report.",
|
|
2414
|
+
functionProperty: "Public function property '{{method}}' in a {{layer}} cannot carry a @step decorator; make it a method with @step."
|
|
2415
|
+
}
|
|
1745
2416
|
},
|
|
1746
2417
|
create(context) {
|
|
1747
2418
|
const conventions = conventionsFor(context);
|
|
1748
2419
|
const info = classify(context.filename, conventions, rootFor(context, conventions));
|
|
1749
2420
|
const layer = info?.kind === "top-scenario" ? "scenario" : info?.layer;
|
|
1750
2421
|
if (layer === void 0 || !STEP_LAYERS.has(layer)) return {};
|
|
1751
|
-
|
|
1752
|
-
if (node.kind !== "method" || node.key.type === "PrivateIdentifier" || node.accessibility === "private" || node.accessibility === "protected" || node.value.type === "TSEmptyBodyFunctionExpression" || node.decorators.some((decorator) => isStepDecorator(decorator, conventions.stepDecorators))) return;
|
|
2422
|
+
const report = (key, messageId) => {
|
|
1753
2423
|
context.report({
|
|
1754
|
-
node:
|
|
1755
|
-
messageId
|
|
2424
|
+
node: key,
|
|
2425
|
+
messageId,
|
|
1756
2426
|
data: {
|
|
1757
|
-
method: context.sourceCode.getText(
|
|
2427
|
+
method: context.sourceCode.getText(key),
|
|
1758
2428
|
layer
|
|
1759
2429
|
}
|
|
1760
2430
|
});
|
|
1761
|
-
}
|
|
2431
|
+
};
|
|
2432
|
+
return {
|
|
2433
|
+
MethodDefinition(node) {
|
|
2434
|
+
if (node.kind !== "method" || !isPublicInstance(node) || node.value.type === "TSEmptyBodyFunctionExpression" || node.decorators.some((decorator) => isStepDecorator(decorator, conventions.stepDecorators))) return;
|
|
2435
|
+
report(node.key, "missingStep");
|
|
2436
|
+
},
|
|
2437
|
+
PropertyDefinition(node) {
|
|
2438
|
+
if (isPublicInstance(node) && node.value && FUNCTION_VALUES.has(node.value.type)) report(node.key, "functionProperty");
|
|
2439
|
+
}
|
|
2440
|
+
};
|
|
1762
2441
|
}
|
|
1763
2442
|
}
|
|
1764
2443
|
};
|
|
@@ -1767,4 +2446,4 @@ const PLUGIN_NAME = "sparta";
|
|
|
1767
2446
|
const recommendedRules = Object.fromEntries(Object.entries(rules).map(([name, rule]) => [`${PLUGIN_NAME}/${name}`, rule.meta?.type === "problem" ? "error" : "warn"]));
|
|
1768
2447
|
export { recommendedRules as n, rules as r, PLUGIN_NAME as t };
|
|
1769
2448
|
|
|
1770
|
-
//# sourceMappingURL=rules-
|
|
2449
|
+
//# sourceMappingURL=rules-D94N3gGx.mjs.map
|