@amritk/helpers 0.16.0 → 0.18.0
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.
|
@@ -5,7 +5,7 @@ export declare const escapeRegexPattern: (pattern: string) => string;
|
|
|
5
5
|
*
|
|
6
6
|
* A 2020-12 `pattern` is meant to be read as Unicode — Ajv compiles with `u` by
|
|
7
7
|
* default, and so does `@amritk/runtime-validators` (`compilePattern` in
|
|
8
|
-
* `interpreter/
|
|
8
|
+
* `interpreter/compile.ts`) — and without the flag `\p{L}` is misread as a
|
|
9
9
|
* literal `p` and `^.$` rejects a single astral character. The flag is not
|
|
10
10
|
* unconditional because `u` also outlaws escapes that are legal without it
|
|
11
11
|
* (`\-`, a bare `\p`), and a pattern using one would turn into a syntax error in
|
|
@@ -23,7 +23,19 @@ export type GenerateIndexBarrelOptions = {
|
|
|
23
23
|
* Builds the `index.ts` barrel that re-exports every generated module. This is
|
|
24
24
|
* the shared version of the near-identical barrel each generator used to build
|
|
25
25
|
* inline: it scans each file's source for `export type` / `export const`
|
|
26
|
-
* declarations and emits
|
|
26
|
+
* declarations and emits re-exports per module, sorted by filename.
|
|
27
|
+
*
|
|
28
|
+
* Values are re-exported as `const` aliases (`import { parseFoo as parseFoo$0 }
|
|
29
|
+
* … export const parseFoo = parseFoo$0`) rather than through `export { parseFoo }
|
|
30
|
+
* from './foo.js'`. Under ESM the two are equivalent, but TypeScript lowers a
|
|
31
|
+
* re-export to CommonJS as an **accessor**
|
|
32
|
+
* (`Object.defineProperty(exports, 'parseFoo', { get() { … } })`), so every call
|
|
33
|
+
* reached through the barrel pays a getter — and a consumer importing from a
|
|
34
|
+
* barrel that itself barrels a subdirectory pays one per hop. The alias form
|
|
35
|
+
* lowers to a plain data property (`exports.parseFoo = foo_1.parseFoo`), which
|
|
36
|
+
* costs nothing, and stays statically analysable for tree-shaking in esbuild and
|
|
37
|
+
* rollup. Types keep the `export type { … } from` form: a type cannot be aliased
|
|
38
|
+
* through a `const`, and a type re-export emits no runtime code either way.
|
|
27
39
|
*
|
|
28
40
|
* Files under `_helpers/` are internal runtime helpers (embedded-mode output)
|
|
29
41
|
* and are never re-exported. Modules that expose nothing are skipped.
|
|
@@ -40,7 +40,8 @@ const generateIndexBarrel = (files, options = {}) => {
|
|
|
40
40
|
const typesOnly = options.typesOnly ?? false;
|
|
41
41
|
const importExt = options.importExt ?? "js";
|
|
42
42
|
const sortedFiles = files.filter((file) => !file.filename.startsWith("_helpers/")).sort((a, b) => a.filename.localeCompare(b.filename));
|
|
43
|
-
|
|
43
|
+
const modules = [];
|
|
44
|
+
const declaredNames = /* @__PURE__ */ new Set();
|
|
44
45
|
for (const file of sortedFiles) {
|
|
45
46
|
const moduleName = file.filename.replace(/\.ts$/, "");
|
|
46
47
|
const typeNames = [];
|
|
@@ -48,16 +49,41 @@ const generateIndexBarrel = (files, options = {}) => {
|
|
|
48
49
|
collectExportNames(file.content, typeNames, constNames);
|
|
49
50
|
if (typeNames.length === 0 && constNames.length === 0)
|
|
50
51
|
continue;
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
52
|
+
modules.push({ specifier: `./${moduleName}.${importExt}`, typeNames, constNames });
|
|
53
|
+
for (const name of constNames)
|
|
54
|
+
declaredNames.add(name);
|
|
55
|
+
}
|
|
56
|
+
if (typesOnly) {
|
|
57
|
+
let indexContent = "";
|
|
58
|
+
for (const { specifier, typeNames } of modules) {
|
|
59
|
+
indexContent += `export type { ${typeNames.join(", ")} } from '${specifier}';
|
|
57
60
|
`;
|
|
58
61
|
}
|
|
62
|
+
return indexContent;
|
|
59
63
|
}
|
|
60
|
-
|
|
64
|
+
const importLines = [];
|
|
65
|
+
const typeLines = [];
|
|
66
|
+
const valueLines = [];
|
|
67
|
+
modules.forEach(({ specifier, typeNames, constNames }, index) => {
|
|
68
|
+
if (typeNames.length > 0) {
|
|
69
|
+
typeLines.push(`export type { ${typeNames.join(", ")} } from '${specifier}';`);
|
|
70
|
+
}
|
|
71
|
+
if (constNames.length === 0)
|
|
72
|
+
return;
|
|
73
|
+
const aliases = constNames.map((name) => {
|
|
74
|
+
let alias = `${name}$${index}`;
|
|
75
|
+
while (declaredNames.has(alias))
|
|
76
|
+
alias += "$";
|
|
77
|
+
declaredNames.add(alias);
|
|
78
|
+
return alias;
|
|
79
|
+
});
|
|
80
|
+
importLines.push(`import { ${constNames.map((name, i) => `${name} as ${aliases[i]}`).join(", ")} } from '${specifier}';`);
|
|
81
|
+
for (const [i, name] of constNames.entries())
|
|
82
|
+
valueLines.push(`export const ${name} = ${aliases[i]};`);
|
|
83
|
+
});
|
|
84
|
+
const sections = [importLines, typeLines, valueLines].filter((section) => section.length > 0);
|
|
85
|
+
return sections.map((section) => `${section.join("\n")}
|
|
86
|
+
`).join("\n");
|
|
61
87
|
};
|
|
62
88
|
export {
|
|
63
89
|
generateIndexBarrel
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Emits code for a JSON Schema `multipleOf` check that agrees with the runtime
|
|
3
|
-
* interpreter (`packages/runtime-validators/src/interpreter/
|
|
3
|
+
* interpreter (`packages/runtime-validators/src/interpreter/compile.ts`).
|
|
4
4
|
*
|
|
5
5
|
* The interpreter answers the keyword in two ways depending on the divisor, and
|
|
6
6
|
* so does this emitter — the two must not diverge, or a generated validator and
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Emits code for the JSON Schema numeric *bound* keywords — `minimum`,
|
|
3
|
+
* `maximum`, `exclusiveMinimum`, `exclusiveMaximum` — that agrees with the
|
|
4
|
+
* runtime interpreter (`packages/runtime-validators/src/interpreter/interpret.ts`).
|
|
5
|
+
*
|
|
6
|
+
* There is one rule here and it is easy to get wrong: **a bound is written as a
|
|
7
|
+
* pass condition and negated, never as a failure condition directly.** For every
|
|
8
|
+
* ordinary value the two spellings are identical:
|
|
9
|
+
*
|
|
10
|
+
* ```
|
|
11
|
+
* !(x >= min) // pass condition, negated
|
|
12
|
+
* x < min // failure condition, written directly
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* For `NaN` they are opposite. `NaN` compares `false` against every relational
|
|
16
|
+
* operator, so `!(NaN >= 5)` is `true` — the value fails the bound, which is
|
|
17
|
+
* Ajv's verdict and the interpreter's — while `NaN < 5` is `false`, which
|
|
18
|
+
* silently *accepts* it. A generated validator spelled the second way was more
|
|
19
|
+
* permissive than the interpreter for the same schema, and nothing caught it:
|
|
20
|
+
* a conformance corpus is JSON, and JSON cannot express `NaN`.
|
|
21
|
+
*
|
|
22
|
+
* The keyword-level semantics this encodes, all matching Ajv:
|
|
23
|
+
*
|
|
24
|
+
* - `NaN` fails any bound it is checked against.
|
|
25
|
+
* - `±Infinity` follows the ordinary comparison, so `Infinity` passes
|
|
26
|
+
* `minimum: 0` and fails `maximum: 10`.
|
|
27
|
+
* - A bare `type: 'number'` with no bound still accepts non-finite numbers.
|
|
28
|
+
* Only a bound (or `multipleOf`) rejects them.
|
|
29
|
+
* - Draft-04's boolean `exclusiveMinimum: true` / `exclusiveMaximum: true`
|
|
30
|
+
* makes a sibling `minimum` / `maximum` strict; draft-06+ replaced it with
|
|
31
|
+
* the standalone numeric keywords. Both forms are spelled through `strict`.
|
|
32
|
+
*
|
|
33
|
+
* `valueExpr` is inlined once per expression, so it must be a side-effect-free
|
|
34
|
+
* expression (a property read or a cached variable — which is all the generators
|
|
35
|
+
* ever pass). Every expression is self-parenthesized, so a caller can drop one
|
|
36
|
+
* into a `&&` / `||` chain or negate it without re-bracketing.
|
|
37
|
+
*
|
|
38
|
+
* Companion to `multiple-of-check`, which does the same for `multipleOf`.
|
|
39
|
+
*/
|
|
40
|
+
/** Which bound a check is for, in the direction it compares. */
|
|
41
|
+
export type BoundKind = 'minimum' | 'maximum';
|
|
42
|
+
/**
|
|
43
|
+
* Boolean expression that is TRUE when `valueExpr` satisfies the bound.
|
|
44
|
+
*
|
|
45
|
+
* @param strict `>` / `<` rather than `>=` / `<=` — draft-06+'s
|
|
46
|
+
* `exclusiveMinimum` / `exclusiveMaximum`, or draft-04's boolean form.
|
|
47
|
+
*/
|
|
48
|
+
export declare const boundPassExpr: (valueExpr: string, kind: BoundKind, bound: number, strict?: boolean) => string;
|
|
49
|
+
/**
|
|
50
|
+
* Boolean expression that is TRUE when `valueExpr` VIOLATES the bound (the error
|
|
51
|
+
* condition) — the negated pass condition, which is the whole point of this
|
|
52
|
+
* module. See the file comment for why the direct spelling is wrong.
|
|
53
|
+
*/
|
|
54
|
+
export declare const boundFailExpr: (valueExpr: string, kind: BoundKind, bound: number, strict?: boolean) => string;
|
|
55
|
+
/**
|
|
56
|
+
* The relational operator a bound's *message* names (`must be >= 5`), kept here
|
|
57
|
+
* so the message and the check it describes cannot drift apart.
|
|
58
|
+
*/
|
|
59
|
+
export declare const boundOperator: (kind: BoundKind, strict?: boolean) => string;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const boundPassExpr = (valueExpr, kind, bound, strict = false) => {
|
|
2
|
+
const operator = kind === "minimum" ? strict ? ">" : ">=" : strict ? "<" : "<=";
|
|
3
|
+
return `(${valueExpr} ${operator} ${bound})`;
|
|
4
|
+
};
|
|
5
|
+
const boundFailExpr = (valueExpr, kind, bound, strict = false) => `!${boundPassExpr(valueExpr, kind, bound, strict)}`;
|
|
6
|
+
const boundOperator = (kind, strict = false) => kind === "minimum" ? strict ? ">" : ">=" : strict ? "<" : "<=";
|
|
7
|
+
export {
|
|
8
|
+
boundFailExpr,
|
|
9
|
+
boundOperator,
|
|
10
|
+
boundPassExpr
|
|
11
|
+
};
|