@wix/validation-infra 0.0.0-stage → 0.5.1

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.
Files changed (152) hide show
  1. package/README.md +192 -2
  2. package/dist/constants.d.ts +29 -0
  3. package/dist/constants.js +45 -0
  4. package/dist/index.d.ts +11 -0
  5. package/dist/index.js +13 -0
  6. package/dist/package-readers/constants.d.ts +6 -0
  7. package/dist/package-readers/constants.js +6 -0
  8. package/dist/package-readers/index.d.ts +1 -0
  9. package/dist/package-readers/index.js +1 -0
  10. package/dist/package-readers/read-from-files.d.ts +6 -0
  11. package/dist/package-readers/read-from-files.js +76 -0
  12. package/dist/package-readers/types.d.ts +7 -0
  13. package/dist/package-readers/types.js +1 -0
  14. package/dist/package-readers/utils.d.ts +4 -0
  15. package/dist/package-readers/utils.js +15 -0
  16. package/dist/page-model/build-page-model.d.ts +3 -0
  17. package/dist/page-model/build-page-model.js +12 -0
  18. package/dist/page-model/connect-css.d.ts +3 -0
  19. package/dist/page-model/connect-css.js +43 -0
  20. package/dist/page-model/constants.d.ts +17 -0
  21. package/dist/page-model/constants.js +17 -0
  22. package/dist/page-model/css-selectors.d.ts +2 -0
  23. package/dist/page-model/css-selectors.js +7 -0
  24. package/dist/page-model/index.d.ts +4 -0
  25. package/dist/page-model/index.js +4 -0
  26. package/dist/page-model/pages.d.ts +2 -0
  27. package/dist/page-model/pages.js +18 -0
  28. package/dist/page-model/read-css.d.ts +4 -0
  29. package/dist/page-model/read-css.js +98 -0
  30. package/dist/page-model/read-structure.d.ts +3 -0
  31. package/dist/page-model/read-structure.js +59 -0
  32. package/dist/page-model/read-theme.d.ts +6 -0
  33. package/dist/page-model/read-theme.js +14 -0
  34. package/dist/page-model/types.d.ts +92 -0
  35. package/dist/page-model/types.js +1 -0
  36. package/dist/page-model/utils.d.ts +16 -0
  37. package/dist/page-model/utils.js +29 -0
  38. package/dist/project-model/index.d.ts +5 -0
  39. package/dist/project-model/index.js +5 -0
  40. package/dist/project-model/menu-files.d.ts +4 -0
  41. package/dist/project-model/menu-files.js +14 -0
  42. package/dist/project-model/package-json.d.ts +5 -0
  43. package/dist/project-model/package-json.js +27 -0
  44. package/dist/project-model/popups.d.ts +4 -0
  45. package/dist/project-model/popups.js +12 -0
  46. package/dist/project-model/project-model.d.ts +4 -0
  47. package/dist/project-model/project-model.js +14 -0
  48. package/dist/project-model/types.d.ts +20 -0
  49. package/dist/project-model/types.js +2 -0
  50. package/dist/rules/index.d.ts +4 -0
  51. package/dist/rules/index.js +5 -0
  52. package/dist/rules/project/define-project-rule.d.ts +5 -0
  53. package/dist/rules/project/define-project-rule.js +4 -0
  54. package/dist/rules/project/index.d.ts +2 -0
  55. package/dist/rules/project/index.js +2 -0
  56. package/dist/rules/project/types.d.ts +16 -0
  57. package/dist/rules/project/types.js +1 -0
  58. package/dist/rules/site-app/define-rule.d.ts +5 -0
  59. package/dist/rules/site-app/define-rule.js +4 -0
  60. package/dist/rules/site-app/index.d.ts +2 -0
  61. package/dist/rules/site-app/index.js +1 -0
  62. package/dist/rules/site-app/types.d.ts +26 -0
  63. package/dist/rules/site-app/types.js +1 -0
  64. package/dist/rules/site-code/define-site-code-rule.d.ts +5 -0
  65. package/dist/rules/site-code/define-site-code-rule.js +4 -0
  66. package/dist/rules/site-code/index.d.ts +2 -0
  67. package/dist/rules/site-code/index.js +2 -0
  68. package/dist/rules/site-code/types.d.ts +11 -0
  69. package/dist/rules/site-code/types.js +1 -0
  70. package/dist/rules/types.d.ts +13 -0
  71. package/dist/rules/types.js +3 -0
  72. package/dist/rules-adapter/eslint/index.d.ts +1 -0
  73. package/dist/rules-adapter/eslint/index.js +1 -0
  74. package/dist/rules-adapter/eslint/types.d.ts +5 -0
  75. package/dist/rules-adapter/eslint/types.js +1 -0
  76. package/dist/rules-adapter/index.d.ts +2 -0
  77. package/dist/rules-adapter/index.js +2 -0
  78. package/dist/rules-adapter/types.d.ts +16 -0
  79. package/dist/rules-adapter/types.js +1 -0
  80. package/dist/rules-adapter/utils.d.ts +7 -0
  81. package/dist/rules-adapter/utils.js +20 -0
  82. package/dist/rules-api/component-catalog.d.ts +8 -0
  83. package/dist/rules-api/component-catalog.js +28 -0
  84. package/dist/rules-api/constants.d.ts +7 -0
  85. package/dist/rules-api/constants.js +7 -0
  86. package/dist/rules-api/index.d.ts +18 -0
  87. package/dist/rules-api/index.js +14 -0
  88. package/dist/rules-api/report.d.ts +8 -0
  89. package/dist/rules-api/report.js +13 -0
  90. package/dist/rules-api/rules-api.d.ts +13 -0
  91. package/dist/rules-api/rules-api.js +139 -0
  92. package/dist/rules-api/types.d.ts +141 -0
  93. package/dist/rules-api/types.js +1 -0
  94. package/dist/rules-api/utils.d.ts +14 -0
  95. package/dist/rules-api/utils.js +27 -0
  96. package/dist/shared/constants.d.ts +188 -0
  97. package/dist/shared/constants.js +242 -0
  98. package/dist/shared/css-variables.d.ts +9 -0
  99. package/dist/shared/css-variables.js +38 -0
  100. package/dist/shared/css.d.ts +78 -0
  101. package/dist/shared/css.js +426 -0
  102. package/dist/shared/element-match.d.ts +16 -0
  103. package/dist/shared/element-match.js +72 -0
  104. package/dist/shared/font-weights.d.ts +2 -0
  105. package/dist/shared/font-weights.js +551 -0
  106. package/dist/shared/index.d.ts +16 -0
  107. package/dist/shared/index.js +15 -0
  108. package/dist/shared/interactions-config.d.ts +15 -0
  109. package/dist/shared/interactions-config.js +120 -0
  110. package/dist/shared/links.d.ts +21 -0
  111. package/dist/shared/links.js +93 -0
  112. package/dist/shared/posix-path.d.ts +8 -0
  113. package/dist/shared/posix-path.js +27 -0
  114. package/dist/shared/site-files-tree-mapping.d.ts +40 -0
  115. package/dist/shared/site-files-tree-mapping.js +41 -0
  116. package/dist/shared/site-folders.d.ts +3 -0
  117. package/dist/shared/site-folders.js +17 -0
  118. package/dist/shared/site-media.d.ts +3 -0
  119. package/dist/shared/site-media.js +11 -0
  120. package/dist/shared/syntax-tree.d.ts +4 -0
  121. package/dist/shared/syntax-tree.js +30 -0
  122. package/dist/shared/transformation-values.d.ts +22 -0
  123. package/dist/shared/transformation-values.js +74 -0
  124. package/dist/shared/types.d.ts +477 -0
  125. package/dist/shared/types.js +1 -0
  126. package/dist/shared/utils.d.ts +155 -0
  127. package/dist/shared/utils.js +553 -0
  128. package/dist/shared/wrapper-contract.d.ts +31 -0
  129. package/dist/shared/wrapper-contract.js +157 -0
  130. package/dist/shared/wrapper-image-props.d.ts +3 -0
  131. package/dist/shared/wrapper-image-props.js +75 -0
  132. package/dist/site-code-model/constants.d.ts +107 -0
  133. package/dist/site-code-model/constants.js +102 -0
  134. package/dist/site-code-model/index.d.ts +3 -0
  135. package/dist/site-code-model/index.js +4 -0
  136. package/dist/site-code-model/manifest-values.d.ts +15 -0
  137. package/dist/site-code-model/manifest-values.js +88 -0
  138. package/dist/site-code-model/read-module.d.ts +4 -0
  139. package/dist/site-code-model/read-module.js +267 -0
  140. package/dist/site-code-model/read-site-code-model.d.ts +9 -0
  141. package/dist/site-code-model/read-site-code-model.js +42 -0
  142. package/dist/site-code-model/source-locations.d.ts +5 -0
  143. package/dist/site-code-model/source-locations.js +95 -0
  144. package/dist/site-code-model/types.d.ts +102 -0
  145. package/dist/site-code-model/types.js +1 -0
  146. package/dist/testing.d.ts +24 -0
  147. package/dist/testing.js +61 -0
  148. package/dist/types.d.ts +95 -0
  149. package/dist/types.js +1 -0
  150. package/dist/validate.d.ts +7 -0
  151. package/dist/validate.js +180 -0
  152. package/package.json +59 -3
