@cerios/sparta-lint-rules 0.0.2 → 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.
@@ -1,3 +1,4 @@
1
+
1
2
  import { createRequire } from "node:module";
2
3
  import path from "node:path";
3
4
  import { existsSync, readFileSync, statSync } from "node:fs";
@@ -109,7 +110,11 @@ const LAYER_FOLDERS = {
109
110
  suffix: "window"
110
111
  }
111
112
  };
112
- /** Layer folders that live directly in an area (`sparta/<area>/builders/`), shared by its groups, not in a group. */
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
+ */
113
118
  const AREA_LAYER_FOLDERS = /* @__PURE__ */ new Set(["builders", "directors"]);
114
119
  const AREAS = [
115
120
  "api",
@@ -252,6 +257,7 @@ function buildPatterns(spartaFolder, testsFolder, areas) {
252
257
  area: new RegExp(`/${sparta}/(${areaNames})${end}(?:/([^/]+))?(?:/([^/]+))?`),
253
258
  layer: new RegExp(`/${sparta}/(?:${areaNames})/[^/]+/([^/]+)/`),
254
259
  top: new RegExp(`/${sparta}/(${TOP_LEVEL_FOLDERS.join("|")})${end}`),
260
+ common: new RegExp(`/${sparta}/common/([^/]+)${end}`),
255
261
  checked: new RegExp(`/(?:${sparta}|${tests})/`),
256
262
  sparta: new RegExp(`/${sparta}/`),
257
263
  tests: new RegExp(`/${tests}${end}`),
@@ -486,15 +492,18 @@ const TOP_LEVEL_KINDS = {
486
492
  const standardFolder = (conventions, folder) => folder === void 0 ? void 0 : conventions.layerFolders.get(folder);
487
493
  /** The layer of a standard layer folder (`stepper` for `steppers`), if it has one. */
488
494
  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];
495
+ /** `name` as a builders or directors folder (`AREA_LAYER_FOLDERS`), if it is one. */
496
+ const builderFolder = (conventions, name) => {
492
497
  const standard = standardFolder(conventions, name);
493
498
  return name !== void 0 && standard !== void 0 && AREA_LAYER_FOLDERS.has(standard) ? {
494
499
  name,
495
500
  standard
496
501
  } : void 0;
497
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]);
498
507
  const caseInsensitivePaths = process.platform === "win32";
499
508
  /**
500
509
  * The part of `filename` below the project `root`, with a leading `/` (`/sparta/ui/...`), or `undefined` for files
@@ -534,7 +543,12 @@ function classify(filename, conventions, root) {
534
543
  };
535
544
  }
536
545
  const top = file.match(patterns.top)?.[1];
537
- return top ? { kind: TOP_LEVEL_KINDS[top] ?? "shared" } : void 0;
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" };
538
552
  }
539
553
  /** Project root per directory, kept for the lifetime of the linter process. */
540
554
  const rootCache = /* @__PURE__ */ new Map();
@@ -609,10 +623,12 @@ function resolveImport(filename, request, root, conventions) {
609
623
  const layerFolder = (filename, conventions, root) => {
610
624
  const file = projectPath(filename, root);
611
625
  if (file === void 0) return void 0;
612
- if (file.match(conventions.patterns.top)?.[1] === "scenarios") return {
626
+ const top = file.match(conventions.patterns.top)?.[1];
627
+ if (top === "scenarios") return {
613
628
  name: "scenarios",
614
629
  standard: "scenarios"
615
630
  };
631
+ if (top === "common") return commonLayerFolder(file, conventions);
616
632
  const areaLayer = areaLayerFolder(file, conventions);
617
633
  if (areaLayer) return areaLayer;
618
634
  const folder = file.match(conventions.patterns.layer)?.[1];
@@ -788,15 +804,17 @@ const FORBIDDEN_LAYERS = {
788
804
  mobile: uiLayers("screen"),
789
805
  ui: uiLayers("page")
790
806
  };
791
- /** The layers of `sparta/<area>/builders/` and `sparta/<area>/directors/`. */
807
+ /** The layers of the `builders/` and `directors/` folders, wherever they live. */
792
808
  const BUILDER_LAYERS = /* @__PURE__ */ new Set(["builder", "director"]);
793
- /** The only group layers that may use their area's builders and directors. */
809
+ /** The only layers, besides directors, that may use builders and directors. */
794
810
  const BUILDER_USERS = /* @__PURE__ */ new Set([
795
811
  "scenario",
796
812
  "stepper",
797
813
  "asserter",
798
814
  "service"
799
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);
800
818
  const DATABASE_ACCESS = [
801
819
  "repository",
802
820
  "base-repository",
@@ -876,7 +894,7 @@ const describe = (info, { spartaFolder, testsFolder }) => ({
876
894
  "area-layer": `${info.areaFolder} ${info.layer}`,
877
895
  "top-scenario": `${spartaFolder}/scenarios`,
878
896
  fixture: `${spartaFolder}/fixtures`,
879
- shared: `${spartaFolder}/common|support`,
897
+ shared: info.layer ? `${spartaFolder}/common ${info.layer}` : `${spartaFolder}/common|support`,
880
898
  test: testsFolder
881
899
  })[info.kind];
882
900
  /** Whether code in an area folder (a group, or the area's builders and directors) may not depend on `target`. */
@@ -910,16 +928,23 @@ function isolationViolation(source, target) {
910
928
  }
911
929
  /**
912
930
  * 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.
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.
914
933
  */
915
934
  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);
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);
918
941
  }
919
942
  function layerViolation(source, target) {
920
- if (!target.layer || !target.area) return false;
943
+ if (!target.layer) return false;
921
944
  const builder = BUILDER_LAYERS.has(target.layer) ? builderViolation(source, target) : void 0;
922
945
  if (builder !== void 0) return builder;
946
+ if (isBuilderLayer(source)) return target.layer !== "service";
947
+ if (!target.area) return false;
923
948
  if (source.kind === "group" || source.kind === "area-common") {
924
949
  if (!source.area || !source.layer) return false;
925
950
  return source.areaFolder === target.areaFolder && Boolean(FORBIDDEN_LAYERS[source.area][source.layer]?.has(target.layer));
@@ -940,8 +965,7 @@ const rule$13 = {
940
965
  isolation: "{{source}} must not depend on {{target}} ({{request}}). Move shared code to the area's common/ folder or the top-level common/ folder.",
941
966
  layer: "{{source}} must not depend on {{target}} ({{request}}).",
942
967
  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."
968
+ staticService: "{{source}} is a static abstract class and must not depend on {{target}} ({{request}}); only a normal class may use services."
945
969
  }
946
970
  },
947
971
  create(context) {
@@ -953,7 +977,7 @@ const rule$13 = {
953
977
  /** Whether the file declares a normal (instantiable) class. Set on `Program`, before any import is visited. */
954
978
  let isNormalClass = false;
955
979
  /** 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;
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;
957
981
  const check = (node, request, reexport = false) => {
958
982
  if (request === void 0) return;
959
983
  const resolved = resolveImport(filename, request, root, conventions);
@@ -974,7 +998,7 @@ const rule$13 = {
974
998
  messageId: "layer",
975
999
  data
976
1000
  });
977
- else if (source.kind === "area-layer" && target.layer === "service" && !isNormalClass) context.report({
1001
+ else if (isBuilderLayer(source) && target.layer === "service" && !isNormalClass) context.report({
978
1002
  node,
979
1003
  messageId: "staticService",
980
1004
  data
@@ -988,15 +1012,6 @@ const rule$13 = {
988
1012
  return {
989
1013
  Program(node) {
990
1014
  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
1015
  },
1001
1016
  ImportDeclaration: (node) => check(node, stringValue(node.source)),
1002
1017
  ExportNamedDeclaration: (node) => check(node, stringValue(node.source), true),
@@ -1084,8 +1099,7 @@ const rule$11 = {
1084
1099
  const filename = context.filename;
1085
1100
  const conventions = conventionsFor(context);
1086
1101
  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 {};
1102
+ if (classify(filename, conventions, root)?.layer !== "director" || moduleName(filename) === "index") return {};
1089
1103
  const isBuilder = (request) => {
1090
1104
  if (request === void 0) return false;
1091
1105
  const resolved = resolveImport(filename, request, root, conventions);
@@ -2446,4 +2460,4 @@ const PLUGIN_NAME = "sparta";
2446
2460
  const recommendedRules = Object.fromEntries(Object.entries(rules).map(([name, rule]) => [`${PLUGIN_NAME}/${name}`, rule.meta?.type === "problem" ? "error" : "warn"]));
2447
2461
  export { recommendedRules as n, rules as r, PLUGIN_NAME as t };
2448
2462
 
2449
- //# sourceMappingURL=rules-D94N3gGx.mjs.map
2463
+ //# sourceMappingURL=rules-Ay2ZlXtg.mjs.map