@cerios/sparta-lint-rules 0.0.1 → 0.0.3
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 +87 -40
- package/dist/conventions-DK3A7JWL.d.cts +97 -0
- package/dist/conventions-DK3A7JWL.d.mts +97 -0
- package/dist/index.cjs +3 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +15 -1
- package/dist/index.d.mts +16 -2
- package/dist/index.mjs +3 -2
- package/dist/index.mjs.map +1 -1
- package/dist/oxlint.cjs +2 -1
- package/dist/oxlint.cjs.map +1 -1
- package/dist/oxlint.d.cts +18 -1
- package/dist/oxlint.d.mts +19 -2
- package/dist/oxlint.mjs +2 -1
- package/dist/oxlint.mjs.map +1 -1
- package/dist/{plugin-types-CgeW0cQl.d.cts → plugin-types-7QJakt0Z.d.cts} +10 -4
- package/dist/{plugin-types-CgeW0cQl.d.mts → plugin-types-7QJakt0Z.d.mts} +10 -4
- package/dist/{rules-COk7kmEU.cjs → rules-5VZcw4ds.cjs} +868 -175
- package/dist/rules-5VZcw4ds.cjs.map +1 -0
- package/dist/{rules-BSRYNh4F.mjs → rules-Ay2ZlXtg.mjs} +868 -175
- package/dist/rules-Ay2ZlXtg.mjs.map +1 -0
- package/dist/settings.cjs +1 -0
- package/dist/settings.d.cts +5 -92
- package/dist/settings.d.mts +5 -92
- package/dist/settings.mjs +1 -0
- package/oxlintrc.schema.json +718 -0
- package/package.json +10 -3
- package/dist/rules-BSRYNh4F.mjs.map +0 -1
- package/dist/rules-COk7kmEU.cjs.map +0 -1
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
|
|
2
|
+
import { createRequire } from "node:module";
|
|
1
3
|
import path from "node:path";
|
|
2
4
|
import { existsSync, readFileSync, statSync } from "node:fs";
|
|
3
5
|
import { parseSync } from "oxc-parser";
|
|
@@ -5,12 +7,51 @@ import { parseSync } from "oxc-parser";
|
|
|
5
7
|
const isIdentifier = (node, name) => node?.type === "Identifier" && node.name === name;
|
|
6
8
|
/** The value of a string literal node, or `undefined` for any other node. */
|
|
7
9
|
const stringValue = (node) => node?.type === "Literal" && typeof node.value === "string" ? node.value : void 0;
|
|
10
|
+
/** The value of a string literal or a template literal without expressions (`\`./x\``), or `undefined`. */
|
|
11
|
+
const staticString = (node) => node?.type === "TemplateLiteral" && node.expressions.length === 0 ? node.quasis[0]?.value.cooked ?? void 0 : stringValue(node);
|
|
8
12
|
/** True for `@step` and `@step("...")` decorators, or any of the given decorator `names`. */
|
|
9
13
|
const isStepDecorator = (decorator, names) => {
|
|
10
14
|
const { expression } = decorator;
|
|
11
15
|
const callee = expression.type === "CallExpression" ? expression.callee : expression;
|
|
12
16
|
return callee.type === "Identifier" && names.has(callee.name);
|
|
13
17
|
};
|
|
18
|
+
// src/utils/class-members.ts
|
|
19
|
+
const isMethod = (member) => member.type === "MethodDefinition" || member.type === "TSAbstractMethodDefinition";
|
|
20
|
+
const FUNCTION_VALUES$1 = /* @__PURE__ */ new Set([
|
|
21
|
+
"ArrowFunctionExpression",
|
|
22
|
+
"FunctionExpression",
|
|
23
|
+
"ClassExpression"
|
|
24
|
+
]);
|
|
25
|
+
/**
|
|
26
|
+
* A plain property declaration (`static readonly PATTERN = /.../;`, `private readonly date: Date;`, …).
|
|
27
|
+
* A property holding a function or class is not plain: it is treated like a method.
|
|
28
|
+
*/
|
|
29
|
+
const isPlainProperty = (member) => {
|
|
30
|
+
if (member.type === "TSIndexSignature") return true;
|
|
31
|
+
if (member.type !== "PropertyDefinition" && member.type !== "TSAbstractPropertyDefinition" && member.type !== "AccessorProperty") return false;
|
|
32
|
+
return member.value === null || !FUNCTION_VALUES$1.has(member.value.type);
|
|
33
|
+
};
|
|
34
|
+
/** `private` or an ECMAScript `#private` name. */
|
|
35
|
+
const isPrivate = (member) => "accessibility" in member && member.accessibility === "private" || "key" in member && member.key.type === "PrivateIdentifier";
|
|
36
|
+
const memberName$1 = (member) => {
|
|
37
|
+
const key = "key" in member ? member.key : void 0;
|
|
38
|
+
if (key && "name" in key && typeof key.name === "string") return key.name;
|
|
39
|
+
if (key && "value" in key && typeof key.value === "string") return key.value;
|
|
40
|
+
return "<computed>";
|
|
41
|
+
};
|
|
42
|
+
/** A member that belongs to the class itself: `static` members, static blocks and a `private constructor`. */
|
|
43
|
+
const isStaticMember = (member) => member.type === "StaticBlock" || "static" in member && member.static || member.type === "MethodDefinition" && member.kind === "constructor" && member.accessibility === "private";
|
|
44
|
+
/** A static abstract class: `abstract`, with only static members, so it is used without ever being instantiated. */
|
|
45
|
+
const isStaticClass = (node) => node.abstract === true && node.body.body.every(isStaticMember);
|
|
46
|
+
/** The classes declared at the top level of a program, including exported ones. */
|
|
47
|
+
function topLevelClasses(program) {
|
|
48
|
+
const classes = [];
|
|
49
|
+
for (const statement of program.body) {
|
|
50
|
+
const declaration = statement.type === "ExportNamedDeclaration" || statement.type === "ExportDefaultDeclaration" ? statement.declaration : statement;
|
|
51
|
+
if (declaration?.type === "ClassDeclaration") classes.push(declaration);
|
|
52
|
+
}
|
|
53
|
+
return classes;
|
|
54
|
+
}
|
|
14
55
|
// src/utils/docs.ts
|
|
15
56
|
const REPOSITORY = "https://github.com/CeriosTesting/sparta-lint-rules";
|
|
16
57
|
const docsUrl = (ruleName) => `${REPOSITORY}/blob/main/docs/rules/${ruleName}.md`;
|
|
@@ -63,11 +104,22 @@ const LAYER_FOLDERS = {
|
|
|
63
104
|
steppers: {
|
|
64
105
|
layer: "stepper",
|
|
65
106
|
suffix: "stepper"
|
|
107
|
+
},
|
|
108
|
+
windows: {
|
|
109
|
+
layer: "window",
|
|
110
|
+
suffix: "window"
|
|
66
111
|
}
|
|
67
112
|
};
|
|
113
|
+
/**
|
|
114
|
+
* Layer folders that may also live outside a group: directly in an area (`sparta/<area>/builders/`), shared by its
|
|
115
|
+
* groups, and in `sparta/common/`, shared by everything. In a group or an area's `common/` they are ordinary layer
|
|
116
|
+
* folders.
|
|
117
|
+
*/
|
|
118
|
+
const AREA_LAYER_FOLDERS = /* @__PURE__ */ new Set(["builders", "directors"]);
|
|
68
119
|
const AREAS = [
|
|
69
120
|
"api",
|
|
70
121
|
"database",
|
|
122
|
+
"desktop",
|
|
71
123
|
"mobile",
|
|
72
124
|
"ui"
|
|
73
125
|
];
|
|
@@ -109,9 +161,9 @@ const suggest = (value, candidates) => {
|
|
|
109
161
|
})).toSorted((a, b) => a.score - b.score);
|
|
110
162
|
return best && best.score <= Math.max(2, Math.floor(best.candidate.length / 3)) ? `; did you mean ${JSON.stringify(best.candidate)}?` : "";
|
|
111
163
|
};
|
|
112
|
-
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
164
|
+
const isRecord$1 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
113
165
|
function record(value, where) {
|
|
114
|
-
if (!isRecord(value)) throw new SettingsError(where, `must be an object, got ${JSON.stringify(value)}.`);
|
|
166
|
+
if (!isRecord$1(value)) throw new SettingsError(where, `must be an object, got ${JSON.stringify(value)}.`);
|
|
115
167
|
return value;
|
|
116
168
|
}
|
|
117
169
|
function checkKeys(value, allowed, where) {
|
|
@@ -200,14 +252,16 @@ function buildPatterns(spartaFolder, testsFolder, areas) {
|
|
|
200
252
|
const sparta = escape(spartaFolder);
|
|
201
253
|
const tests = escape(testsFolder);
|
|
202
254
|
const areaNames = [...areas.keys()].map(escape).join("|");
|
|
255
|
+
const end = "(?=/|$)";
|
|
203
256
|
return {
|
|
204
|
-
area: new RegExp(`/${sparta}/(${areaNames})
|
|
257
|
+
area: new RegExp(`/${sparta}/(${areaNames})${end}(?:/([^/]+))?(?:/([^/]+))?`),
|
|
205
258
|
layer: new RegExp(`/${sparta}/(?:${areaNames})/[^/]+/([^/]+)/`),
|
|
206
|
-
top: new RegExp(`/${sparta}/(${TOP_LEVEL_FOLDERS.join("|")})
|
|
259
|
+
top: new RegExp(`/${sparta}/(${TOP_LEVEL_FOLDERS.join("|")})${end}`),
|
|
260
|
+
common: new RegExp(`/${sparta}/common/([^/]+)${end}`),
|
|
207
261
|
checked: new RegExp(`/(?:${sparta}|${tests})/`),
|
|
208
262
|
sparta: new RegExp(`/${sparta}/`),
|
|
209
|
-
tests: new RegExp(`/${tests}
|
|
210
|
-
framework: new RegExp(`/${tests}/framework
|
|
263
|
+
tests: new RegExp(`/${tests}${end}`),
|
|
264
|
+
framework: new RegExp(`/${tests}/framework${end}`)
|
|
211
265
|
};
|
|
212
266
|
}
|
|
213
267
|
const STANDARD_LAYER_FOLDERS = Object.keys(LAYER_FOLDERS);
|
|
@@ -238,6 +292,196 @@ function parseSettings(raw, cwd) {
|
|
|
238
292
|
};
|
|
239
293
|
}
|
|
240
294
|
parseSettings(void 0, process.cwd());
|
|
295
|
+
// src/utils/tsconfig.ts
|
|
296
|
+
const normalize$1 = (value) => value.split("\\").join("/");
|
|
297
|
+
const CONFIG_DIR = "${configDir}";
|
|
298
|
+
/** Where the string or comment that starts at `index` ends (exclusive), or `undefined` when none starts there. */
|
|
299
|
+
function skippedEnd(text, index) {
|
|
300
|
+
const start = text.slice(index, index + 2);
|
|
301
|
+
if (text[index] === "\"") {
|
|
302
|
+
let end = index + 1;
|
|
303
|
+
while (end < text.length && text[end] !== "\"") end += text[end] === "\\" ? 2 : 1;
|
|
304
|
+
return end + 1;
|
|
305
|
+
}
|
|
306
|
+
if (start === "//") {
|
|
307
|
+
const end = text.indexOf("\n", index);
|
|
308
|
+
return end === -1 ? text.length : end;
|
|
309
|
+
}
|
|
310
|
+
if (start === "/*") {
|
|
311
|
+
const end = text.indexOf("*/", index + 2);
|
|
312
|
+
return end === -1 ? text.length : end + 2;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Parses JSON with comments and trailing commas, as `tsconfig.json` allows. Comments are only recognised outside
|
|
317
|
+
* strings, so `"https://..."` stays intact.
|
|
318
|
+
*/
|
|
319
|
+
function parseJsonc(text) {
|
|
320
|
+
let out = "";
|
|
321
|
+
/** Position in `out` of a comma that may still turn out to be a trailing one. */
|
|
322
|
+
let pendingComma = -1;
|
|
323
|
+
let index = text.charCodeAt(0) === 65279 ? 1 : 0;
|
|
324
|
+
while (index < text.length) {
|
|
325
|
+
const char = text[index];
|
|
326
|
+
const end = skippedEnd(text, index);
|
|
327
|
+
if (end !== void 0) {
|
|
328
|
+
if (char === "\"") {
|
|
329
|
+
out += text.slice(index, end);
|
|
330
|
+
pendingComma = -1;
|
|
331
|
+
}
|
|
332
|
+
index = end;
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
if (char === ",") pendingComma = out.length;
|
|
336
|
+
else if ((char === "}" || char === "]") && pendingComma !== -1) {
|
|
337
|
+
out = `${out.slice(0, pendingComma)} ${out.slice(pendingComma + 1)}`;
|
|
338
|
+
pendingComma = -1;
|
|
339
|
+
} else if (!/\s/.test(char)) pendingComma = -1;
|
|
340
|
+
out += char;
|
|
341
|
+
index++;
|
|
342
|
+
}
|
|
343
|
+
return JSON.parse(out);
|
|
344
|
+
}
|
|
345
|
+
const isFile$1 = (file) => statSync(file, { throwIfNoEntry: false })?.isFile() ?? false;
|
|
346
|
+
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
347
|
+
var TsconfigError = class extends Error {
|
|
348
|
+
constructor(file, message) {
|
|
349
|
+
super(`sparta-lint-rules: cannot read ${normalize$1(file)}: ${message}`);
|
|
350
|
+
this.name = "TsconfigError";
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
function readConfig(file) {
|
|
354
|
+
let config;
|
|
355
|
+
try {
|
|
356
|
+
config = parseJsonc(readFileSync(file, "utf8"));
|
|
357
|
+
} catch (error) {
|
|
358
|
+
throw new TsconfigError(file, error instanceof Error ? error.message : String(error));
|
|
359
|
+
}
|
|
360
|
+
if (!isRecord(config)) throw new TsconfigError(file, "the config must be a JSON object.");
|
|
361
|
+
return config;
|
|
362
|
+
}
|
|
363
|
+
/** The file an `extends` entry points to: a path relative to the config, or a package such as `@tsconfig/node22`. */
|
|
364
|
+
function resolveExtends(specifier, from) {
|
|
365
|
+
if (specifier.startsWith(".") || path.isAbsolute(specifier)) {
|
|
366
|
+
const file = path.resolve(path.dirname(from), specifier);
|
|
367
|
+
if (isFile$1(file)) return file;
|
|
368
|
+
if (!file.endsWith(".json") && isFile$1(`${file}.json`)) return `${file}.json`;
|
|
369
|
+
} else {
|
|
370
|
+
const require = createRequire(from);
|
|
371
|
+
const candidates = specifier.endsWith(".json") ? [specifier] : [`${specifier}/tsconfig.json`, specifier];
|
|
372
|
+
for (const candidate of candidates) try {
|
|
373
|
+
return require.resolve(candidate);
|
|
374
|
+
} catch {}
|
|
375
|
+
}
|
|
376
|
+
throw new TsconfigError(from, `extends ${JSON.stringify(specifier)}, which cannot be found.`);
|
|
377
|
+
}
|
|
378
|
+
const substitute = (value, configDir) => value.split(CONFIG_DIR).join(configDir);
|
|
379
|
+
function loadOptions(file, configDir, chain, read) {
|
|
380
|
+
if (chain.includes(file)) throw new TsconfigError(chain[0], `circular extends: ${[...chain, file].map(normalize$1).join(" -> ")}.`);
|
|
381
|
+
const config = readConfig(file);
|
|
382
|
+
const dir = path.dirname(file);
|
|
383
|
+
const parents = typeof config.extends === "string" ? [config.extends] : config.extends;
|
|
384
|
+
let options = {};
|
|
385
|
+
read.push(file);
|
|
386
|
+
if (Array.isArray(parents)) for (const parent of parents) {
|
|
387
|
+
if (typeof parent !== "string") continue;
|
|
388
|
+
const inherited = loadOptions(resolveExtends(parent, file), configDir, [...chain, file], read);
|
|
389
|
+
options = {
|
|
390
|
+
...options,
|
|
391
|
+
...inherited
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
const compilerOptions = isRecord(config.compilerOptions) ? config.compilerOptions : {};
|
|
395
|
+
if (typeof compilerOptions.baseUrl === "string") options.baseUrl = path.resolve(dir, substitute(compilerOptions.baseUrl, configDir));
|
|
396
|
+
if (isRecord(compilerOptions.paths)) options.paths = {
|
|
397
|
+
mapping: compilerOptions.paths,
|
|
398
|
+
base: dir
|
|
399
|
+
};
|
|
400
|
+
return options;
|
|
401
|
+
}
|
|
402
|
+
function toMappings(options, configDir) {
|
|
403
|
+
if (!options.paths) return [];
|
|
404
|
+
const base = options.baseUrl ?? options.paths.base;
|
|
405
|
+
return Object.entries(options.paths.mapping).flatMap(([pattern, value]) => {
|
|
406
|
+
const targets = Array.isArray(value) ? value.filter((target) => typeof target === "string") : [];
|
|
407
|
+
const star = pattern.indexOf("*");
|
|
408
|
+
if (targets.length === 0) return [];
|
|
409
|
+
return [{
|
|
410
|
+
pattern,
|
|
411
|
+
prefix: star === -1 ? pattern : pattern.slice(0, star),
|
|
412
|
+
suffix: star === -1 ? "" : pattern.slice(star + 1),
|
|
413
|
+
wildcard: star !== -1,
|
|
414
|
+
targets: targets.map((target) => normalize$1(path.resolve(base, substitute(target, configDir))))
|
|
415
|
+
}];
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
/** Parsed configs per file, kept for the lifetime of the linter process and refreshed when any file in the chain changes. */
|
|
419
|
+
const configCache = /* @__PURE__ */ new Map();
|
|
420
|
+
const mtime = (file) => statSync(file, { throwIfNoEntry: false })?.mtimeMs;
|
|
421
|
+
/** The `paths` and `baseUrl` of a config file, including what it extends. Throws for unreadable or invalid configs. */
|
|
422
|
+
function loadTsconfig(file) {
|
|
423
|
+
const cached = configCache.get(file);
|
|
424
|
+
if (cached?.read.every((entry) => mtime(entry.file) === entry.mtimeMs)) return cached.value;
|
|
425
|
+
const configDir = path.dirname(file);
|
|
426
|
+
const read = [];
|
|
427
|
+
const options = loadOptions(file, configDir, [], read);
|
|
428
|
+
const value = {
|
|
429
|
+
file: normalize$1(file),
|
|
430
|
+
baseUrl: options.baseUrl === void 0 ? void 0 : normalize$1(options.baseUrl),
|
|
431
|
+
paths: toMappings(options, configDir)
|
|
432
|
+
};
|
|
433
|
+
configCache.set(file, {
|
|
434
|
+
read: read.map((entry) => ({
|
|
435
|
+
file: entry,
|
|
436
|
+
mtimeMs: mtime(entry) ?? -1
|
|
437
|
+
})),
|
|
438
|
+
value
|
|
439
|
+
});
|
|
440
|
+
return value;
|
|
441
|
+
}
|
|
442
|
+
/** Nearest `tsconfig.json` per directory (`null` for none), kept for the lifetime of the linter process. */
|
|
443
|
+
const lookupCache = /* @__PURE__ */ new Map();
|
|
444
|
+
/** The nearest `tsconfig.json`, from the file's folder upwards, or `undefined` when there is none. */
|
|
445
|
+
function findTsconfig(filename) {
|
|
446
|
+
const visited = [];
|
|
447
|
+
let found;
|
|
448
|
+
for (let dir = path.dirname(path.resolve(filename));;) {
|
|
449
|
+
found = lookupCache.get(dir);
|
|
450
|
+
if (found !== void 0) break;
|
|
451
|
+
visited.push(dir);
|
|
452
|
+
const candidate = path.join(dir, "tsconfig.json");
|
|
453
|
+
if (isFile$1(candidate)) {
|
|
454
|
+
found = candidate;
|
|
455
|
+
break;
|
|
456
|
+
}
|
|
457
|
+
const parent = path.dirname(dir);
|
|
458
|
+
if (parent === dir) {
|
|
459
|
+
found = null;
|
|
460
|
+
break;
|
|
461
|
+
}
|
|
462
|
+
dir = parent;
|
|
463
|
+
}
|
|
464
|
+
for (const dir of visited) lookupCache.set(dir, found);
|
|
465
|
+
return found ?? void 0;
|
|
466
|
+
}
|
|
467
|
+
/** The `paths` and `baseUrl` that apply to a file, from its nearest `tsconfig.json`. */
|
|
468
|
+
function tsconfigFor(filename) {
|
|
469
|
+
const file = findTsconfig(filename);
|
|
470
|
+
return file === void 0 ? void 0 : loadTsconfig(file);
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
473
|
+
* The candidate paths a `paths` entry maps a request to, as TypeScript picks them: an exact pattern first,
|
|
474
|
+
* otherwise the wildcard pattern with the longest prefix. `undefined` when no pattern matches.
|
|
475
|
+
*/
|
|
476
|
+
function matchPaths(request, mappings) {
|
|
477
|
+
const exact = mappings.find((mapping) => !mapping.wildcard && mapping.pattern === request);
|
|
478
|
+
if (exact) return exact.targets;
|
|
479
|
+
let best;
|
|
480
|
+
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;
|
|
481
|
+
if (best === void 0) return void 0;
|
|
482
|
+
const matched = request.slice(best.prefix.length, request.length - best.suffix.length);
|
|
483
|
+
return best.targets.map((target) => target.replace("*", matched));
|
|
484
|
+
}
|
|
241
485
|
// src/utils/sparta-paths.ts
|
|
242
486
|
const normalize = (value) => value.split("\\").join("/");
|
|
243
487
|
const TOP_LEVEL_KINDS = {
|
|
@@ -246,6 +490,20 @@ const TOP_LEVEL_KINDS = {
|
|
|
246
490
|
};
|
|
247
491
|
/** The standard layer folder that a project's folder name follows, if any. */
|
|
248
492
|
const standardFolder = (conventions, folder) => folder === void 0 ? void 0 : conventions.layerFolders.get(folder);
|
|
493
|
+
/** The layer of a standard layer folder (`stepper` for `steppers`), if it has one. */
|
|
494
|
+
const layerOf = (standard) => standard === void 0 ? void 0 : LAYER_FOLDERS[standard].layer;
|
|
495
|
+
/** `name` as a builders or directors folder (`AREA_LAYER_FOLDERS`), if it is one. */
|
|
496
|
+
const builderFolder = (conventions, name) => {
|
|
497
|
+
const standard = standardFolder(conventions, name);
|
|
498
|
+
return name !== void 0 && standard !== void 0 && AREA_LAYER_FOLDERS.has(standard) ? {
|
|
499
|
+
name,
|
|
500
|
+
standard
|
|
501
|
+
} : void 0;
|
|
502
|
+
};
|
|
503
|
+
/** The area-level layer folder (`builders/`, `directors/` directly below an area) a project path is in, if any. */
|
|
504
|
+
const areaLayerFolder = (file, conventions) => builderFolder(conventions, file.match(conventions.patterns.area)?.[2]);
|
|
505
|
+
/** The `builders/` or `directors/` folder directly below `sparta/common/` a project path is in, if any. */
|
|
506
|
+
const commonLayerFolder = (file, conventions) => builderFolder(conventions, file.match(conventions.patterns.common)?.[1]);
|
|
249
507
|
const caseInsensitivePaths = process.platform === "win32";
|
|
250
508
|
/**
|
|
251
509
|
* The part of `filename` below the project `root`, with a leading `/` (`/sparta/ui/...`), or `undefined` for files
|
|
@@ -267,9 +525,15 @@ function classify(filename, conventions, root) {
|
|
|
267
525
|
const area = file.match(patterns.area);
|
|
268
526
|
if (area) {
|
|
269
527
|
const [, areaName, group, folder] = area;
|
|
528
|
+
const areaLayer = areaLayerFolder(file, conventions);
|
|
529
|
+
if (areaLayer) return {
|
|
530
|
+
kind: "area-layer",
|
|
531
|
+
area: conventions.areas.get(areaName),
|
|
532
|
+
areaFolder: areaName,
|
|
533
|
+
layer: layerOf(areaLayer.standard)
|
|
534
|
+
};
|
|
270
535
|
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;
|
|
536
|
+
const layer = base ? `base-${base}` : layerOf(standardFolder(conventions, folder));
|
|
273
537
|
return {
|
|
274
538
|
kind: group === "common" ? "area-common" : "group",
|
|
275
539
|
area: conventions.areas.get(areaName),
|
|
@@ -279,7 +543,12 @@ function classify(filename, conventions, root) {
|
|
|
279
543
|
};
|
|
280
544
|
}
|
|
281
545
|
const top = file.match(patterns.top)?.[1];
|
|
282
|
-
|
|
546
|
+
if (top === void 0) return void 0;
|
|
547
|
+
const commonLayer = top === "common" ? commonLayerFolder(file, conventions) : void 0;
|
|
548
|
+
return commonLayer ? {
|
|
549
|
+
kind: "shared",
|
|
550
|
+
layer: layerOf(commonLayer.standard)
|
|
551
|
+
} : { kind: TOP_LEVEL_KINDS[top] ?? "shared" };
|
|
283
552
|
}
|
|
284
553
|
/** Project root per directory, kept for the lifetime of the linter process. */
|
|
285
554
|
const rootCache = /* @__PURE__ */ new Map();
|
|
@@ -308,22 +577,60 @@ function projectRoot(filename) {
|
|
|
308
577
|
for (const dir of visited) rootCache.set(dir, root);
|
|
309
578
|
return root;
|
|
310
579
|
}
|
|
580
|
+
/** The extensions tried, in order, to find the file behind an extension-less import. */
|
|
581
|
+
const MODULE_EXTENSIONS = [
|
|
582
|
+
"",
|
|
583
|
+
".ts",
|
|
584
|
+
".tsx",
|
|
585
|
+
".mts",
|
|
586
|
+
".cts",
|
|
587
|
+
"/index.ts"
|
|
588
|
+
];
|
|
589
|
+
const isFile = (file) => statSync(file, { throwIfNoEntry: false })?.isFile() ?? false;
|
|
590
|
+
/** The TypeScript file an import path points to (`./x` -> `./x.ts`, `./x.js` -> `./x.ts`), if it exists. */
|
|
591
|
+
function moduleFile(base) {
|
|
592
|
+
const stripped = base.replace(/\.(?:m|c)?js$/, "");
|
|
593
|
+
for (const candidate of [base, ...MODULE_EXTENSIONS.map((extension) => `${stripped}${extension}`)]) if (isFile(candidate)) return normalize(candidate);
|
|
594
|
+
}
|
|
595
|
+
/** True when an import path points to an existing file or folder. */
|
|
596
|
+
const moduleExists = (base) => (statSync(base, { throwIfNoEntry: false })?.isDirectory() ?? false) || moduleFile(base) !== void 0;
|
|
311
597
|
/**
|
|
312
|
-
* Resolves
|
|
313
|
-
*
|
|
598
|
+
* Resolves an import to an absolute, normalized path, the way TypeScript would:
|
|
599
|
+
*
|
|
600
|
+
* 1. relative imports, against the importing file;
|
|
601
|
+
* 2. `compilerOptions.paths` of the nearest `tsconfig.json`, taking the first target that exists;
|
|
602
|
+
* 3. the `@sparta/*` and `@tests/*` aliases and `settings.sparta.aliases`, against `root`;
|
|
603
|
+
* 4. the first `paths` target when none exists (yet);
|
|
604
|
+
* 5. `compilerOptions.baseUrl`, only when the file exists, so package imports are not mistaken for project files.
|
|
605
|
+
*
|
|
606
|
+
* Other imports (packages) resolve to `undefined`.
|
|
314
607
|
*/
|
|
315
608
|
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
609
|
if (request.startsWith(".")) return normalize(path.resolve(path.dirname(filename), request));
|
|
610
|
+
const tsconfig = tsconfigFor(filename);
|
|
611
|
+
const mapped = tsconfig === void 0 ? void 0 : matchPaths(request, tsconfig.paths);
|
|
612
|
+
const existing = mapped?.find(moduleExists);
|
|
613
|
+
if (existing !== void 0) return existing;
|
|
614
|
+
for (const [prefix, target] of conventions.aliases) if (request.startsWith(prefix)) return `${root}/${target === "sparta" ? conventions.spartaFolder : conventions.testsFolder}/${request.slice(prefix.length)}`;
|
|
615
|
+
if (mapped !== void 0 && mapped.length > 0) return mapped[0];
|
|
616
|
+
const fromBaseUrl = tsconfig?.baseUrl === void 0 ? void 0 : `${tsconfig.baseUrl}/${request}`;
|
|
617
|
+
return fromBaseUrl !== void 0 && moduleExists(fromBaseUrl) ? fromBaseUrl : void 0;
|
|
318
618
|
}
|
|
319
|
-
/**
|
|
619
|
+
/**
|
|
620
|
+
* The layer folder a file lives in (`steppers`, `pages`, ...), with `sparta/scenarios/` counting as `scenarios`
|
|
621
|
+
* and `sparta/<area>/builders/` and `directors/` as themselves.
|
|
622
|
+
*/
|
|
320
623
|
const layerFolder = (filename, conventions, root) => {
|
|
321
624
|
const file = projectPath(filename, root);
|
|
322
625
|
if (file === void 0) return void 0;
|
|
323
|
-
|
|
626
|
+
const top = file.match(conventions.patterns.top)?.[1];
|
|
627
|
+
if (top === "scenarios") return {
|
|
324
628
|
name: "scenarios",
|
|
325
629
|
standard: "scenarios"
|
|
326
630
|
};
|
|
631
|
+
if (top === "common") return commonLayerFolder(file, conventions);
|
|
632
|
+
const areaLayer = areaLayerFolder(file, conventions);
|
|
633
|
+
if (areaLayer) return areaLayer;
|
|
327
634
|
const folder = file.match(conventions.patterns.layer)?.[1];
|
|
328
635
|
return folder === void 0 ? void 0 : {
|
|
329
636
|
name: folder,
|
|
@@ -332,6 +639,10 @@ const layerFolder = (filename, conventions, root) => {
|
|
|
332
639
|
};
|
|
333
640
|
/** The file name suffix the files in a layer folder must use (`stepper` for `steppers/`), if it has one. */
|
|
334
641
|
const layerSuffix = (folder) => folder?.standard === void 0 ? void 0 : LAYER_FOLDERS[folder.standard].suffix;
|
|
642
|
+
/** The file name without its TypeScript extension: `orders-stepper` for `orders-stepper.ts` or `.mts`. */
|
|
643
|
+
const moduleName = (filename) => path.basename(filename).replace(/\.[cm]?tsx?$/, "");
|
|
644
|
+
/** `.d.ts`, `.d.mts` and `.d.cts` files, which only declare types. */
|
|
645
|
+
const isDeclarationFile = (filename) => /\.d\.[cm]?ts$/.test(filename);
|
|
335
646
|
/** True for files that follow the layer naming convention (layer folders and fixtures). */
|
|
336
647
|
const isLayerFile = (filename, conventions, root) => layerSuffix(layerFolder(filename, conventions, root)) !== void 0 || projectPath(filename, root)?.match(conventions.patterns.top)?.[1] === "fixtures";
|
|
337
648
|
// src/utils/settings.ts
|
|
@@ -365,22 +676,18 @@ const DATABASE_LAYERS = [
|
|
|
365
676
|
"service",
|
|
366
677
|
"stepper"
|
|
367
678
|
];
|
|
368
|
-
/** The UI layers; `view` is the area's name for a page (`page` for web, `screen` for mobile). */
|
|
679
|
+
/** The UI layers; `view` is the area's name for a page (`page` for web, `screen` for mobile, `window` for desktop). */
|
|
369
680
|
const uiLayers = (view) => ({
|
|
370
681
|
component: /* @__PURE__ */ new Set([
|
|
371
682
|
view,
|
|
372
683
|
"stepper",
|
|
373
684
|
"asserter",
|
|
374
|
-
"scenario"
|
|
375
|
-
"builder",
|
|
376
|
-
"director"
|
|
685
|
+
"scenario"
|
|
377
686
|
]),
|
|
378
687
|
[view]: /* @__PURE__ */ new Set([
|
|
379
688
|
"stepper",
|
|
380
689
|
"asserter",
|
|
381
|
-
"scenario"
|
|
382
|
-
"builder",
|
|
383
|
-
"director"
|
|
690
|
+
"scenario"
|
|
384
691
|
]),
|
|
385
692
|
stepper: /* @__PURE__ */ new Set(["asserter", "scenario"]),
|
|
386
693
|
asserter: /* @__PURE__ */ new Set([
|
|
@@ -388,23 +695,9 @@ const uiLayers = (view) => ({
|
|
|
388
695
|
"asserter",
|
|
389
696
|
"scenario"
|
|
390
697
|
]),
|
|
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
698
|
scenario: /* @__PURE__ */ new Set([view, "component"])
|
|
406
699
|
});
|
|
407
|
-
/** Forbidden target layers inside the same area (API -> API, UI -> UI, database -> database,
|
|
700
|
+
/** Forbidden target layers inside the same area (API -> API, UI -> UI, database -> database, and so on). */
|
|
408
701
|
const FORBIDDEN_LAYERS = {
|
|
409
702
|
api: {
|
|
410
703
|
client: /* @__PURE__ */ new Set([
|
|
@@ -507,9 +800,21 @@ const FORBIDDEN_LAYERS = {
|
|
|
507
800
|
"base-repository"
|
|
508
801
|
])
|
|
509
802
|
},
|
|
803
|
+
desktop: uiLayers("window"),
|
|
510
804
|
mobile: uiLayers("screen"),
|
|
511
805
|
ui: uiLayers("page")
|
|
512
806
|
};
|
|
807
|
+
/** The layers of the `builders/` and `directors/` folders, wherever they live. */
|
|
808
|
+
const BUILDER_LAYERS = /* @__PURE__ */ new Set(["builder", "director"]);
|
|
809
|
+
/** The only layers, besides directors, that may use builders and directors. */
|
|
810
|
+
const BUILDER_USERS = /* @__PURE__ */ new Set([
|
|
811
|
+
"scenario",
|
|
812
|
+
"stepper",
|
|
813
|
+
"asserter",
|
|
814
|
+
"service"
|
|
815
|
+
]);
|
|
816
|
+
/** Whether a file is a builder or director: in a group, an area's `common/`, an area, or `sparta/common/`. */
|
|
817
|
+
const isBuilderLayer = (info) => info.layer !== void 0 && BUILDER_LAYERS.has(info.layer);
|
|
513
818
|
const DATABASE_ACCESS = [
|
|
514
819
|
"repository",
|
|
515
820
|
"base-repository",
|
|
@@ -528,6 +833,7 @@ const FORBIDDEN_FOR_ORCHESTRATION = {
|
|
|
528
833
|
"base-service"
|
|
529
834
|
]),
|
|
530
835
|
database: new Set(DATABASE_ACCESS),
|
|
836
|
+
desktop: /* @__PURE__ */ new Set(["window", "component"]),
|
|
531
837
|
mobile: /* @__PURE__ */ new Set(["screen", "component"]),
|
|
532
838
|
ui: /* @__PURE__ */ new Set(["page", "component"])
|
|
533
839
|
},
|
|
@@ -545,6 +851,12 @@ const FORBIDDEN_FOR_ORCHESTRATION = {
|
|
|
545
851
|
"stepper",
|
|
546
852
|
"asserter"
|
|
547
853
|
]),
|
|
854
|
+
desktop: /* @__PURE__ */ new Set([
|
|
855
|
+
"window",
|
|
856
|
+
"component",
|
|
857
|
+
"stepper",
|
|
858
|
+
"asserter"
|
|
859
|
+
]),
|
|
548
860
|
mobile: /* @__PURE__ */ new Set([
|
|
549
861
|
"screen",
|
|
550
862
|
"component",
|
|
@@ -561,32 +873,45 @@ const FORBIDDEN_FOR_ORCHESTRATION = {
|
|
|
561
873
|
};
|
|
562
874
|
const REEXPORT_LAYERS = /* @__PURE__ */ new Set([
|
|
563
875
|
"asserter",
|
|
876
|
+
"builder",
|
|
564
877
|
"client",
|
|
565
878
|
"base-client",
|
|
566
879
|
"base-repository",
|
|
567
880
|
"component",
|
|
881
|
+
"director",
|
|
568
882
|
"page",
|
|
569
883
|
"repository",
|
|
570
884
|
"scenario",
|
|
571
885
|
"screen",
|
|
572
886
|
"service",
|
|
573
887
|
"base-service",
|
|
574
|
-
"stepper"
|
|
888
|
+
"stepper",
|
|
889
|
+
"window"
|
|
575
890
|
]);
|
|
576
891
|
const describe = (info, { spartaFolder, testsFolder }) => ({
|
|
577
|
-
group: `${info.areaFolder}
|
|
892
|
+
group: `${info.areaFolder}${info.group === void 0 ? "" : `/${info.group}`}${info.layer ? ` ${info.layer}` : ""}`,
|
|
578
893
|
"area-common": `${info.areaFolder}/common`,
|
|
894
|
+
"area-layer": `${info.areaFolder} ${info.layer}`,
|
|
579
895
|
"top-scenario": `${spartaFolder}/scenarios`,
|
|
580
896
|
fixture: `${spartaFolder}/fixtures`,
|
|
581
|
-
shared: `${spartaFolder}/common|support`,
|
|
897
|
+
shared: info.layer ? `${spartaFolder}/common ${info.layer}` : `${spartaFolder}/common|support`,
|
|
582
898
|
test: testsFolder
|
|
583
899
|
})[info.kind];
|
|
900
|
+
/** Whether code in an area folder (a group, or the area's builders and directors) may not depend on `target`. */
|
|
901
|
+
function areaIsolationViolation(source, target) {
|
|
902
|
+
if (["fixture", "top-scenario"].includes(target.kind)) return true;
|
|
903
|
+
if (target.areaFolder === void 0) return false;
|
|
904
|
+
if (target.areaFolder !== source.areaFolder) return true;
|
|
905
|
+
if (target.kind !== "group") return false;
|
|
906
|
+
return source.kind === "area-layer" ? target.layer !== "service" : target.group !== source.group;
|
|
907
|
+
}
|
|
584
908
|
/** Isolation compares area folders, not area types: `web/` and `ui/` are separate areas even when both follow `ui`. */
|
|
585
909
|
function isolationViolation(source, target) {
|
|
586
910
|
if (target.kind === "test") return source.kind !== "test";
|
|
587
911
|
if (source.kind === "shared") return [
|
|
588
912
|
"group",
|
|
589
913
|
"area-common",
|
|
914
|
+
"area-layer",
|
|
590
915
|
"fixture",
|
|
591
916
|
"top-scenario"
|
|
592
917
|
].includes(target.kind);
|
|
@@ -594,20 +919,32 @@ function isolationViolation(source, target) {
|
|
|
594
919
|
if ([
|
|
595
920
|
"fixture",
|
|
596
921
|
"top-scenario",
|
|
597
|
-
"group"
|
|
922
|
+
"group",
|
|
923
|
+
"area-layer"
|
|
598
924
|
].includes(target.kind)) return true;
|
|
599
925
|
return target.kind === "area-common" && target.areaFolder !== source.areaFolder;
|
|
600
926
|
}
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
927
|
+
return source.kind === "group" || source.kind === "area-layer" ? areaIsolationViolation(source, target) : false;
|
|
928
|
+
}
|
|
929
|
+
/**
|
|
930
|
+
* Whether `source` may not use the builder or director `target`: builders use no builders or directors, directors
|
|
931
|
+
* only use builders, and otherwise only scenarios, steppers, asserters and services may use them. Fixtures,
|
|
932
|
+
* top-level scenarios and tests are left to the other checks. Which builders a file can see is up to isolation.
|
|
933
|
+
*/
|
|
934
|
+
function builderViolation(source, target) {
|
|
935
|
+
if (isBuilderLayer(source)) return source.layer === "builder" || target.layer === "director";
|
|
936
|
+
if ([
|
|
937
|
+
"group",
|
|
938
|
+
"area-common",
|
|
939
|
+
"shared"
|
|
940
|
+
].includes(source.kind)) return source.layer === void 0 || !BUILDER_USERS.has(source.layer);
|
|
608
941
|
}
|
|
609
942
|
function layerViolation(source, target) {
|
|
610
|
-
if (!target.layer
|
|
943
|
+
if (!target.layer) return false;
|
|
944
|
+
const builder = BUILDER_LAYERS.has(target.layer) ? builderViolation(source, target) : void 0;
|
|
945
|
+
if (builder !== void 0) return builder;
|
|
946
|
+
if (isBuilderLayer(source)) return target.layer !== "service";
|
|
947
|
+
if (!target.area) return false;
|
|
611
948
|
if (source.kind === "group" || source.kind === "area-common") {
|
|
612
949
|
if (!source.area || !source.layer) return false;
|
|
613
950
|
return source.areaFolder === target.areaFolder && Boolean(FORBIDDEN_LAYERS[source.area][source.layer]?.has(target.layer));
|
|
@@ -616,7 +953,7 @@ function layerViolation(source, target) {
|
|
|
616
953
|
return false;
|
|
617
954
|
}
|
|
618
955
|
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$
|
|
956
|
+
const rule$13 = {
|
|
620
957
|
meta: {
|
|
621
958
|
type: "problem",
|
|
622
959
|
docs: {
|
|
@@ -627,7 +964,8 @@ const rule$12 = {
|
|
|
627
964
|
messages: {
|
|
628
965
|
isolation: "{{source}} must not depend on {{target}} ({{request}}). Move shared code to the area's common/ folder or the top-level common/ folder.",
|
|
629
966
|
layer: "{{source}} must not depend on {{target}} ({{request}}).",
|
|
630
|
-
reexport: "Do not re-export {{target}} outside its layer; this bypasses the architecture boundaries."
|
|
967
|
+
reexport: "Do not re-export {{target}} outside its layer; this bypasses the architecture boundaries.",
|
|
968
|
+
staticService: "{{source}} is a static abstract class and must not depend on {{target}} ({{request}}); only a normal class may use services."
|
|
631
969
|
}
|
|
632
970
|
},
|
|
633
971
|
create(context) {
|
|
@@ -636,11 +974,15 @@ const rule$12 = {
|
|
|
636
974
|
const root = rootFor(context, conventions);
|
|
637
975
|
const source = classify(filename, conventions, root);
|
|
638
976
|
if (!source) return {};
|
|
977
|
+
/** Whether the file declares a normal (instantiable) class. Set on `Program`, before any import is visited. */
|
|
978
|
+
let isNormalClass = false;
|
|
979
|
+
/** An `index.ts` barrel in `builders/` or `directors/` may import and re-export its own layer. */
|
|
980
|
+
const isOwnBarrel = (target) => isBuilderLayer(source) && moduleName(filename) === "index" && target.kind === source.kind && target.areaFolder === source.areaFolder && target.group === source.group && target.layer === source.layer;
|
|
639
981
|
const check = (node, request, reexport = false) => {
|
|
640
982
|
if (request === void 0) return;
|
|
641
983
|
const resolved = resolveImport(filename, request, root, conventions);
|
|
642
984
|
const target = resolved ? classify(resolved, conventions, root) : void 0;
|
|
643
|
-
if (!target) return;
|
|
985
|
+
if (!target || isOwnBarrel(target)) return;
|
|
644
986
|
const data = {
|
|
645
987
|
source: describe(source, conventions),
|
|
646
988
|
target: describe(target, conventions),
|
|
@@ -656,6 +998,11 @@ const rule$12 = {
|
|
|
656
998
|
messageId: "layer",
|
|
657
999
|
data
|
|
658
1000
|
});
|
|
1001
|
+
else if (isBuilderLayer(source) && target.layer === "service" && !isNormalClass) context.report({
|
|
1002
|
+
node,
|
|
1003
|
+
messageId: "staticService",
|
|
1004
|
+
data
|
|
1005
|
+
});
|
|
659
1006
|
else if (reexport && isForbiddenReexport(source, target)) context.report({
|
|
660
1007
|
node,
|
|
661
1008
|
messageId: "reexport",
|
|
@@ -663,15 +1010,19 @@ const rule$12 = {
|
|
|
663
1010
|
});
|
|
664
1011
|
};
|
|
665
1012
|
return {
|
|
1013
|
+
Program(node) {
|
|
1014
|
+
isNormalClass = topLevelClasses(node).some((declaration) => !isStaticClass(declaration));
|
|
1015
|
+
},
|
|
666
1016
|
ImportDeclaration: (node) => check(node, stringValue(node.source)),
|
|
667
1017
|
ExportNamedDeclaration: (node) => check(node, stringValue(node.source), true),
|
|
668
1018
|
ExportAllDeclaration: (node) => check(node, stringValue(node.source), true),
|
|
669
|
-
ImportExpression: (node) => check(node,
|
|
1019
|
+
ImportExpression: (node) => check(node, staticString(node.source)),
|
|
1020
|
+
TSImportType: (node) => check(node, stringValue(node.source)),
|
|
670
1021
|
TSImportEqualsDeclaration(node) {
|
|
671
1022
|
if (node.moduleReference.type === "TSExternalModuleReference") check(node, stringValue(node.moduleReference.expression));
|
|
672
1023
|
},
|
|
673
1024
|
CallExpression(node) {
|
|
674
|
-
if (node.callee.type === "Identifier" && node.callee.name === "require") check(node,
|
|
1025
|
+
if (node.callee.type === "Identifier" && node.callee.name === "require") check(node, staticString(node.arguments[0]));
|
|
675
1026
|
}
|
|
676
1027
|
};
|
|
677
1028
|
}
|
|
@@ -682,7 +1033,7 @@ const classOf = (statement) => {
|
|
|
682
1033
|
const node = (statement.type === "ExportNamedDeclaration" || statement.type === "ExportDefaultDeclaration") && statement.declaration ? statement.declaration : statement;
|
|
683
1034
|
return node.type === "ClassDeclaration" ? node : void 0;
|
|
684
1035
|
};
|
|
685
|
-
const rule$
|
|
1036
|
+
const rule$12 = {
|
|
686
1037
|
meta: {
|
|
687
1038
|
type: "suggestion",
|
|
688
1039
|
docs: {
|
|
@@ -692,15 +1043,29 @@ const rule$11 = {
|
|
|
692
1043
|
schema: [],
|
|
693
1044
|
messages: {
|
|
694
1045
|
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."
|
|
1046
|
+
single: "A layer file contains exactly one class; move '{{found}}' to its own file.",
|
|
1047
|
+
missing: "A layer file contains exactly one class; expected a class named '{{expected}}'."
|
|
696
1048
|
}
|
|
697
1049
|
},
|
|
698
1050
|
create(context) {
|
|
699
1051
|
const conventions = conventionsFor(context);
|
|
700
|
-
|
|
701
|
-
const
|
|
1052
|
+
const root = rootFor(context, conventions);
|
|
1053
|
+
const { filename } = context;
|
|
1054
|
+
if (isDeclarationFile(filename) || !isLayerFile(filename, conventions, root)) return {};
|
|
1055
|
+
const module = moduleName(filename);
|
|
1056
|
+
const expected = toPascalCase(module);
|
|
1057
|
+
const requiresClass = module !== "index" && layerSuffix(layerFolder(filename, conventions, root)) !== void 0;
|
|
702
1058
|
return { Program(program) {
|
|
703
|
-
program.body.map(classOf).filter((node) => node !== void 0)
|
|
1059
|
+
const classes = program.body.map(classOf).filter((node) => node !== void 0);
|
|
1060
|
+
if (classes.length === 0 && requiresClass) {
|
|
1061
|
+
context.report({
|
|
1062
|
+
node: program,
|
|
1063
|
+
messageId: "missing",
|
|
1064
|
+
data: { expected }
|
|
1065
|
+
});
|
|
1066
|
+
return;
|
|
1067
|
+
}
|
|
1068
|
+
classes.forEach((node, index) => {
|
|
704
1069
|
const found = node.id?.name ?? "(anonymous)";
|
|
705
1070
|
if (index > 0) context.report({
|
|
706
1071
|
node: node.id ?? node,
|
|
@@ -719,6 +1084,67 @@ const rule$11 = {
|
|
|
719
1084
|
} };
|
|
720
1085
|
}
|
|
721
1086
|
};
|
|
1087
|
+
// src/rules/director-composes-builders.ts
|
|
1088
|
+
const rule$11 = {
|
|
1089
|
+
meta: {
|
|
1090
|
+
type: "suggestion",
|
|
1091
|
+
docs: {
|
|
1092
|
+
description: "Require a director to compose several builders; a director using a single builder belongs on that builder.",
|
|
1093
|
+
url: docsUrl("director-composes-builders")
|
|
1094
|
+
},
|
|
1095
|
+
schema: [],
|
|
1096
|
+
messages: { singleBuilder: "This director only uses '{{builder}}'; make it a custom method on '{{builder}}'. A director composes several builders." }
|
|
1097
|
+
},
|
|
1098
|
+
create(context) {
|
|
1099
|
+
const filename = context.filename;
|
|
1100
|
+
const conventions = conventionsFor(context);
|
|
1101
|
+
const root = rootFor(context, conventions);
|
|
1102
|
+
if (classify(filename, conventions, root)?.layer !== "director" || moduleName(filename) === "index") return {};
|
|
1103
|
+
const isBuilder = (request) => {
|
|
1104
|
+
if (request === void 0) return false;
|
|
1105
|
+
const resolved = resolveImport(filename, request, root, conventions);
|
|
1106
|
+
return resolved !== void 0 && classify(resolved, conventions, root)?.layer === "builder";
|
|
1107
|
+
};
|
|
1108
|
+
/** The builders the director uses, by imported name, with the import to report. */
|
|
1109
|
+
const builders = /* @__PURE__ */ new Map();
|
|
1110
|
+
/** Set when a builder is imported in a way that does not tell how many builders are used. */
|
|
1111
|
+
let unknown = false;
|
|
1112
|
+
return {
|
|
1113
|
+
ImportDeclaration(node) {
|
|
1114
|
+
if (node.importKind === "type" || typeof node.source.value !== "string" || !isBuilder(node.source.value)) return;
|
|
1115
|
+
for (const specifier of node.specifiers) if (specifier.type === "ImportNamespaceSpecifier") unknown = true;
|
|
1116
|
+
else if (specifier.type === "ImportDefaultSpecifier") builders.set(specifier.local.name, {
|
|
1117
|
+
local: specifier.local.name,
|
|
1118
|
+
node
|
|
1119
|
+
});
|
|
1120
|
+
else if (specifier.importKind !== "type") {
|
|
1121
|
+
const imported = specifier.imported.type === "Identifier" ? specifier.imported.name : String(specifier.imported.value);
|
|
1122
|
+
builders.set(imported, {
|
|
1123
|
+
local: specifier.local.name,
|
|
1124
|
+
node
|
|
1125
|
+
});
|
|
1126
|
+
}
|
|
1127
|
+
},
|
|
1128
|
+
ImportExpression(node) {
|
|
1129
|
+
if (isBuilder(staticString(node.source))) unknown = true;
|
|
1130
|
+
},
|
|
1131
|
+
CallExpression(node) {
|
|
1132
|
+
if (node.callee.type === "Identifier" && node.callee.name === "require") {
|
|
1133
|
+
if (isBuilder(staticString(node.arguments[0]))) unknown = true;
|
|
1134
|
+
}
|
|
1135
|
+
},
|
|
1136
|
+
"Program:exit"() {
|
|
1137
|
+
const [only] = builders.values();
|
|
1138
|
+
if (unknown || builders.size !== 1 || only === void 0) return;
|
|
1139
|
+
context.report({
|
|
1140
|
+
node: only.node,
|
|
1141
|
+
messageId: "singleBuilder",
|
|
1142
|
+
data: { builder: only.local }
|
|
1143
|
+
});
|
|
1144
|
+
}
|
|
1145
|
+
};
|
|
1146
|
+
}
|
|
1147
|
+
};
|
|
722
1148
|
// src/rules/expect-from-support.ts
|
|
723
1149
|
/** Files inside the sparta folder, except its support folder (where `expect` itself is defined). */
|
|
724
1150
|
const appliesTo = (filename, { patterns }, root) => {
|
|
@@ -739,10 +1165,21 @@ const rule$10 = {
|
|
|
739
1165
|
create(context) {
|
|
740
1166
|
const conventions = conventionsFor(context);
|
|
741
1167
|
const { supportModule } = conventions;
|
|
742
|
-
|
|
1168
|
+
const root = rootFor(context, conventions);
|
|
1169
|
+
if (!appliesTo(context.filename, conventions, root)) return {};
|
|
1170
|
+
/**
|
|
1171
|
+
* The support module, a subpath of it, or a relative import into the support folder. Other aliases for that
|
|
1172
|
+
* folder are reported, so the configured module name stays the one that is used.
|
|
1173
|
+
*/
|
|
1174
|
+
const isSupport = (source) => {
|
|
1175
|
+
if (source === supportModule || source.startsWith(`${supportModule}/`)) return true;
|
|
1176
|
+
if (!source.startsWith(".")) return false;
|
|
1177
|
+
const resolved = resolveImport(context.filename, source, root, conventions);
|
|
1178
|
+
return (resolved === void 0 ? void 0 : projectPath(resolved, root))?.match(conventions.patterns.top)?.[1] === "support";
|
|
1179
|
+
};
|
|
743
1180
|
return { ImportDeclaration(node) {
|
|
744
1181
|
const source = node.source.value;
|
|
745
|
-
if (source
|
|
1182
|
+
if (isSupport(String(source))) return;
|
|
746
1183
|
const specifier = node.specifiers.find((item) => item.type === "ImportSpecifier" && item.imported.type === "Identifier" && item.imported.name === "expect");
|
|
747
1184
|
if (specifier === void 0) return;
|
|
748
1185
|
const data = {
|
|
@@ -767,24 +1204,35 @@ const rule$10 = {
|
|
|
767
1204
|
}
|
|
768
1205
|
};
|
|
769
1206
|
// src/rules/file-naming.ts
|
|
1207
|
+
/** Lowercase words of letters and digits, joined by single hyphens: `orders-v2-stepper`. */
|
|
1208
|
+
const KEBAB_CASE = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
1209
|
+
/** `myOrders`, `My_APIOrders` and `checkout--flow` become `my-orders`, `my-api-orders` and `checkout-flow`. */
|
|
1210
|
+
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
1211
|
const rule$9 = {
|
|
771
1212
|
meta: {
|
|
772
1213
|
type: "suggestion",
|
|
773
1214
|
docs: {
|
|
774
|
-
description: "Require files in a layer folder to end with the layer suffix (e.g.
|
|
1215
|
+
description: "Require files in a layer folder to be kebab-case and end with the layer suffix (e.g. `orders-stepper.ts`).",
|
|
775
1216
|
url: docsUrl("file-naming")
|
|
776
1217
|
},
|
|
777
1218
|
schema: [],
|
|
778
|
-
messages: {
|
|
1219
|
+
messages: {
|
|
1220
|
+
suffix: "Files in '{{folder}}/' must be named '*-{{suffix}}.ts' (found '{{name}}').",
|
|
1221
|
+
kebabCase: "File name '{{name}}' must be kebab-case: '{{expected}}'."
|
|
1222
|
+
}
|
|
779
1223
|
},
|
|
780
1224
|
create(context) {
|
|
781
1225
|
const conventions = conventionsFor(context);
|
|
782
1226
|
const folder = layerFolder(context.filename, conventions, rootFor(context, conventions));
|
|
783
1227
|
const suffix = layerSuffix(folder);
|
|
784
1228
|
const name = path.basename(context.filename);
|
|
785
|
-
|
|
1229
|
+
const module = moduleName(context.filename);
|
|
1230
|
+
if (folder === void 0 || suffix === void 0 || isDeclarationFile(name) || module === "index") return {};
|
|
1231
|
+
const hasSuffix = module.endsWith(`-${suffix}`);
|
|
1232
|
+
const isKebabCase = KEBAB_CASE.test(module);
|
|
1233
|
+
if (hasSuffix && isKebabCase) return {};
|
|
786
1234
|
return { Program(node) {
|
|
787
|
-
context.report({
|
|
1235
|
+
if (!hasSuffix) context.report({
|
|
788
1236
|
node,
|
|
789
1237
|
messageId: "suffix",
|
|
790
1238
|
data: {
|
|
@@ -793,30 +1241,61 @@ const rule$9 = {
|
|
|
793
1241
|
name
|
|
794
1242
|
}
|
|
795
1243
|
});
|
|
1244
|
+
if (!isKebabCase) {
|
|
1245
|
+
const expected = `${toKebabCase(module)}${name.slice(module.length)}`;
|
|
1246
|
+
context.report({
|
|
1247
|
+
node,
|
|
1248
|
+
messageId: "kebabCase",
|
|
1249
|
+
data: {
|
|
1250
|
+
name,
|
|
1251
|
+
expected
|
|
1252
|
+
}
|
|
1253
|
+
});
|
|
1254
|
+
}
|
|
796
1255
|
} };
|
|
797
1256
|
}
|
|
798
1257
|
};
|
|
799
1258
|
// src/rules/lines-between-class-members.ts
|
|
800
1259
|
const BLANK_LINE$1 = /\r?\n[\t ]*\r?\n/;
|
|
1260
|
+
const BLANK_LINES$1 = /(\r?\n)(?:[\t ]*\r?\n)+/g;
|
|
1261
|
+
/** An overload signature (or abstract method): a method without a body. */
|
|
1262
|
+
const isSignature = (member) => isMethod(member) && member.value.body === null;
|
|
801
1263
|
const rule$8 = {
|
|
802
1264
|
meta: {
|
|
803
1265
|
type: "layout",
|
|
804
1266
|
docs: {
|
|
805
|
-
description: "Require a blank line after every multi-line class member.",
|
|
1267
|
+
description: "Require a blank line after every multi-line method-like class member, and disallow blank lines between method overloads.",
|
|
806
1268
|
url: docsUrl("lines-between-class-members")
|
|
807
1269
|
},
|
|
808
1270
|
fixable: "whitespace",
|
|
809
1271
|
schema: [],
|
|
810
|
-
messages: {
|
|
1272
|
+
messages: {
|
|
1273
|
+
expectedBlankLine: "Expected a blank line between class members.",
|
|
1274
|
+
unexpectedBlankLine: "Unexpected blank line between overloads of the same method."
|
|
1275
|
+
}
|
|
811
1276
|
},
|
|
812
1277
|
create(context) {
|
|
813
|
-
const {
|
|
1278
|
+
const { sourceCode } = context;
|
|
1279
|
+
const { text } = sourceCode;
|
|
1280
|
+
const keyOf = (method) => `${method.static ? "static " : ""}${method.computed ? "[]" : ""}${sourceCode.getText(method.key)}`;
|
|
1281
|
+
/** `current` continues the overload group that `previous` (a bodyless signature) belongs to. */
|
|
1282
|
+
const isOverloadOf = (previous, current) => isSignature(previous) && isMethod(current) && current.kind === previous.kind && keyOf(current) === keyOf(previous);
|
|
814
1283
|
return { ClassBody(node) {
|
|
815
1284
|
for (let index = 1; index < node.body.length; index++) {
|
|
816
1285
|
const previous = node.body[index - 1];
|
|
817
1286
|
const current = node.body[index];
|
|
818
|
-
if (previous.loc.start.line === previous.loc.end.line) continue;
|
|
819
1287
|
const textBetween = text.slice(previous.range[1], current.range[0]);
|
|
1288
|
+
if (isOverloadOf(previous, current)) {
|
|
1289
|
+
if (!BLANK_LINE$1.test(textBetween)) continue;
|
|
1290
|
+
context.report({
|
|
1291
|
+
node: current,
|
|
1292
|
+
messageId: "unexpectedBlankLine",
|
|
1293
|
+
fix: (fixer) => fixer.replaceTextRange([previous.range[1], current.range[0]], textBetween.replace(BLANK_LINES$1, "$1"))
|
|
1294
|
+
});
|
|
1295
|
+
continue;
|
|
1296
|
+
}
|
|
1297
|
+
if (previous.loc.start.line === previous.loc.end.line) continue;
|
|
1298
|
+
if (isPlainProperty(previous)) continue;
|
|
820
1299
|
if (BLANK_LINE$1.test(textBetween)) continue;
|
|
821
1300
|
context.report({
|
|
822
1301
|
node: current,
|
|
@@ -831,9 +1310,154 @@ const rule$8 = {
|
|
|
831
1310
|
} };
|
|
832
1311
|
}
|
|
833
1312
|
};
|
|
834
|
-
// src/rules/
|
|
835
|
-
const
|
|
1313
|
+
// src/rules/member-ordering.ts
|
|
1314
|
+
const KINDS = [
|
|
1315
|
+
"property",
|
|
1316
|
+
"constructor",
|
|
1317
|
+
"method"
|
|
1318
|
+
];
|
|
1319
|
+
const ACCESSES = [
|
|
1320
|
+
"public",
|
|
1321
|
+
"protected",
|
|
1322
|
+
"private"
|
|
1323
|
+
];
|
|
1324
|
+
/** Properties (incl. index signatures) and methods; a property holding a function or class counts as a method. */
|
|
1325
|
+
const kindOf = (member) => {
|
|
1326
|
+
if (isMethod(member)) return member.kind === "constructor" ? "constructor" : "method";
|
|
1327
|
+
if (member.type === "StaticBlock") return void 0;
|
|
1328
|
+
return isPlainProperty(member) ? "property" : "method";
|
|
1329
|
+
};
|
|
1330
|
+
const accessOf = (member) => {
|
|
1331
|
+
if (isPrivate(member)) return "private";
|
|
1332
|
+
if ("accessibility" in member && member.accessibility === "protected") return "protected";
|
|
1333
|
+
return "public";
|
|
1334
|
+
};
|
|
1335
|
+
/**
|
|
1336
|
+
* The required order: static properties, instance properties, constructor, static methods, instance methods,
|
|
1337
|
+
* each from public to protected to private.
|
|
1338
|
+
*/
|
|
1339
|
+
const groupOf = (member) => {
|
|
1340
|
+
const kind = kindOf(member);
|
|
1341
|
+
if (kind === void 0) return void 0;
|
|
1342
|
+
if (kind === "constructor") return {
|
|
1343
|
+
rank: KINDS.indexOf(kind) * 6,
|
|
1344
|
+
label: "constructor"
|
|
1345
|
+
};
|
|
1346
|
+
const isStatic = "static" in member && member.static;
|
|
1347
|
+
const access = accessOf(member);
|
|
1348
|
+
return {
|
|
1349
|
+
rank: KINDS.indexOf(kind) * 6 + (isStatic ? 0 : 3) + ACCESSES.indexOf(access),
|
|
1350
|
+
label: `${isStatic ? "static " : ""}${access} ${kind}`
|
|
1351
|
+
};
|
|
1352
|
+
};
|
|
1353
|
+
const SKIPPED_KEYS$1 = /* @__PURE__ */ new Set([
|
|
1354
|
+
"parent",
|
|
1355
|
+
"loc",
|
|
1356
|
+
"range",
|
|
1357
|
+
"start",
|
|
1358
|
+
"end",
|
|
1359
|
+
"typeAnnotation"
|
|
1360
|
+
]);
|
|
1361
|
+
const isNode$1 = (value) => typeof value === "object" && value !== null && typeof value.type === "string";
|
|
1362
|
+
const FUNCTIONS = /* @__PURE__ */ new Set([
|
|
1363
|
+
"ArrowFunctionExpression",
|
|
1364
|
+
"FunctionExpression",
|
|
1365
|
+
"ClassExpression"
|
|
1366
|
+
]);
|
|
1367
|
+
/** `x` for `this.x`, `this.#x` and `ClassName.x`. */
|
|
1368
|
+
const ownPropertyRead = (node, className) => {
|
|
1369
|
+
if (node.type !== "MemberExpression" || node.computed) return void 0;
|
|
1370
|
+
const { object, property } = node;
|
|
1371
|
+
return (object.type === "ThisExpression" || className !== void 0 && object.type === "Identifier" && object.name === className) && (property.type === "Identifier" || property.type === "PrivateIdentifier") ? property.name : void 0;
|
|
1372
|
+
};
|
|
1373
|
+
/**
|
|
1374
|
+
* The property names an initializer reads while it runs, as `this.x`, `this.#x` or `ClassName.x`. Code inside
|
|
1375
|
+
* functions is left out: it runs later, when every property is initialized.
|
|
1376
|
+
*/
|
|
1377
|
+
function readsDuringInit(value, className) {
|
|
1378
|
+
const names = /* @__PURE__ */ new Set();
|
|
1379
|
+
const visit = (node) => {
|
|
1380
|
+
if (FUNCTIONS.has(node.type)) return;
|
|
1381
|
+
const read = ownPropertyRead(node, className);
|
|
1382
|
+
if (read !== void 0) names.add(read);
|
|
1383
|
+
for (const [key, child] of Object.entries(node)) {
|
|
1384
|
+
if (SKIPPED_KEYS$1.has(key)) continue;
|
|
1385
|
+
if (Array.isArray(child)) {
|
|
1386
|
+
for (const item of child) if (isNode$1(item)) visit(item);
|
|
1387
|
+
} else if (isNode$1(child)) visit(child);
|
|
1388
|
+
}
|
|
1389
|
+
};
|
|
1390
|
+
visit(value);
|
|
1391
|
+
return names;
|
|
1392
|
+
}
|
|
1393
|
+
/**
|
|
1394
|
+
* Initializers run in source order, so a property that reads a property declared before it must stay below it,
|
|
1395
|
+
* even when the order of the groups says otherwise.
|
|
1396
|
+
*/
|
|
1397
|
+
const dependsOnEarlier = (member, earlier, className) => {
|
|
1398
|
+
if (member.type !== "PropertyDefinition" && member.type !== "AccessorProperty") return false;
|
|
1399
|
+
if (!member.value) return false;
|
|
1400
|
+
return [...readsDuringInit(member.value, className)].some((name) => earlier.has(name));
|
|
1401
|
+
};
|
|
836
1402
|
const rule$7 = {
|
|
1403
|
+
meta: {
|
|
1404
|
+
type: "suggestion",
|
|
1405
|
+
docs: {
|
|
1406
|
+
description: "Require class members in a fixed order: properties, constructor, methods; static before instance; public before protected before private.",
|
|
1407
|
+
url: docsUrl("member-ordering")
|
|
1408
|
+
},
|
|
1409
|
+
schema: [],
|
|
1410
|
+
messages: { memberOrdering: "'{{name}}' ({{group}}) must come before '{{otherName}}' ({{otherGroup}})." }
|
|
1411
|
+
},
|
|
1412
|
+
create(context) {
|
|
1413
|
+
return { ClassBody(node) {
|
|
1414
|
+
let highest;
|
|
1415
|
+
const { parent } = node;
|
|
1416
|
+
const className = (parent.type === "ClassDeclaration" || parent.type === "ClassExpression") && parent.id ? parent.id.name : void 0;
|
|
1417
|
+
/** Names of the properties declared so far. */
|
|
1418
|
+
const earlier = /* @__PURE__ */ new Set();
|
|
1419
|
+
for (const member of node.body) {
|
|
1420
|
+
const group = groupOf(member);
|
|
1421
|
+
const dependent = dependsOnEarlier(member, earlier, className);
|
|
1422
|
+
if (isPlainProperty(member) && member.type !== "TSIndexSignature") earlier.add(memberName$1(member));
|
|
1423
|
+
if (group === void 0) continue;
|
|
1424
|
+
if (highest === void 0 || group.rank >= highest.group.rank) {
|
|
1425
|
+
highest = {
|
|
1426
|
+
member,
|
|
1427
|
+
group
|
|
1428
|
+
};
|
|
1429
|
+
continue;
|
|
1430
|
+
}
|
|
1431
|
+
if (dependent) continue;
|
|
1432
|
+
context.report({
|
|
1433
|
+
node: member,
|
|
1434
|
+
messageId: "memberOrdering",
|
|
1435
|
+
data: {
|
|
1436
|
+
name: memberName$1(member),
|
|
1437
|
+
group: group.label,
|
|
1438
|
+
otherName: memberName$1(highest.member),
|
|
1439
|
+
otherGroup: highest.group.label
|
|
1440
|
+
}
|
|
1441
|
+
});
|
|
1442
|
+
}
|
|
1443
|
+
} };
|
|
1444
|
+
}
|
|
1445
|
+
};
|
|
1446
|
+
// src/rules/no-direct-construction-in-tests.ts
|
|
1447
|
+
/** Names of SPARTA layer classes, for classes whose import does not tell their layer. */
|
|
1448
|
+
const LAYER_CLASS = /(Stepper|Asserter|Scenario|Service|Client|Repository|Page|Screen|Window|Component|Builder|Director|Factory|Provider|Container|Database)$/;
|
|
1449
|
+
/** The class a `new` expression constructs: `X` for `new X()`, `ns` and `X` for `new ns.X()`. */
|
|
1450
|
+
const constructed = (callee) => {
|
|
1451
|
+
if (callee.type === "Identifier") return {
|
|
1452
|
+
binding: callee.name,
|
|
1453
|
+
name: callee.name
|
|
1454
|
+
};
|
|
1455
|
+
if (callee.type === "MemberExpression" && !callee.computed && callee.object.type === "Identifier" && callee.property.type === "Identifier") return {
|
|
1456
|
+
binding: callee.object.name,
|
|
1457
|
+
name: callee.property.name
|
|
1458
|
+
};
|
|
1459
|
+
};
|
|
1460
|
+
const rule$6 = {
|
|
837
1461
|
meta: {
|
|
838
1462
|
type: "problem",
|
|
839
1463
|
docs: {
|
|
@@ -845,15 +1469,36 @@ const rule$7 = {
|
|
|
845
1469
|
},
|
|
846
1470
|
create(context) {
|
|
847
1471
|
const conventions = conventionsFor(context);
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
1472
|
+
const root = rootFor(context, conventions);
|
|
1473
|
+
if (classify(context.filename, conventions, root)?.kind !== "test") return {};
|
|
1474
|
+
/** Local names of imports (named, default and namespace) to their module. */
|
|
1475
|
+
const imports = /* @__PURE__ */ new Map();
|
|
1476
|
+
/**
|
|
1477
|
+
* Whether constructing `name` (bound to the local `binding`) creates a SPARTA layer object. An import decides by
|
|
1478
|
+
* where it points: never for packages, always for layer folders. Otherwise the class name decides.
|
|
1479
|
+
*/
|
|
1480
|
+
const isLayerClass = (binding, name) => {
|
|
1481
|
+
const source = imports.get(binding);
|
|
1482
|
+
if (source === void 0) return LAYER_CLASS.test(name);
|
|
1483
|
+
const resolved = resolveImport(context.filename, source, root, conventions);
|
|
1484
|
+
if (resolved === void 0) return false;
|
|
1485
|
+
const target = classify(resolved, conventions, root);
|
|
1486
|
+
return target?.layer !== void 0 || target?.kind === "top-scenario" || layerSuffix(layerFolder(resolved, conventions, root)) !== void 0 || LAYER_CLASS.test(name);
|
|
1487
|
+
};
|
|
1488
|
+
return {
|
|
1489
|
+
ImportDeclaration(node) {
|
|
1490
|
+
if (typeof node.source.value !== "string") return;
|
|
1491
|
+
for (const specifier of node.specifiers) imports.set(specifier.local.name, node.source.value);
|
|
1492
|
+
},
|
|
1493
|
+
NewExpression(node) {
|
|
1494
|
+
const found = constructed(node.callee);
|
|
1495
|
+
if (found && isLayerClass(found.binding, found.name)) context.report({
|
|
1496
|
+
node,
|
|
1497
|
+
messageId: "useFixture",
|
|
1498
|
+
data: { name: context.sourceCode.getText(node.callee) }
|
|
1499
|
+
});
|
|
1500
|
+
}
|
|
1501
|
+
};
|
|
857
1502
|
}
|
|
858
1503
|
};
|
|
859
1504
|
// src/rules/no-expect-in-tests.ts
|
|
@@ -862,7 +1507,7 @@ const rule$7 = {
|
|
|
862
1507
|
* `expected`, `expecting` and the like are left alone. `settings.sparta.expectNames` adds exact names.
|
|
863
1508
|
*/
|
|
864
1509
|
const EXPECT_NAME = /^expect(?:ly)?(?:[A-Z_$\d]|$)/;
|
|
865
|
-
const rule$
|
|
1510
|
+
const rule$5 = {
|
|
866
1511
|
meta: {
|
|
867
1512
|
type: "problem",
|
|
868
1513
|
docs: {
|
|
@@ -925,7 +1570,15 @@ const resolvesPromise = (callback, resolve) => {
|
|
|
925
1570
|
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
1571
|
return call?.type === "CallExpression" && isIdentifier(call.callee, resolve);
|
|
927
1572
|
};
|
|
928
|
-
|
|
1573
|
+
/** The name of an import specifier or object pattern key (`setTimeout`, `"setTimeout"`). */
|
|
1574
|
+
const propertyName = (node) => node.type === "Identifier" ? node.name : stringValue(node);
|
|
1575
|
+
/** `require("timers/promises")` or `require("node:timers/promises")`. */
|
|
1576
|
+
const isTimersRequire = (node) => {
|
|
1577
|
+
if (node?.type !== "CallExpression" || !isIdentifier(node.callee, "require")) return false;
|
|
1578
|
+
const source = staticString(node.arguments[0]);
|
|
1579
|
+
return source !== void 0 && TIMER_MODULES.has(source);
|
|
1580
|
+
};
|
|
1581
|
+
const rule$4 = {
|
|
929
1582
|
meta: {
|
|
930
1583
|
type: "problem",
|
|
931
1584
|
docs: {
|
|
@@ -940,17 +1593,47 @@ const rule$5 = {
|
|
|
940
1593
|
const { patterns } = conventions;
|
|
941
1594
|
const file = projectPath(context.filename, rootFor(context, conventions));
|
|
942
1595
|
if (file === void 0 || !patterns.checked.test(file) || patterns.framework.test(file)) return {};
|
|
1596
|
+
/** Local names bound to the `timers/promises` module (`timers`) and to its `scheduler` (`schedulers`). */
|
|
1597
|
+
const timers = /* @__PURE__ */ new Set();
|
|
1598
|
+
const schedulers = /* @__PURE__ */ new Set();
|
|
1599
|
+
/** Records a named binding from `timers/promises`; `setTimeout` itself is reported where it is bound. */
|
|
1600
|
+
const bindNamed = (imported, local, node) => {
|
|
1601
|
+
if (imported === "setTimeout") context.report({
|
|
1602
|
+
node,
|
|
1603
|
+
messageId: "hardWait"
|
|
1604
|
+
});
|
|
1605
|
+
if (imported === "scheduler" && local?.type === "Identifier") schedulers.add(local.name);
|
|
1606
|
+
};
|
|
1607
|
+
/** `timers.setTimeout(...)`, `scheduler.wait(...)` and `timers.scheduler.wait(...)`. */
|
|
1608
|
+
const isTimersSleep = (callee) => {
|
|
1609
|
+
if (callee.type !== "MemberExpression") return false;
|
|
1610
|
+
const { object, property } = callee;
|
|
1611
|
+
if (isIdentifier(property, "setTimeout")) return object.type === "Identifier" && timers.has(object.name);
|
|
1612
|
+
if (!isIdentifier(property, "wait")) return false;
|
|
1613
|
+
if (object.type === "Identifier") return schedulers.has(object.name);
|
|
1614
|
+
return object.type === "MemberExpression" && object.object.type === "Identifier" && timers.has(object.object.name) && isIdentifier(object.property, "scheduler");
|
|
1615
|
+
};
|
|
943
1616
|
return {
|
|
944
1617
|
ImportDeclaration(node) {
|
|
945
1618
|
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
|
-
|
|
1619
|
+
for (const specifier of node.specifiers) if (specifier.type === "ImportSpecifier") bindNamed(propertyName(specifier.imported) ?? "", specifier.local, specifier);
|
|
1620
|
+
else timers.add(specifier.local.name);
|
|
1621
|
+
},
|
|
1622
|
+
VariableDeclarator(node) {
|
|
1623
|
+
if (!isTimersRequire(node.init)) return;
|
|
1624
|
+
if (node.id.type === "Identifier") {
|
|
1625
|
+
timers.add(node.id.name);
|
|
1626
|
+
return;
|
|
1627
|
+
}
|
|
1628
|
+
if (node.id.type !== "ObjectPattern") return;
|
|
1629
|
+
for (const property of node.id.properties) {
|
|
1630
|
+
if (property.type !== "Property" || property.computed) continue;
|
|
1631
|
+
bindNamed(propertyName(property.key) ?? "", property.value, property);
|
|
1632
|
+
}
|
|
950
1633
|
},
|
|
951
1634
|
CallExpression(node) {
|
|
952
1635
|
const { callee } = node;
|
|
953
|
-
if (callee.type === "MemberExpression" && isIdentifier(callee.property, "waitForTimeout")) {
|
|
1636
|
+
if (callee.type === "MemberExpression" && isIdentifier(callee.property, "waitForTimeout") || isTimersSleep(callee)) {
|
|
954
1637
|
context.report({
|
|
955
1638
|
node,
|
|
956
1639
|
messageId: "hardWait"
|
|
@@ -978,7 +1661,7 @@ const CALLBACK_ARGUMENT_METHODS = /* @__PURE__ */ new Set(["poll", "pollAndRetur
|
|
|
978
1661
|
/** Methods that retry the callback given to the start of their chain (`expect(cb).toPass()`). */
|
|
979
1662
|
const CALLBACK_SUBJECT_METHODS = /* @__PURE__ */ new Set(["toPass", "toPassAndReturn"]);
|
|
980
1663
|
/** The static name of a member access (`a.b`, `a["b"]`), or `undefined` for computed or private access. */
|
|
981
|
-
const memberName
|
|
1664
|
+
const memberName = (node) => {
|
|
982
1665
|
if (node.type !== "MemberExpression") return void 0;
|
|
983
1666
|
const { property } = node;
|
|
984
1667
|
if (!node.computed && property.type === "Identifier") return property.name;
|
|
@@ -987,7 +1670,7 @@ const memberName$1 = (node) => {
|
|
|
987
1670
|
/** The polling method a call invokes (`toPass` for `expect(cb).toPass()`), or `undefined` for any other node. */
|
|
988
1671
|
const pollingMethod = (node, methods) => {
|
|
989
1672
|
if (node.type !== "CallExpression") return void 0;
|
|
990
|
-
const name = memberName
|
|
1673
|
+
const name = memberName(node.callee);
|
|
991
1674
|
return name !== void 0 && methods.has(name) ? name : void 0;
|
|
992
1675
|
};
|
|
993
1676
|
const isFunction$1 = (node) => node?.type === "ArrowFunctionExpression" || node?.type === "FunctionExpression";
|
|
@@ -1029,14 +1712,6 @@ const SKIPPED_KEYS = /* @__PURE__ */ new Set([
|
|
|
1029
1712
|
"returnType",
|
|
1030
1713
|
"decorators"
|
|
1031
1714
|
]);
|
|
1032
|
-
const MODULE_EXTENSIONS = [
|
|
1033
|
-
"",
|
|
1034
|
-
".ts",
|
|
1035
|
-
".tsx",
|
|
1036
|
-
".mts",
|
|
1037
|
-
".cts",
|
|
1038
|
-
"/index.ts"
|
|
1039
|
-
];
|
|
1040
1715
|
const isNode = (value) => typeof value === "object" && value !== null && typeof value.type === "string";
|
|
1041
1716
|
/** Visits `node` and its descendants depth-first. Returning `false` from `visit` skips the node's children. */
|
|
1042
1717
|
function walk(node, visit) {
|
|
@@ -1071,11 +1746,16 @@ function summarizeClass(name, node) {
|
|
|
1071
1746
|
}
|
|
1072
1747
|
return info;
|
|
1073
1748
|
}
|
|
1074
|
-
/** A typed field (`x: X`)
|
|
1749
|
+
/** A typed field (`x: X`), a field created with `new X()`, or a function-valued field, which acts as a method. */
|
|
1075
1750
|
function summarizeField(info, member) {
|
|
1076
1751
|
const field = member.computed ? void 0 : keyName(member.key);
|
|
1752
|
+
if (field === void 0) return;
|
|
1753
|
+
if (isFunctionLike(member.value)) {
|
|
1754
|
+
info.methods.set(field, member.value);
|
|
1755
|
+
return;
|
|
1756
|
+
}
|
|
1077
1757
|
const type = typeName(member.typeAnnotation) ?? newName(member.value);
|
|
1078
|
-
if (
|
|
1758
|
+
if (type !== void 0) info.fields.set(field, type);
|
|
1079
1759
|
}
|
|
1080
1760
|
/** Methods, getters (whose return type acts as a field type) and the constructor. */
|
|
1081
1761
|
function summarizeMethod(info, member) {
|
|
@@ -1100,7 +1780,7 @@ function summarizeConstructor(info, constructor) {
|
|
|
1100
1780
|
walk(constructor.body, (node) => {
|
|
1101
1781
|
if (node.type !== "AssignmentExpression" || node.left.type !== "MemberExpression") return;
|
|
1102
1782
|
if (node.left.object.type !== "ThisExpression") return;
|
|
1103
|
-
const field = memberName
|
|
1783
|
+
const field = memberName(node.left);
|
|
1104
1784
|
const type = newName(node.right);
|
|
1105
1785
|
if (field !== void 0 && type !== void 0 && !info.fields.has(field)) info.fields.set(field, type);
|
|
1106
1786
|
});
|
|
@@ -1172,13 +1852,10 @@ function summarizeNamedExport(summary, statement) {
|
|
|
1172
1852
|
else summary.exports.set(exported, local);
|
|
1173
1853
|
}
|
|
1174
1854
|
}
|
|
1175
|
-
|
|
1176
|
-
/** Resolves a relative or aliased import to a TypeScript file. Package imports resolve to `undefined`. */
|
|
1855
|
+
/** Resolves a relative, aliased or tsconfig-mapped import to a TypeScript file. Package imports resolve to `undefined`. */
|
|
1177
1856
|
function resolveModule(fromFile, request, root, conventions) {
|
|
1178
1857
|
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);
|
|
1858
|
+
return base === void 0 ? void 0 : moduleFile(base);
|
|
1182
1859
|
}
|
|
1183
1860
|
/**
|
|
1184
1861
|
* Summaries of files on disk per project root and conventions (imports resolve against them), kept for the lifetime of the
|
|
@@ -1283,7 +1960,7 @@ var ProjectIndex = class {
|
|
|
1283
1960
|
fn
|
|
1284
1961
|
} : void 0;
|
|
1285
1962
|
}
|
|
1286
|
-
const method = memberName
|
|
1963
|
+
const method = memberName(node);
|
|
1287
1964
|
if (method === void 0 || node.type !== "MemberExpression") return void 0;
|
|
1288
1965
|
const receiver = this.receiverClass(node.object, scope);
|
|
1289
1966
|
return receiver ? this.findMethod(receiver, method) : void 0;
|
|
@@ -1313,13 +1990,13 @@ var ProjectIndex = class {
|
|
|
1313
1990
|
case "ThisExpression": return scope.cls;
|
|
1314
1991
|
case "Identifier": {
|
|
1315
1992
|
const type = scope.locals.get(node.name);
|
|
1316
|
-
return
|
|
1993
|
+
return this.resolveClass(scope.file, type ?? node.name);
|
|
1317
1994
|
}
|
|
1318
1995
|
case "NewExpression": return node.callee.type === "Identifier" ? this.resolveClass(scope.file, node.callee.name) : void 0;
|
|
1319
1996
|
case "TSNonNullExpression":
|
|
1320
1997
|
case "ParenthesizedExpression": return this.receiverClass(node.expression, scope, depth + 1);
|
|
1321
1998
|
case "MemberExpression": {
|
|
1322
|
-
const field = memberName
|
|
1999
|
+
const field = memberName(node);
|
|
1323
2000
|
const owner = field === void 0 ? void 0 : this.receiverClass(node.object, scope, depth + 1);
|
|
1324
2001
|
return owner && field !== void 0 ? this.fieldClass(owner, field) : void 0;
|
|
1325
2002
|
}
|
|
@@ -1381,11 +2058,29 @@ var ProjectIndex = class {
|
|
|
1381
2058
|
}
|
|
1382
2059
|
}
|
|
1383
2060
|
};
|
|
1384
|
-
/**
|
|
2061
|
+
/** `x` and `X` for a parameter `x: X` or `x = new X()`. Parameter properties are fields, not locals. */
|
|
2062
|
+
function typedParameter(param) {
|
|
2063
|
+
const binding = param.type === "AssignmentPattern" ? param.left : param;
|
|
2064
|
+
if (binding.type !== "Identifier") return void 0;
|
|
2065
|
+
const type = typeName(binding.typeAnnotation) ?? (param.type === "AssignmentPattern" ? newName(param.right) : void 0);
|
|
2066
|
+
return type === void 0 ? void 0 : [binding.name, type];
|
|
2067
|
+
}
|
|
2068
|
+
/**
|
|
2069
|
+
* Locals whose class is known: typed parameters of `fn` and the functions inside it, `const x = new X()` and
|
|
2070
|
+
* `const x: X = ...`.
|
|
2071
|
+
*/
|
|
1385
2072
|
function collectLocals(fn) {
|
|
1386
2073
|
const locals = /* @__PURE__ */ new Map();
|
|
2074
|
+
const addParameters = (node) => {
|
|
2075
|
+
for (const param of node.params) {
|
|
2076
|
+
const found = typedParameter(param);
|
|
2077
|
+
if (found) locals.set(...found);
|
|
2078
|
+
}
|
|
2079
|
+
};
|
|
2080
|
+
addParameters(fn);
|
|
1387
2081
|
if (!fn.body) return locals;
|
|
1388
2082
|
walk(fn.body, (node) => {
|
|
2083
|
+
if (isFunctionLike(node)) addParameters(node);
|
|
1389
2084
|
if (node.type !== "VariableDeclarator" || node.id.type !== "Identifier") return;
|
|
1390
2085
|
const type = typeName(node.id.typeAnnotation) ?? newName(node.init);
|
|
1391
2086
|
if (type !== void 0) locals.set(node.id.name, type);
|
|
@@ -1406,7 +2101,7 @@ function enclosing(node) {
|
|
|
1406
2101
|
}
|
|
1407
2102
|
return { fn };
|
|
1408
2103
|
}
|
|
1409
|
-
const rule$
|
|
2104
|
+
const rule$3 = {
|
|
1410
2105
|
meta: {
|
|
1411
2106
|
type: "problem",
|
|
1412
2107
|
docs: {
|
|
@@ -1477,6 +2172,7 @@ const rule$4 = {
|
|
|
1477
2172
|
};
|
|
1478
2173
|
// src/rules/padding-lines.ts
|
|
1479
2174
|
const BLANK_LINE = /\r?\n[\t ]*\r?\n/;
|
|
2175
|
+
const BLANK_LINES = /(\r?\n)(?:[\t ]*\r?\n)+/g;
|
|
1480
2176
|
const unwrapExport = (node) => (node.type === "ExportNamedDeclaration" || node.type === "ExportDefaultDeclaration") && node.declaration ? node.declaration : node;
|
|
1481
2177
|
const isFunctionValue = (node) => node?.type === "ArrowFunctionExpression" || node?.type === "FunctionExpression" || node?.type === "TSEmptyBodyFunctionExpression";
|
|
1482
2178
|
const isImport = (node) => node.type === "ImportDeclaration" || node.type === "TSImportEqualsDeclaration" && node.moduleReference.type === "TSExternalModuleReference";
|
|
@@ -1493,12 +2189,16 @@ const isOverloadPair = (previous, current) => {
|
|
|
1493
2189
|
const name = functionStatementName(previous);
|
|
1494
2190
|
return unwrapExport(previous).type === "TSDeclareFunction" && name !== void 0 && name === functionStatementName(current);
|
|
1495
2191
|
};
|
|
1496
|
-
|
|
1497
|
-
|
|
2192
|
+
/**
|
|
2193
|
+
* A multi-line method, getter, setter or property holding a function. Single-line ones need no blank lines,
|
|
2194
|
+
* as in `lines-between-class-members`.
|
|
2195
|
+
*/
|
|
2196
|
+
const isMethodProperty = (property) => property.type === "Property" && property.loc.start.line !== property.loc.end.line && (property.method || property.kind !== "init" || isFunctionValue(property.value));
|
|
2197
|
+
const rule$2 = {
|
|
1498
2198
|
meta: {
|
|
1499
2199
|
type: "layout",
|
|
1500
2200
|
docs: {
|
|
1501
|
-
description: "Require blank lines after imports, around top-level functions and around object methods.",
|
|
2201
|
+
description: "Require blank lines after imports, around top-level functions and around object methods, and disallow blank lines between function overloads.",
|
|
1502
2202
|
url: docsUrl("padding-lines")
|
|
1503
2203
|
},
|
|
1504
2204
|
fixable: "whitespace",
|
|
@@ -1506,7 +2206,8 @@ const rule$3 = {
|
|
|
1506
2206
|
messages: {
|
|
1507
2207
|
afterImports: "Expected a blank line after the import declarations.",
|
|
1508
2208
|
aroundFunctions: "Expected a blank line between a function and the adjacent statement.",
|
|
1509
|
-
betweenObjectMethods: "Expected a blank line around methods in an object literal."
|
|
2209
|
+
betweenObjectMethods: "Expected a blank line around methods in an object literal.",
|
|
2210
|
+
betweenOverloads: "Unexpected blank line between overloads of the same function."
|
|
1510
2211
|
}
|
|
1511
2212
|
},
|
|
1512
2213
|
create(context) {
|
|
@@ -1525,12 +2226,23 @@ const rule$3 = {
|
|
|
1525
2226
|
}
|
|
1526
2227
|
});
|
|
1527
2228
|
};
|
|
2229
|
+
/** Overload signatures and the implementation form one group without blank lines; comments are kept. */
|
|
2230
|
+
const forbidBlankLines = (previous, current) => {
|
|
2231
|
+
const textBetween = text.slice(previous.range[1], current.range[0]);
|
|
2232
|
+
if (!BLANK_LINE.test(textBetween)) return;
|
|
2233
|
+
context.report({
|
|
2234
|
+
node: current,
|
|
2235
|
+
messageId: "betweenOverloads",
|
|
2236
|
+
fix: (fixer) => fixer.replaceTextRange([previous.range[1], current.range[0]], textBetween.replace(BLANK_LINES, "$1"))
|
|
2237
|
+
});
|
|
2238
|
+
};
|
|
1528
2239
|
const checkStatements = (statements) => {
|
|
1529
2240
|
for (let index = 1; index < statements.length; index++) {
|
|
1530
2241
|
const previous = statements[index - 1];
|
|
1531
2242
|
const current = statements[index];
|
|
1532
2243
|
if (isImport(previous) && !isImport(current)) requireBlankLine(previous, current, "afterImports");
|
|
1533
|
-
else if (
|
|
2244
|
+
else if (isOverloadPair(previous, current)) forbidBlankLines(previous, current);
|
|
2245
|
+
else if (isFunctionStatement(previous) || isFunctionStatement(current)) requireBlankLine(previous, current, "aroundFunctions");
|
|
1534
2246
|
}
|
|
1535
2247
|
};
|
|
1536
2248
|
return {
|
|
@@ -1547,51 +2259,12 @@ const rule$3 = {
|
|
|
1547
2259
|
};
|
|
1548
2260
|
}
|
|
1549
2261
|
};
|
|
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
2262
|
// src/rules/step-placeholders.ts
|
|
1591
2263
|
/** The same pattern `@step` uses at runtime: the path is taken as written, so `{{ name }}` looks up " name ". */
|
|
1592
2264
|
const NAMED = /\{\{(.*?)\}\}/g;
|
|
1593
2265
|
const INDEX = /\[\[(\d+)\]\]/g;
|
|
1594
|
-
|
|
2266
|
+
/** A lookbehind instead of a consumed prefix character, so adjacent placeholders (`{a}{b}`) are all found. */
|
|
2267
|
+
const SINGLE_BRACES = /(?<!\{)\{([A-Za-z_$][\w$]*(?:\.[\w$]+)*)\}(?!\})/g;
|
|
1595
2268
|
const findAll = (pattern, text) => {
|
|
1596
2269
|
const regex = new RegExp(pattern.source, "g");
|
|
1597
2270
|
const found = [];
|
|
@@ -1603,13 +2276,18 @@ const paramName = (param) => {
|
|
|
1603
2276
|
const binding = target.type === "AssignmentPattern" ? target.left : target.type === "RestElement" ? target.argument : target;
|
|
1604
2277
|
return binding.type === "Identifier" ? binding.name : void 0;
|
|
1605
2278
|
};
|
|
2279
|
+
/** True when a placeholder path starts at a parameter, or at `this` (a property of the instance). */
|
|
2280
|
+
const isKnownRoot = (path, names) => {
|
|
2281
|
+
const [root] = path.split(".");
|
|
2282
|
+
return root === "this" || names.includes(root);
|
|
2283
|
+
};
|
|
1606
2284
|
/**
|
|
1607
2285
|
* What is wrong with the path of a `{{path}}` placeholder, if anything. `@step` reads the path exactly as written,
|
|
1608
2286
|
* so spaces around it either fail the parameter lookup or read a property that does not exist.
|
|
1609
2287
|
*/
|
|
1610
2288
|
const namedProblem = (path, names) => {
|
|
1611
2289
|
const trimmed = path.trim();
|
|
1612
|
-
const isParameter = (value) =>
|
|
2290
|
+
const isParameter = (value) => isKnownRoot(value, names);
|
|
1613
2291
|
if (trimmed !== path && isParameter(trimmed)) return "whitespace";
|
|
1614
2292
|
return trimmed === path && isParameter(path) ? void 0 : "unknownParameter";
|
|
1615
2293
|
};
|
|
@@ -1691,14 +2369,14 @@ const rule$1 = {
|
|
|
1691
2369
|
});
|
|
1692
2370
|
}
|
|
1693
2371
|
for (const match of findAll(SINGLE_BRACES, raw)) {
|
|
1694
|
-
const placeholder = match[
|
|
1695
|
-
const from = start + match.index
|
|
2372
|
+
const placeholder = match[1];
|
|
2373
|
+
const from = start + match.index;
|
|
1696
2374
|
const range = [from, from + placeholder.length + 2];
|
|
1697
2375
|
const data = {
|
|
1698
2376
|
found: `{${placeholder}}`,
|
|
1699
2377
|
expected: `{{${placeholder}}}`
|
|
1700
2378
|
};
|
|
1701
|
-
if (
|
|
2379
|
+
if (isKnownRoot(placeholder, names)) context.report({
|
|
1702
2380
|
node: description,
|
|
1703
2381
|
messageId: "singleBraces",
|
|
1704
2382
|
data,
|
|
@@ -1719,19 +2397,23 @@ const STEP_LAYERS = /* @__PURE__ */ new Set([
|
|
|
1719
2397
|
"asserter",
|
|
1720
2398
|
"scenario"
|
|
1721
2399
|
]);
|
|
2400
|
+
const FUNCTION_VALUES = /* @__PURE__ */ new Set(["ArrowFunctionExpression", "FunctionExpression"]);
|
|
2401
|
+
/** Public instance members are steps; static members are helpers (such as factories). */
|
|
2402
|
+
const isPublicInstance = (member) => !member.static && member.key.type !== "PrivateIdentifier" && member.accessibility !== "private" && member.accessibility !== "protected";
|
|
1722
2403
|
// src/rules/index.ts
|
|
1723
2404
|
const rules = {
|
|
1724
|
-
"architecture-boundaries": rule$
|
|
1725
|
-
"class-matches-filename": rule$
|
|
2405
|
+
"architecture-boundaries": rule$13,
|
|
2406
|
+
"class-matches-filename": rule$12,
|
|
2407
|
+
"director-composes-builders": rule$11,
|
|
1726
2408
|
"expect-from-support": rule$10,
|
|
1727
2409
|
"file-naming": rule$9,
|
|
1728
2410
|
"lines-between-class-members": rule$8,
|
|
1729
|
-
"
|
|
1730
|
-
"no-
|
|
1731
|
-
"no-
|
|
1732
|
-
"no-
|
|
1733
|
-
"
|
|
1734
|
-
"
|
|
2411
|
+
"member-ordering": rule$7,
|
|
2412
|
+
"no-direct-construction-in-tests": rule$6,
|
|
2413
|
+
"no-expect-in-tests": rule$5,
|
|
2414
|
+
"no-hard-waits": rule$4,
|
|
2415
|
+
"no-nested-polling": rule$3,
|
|
2416
|
+
"padding-lines": rule$2,
|
|
1735
2417
|
"step-placeholders": rule$1,
|
|
1736
2418
|
"step-required": {
|
|
1737
2419
|
meta: {
|
|
@@ -1741,24 +2423,35 @@ const rules = {
|
|
|
1741
2423
|
url: docsUrl("step-required")
|
|
1742
2424
|
},
|
|
1743
2425
|
schema: [],
|
|
1744
|
-
messages: {
|
|
2426
|
+
messages: {
|
|
2427
|
+
missingStep: "Public method '{{method}}' in a {{layer}} needs a @step decorator so it is visible in the report.",
|
|
2428
|
+
functionProperty: "Public function property '{{method}}' in a {{layer}} cannot carry a @step decorator; make it a method with @step."
|
|
2429
|
+
}
|
|
1745
2430
|
},
|
|
1746
2431
|
create(context) {
|
|
1747
2432
|
const conventions = conventionsFor(context);
|
|
1748
2433
|
const info = classify(context.filename, conventions, rootFor(context, conventions));
|
|
1749
2434
|
const layer = info?.kind === "top-scenario" ? "scenario" : info?.layer;
|
|
1750
2435
|
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;
|
|
2436
|
+
const report = (key, messageId) => {
|
|
1753
2437
|
context.report({
|
|
1754
|
-
node:
|
|
1755
|
-
messageId
|
|
2438
|
+
node: key,
|
|
2439
|
+
messageId,
|
|
1756
2440
|
data: {
|
|
1757
|
-
method: context.sourceCode.getText(
|
|
2441
|
+
method: context.sourceCode.getText(key),
|
|
1758
2442
|
layer
|
|
1759
2443
|
}
|
|
1760
2444
|
});
|
|
1761
|
-
}
|
|
2445
|
+
};
|
|
2446
|
+
return {
|
|
2447
|
+
MethodDefinition(node) {
|
|
2448
|
+
if (node.kind !== "method" || !isPublicInstance(node) || node.value.type === "TSEmptyBodyFunctionExpression" || node.decorators.some((decorator) => isStepDecorator(decorator, conventions.stepDecorators))) return;
|
|
2449
|
+
report(node.key, "missingStep");
|
|
2450
|
+
},
|
|
2451
|
+
PropertyDefinition(node) {
|
|
2452
|
+
if (isPublicInstance(node) && node.value && FUNCTION_VALUES.has(node.value.type)) report(node.key, "functionProperty");
|
|
2453
|
+
}
|
|
2454
|
+
};
|
|
1762
2455
|
}
|
|
1763
2456
|
}
|
|
1764
2457
|
};
|
|
@@ -1767,4 +2460,4 @@ const PLUGIN_NAME = "sparta";
|
|
|
1767
2460
|
const recommendedRules = Object.fromEntries(Object.entries(rules).map(([name, rule]) => [`${PLUGIN_NAME}/${name}`, rule.meta?.type === "problem" ? "error" : "warn"]));
|
|
1768
2461
|
export { recommendedRules as n, rules as r, PLUGIN_NAME as t };
|
|
1769
2462
|
|
|
1770
|
-
//# sourceMappingURL=rules-
|
|
2463
|
+
//# sourceMappingURL=rules-Ay2ZlXtg.mjs.map
|