@@ -0,0 +1,95 @@
1
+ import type { CompanionKinds, ComponentCatalog, ValueOrReader } from './rules-api/index.ts';
2
+ import type { Rule, ProjectRule, SiteCodeRule, Editor, Severity } from './rules/index.ts';
3
+ import type { Files, FontCatalog, ReadFile, ReadPackageFile, WrapperContract } from './shared/index.ts';
4
+ /** A page rule (defineRule), a project rule (defineProjectRule) or a site-code rule (defineSiteCodeRule): what a rule package exports, in one list. */
5
+ export type AnyRule = Rule<string> | ProjectRule<string> | SiteCodeRule<string>;
6
+ export interface ValidateInput {
7
+ /** Every site file the rules may read, by its path from the site root: `packages/site-app/src/pages/<id>/structure.jsx`, its CSS, the theme… */
8
+ files: Files;
9
+ /** The rules to run, page, project and site-code rules alike, from any rule package. Rules of one scope with one name are combined into one rule;
10
+ * a check key declared by two of them throws. Rules of two scopes with one name stay two rules. */
11
+ rules: readonly AnyRule[];
12
+ /** `packages/site-app/site-component-catalog.json`, parsed, or what reads it. Read from `files` when not given: parsed when a rule first
13
+ * asks about a tag from an app's wrapper, once per validate(), so a broken catalog throws only on a page that has one (the page's rules crash). */
14
+ catalog?: ValueOrReader<ComponentCatalog | undefined>;
15
+ /** The Wix font catalog the theme font rules read: each font id and its weights. Default: the copy in this package, from @wix/origami-fonts. */
16
+ fonts?: FontCatalog;
17
+ /** The files of the site's installed packages the rules read (each `@origami-wrappers/<app>` and `@wix/origami-system-components`), by
18
+ * their path from the site root as they are on disk: `node_modules/@origami-wrappers/<app>/package.json`, `…/<name>/component-contract.json`,
19
+ * `…/<name>/defaults.css`, `…/<name>/definition.ts`. Next to `files`, not in it: no rule lints them. The caller reads them from the site's
20
+ * node_modules, or is sent them; validate() finds a package and resolves its `exports` in them as node does. A package that isn't here
21
+ * is not installed. Default: none, so no package is installed and no tag has a contract. */
22
+ packageFiles?: Files;
23
+ /** Enables the experimental popup checks, as ORIGAMI_EXPERIMENTAL_POPUPS does in origami-lint. Default: false. */
24
+ experimentalPopups?: boolean;
25
+ /** The site code's companion registrations (the `.extension.ts` files under packages/site-code/src), as import-site reads them: each module's kind by the
26
+ * name a page imports it from (`@site/site-code/myData` → 'contextProvider'), so a provider added in the site code counts before the next
27
+ * export lists it in the catalog. The caller reads them (origami-transforms readCompanionRegistrations), or gives what reads them: read
28
+ * when a rule first asks about a tag from the site code, once per validate(), so a registration that throws crashes only such a page. Default: none. */
29
+ companions?: ValueOrReader<CompanionKinds>;
30
+ /** The editor the site is imported into. Only the rules whose `editors` include it run (plus the ones for every editor). Default: every rule. */
31
+ editor?: Editor;
32
+ /** The site's folder, as ESLint names a page by it: in a crash's message (`Occurred while linting <root>/<page>`) and in a rule's
33
+ * `context.cwd` and `context.filename`, as origami-lint master's ESLint did. Only a name: nothing is read from it. Default: '' (`/<page>`). */
34
+ root?: string;
35
+ /** Only these structure files; one that isn't in `files` is skipped. Default: every page in `files`. */
36
+ only?: string[];
37
+ /** Told what ran and how long it took, for a caller that measures (origami-lint's BI). Never given a message or a file's text. */
38
+ onEvent?: (event: ValidateEvent) => void;
39
+ }
40
+ export interface Problem {
41
+ /** The rule's name; `parse` for a structure file that doesn't parse. A rule that threw reports one problem with the check `crashed`. */
42
+ rule: string;
43
+ check: string;
44
+ severity: Severity;
45
+ /** The file the problem is in, from the site root. A page rule's CSS problem is on its structure file, with `file:line:` leading the message. */
46
+ file: string;
47
+ line: number;
48
+ column: number;
49
+ message: string;
50
+ }
51
+ /** Why a page's model couldn't be built: a CSS file that doesn't parse, or anything else. */
52
+ export type PageModelError = 'css_syntax_error' | 'unknown_error';
53
+ export type ValidateEvent = {
54
+ kind: 'project_rule';
55
+ rule: string;
56
+ severity: Severity;
57
+ duration_ms: number;
58
+ crashed: boolean;
59
+ }
60
+ /** One site-code rule ran, after the project rules: the time spent in its check, the model's read included for the first one. */
61
+ | {
62
+ kind: 'site_code_rule';
63
+ rule: string;
64
+ severity: Severity;
65
+ duration_ms: number;
66
+ crashed: boolean;
67
+ }
68
+ /** A page's model was built (once per page, when the first rule asks for it). id: its root component's id, when it is a plain one. */
69
+ | {
70
+ kind: 'page_model';
71
+ file: string;
72
+ id?: string;
73
+ components: number;
74
+ css_files: number;
75
+ duration_ms: number;
76
+ error?: PageModelError;
77
+ }
78
+ /** One rule ran on one page: the time spent in its callbacks. Every rule reports, a passing one too; none when the file doesn't parse or the rules crashed. */
79
+ | {
80
+ kind: 'page_rule';
81
+ file: string;
82
+ rule: string;
83
+ duration_ms: number;
84
+ };
85
+ /** What a page's rules API reads of the site beyond the page: its files, its component catalog, the font catalog, its wrappers' contracts and
86
+ * its site code's companion registrations (the catalog, a contract and the registrations read on first use, once per validate()). */
87
+ export interface SiteReaders {
88
+ root: string;
89
+ readFile: ReadFile;
90
+ readCatalog: () => ComponentCatalog | undefined;
91
+ fonts: FontCatalog | undefined;
92
+ readContract: (source: string) => WrapperContract | null;
93
+ readCompanions: () => CompanionKinds;
94
+ readPackageFile: ReadPackageFile;
95
+ }
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,7 @@
1
+ import type { Problem, ValidateInput } from './types.ts';
2
+ /** Every problem, project rules first, then site-code rules, then each page in the order given. A page's problems are ESLint's, in its order: by line, then
3
+ * column, the ones at one place in the order the rules reported them; an `eslint-disable` comment's left out, as on origami-lint master.
4
+ * A structure file that doesn't parse is one problem with the rule 'parse'. */
5
+ export declare const validate: (input: ValidateInput) => Problem[];
6
+ /** validate(), each page's problems in the order its rules found them: what testing.ts's lint() groups by rule for a rule's test. */
7
+ export declare const validateInFoundOrder: (input: ValidateInput) => Problem[];
@@ -0,0 +1,180 @@
1
+ // The one entry point: every site file in memory → every problem the rules find. No disk, no git, no process.
2
+ // The CLI (origami-lint), an editor agent or an import server each build `files` their own way and call this.
3
+ import { Linter } from 'eslint';
4
+ import { CRASHED, DIRECTIVE, PAGE_RULES, PLUGIN, STRUCTURE_FILES } from "./constants.js";
5
+ import { buildPageModel, findPages } from "./page-model/index.js";
6
+ import { readPackageFileFromFiles } from "./package-readers/index.js";
7
+ import { createProjectModel } from "./project-model/index.js";
8
+ import { createExtensionReader, readSiteCodeModel } from "./site-code-model/index.js";
9
+ import { createReport, createToolCallbacks } from "./rules-adapter/index.js";
10
+ import { createRulesApi, readOnce } from "./rules-api/index.js";
11
+ import { COMPONENT_CATALOG_FILE, createContractReader, } from "./shared/index.js";
12
+ /** Every problem, project rules first, then site-code rules, then each page in the order given. A page's problems are ESLint's, in its order: by line, then
13
+ * column, the ones at one place in the order the rules reported them; an `eslint-disable` comment's left out, as on origami-lint master.
14
+ * A structure file that doesn't parse is one problem with the rule 'parse'. */
15
+ export const validate = (input) => run(input, 'eslint');
16
+ /** validate(), each page's problems in the order its rules found them: what testing.ts's lint() groups by rule for a rule's test. */
17
+ export const validateInFoundOrder = (input) => run(input, 'found');
18
+ function run({ files, rules, catalog, fonts, packageFiles = {}, experimentalPopups = false, companions = new Map(), editor, only, root = '', onEvent = () => { } }, order) {
19
+ assertUniqueCheckKeys(rules);
20
+ // Read once, when a rule first asks: a broken catalog crashes only a page with a tag from an app's wrapper, as on origami-lint master.
21
+ const readCatalog = readOnce(catalog ?? (() => (files[COMPONENT_CATALOG_FILE] === undefined ? undefined : JSON.parse(files[COMPONENT_CATALOG_FILE]))));
22
+ const readCompanions = readOnce(companions);
23
+ const readFile = (file) => files[file];
24
+ const allFiles = Object.keys(files);
25
+ const running = rules.filter((rule) => editor === undefined || rule.editors === 'all' || rule.editors.includes(editor));
26
+ const problems = [];
27
+ const projectOf = (rule) => createProjectModel(rule.name, allFiles, readFile, experimentalPopups);
28
+ for (const rule of running.filter(isProjectRule))
29
+ problems.push(...runSiteRule(rule, 'project_rule', () => rule.check(projectOf(rule)), onEvent));
30
+ // The site code's model: read once, when the first site-code rule runs (none: never read). Site-code rules, like project rules, ignore `only`.
31
+ const readSiteCode = readOnce(() => {
32
+ const project = createProjectModel('site-code', allFiles, readFile, experimentalPopups);
33
+ return readSiteCodeModel(project.files, createExtensionReader(project.readFile));
34
+ });
35
+ for (const rule of running.filter(isSiteCodeRule))
36
+ problems.push(...runSiteRule(rule, 'site_code_rule', () => rule.check(readSiteCode(), projectOf(rule)), onEvent));
37
+ const pageRules = running.filter(isPageRule);
38
+ const readPackageFile = readPackageFileFromFiles(packageFiles);
39
+ const readContract = createContractReader(readPackageFile);
40
+ for (const jsxFile of only ?? findPages(allFiles, allFiles)) {
41
+ if (files[jsxFile] === undefined)
42
+ continue;
43
+ const site = { root, readFile, readCatalog, fonts, readContract: (source) => readContract(source, jsxFile), readCompanions, readPackageFile };
44
+ problems.push(...lintPage(jsxFile, files[jsxFile], pageRules, site, onEvent)[order]);
45
+ }
46
+ return problems;
47
+ }
48
+ const isProjectRule = (rule) => rule.scope === 'project';
49
+ const isSiteCodeRule = (rule) => rule.scope === 'site-code';
50
+ const isPageRule = (rule) => rule.scope !== 'project' && rule.scope !== 'site-code';
51
+ /** A project or site-code rule's problems, each at its file and line (1 when not given), and its event. */
52
+ function runSiteRule(rule, kind, check, onEvent) {
53
+ const problems = [];
54
+ const started = performance.now();
55
+ let crashed = false;
56
+ try {
57
+ for (const problem of check()) {
58
+ problems.push({ rule: rule.name, check: problem.check, severity: rule.severity ?? 'error', file: problem.file, line: problem.line ?? 1, column: 1, message: problem.message });
59
+ }
60
+ }
61
+ catch (error) {
62
+ // A rule that couldn't finish (a file it can't parse, a path out of the site) fails the run, whatever its severity.
63
+ crashed = true;
64
+ problems.push(crashProblem(rule.name, "the rule couldn't finish", error));
65
+ }
66
+ onEvent({ kind, rule: rule.name, severity: rule.severity ?? 'error', duration_ms: performance.now() - started, crashed });
67
+ return problems;
68
+ }
69
+ /** Throws when two rules of one scope and one name (combined into one rule) declare the same check key: their problems couldn't be told apart. */
70
+ function assertUniqueCheckKeys(rules) {
71
+ const seen = new Set();
72
+ for (const rule of rules)
73
+ for (const check of new Set(rule.checks)) {
74
+ const scope = rule.scope ?? 'page';
75
+ const key = JSON.stringify([scope, rule.name, check]);
76
+ if (seen.has(key))
77
+ throw new Error(`${scope} rule ${rule.name} has check ${check} more than once: give each check its own key`);
78
+ seen.add(key);
79
+ }
80
+ }
81
+ const crashProblem = (rule, what, error) => ({ rule, check: CRASHED, severity: 'error', file: '.', line: 1, column: 1, message: `${what}, so this run can't pass: ${error instanceof Error ? error.message : String(error)}` });
82
+ /** One structure file through ESLint's in-process Linter: each rule name one ESLint rule, `origami/<name>` as on origami-lint master (what
83
+ * a site's `eslint-disable` comment names), its rules' callbacks side by side on the same nodes; the page built from `files` once and shared
84
+ * by every rule. `eslint`: ESLint's messages as problems, in its order; `found`: the same problems, ESLint's own (a parse error, an ESLint
85
+ * comment's) first, then the rules' in the order they found them. */
86
+ function lintPage(jsxFile, text, rules, site, onEvent) {
87
+ const found = [];
88
+ // What each report sent ESLint, by ESLint's rule id, place and message: the problem its message is, its check and severity with it.
89
+ const sent = new Map();
90
+ const durations = new Map();
91
+ let rulesApi;
92
+ const names = [...new Set(rules.map((rule) => rule.name))];
93
+ // Rules sharing a name (one per package) run side by side: every problem shows the shared name, with its own rule's severity.
94
+ const toEslintRule = (name) => ({
95
+ meta: { type: 'problem', schema: [], messages: { problem: '{{message}}' } },
96
+ defaultOptions: [],
97
+ create: (context) => {
98
+ const listeners = new Map();
99
+ for (const rule of rules.filter((rule) => rule.name === name)) {
100
+ const report = createReport((problem) => {
101
+ const reported = { rule: name, check: problem.check, severity: rule.severity ?? 'error', file: jsxFile, line: problem.loc.start.line, column: problem.loc.start.column + 1, message: problem.message };
102
+ found.push(reported);
103
+ const key = messageKey(`${PLUGIN}/${name}`, reported.line, reported.column, reported.message);
104
+ sent.set(key, [...(sent.get(key) ?? []), reported]);
105
+ context.report({ messageId: 'problem', data: { message: problem.message }, loc: problem.loc });
106
+ });
107
+ const callbacks = createToolCallbacks(rule, { rulesApi: () => (rulesApi ??= buildRulesApi(jsxFile, context, site, onEvent)), report, context });
108
+ for (const [selector, callback] of Object.entries(callbacks))
109
+ listeners.set(selector, [...(listeners.get(selector) ?? []), callback]);
110
+ }
111
+ return Object.fromEntries([...listeners].map(([selector, callbacks]) => [selector, (node) => {
112
+ const started = performance.now();
113
+ try {
114
+ for (const callback of callbacks)
115
+ callback(node);
116
+ }
117
+ finally {
118
+ durations.set(name, (durations.get(name) ?? 0) + performance.now() - started);
119
+ }
120
+ }]));
121
+ },
122
+ });
123
+ const config = [{
124
+ ...STRUCTURE_FILES,
125
+ plugins: { [PLUGIN]: { rules: Object.fromEntries(names.map((name) => [name, toEslintRule(name)])) } },
126
+ rules: Object.fromEntries(names.map((name) => [`${PLUGIN}/${name}`, 'error'])),
127
+ }];
128
+ let messages;
129
+ try {
130
+ messages = new Linter({ cwd: site.root || '/' }).verify(text, config, { filename: `${site.root}/${jsxFile}` });
131
+ }
132
+ catch (error) {
133
+ // A rule that threw stops ESLint on the page: the page can't pass, and has no other problem (ESLint gives none).
134
+ const crashed = [crashProblem(PAGE_RULES, "the page rules couldn't finish", error)];
135
+ return { eslint: crashed, found: crashed };
136
+ }
137
+ const kept = new Set();
138
+ const problems = messages.map((message) => {
139
+ if (message.fatal)
140
+ return { rule: 'parse', check: 'syntax', severity: 'error', file: jsxFile, line: message.line, column: message.column, message: message.message.replace(/^Parsing error: /, '') };
141
+ const reported = message.ruleId === null ? undefined : sent.get(messageKey(message.ruleId, message.line, message.column, message.message))?.shift();
142
+ if (reported) {
143
+ kept.add(reported);
144
+ return reported;
145
+ }
146
+ // ESLint's own problem with an ESLint comment in the file (`// eslint-disable-next-line react-hooks/exhaustive-deps`: a rule it
147
+ // doesn't have), under the rule the comment names, as origami-lint master printed it.
148
+ return { rule: message.ruleId?.replace(new RegExp(`^${PLUGIN}/`), '') ?? 'parse', check: DIRECTIVE, severity: message.severity === 2 ? 'error' : 'warn', file: jsxFile, line: message.line, column: message.column,
149
+ message: message.ruleId === null ? message.message.replace(/^Parsing error: /, '') : message.message };
150
+ });
151
+ if (!messages.some((message) => message.fatal))
152
+ for (const name of names)
153
+ onEvent({ kind: 'page_rule', file: jsxFile, rule: name, duration_ms: durations.get(name) ?? 0 });
154
+ return { eslint: problems, found: [...problems.filter((problem) => !kept.has(problem)), ...found.filter((problem) => kept.has(problem))] };
155
+ }
156
+ /** An ESLint message's rule, place and text: what one report sent it, so its message finds that problem back. */
157
+ const messageKey = (ruleId, line, column, message) => JSON.stringify([ruleId, line, column, message]);
158
+ /** The page's model from ESLint's tree (parsed once), then the rules API over it. Reports the build, with what went wrong when it did. */
159
+ function buildRulesApi(jsxFile, context, { readFile, readCatalog, fonts, readContract, readCompanions, readPackageFile }, onEvent) {
160
+ const started = performance.now();
161
+ let page;
162
+ let error;
163
+ try {
164
+ page = buildPageModel({ jsxFile, text: context.sourceCode.text, program: context.sourceCode.ast }, readFile);
165
+ return createRulesApi(page, readCatalog, readFile, fonts, readContract, readCompanions, readPackageFile);
166
+ }
167
+ catch (thrown) {
168
+ error = thrown instanceof Error && thrown.name === 'CssSyntaxError' ? 'css_syntax_error' : 'unknown_error';
169
+ throw thrown;
170
+ }
171
+ finally {
172
+ onEvent({ kind: 'page_model', file: jsxFile, id: page && pageIdOf(page), duration_ms: performance.now() - started,
173
+ components: page ? Object.keys(page.components).length : 0, css_files: page ? Object.values(page.css.files).filter(Boolean).length : 0, ...(error ? { error } : {}) });
174
+ }
175
+ }
176
+ /** The root component's id when it's a plain one: what BI may name a page by. Never its path. */
177
+ function pageIdOf(page) {
178
+ const id = Object.values(page.components).find((component) => component.parentKey === null)?.id;
179
+ return id && /^[\w-]+$/.test(id) ? id : undefined;
180
+ }
package/package.json CHANGED
@@ -1,6 +1,62 @@
1
1
  {
2
2
  "name": "@wix/validation-infra",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.5.1",
4
+ "description": "The infra of the Origami site validations: write lint rules in any repo (defineRule, defineProjectRule, the rules API) and run them on a site (validate): every site file in memory in, every problem out. No disk, no git, no process",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./dist/index.d.ts",
9
+ "default": "./dist/index.js"
10
+ },
11
+ "./testing": {
12
+ "types": "./dist/testing.d.ts",
13
+ "default": "./dist/testing.js"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist",
18
+ "README.md"
19
+ ],
20
+ "engines": {
21
+ "node": "^20.19.0 || ^22.13.0 || >=24"
22
+ },
23
+ "scripts": {
24
+ "build": "rm -rf dist && tsc -p . && tsc -p tsconfig.build.json",
25
+ "lint": "oxlint .",
26
+ "typecheck": "tsc -p .",
27
+ "test": "node --test 'test/*.test.ts'"
28
+ },
29
+ "dependencies": {
30
+ "@typescript-eslint/parser": "^8.71.2",
31
+ "@typescript-eslint/utils": "^8.71.2",
32
+ "@wix/origami-types": "^2.0.0",
33
+ "eslint": "^10.12.0",
34
+ "postcss": "^8.5.28",
35
+ "typescript": "^5.8.3 || ~6.0.3"
36
+ },
37
+ "devDependencies": {
38
+ "@types/node": "^22.0.0",
39
+ "@wix/origami-fonts": "^1.0.4"
40
+ },
41
+ "publishConfig": {
42
+ "registry": "https://registry.npmjs.org/",
43
+ "access": "public"
44
+ },
45
+ "wix": {
46
+ "artifact": {
47
+ "groupId": "com.wixpress.npm",
48
+ "artifactId": "validation-infra",
49
+ "targets": {
50
+ "docker": false
51
+ }
52
+ },
53
+ "validations": {
54
+ "postBuild": [
55
+ "typecheck",
56
+ "test",
57
+ "lint"
58
+ ]
59
+ }
60
+ },
61
+ "falconPackageHash": "993a6b5134e9520f5893123418bc5cf371eaecc2aa253f77d3ed6491"
6
62
  }