@wix/origami-lint 1.34.1 → 1.34.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/dist/lint.js CHANGED
@@ -1,13 +1,17 @@
1
1
  #!/usr/bin/env node
2
- // Origami lint: lints the pages whose files changed since the last passing run. See lint/README.md.
2
+ // Origami lint: runs the project rules on the site's own files, then lints the pages whose files changed since the last
3
+ // passing run. See lint/README.md.
3
4
  import { getChangedFiles, markPassing } from "./history/index.js";
4
5
  import { runRules } from "./rules-adapter/index.js";
5
6
  import { findPagesToLint } from "./page-model/index.js";
7
+ import { runProjectRules } from "./project-rules-runner/index.js";
6
8
  import { checkStartedAtSiteRoot } from "./shared/paths.js";
7
9
  checkStartedAtSiteRoot();
8
10
  const changedFiles = getChangedFiles();
11
+ const projectPassed = runProjectRules(changedFiles.all);
9
12
  const pagesToLint = findPagesToLint(changedFiles);
10
- const passed = runRules(pagesToLint);
13
+ const pagesPassed = runRules(pagesToLint);
14
+ const passed = projectPassed && pagesPassed;
11
15
  if (passed)
12
16
  markPassing(changedFiles);
13
17
  process.exit(passed ? 0 : 1);
@@ -0,0 +1,2 @@
1
+ // The project model, beside the page model (part 2): what a project rule sees of the site.
2
+ export { createProjectModel } from "./project-model.js";
@@ -0,0 +1,15 @@
1
+ /** A path from the site root that stays inside the site: no leading /, no .. segment. */
2
+ function isSitePath(file) {
3
+ return !file.startsWith('/') && !file.split('/').includes('..');
4
+ }
5
+ /** The rule's view of the site: its files, and readFile. ruleName names the rule in readFile's error. */
6
+ export function createProjectModel(ruleName, files, readFile) {
7
+ return {
8
+ files,
9
+ readFile(file) {
10
+ if (!isSitePath(file))
11
+ throw new Error(`project rule ${ruleName} read ${file}: write a path from the site root, without ..`);
12
+ return readFile(file);
13
+ },
14
+ };
15
+ }
@@ -0,0 +1,3 @@
1
+ // The project rules runner, beside the rules adapter (part 5): runs the rules that check the site's own files and
2
+ // folders (lint/rules/project/), not one page.
3
+ export { runProjectRules } from "./run-project-rules.js";
@@ -0,0 +1,44 @@
1
+ // Runs every project rule, on every run, on the whole site; prints each problem in the line shape the page rules'
2
+ // problems print in.
3
+ import { readFileSync } from 'node:fs';
4
+ import path from 'node:path';
5
+ import { projectRuleRegistry } from "../rules/project/index.js";
6
+ import { log } from "../shared/log.js";
7
+ import { ROOT } from "../shared/paths.js";
8
+ import { createProjectModel } from "../project-model/index.js";
9
+ /** true when no error rule reported a problem, on the site of these files. Logs how many rules run, and whether they passed. */
10
+ export function runProjectRules(files, { rules = projectRuleRegistry, readFile = readFromSite, print = console.log } = {}) {
11
+ log(`${rules.length} project rules to run`);
12
+ let passed = true;
13
+ for (const rule of rules) {
14
+ let problems;
15
+ try {
16
+ problems = rule.check(createProjectModel(rule.name, files, readFile));
17
+ }
18
+ catch (error) {
19
+ // A rule that couldn't finish (a file it can't parse, a path out of the site) fails the run, whatever its severity.
20
+ print(formatProblem({ name: rule.name }, { file: '.', message: `the rule couldn't finish, so this run can't pass: ${error instanceof Error ? error.message : String(error)}` }));
21
+ passed = false;
22
+ continue;
23
+ }
24
+ for (const problem of problems) {
25
+ print(formatProblem(rule, problem));
26
+ if ((rule.severity ?? 'error') === 'error')
27
+ passed = false;
28
+ }
29
+ }
30
+ log(passed ? 'project rules passed' : 'project rules failed');
31
+ return passed;
32
+ }
33
+ /** `file:line:1: error origami(name): message`, the shape oxlint prints the page rules' problems in. */
34
+ export function formatProblem({ name, severity = 'error' }, { file, line = 1, message }) {
35
+ return `${file}:${line}:1: ${severity === 'error' ? 'error' : 'warning'} origami(${name}): ${message}`;
36
+ }
37
+ function readFromSite(file) {
38
+ try {
39
+ return readFileSync(path.join(ROOT, file), 'utf8');
40
+ }
41
+ catch {
42
+ return undefined;
43
+ }
44
+ }
@@ -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.js";
2
+ export const projectRuleRegistry = [];
@@ -31,7 +31,7 @@ export const RESERVED_ENTRY_KEYS = ['data', 'slots', 'elementProps', 'bindings',
31
31
  export const DATA_KEY = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
32
32
  /** By import source; the wrappers also by the imported name, since their packages export other things (`Show`, `Interact`). */
33
33
  const TYPE_BY_IMPORT_SOURCE = [
34
- [new RegExp(`^${SYSTEM}/Page$`), 'page'],
34
+ [new RegExp(`^${SYSTEM}/(?:layout/)?Page$`), 'page'],
35
35
  [new RegExp(`^${SYSTEM}/layout/Header$`), 'headerLandmark'],
36
36
  [new RegExp(`^${SYSTEM}/layout/Footer$`), 'footerLandmark'],
37
37
  [new RegExp(`^${SYSTEM}/(?:layout/)?Section$`), 'section'],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wix/origami-lint",
3
- "version": "1.34.1",
3
+ "version": "1.34.3",
4
4
  "description": "Offline lint for an exported Origami site: lints the pages that changed since the last passing run",
5
5
  "type": "module",
6
6
  "bin": {
@@ -51,5 +51,5 @@
51
51
  ]
52
52
  }
53
53
  },
54
- "falconPackageHash": "73a2fcabe83927b716b3f75d2c0db9dc6081ecbb3de57134cbd36383"
54
+ "falconPackageHash": "162134940d5c0140fe1b81ff9111d25a7f98f0201dfbbc32130245ca"
55
55
  }