@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,98 @@
1
+ // A page's layout.css + style.css, and theme.css → PageCss: every line under its selector's class name and its kind, or in globalCss;
2
+ // each file as parsed.
3
+ // Nested rules are joined to their parent, and `@apply --mixin` is replaced by the theme's mixin, as kind 'theme'.
4
+ import postcss, { CssSyntaxError } from 'postcss';
5
+ import { CSS_FILES, KIND_OF_FILE } from "./constants.js";
6
+ import { nestSelectors } from "./css-selectors.js";
7
+ import { parseComponentSelector, unescapeName } from "../shared/index.js";
8
+ import { THEME_FILE } from "../shared/index.js";
9
+ import { mixinName, readMixins } from "./read-theme.js";
10
+ import { emptyComponentCss } from "./utils.js";
11
+ /** A property's name as the browser reads it: `Position` is `position`; a custom property (`--Gap`) keeps its case, which counts. */
12
+ const propertyName = (prop) => (prop.startsWith('--') ? prop : prop.toLowerCase());
13
+ const placeOf = (node) => ({ line: node.source?.start?.line ?? 1, column: node.source?.start?.column ?? 1 });
14
+ /** theme.css is read and parsed once: its tree feeds the mixins `@apply` puts in, and rulesApi reads its tokens and breakpoints. */
15
+ export function readCss(page, readFile) {
16
+ const errors = [];
17
+ const theme = parseCss('theme.css', readFile(THEME_FILE), errors);
18
+ const css = {
19
+ // No prototype: a class named `constructor` or `__proto__` is a key like any other, not Object's.
20
+ byClassName: Object.create(null),
21
+ unconnectedCss: Object.create(null),
22
+ globalCss: emptyComponentCss(),
23
+ files: { layout: undefined, style: undefined, theme },
24
+ errors,
25
+ };
26
+ const mixins = readMixins(theme);
27
+ const addLine = lineAdderTo(css);
28
+ for (const file of CSS_FILES) {
29
+ const root = parseCss(file, readFile(`${page}/${file}`), errors);
30
+ if (root === undefined)
31
+ continue;
32
+ const kind = KIND_OF_FILE[file];
33
+ css.files[kind] = root;
34
+ readNodes(root.nodes, { selectors: [], media: null, target: null, kind, file, applying: [], appliedAt: null }, mixins, addLine);
35
+ }
36
+ return css;
37
+ }
38
+ /** A line goes under its selector's first class name, or in globalCss when the selector has no `.x` or `#x`. A `#x` owner is
39
+ * unconnected from the start: the import doesn't read #id since converter 0.63. */
40
+ function lineAdderTo(css) {
41
+ return (selector, property, value, { media, target, kind, file }, { line, column }) => {
42
+ const { owner, selector: componentSelector } = parseComponentSelector(selector);
43
+ const componentCss = !owner ? css.globalCss
44
+ : owner.startsWith('.') ? (css.byClassName[unescapeName(owner.slice(1))] ??= emptyComponentCss())
45
+ : (css.unconnectedCss[owner] ??= emptyComponentCss());
46
+ (componentCss[kind][property] ??= []).push({ value, media, selector: target ? `${componentSelector} @origami-target ${target}` : componentSelector, file, line, column });
47
+ };
48
+ }
49
+ function readNodes(nodes, context, mixins, addLine) {
50
+ for (const node of nodes) {
51
+ if (node.type === 'decl' && context.selectors.length) {
52
+ for (const selector of context.selectors)
53
+ addLine(selector, propertyName(node.prop), node.value.trim(), context, context.appliedAt ?? placeOf(node));
54
+ }
55
+ else if (node.type === 'rule') {
56
+ readNodes(node.nodes, { ...context, selectors: nestSelectors(context.selectors, node.selectors) }, mixins, addLine);
57
+ }
58
+ else if (node.type === 'atrule') {
59
+ readAtRule(node, context, mixins, addLine);
60
+ }
61
+ }
62
+ }
63
+ function readAtRule(node, context, mixins, addLine) {
64
+ const params = node.params.trim();
65
+ if (node.name === 'mixin')
66
+ return;
67
+ if (node.name === 'apply')
68
+ return applyMixin(mixinName(params), { ...context, appliedAt: context.appliedAt ?? placeOf(node) }, mixins, addLine);
69
+ if (!node.nodes)
70
+ return;
71
+ if (node.name === 'media')
72
+ readNodes(node.nodes, { ...context, media: context.media ? `${context.media} and ${params}` : params }, mixins, addLine);
73
+ else if (node.name === 'origami-target')
74
+ readNodes(node.nodes, { ...context, target: params }, mixins, addLine);
75
+ else
76
+ readNodes(node.nodes, context, mixins, addLine);
77
+ }
78
+ /** A missing mixin, or one that applies itself, adds nothing. */
79
+ function applyMixin(name, context, mixins, addLine) {
80
+ const body = mixins.get(name);
81
+ if (!body || context.applying.includes(name))
82
+ return;
83
+ readNodes(body, { ...context, kind: 'theme', applying: [...context.applying, name] }, mixins, addLine);
84
+ }
85
+ /** A file's syntax tree; undefined for a file the site doesn't have, or one postcss refuses, which goes to `errors` with postcss's reason and place. */
86
+ function parseCss(file, text, errors) {
87
+ if (text === undefined)
88
+ return undefined;
89
+ try {
90
+ return postcss.parse(text);
91
+ }
92
+ catch (error) {
93
+ if (!(error instanceof CssSyntaxError))
94
+ throw error;
95
+ errors.push({ file, line: error.line ?? 1, column: error.column ?? 1, reason: error.reason });
96
+ return undefined;
97
+ }
98
+ }
@@ -0,0 +1,3 @@
1
+ import type { Program, Structure } from './types.ts';
2
+ /** program: the same text's syntax tree, when the lint tool already has it (ESLint's, from the same parser); otherwise parsed here. */
3
+ export declare function readStructure(jsxFile: string, text: string, program?: Program): Structure;
@@ -0,0 +1,59 @@
1
+ // structure.jsx (or .tsx) → components. Every JSX element inside Structure is a component.
2
+ // Only what the lint tool can't give a rule: each component's key and parentKey, to connect its CSS. The rest is in the tool's syntax tree.
3
+ import { parse } from '@typescript-eslint/parser';
4
+ import { getLiteralClassNames, getLiteralId, getNestingContext, emptyComponentCss } from "./utils.js";
5
+ import { createSourceReader, findDeclaration, getAttribute, walk } from "../shared/index.js";
6
+ /** program: the same text's syntax tree, when the lint tool already has it (ESLint's, from the same parser); otherwise parsed here. */
7
+ export function readStructure(jsxFile, text, program = parse(text, { filePath: jsxFile, ecmaFeatures: { jsx: true }, range: true })) {
8
+ const source = createSourceReader(text);
9
+ const elements = findElements(program);
10
+ return { components: toComponents(elements, assignKeys(elements, source), source) };
11
+ }
12
+ /** Every JSX element inside the `Structure` declaration, outermost first. */
13
+ function findElements(program) {
14
+ const structure = findDeclaration(program, 'Structure');
15
+ if (!structure)
16
+ return [];
17
+ const elements = [];
18
+ const siblingCounts = new Map();
19
+ walk(structure, [], (node, ancestors) => {
20
+ if (node.type !== 'JSXElement')
21
+ return;
22
+ const { parent, slot } = getNestingContext(ancestors);
23
+ const siblings = `${parent?.range[0]}|${slot}`;
24
+ const index = siblingCounts.get(siblings) ?? 0;
25
+ siblingCounts.set(siblings, index + 1);
26
+ elements.push({ node: node, parent, slot, index });
27
+ });
28
+ return elements;
29
+ }
30
+ /** Its literal id when no other element uses it, else its path: 'comp-x/div[1]', 'comp-x/backgroundLayer:Background[0]'. */
31
+ function assignKeys(elements, source) {
32
+ const ids = elements.map(({ node }) => getLiteralId(node)).filter((id) => id !== null);
33
+ const isUnique = (id) => ids.indexOf(id) === ids.lastIndexOf(id);
34
+ const keys = new Map();
35
+ for (const { node, parent, slot, index } of elements) {
36
+ const id = getLiteralId(node);
37
+ const parentKey = parent ? keys.get(parent) : null;
38
+ const path = `${parentKey ? `${parentKey}/` : ''}${slot ? `${slot}:` : ''}${source.code(node.openingElement.name)}[${index}]`;
39
+ keys.set(node, id && isUnique(id) ? id : path);
40
+ }
41
+ return keys;
42
+ }
43
+ /** slot, tag and index make the path keys; they aren't kept (a rule reads where an element sits with rulesApi.getAttributeElements). */
44
+ function toComponents(elements, keys, source) {
45
+ const components = {};
46
+ for (const { node, parent } of elements) {
47
+ const key = keys.get(node);
48
+ components[key] = {
49
+ key,
50
+ sourceStart: node.range[0],
51
+ id: getLiteralId(node),
52
+ classNames: getLiteralClassNames(node),
53
+ parentKey: parent ? keys.get(parent) : null,
54
+ css: emptyComponentCss(),
55
+ loc: source.loc(getAttribute(node, 'id') ?? node.openingElement.name),
56
+ };
57
+ }
58
+ return components;
59
+ }
@@ -0,0 +1,6 @@
1
+ import type { Root } from 'postcss';
2
+ import type { Mixins } from './types.ts';
3
+ /** `@mixin --name { @result { … } }` (or without @result) → name → its body. No theme → no mixins. */
4
+ export declare function readMixins(theme: Root | undefined): Mixins;
5
+ /** '--button-primary' and '--button-primary()' are the same mixin. */
6
+ export declare const mixinName: (params: string) => string;
@@ -0,0 +1,14 @@
1
+ /** `@mixin --name { @result { … } }` (or without @result) → name → its body. No theme → no mixins. */
2
+ export function readMixins(theme) {
3
+ const mixins = new Map();
4
+ if (theme === undefined)
5
+ return mixins;
6
+ theme.walkAtRules('mixin', (mixin) => {
7
+ const first = mixin.first;
8
+ const body = first?.type === 'atrule' && first.name === 'result' ? first : mixin;
9
+ mixins.set(mixinName(mixin.params), body.nodes ?? []);
10
+ });
11
+ return mixins;
12
+ }
13
+ /** '--button-primary' and '--button-primary()' are the same mixin. */
14
+ export const mixinName = (params) => params.trim().replace(/\(\)$/, '');
@@ -0,0 +1,92 @@
1
+ import type { TSESTree } from '@typescript-eslint/utils';
2
+ import type { ChildNode, Root } from 'postcss';
3
+ import type { Loc, Position } from '../shared/index.ts';
4
+ import type { CSS_FILES, CSS_KINDS } from './constants.ts';
5
+ export interface PageModel {
6
+ path: string;
7
+ kind: 'page' | 'global';
8
+ jsxFile: string;
9
+ components: Record<string, Component>;
10
+ css: PageCss;
11
+ }
12
+ export interface Component {
13
+ /** Its id when it's a literal used once in the file, else its path: 'comp-x/div[1]'. */
14
+ key: string;
15
+ /** JSX element start offset, used to match a lint tool's JSX nodes. */
16
+ sourceStart: number;
17
+ id: string | null;
18
+ /** Its literal className, split on spaces: className="comp-x card" → ['comp-x', 'card']; of an array, its first item (getLiteralClassNames). The import matches a `.x` rule to the element whose className has x. */
19
+ classNames: string[];
20
+ parentKey: string | null;
21
+ /** Its CSS: the `.x` rules of each of its class names. A component without a className has none. */
22
+ css: ComponentCss;
23
+ loc: Loc;
24
+ }
25
+ /** layout.css, style.css, and what a component gets from the theme through @apply. */
26
+ export type CssKind = (typeof CSS_KINDS)[number];
27
+ export interface CssDeclaration {
28
+ value: string;
29
+ media: string | null;
30
+ /** The selector with the component written as `&`: '&', '&:hover', '& [data-ie-name="label"]', plus its @origami-target. */
31
+ selector: string;
32
+ /** Where it's written in the page: its own line, or for a mixin's line (kind 'theme') the `@apply` that puts it in. */
33
+ file: CssFile;
34
+ line: number;
35
+ column: number;
36
+ }
37
+ /** A page's own CSS files, in the order they're read. */
38
+ export type CssFile = (typeof CSS_FILES)[number];
39
+ export type CssByProperty = Record<string, CssDeclaration[]>;
40
+ /** One component's CSS by kind. */
41
+ export type ComponentCss = Record<CssKind, CssByProperty>;
42
+ export interface PageCss {
43
+ /** `.x` rules by class name, for the class names a component has. */
44
+ byClassName: Record<string, ComponentCss>;
45
+ /** CSS no component takes, by its selector's first class or id as written: '.gone' (no component has that class), '#comp-x' (the import doesn't read #id). */
46
+ unconnectedCss: Record<string, ComponentCss>;
47
+ /** Selectors without a `.x` or `#x` (`:root`, `div`). Their `selector` is the whole selector. */
48
+ globalCss: ComponentCss;
49
+ /** The page's CSS files as parsed (postcss), theme.css included (read and parsed once per page), for rules about how a file is written; undefined for a file the site doesn't have, or one postcss can't parse (then it's in `errors`). */
50
+ files: Record<CssKind, Root | undefined>;
51
+ /** The files postcss couldn't parse, with its reason: nothing of such a file is read. */
52
+ errors: CssError[];
53
+ }
54
+ /** A CSS file postcss refused: a missing `}`, an unclosed `[` or quote. The import's parser (converter cssFileParser.ts) is postcss too. */
55
+ export interface CssError {
56
+ file: CssFile | 'theme.css';
57
+ line: number;
58
+ column: number;
59
+ /** postcss's reason: 'Unclosed block', 'Unclosed bracket'. */
60
+ reason: string;
61
+ }
62
+ /** The structure file's text, and its syntax tree when the lint tool already parsed it. */
63
+ export interface StructureSource {
64
+ jsxFile: string;
65
+ text: string;
66
+ program?: TSESTree.Program;
67
+ }
68
+ export type Structure = Pick<PageModel, 'components'>;
69
+ export type JSXElement = TSESTree.JSXElement;
70
+ export type Program = TSESTree.Program;
71
+ /** A JSX element inside Structure, where it sits, and its position among its siblings. */
72
+ export interface StructureElement {
73
+ node: JSXElement;
74
+ parent: JSXElement | null;
75
+ slot: string | null;
76
+ index: number;
77
+ }
78
+ /** Where a CSS node is: the full selectors of the rules around it, and the @media / @origami-target it's in. */
79
+ export interface CssContext {
80
+ selectors: string[];
81
+ media: string | null;
82
+ target: string | null;
83
+ kind: CssKind;
84
+ file: CssFile;
85
+ /** The mixins being applied around this node, to stop a mixin that applies itself. */
86
+ applying: string[];
87
+ /** Inside a mixin: the page's `@apply` that put it in, where its lines are placed. */
88
+ appliedAt: Position | null;
89
+ }
90
+ export type AddLine = (selector: string, property: string, value: string, context: CssContext, place: Position) => void;
91
+ /** theme.css's mixins: what `@apply --name` puts in a rule, by name. */
92
+ export type Mixins = Map<string, ChildNode[]>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,16 @@
1
+ import { type AstNode } from '../shared/index.ts';
2
+ import type { ComponentCss, JSXElement } from './types.ts';
3
+ /** The nearest enclosing element, and the prop we came through if the element is passed in one. */
4
+ export declare function getNestingContext(ancestors: AstNode[]): {
5
+ parent: JSXElement | null;
6
+ slot: string | null;
7
+ };
8
+ /** id="comp-x" → 'comp-x'; no id, or id={variable} → null. */
9
+ export declare const getLiteralId: (node: JSXElement) => string | null;
10
+ /**
11
+ * className="comp-x card" → ['comp-x', 'card']; className={['comp-x', 'open', {…}]} → ['comp-x'], as the import reads it: the
12
+ * array's first item is the class, the rest are states (parseJsx.ts splitClassName). No className, or className={variable} → [].
13
+ */
14
+ export declare function getLiteralClassNames(node: JSXElement): string[];
15
+ /** No CSS of any kind. No prototype: a property written `constructor` or `__proto__` is a key like any other, not Object's. */
16
+ export declare const emptyComponentCss: () => ComponentCss;
@@ -0,0 +1,29 @@
1
+ import { getAttribute, getName } from "../shared/index.js";
2
+ import { CSS_KINDS } from "./constants.js";
3
+ // ── JSX ──
4
+ /** The nearest enclosing element, and the prop we came through if the element is passed in one. */
5
+ export function getNestingContext(ancestors) {
6
+ let slot = null;
7
+ for (const node of [...ancestors].reverse()) {
8
+ if (node.type === 'JSXAttribute' && slot === null)
9
+ slot = getName(node);
10
+ if (node.type === 'JSXElement')
11
+ return { parent: node, slot };
12
+ }
13
+ return { parent: null, slot: null };
14
+ }
15
+ /** id="comp-x" → 'comp-x'; no id, or id={variable} → null. */
16
+ export const getLiteralId = (node) => getStringLiteral(getAttribute(node, 'id')?.value);
17
+ /**
18
+ * className="comp-x card" → ['comp-x', 'card']; className={['comp-x', 'open', {…}]} → ['comp-x'], as the import reads it: the
19
+ * array's first item is the class, the rest are states (parseJsx.ts splitClassName). No className, or className={variable} → [].
20
+ */
21
+ export function getLiteralClassNames(node) {
22
+ const value = getAttribute(node, 'className')?.value;
23
+ const classes = value?.type === 'JSXExpressionContainer' && value.expression.type === 'ArrayExpression' ? getStringLiteral(value.expression.elements[0]) : getStringLiteral(value);
24
+ return classes?.split(/\s+/).filter(Boolean) ?? [];
25
+ }
26
+ const getStringLiteral = (node) => node?.type === 'Literal' && typeof node.value === 'string' ? node.value : null;
27
+ // ── CSS ──
28
+ /** No CSS of any kind. No prototype: a property written `constructor` or `__proto__` is a key like any other, not Object's. */
29
+ export const emptyComponentCss = () => Object.fromEntries(CSS_KINDS.map((kind) => [kind, Object.create(null)]));
@@ -0,0 +1,5 @@
1
+ export { getPackageJsonLine, readPackageJson } from './package-json.ts';
2
+ export { createProjectModel } from './project-model.ts';
3
+ export type { PackageJson, ProjectModel } from './types.ts';
4
+ export { getMenuFiles } from './menu-files.ts';
5
+ export { getPopupFiles } from './popups.ts';
@@ -0,0 +1,5 @@
1
+ // The project model, beside the page model (part 2): what a project rule sees of the site.
2
+ export { getPackageJsonLine, readPackageJson } from "./package-json.js";
3
+ export { createProjectModel } from "./project-model.js";
4
+ export { getMenuFiles } from "./menu-files.js";
5
+ export { getPopupFiles } from "./popups.js";
@@ -0,0 +1,4 @@
1
+ import type { ProjectModel } from './types.ts';
2
+ /** The files of the menus item the converter gets, the way the import reads its nodes: not its version, not a skipped
3
+ * name, not in a sub-folder, and of a format the import sends (another one stops it first: item-folders reports it). */
4
+ export declare function getMenuFiles(project: ProjectModel): string[];
@@ -0,0 +1,14 @@
1
+ // The site's menu files, as a project rule reads them.
2
+ import { isSkippedName, itemFormats } from '@wix/origami-types';
3
+ import { GLOBAL_MENUS_FOLDER as MENUS_FOLDER } from "../shared/index.js";
4
+ /** The files of the menus item the converter gets, the way the import reads its nodes: not its version, not a skipped
5
+ * name, not in a sub-folder, and of a format the import sends (another one stops it first: item-folders reports it). */
6
+ export function getMenuFiles(project) {
7
+ return project.files.filter((file) => {
8
+ if (!file.startsWith(`${MENUS_FOLDER}/`))
9
+ return false;
10
+ const name = file.slice(MENUS_FOLDER.length + 1);
11
+ const dot = name.lastIndexOf('.');
12
+ return !name.includes('/') && name !== 'version' && !isSkippedName(name) && itemFormats.includes(dot < 0 ? '' : name.slice(dot + 1));
13
+ });
14
+ }
@@ -0,0 +1,5 @@
1
+ import type { PackageJson, ProjectModel } from './types.ts';
2
+ /** The file parsed; undefined when the site doesn't have it. Throws, naming the file, for one that isn't JSON (yarn fails on it before the lint runs). */
3
+ export declare function readPackageJson(project: ProjectModel, file: string): PackageJson | undefined;
4
+ /** The line of the section's key (`"dependencies": {`), or of the key inside it; the section's line when the key isn't in it, 1 when the section isn't there. */
5
+ export declare function getPackageJsonLine(project: ProjectModel, file: string, section: string, key?: string): number;
@@ -0,0 +1,27 @@
1
+ /** The file parsed; undefined when the site doesn't have it. Throws, naming the file, for one that isn't JSON (yarn fails on it before the lint runs). */
2
+ export function readPackageJson(project, file) {
3
+ const text = project.readFile(file);
4
+ if (text === undefined)
5
+ return undefined;
6
+ try {
7
+ return JSON.parse(text);
8
+ }
9
+ catch (error) {
10
+ throw new SyntaxError(`${file} is not valid JSON: ${error instanceof Error ? error.message : String(error)}`);
11
+ }
12
+ }
13
+ /** The line of the section's key (`"dependencies": {`), or of the key inside it; the section's line when the key isn't in it, 1 when the section isn't there. */
14
+ export function getPackageJsonLine(project, file, section, key) {
15
+ const lines = (project.readFile(file) ?? '').split('\n');
16
+ const start = lines.findIndex((line) => line.trimStart().startsWith(`${JSON.stringify(section)}:`));
17
+ if (start === -1)
18
+ return 1;
19
+ const indent = lines[start].length - lines[start].trimStart().length;
20
+ for (let at = start + 1; key !== undefined && at < lines.length; at++) {
21
+ if (lines[at].trimStart().startsWith('}') && lines[at].length - lines[at].trimStart().length === indent)
22
+ break;
23
+ if (lines[at].trimStart().startsWith(`${JSON.stringify(key)}:`))
24
+ return at + 1;
25
+ }
26
+ return start + 1;
27
+ }
@@ -0,0 +1,4 @@
1
+ import type { ProjectModel } from './types.ts';
2
+ /** Each popup's structure file: one popup per folder directly under popups/, written like a page (structure.tsx wins over structure.jsx).
3
+ * findPages, which picks the page rules' pages, has no popups. */
4
+ export declare function getPopupFiles(project: ProjectModel): string[];
@@ -0,0 +1,12 @@
1
+ // The site's popups, as a project rule reads them.
2
+ import { JSX_FILES } from "../page-model/index.js";
3
+ import { POPUPS_FOLDER } from "../shared/index.js";
4
+ /** Each popup's structure file: one popup per folder directly under popups/, written like a page (structure.tsx wins over structure.jsx).
5
+ * findPages, which picks the page rules' pages, has no popups. */
6
+ export function getPopupFiles(project) {
7
+ const existing = new Set(project.files);
8
+ const folders = new Set(project.files
9
+ .filter((file) => file.startsWith(`${POPUPS_FOLDER}/`) && file.split('/').length === POPUPS_FOLDER.split('/').length + 2)
10
+ .map((file) => file.slice(0, file.lastIndexOf('/'))));
11
+ return [...folders].flatMap((folder) => JSX_FILES.map((name) => `${folder}/${name}`).find((file) => existing.has(file)) ?? []);
12
+ }
@@ -0,0 +1,4 @@
1
+ import { type ReadFile } from '../shared/index.ts';
2
+ import type { ProjectModel } from './types.ts';
3
+ /** The rule's view of the site: its files, and readFile. ruleName names the rule in readFile's error. */
4
+ export declare function createProjectModel(ruleName: string, files: readonly string[], readFile: ReadFile, experimentalPopups?: boolean): ProjectModel;
@@ -0,0 +1,14 @@
1
+ // What a project rule sees of the site: every file of it, and a readFile that stays inside the site.
2
+ import { posixPath } from "../shared/index.js";
3
+ /** The rule's view of the site: its files, and readFile. ruleName names the rule in readFile's error. */
4
+ export function createProjectModel(ruleName, files, readFile, experimentalPopups = false) {
5
+ return {
6
+ experimentalPopups,
7
+ files,
8
+ readFile(file) {
9
+ if (!posixPath.isSitePath(file))
10
+ throw new Error(`project rule ${ruleName} read ${file}: write a path from the site root, without ..`);
11
+ return readFile(file);
12
+ },
13
+ };
14
+ }
@@ -0,0 +1,20 @@
1
+ export interface ProjectModel {
2
+ /** Whether the caller enabled experimental popup validation. Omitted by older callers means disabled. */
3
+ readonly experimentalPopups?: boolean;
4
+ /** Every file of the site, from the site root. */
5
+ readonly files: readonly string[];
6
+ /** The file's text; undefined when it can't be read. Throws for a path that leaves the site (.., a leading /). */
7
+ readFile(file: string): string | undefined;
8
+ }
9
+ /** The fields of a package.json the project rules check. */
10
+ export interface PackageJson {
11
+ name?: string;
12
+ scripts?: Record<string, string>;
13
+ dependencies?: Record<string, string>;
14
+ devDependencies?: Record<string, string>;
15
+ /** The site package's export format: its adapter and layout (siteLayout.ts reads `layout`). */
16
+ origami?: {
17
+ adapter?: string;
18
+ layout?: unknown;
19
+ };
20
+ }
@@ -0,0 +1,2 @@
1
+ // What a project rule sees of the site, and a package.json of it as a project rule reads it.
2
+ export {};
@@ -0,0 +1,4 @@
1
+ export * from './site-app/index.ts';
2
+ export * from './project/index.ts';
3
+ export * from './site-code/index.ts';
4
+ export type { Editor, RuleBase, Severity } from './types.ts';
@@ -0,0 +1,5 @@
1
+ // Part 4 — rules: how a page rule is written (site-app/). The rules themselves live in rule packages, which list them and
2
+ // hand the list to validate().
3
+ export * from "./site-app/index.js";
4
+ export * from "./project/index.js";
5
+ export * from "./site-code/index.js";
@@ -0,0 +1,5 @@
1
+ import type { ProjectRule } from './types.ts';
2
+ /** Nothing changes a rule once it's defined. */
3
+ export declare function defineProjectRule<const Checks extends readonly string[]>(rule: Omit<ProjectRule<NoInfer<Checks[number]>>, 'scope' | 'checks'> & {
4
+ checks: Checks;
5
+ }): ProjectRule<Checks[number]>;
@@ -0,0 +1,4 @@
1
+ /** Nothing changes a rule once it's defined. */
2
+ export function defineProjectRule(rule) {
3
+ return Object.freeze({ ...rule, scope: 'project' });
4
+ }
@@ -0,0 +1,2 @@
1
+ export { defineProjectRule } from './define-project-rule.ts';
2
+ export type { ProjectProblem, ProjectRule } from './types.ts';
@@ -0,0 +1,2 @@
1
+ // How a project rule is written: what a project rule file imports.
2
+ export { defineProjectRule } from "./define-project-rule.js";
@@ -0,0 +1,16 @@
1
+ import type { ProjectModel } from '../../project-model/index.ts';
2
+ import type { RuleBase } from '../index.ts';
3
+ /** One problem a project rule found: the file or folder it is on (from the site root), and what to write instead. */
4
+ export interface ProjectProblem<Check extends string> {
5
+ check: Check;
6
+ file: string;
7
+ /** 1 when not given. */
8
+ line?: number;
9
+ message: string;
10
+ }
11
+ /** name, editors, tags? and severity? come from RuleBase, the same as a page rule's. */
12
+ export interface ProjectRule<Check extends string> extends RuleBase {
13
+ checks: readonly Check[];
14
+ scope: 'project';
15
+ check(project: ProjectModel): ProjectProblem<Check>[];
16
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,5 @@
1
+ import type { Rule } from './types.ts';
2
+ /** Nothing changes a rule once it's defined. */
3
+ export declare function defineRule<const Checks extends readonly string[]>(rule: Omit<Rule<NoInfer<Checks[number]>>, 'checks'> & {
4
+ checks: Checks;
5
+ }): Rule<Checks[number]>;
@@ -0,0 +1,4 @@
1
+ /** Nothing changes a rule once it's defined. */
2
+ export function defineRule(rule) {
3
+ return Object.freeze(rule);
4
+ }
@@ -0,0 +1,2 @@
1
+ export { defineRule } from './define-rule.ts';
2
+ export type { Callbacks, JSXElementInput, Rule, RuleContext, RuleInput } from './types.ts';
@@ -0,0 +1 @@
1
+ export { defineRule } from "./define-rule.js";
@@ -0,0 +1,26 @@
1
+ import type { JSXElement, RulesApi, Report } from '../../rules-api/index.ts';
2
+ import type { CallbackName, CallbackNode, ToolContext } from '../../rules-adapter/index.ts';
3
+ import type { RuleBase } from '../index.ts';
4
+ /** The tool's context for the file (filename, cwd, sourceCode, options), without its report: `report` is the only way to report. */
5
+ export type RuleContext = Omit<ToolContext, 'report'>;
6
+ /** What every callback gets, by name. */
7
+ export interface RuleInput<Node, Check extends string> {
8
+ node: Node;
9
+ /** This file's page and the functions over it. The same object for every rule on the file. */
10
+ rulesApi: RulesApi;
11
+ report: Report<Check>;
12
+ context: RuleContext;
13
+ }
14
+ /** What a callback on a JSX element gets. */
15
+ export type JSXElementInput<Check extends string> = RuleInput<JSXElement, Check>;
16
+ /** Callback name (the tool's node type, or the node type + ':exit') → the check. node is typed for its name. */
17
+ export type Callbacks<Check extends string> = {
18
+ [K in CallbackName]?: (input: RuleInput<CallbackNode<K>, Check>) => void;
19
+ };
20
+ /** A page rule: ESLint runs its callbacks on each page's structure file. */
21
+ export interface Rule<Check extends string> extends RuleBase {
22
+ checks: readonly Check[];
23
+ /** What the rule runs on: 'page' (the default), one page at a time. */
24
+ scope?: 'page';
25
+ callbacks: Callbacks<Check>;
26
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,5 @@
1
+ import type { SiteCodeRule } from './types.ts';
2
+ /** Nothing changes a rule once it's defined. */
3
+ export declare function defineSiteCodeRule<const Checks extends readonly string[]>(rule: Omit<SiteCodeRule<NoInfer<Checks[number]>>, 'scope' | 'checks'> & {
4
+ checks: Checks;
5
+ }): SiteCodeRule<Checks[number]>;
@@ -0,0 +1,4 @@
1
+ /** Nothing changes a rule once it's defined. */
2
+ export function defineSiteCodeRule(rule) {
3
+ return Object.freeze({ ...rule, scope: 'site-code' });
4
+ }
@@ -0,0 +1,2 @@
1
+ export { defineSiteCodeRule } from './define-site-code-rule.ts';
2
+ export type { SiteCodeRule } from './types.ts';
@@ -0,0 +1,2 @@
1
+ // How a site-code rule is written: what a site-code rule file imports.
2
+ export { defineSiteCodeRule } from "./define-site-code-rule.js";
@@ -0,0 +1,11 @@
1
+ import type { ProjectModel } from '../../project-model/index.ts';
2
+ import type { SiteCodeModel } from '../../site-code-model/index.ts';
3
+ import type { ProjectProblem } from '../project/index.ts';
4
+ import type { RuleBase } from '../types.ts';
5
+ /** name, editors, tags? and severity? come from RuleBase, the same as a page rule's. Its problems are a project rule's. */
6
+ export interface SiteCodeRule<Check extends string> extends RuleBase {
7
+ checks: readonly Check[];
8
+ scope: 'site-code';
9
+ /** siteCode: the site code's model, read once per validate() for every site-code rule; project: the site's files, as a project rule sees them. */
10
+ check(siteCode: SiteCodeModel, project: ProjectModel): ProjectProblem<Check>[];
11
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,13 @@
1
+ /** An editor a site can be imported into. Only Studio 2 today; Harmony joins it later (see docs/parts.md "Editors"). */
2
+ export type Editor = 'studio2';
3
+ /** 'error' (the default) fails the run; 'warn' only reports the problem. */
4
+ export type Severity = 'error' | 'warn';
5
+ /** The fields every kind of rule shares. */
6
+ export interface RuleBase {
7
+ name: string;
8
+ /** The editors whose import this rule validates. 'all': every editor, and any run that isn't for an import. */
9
+ editors: 'all' | readonly Editor[];
10
+ /** Free-form labels for grouping rules (e.g. picking which ones an action runs). Nothing reads them yet. */
11
+ tags?: readonly string[];
12
+ severity?: Severity;
13
+ }