Skip to content

Commit 291adbd

Browse files
committed
fix(spec): a refusal code or reason is literal at save only when its quote closes the value
The four save-time readers (code and reason, property and assignment) stopped at the closing quote, so a value built from a literal such as 'OVER' + suffix was read as the literal OVER and refused at save. A property value is literal now only when `,` or `}` follows; an assigned one only when `;`, `,`, `}`, `)`, the source end, or a line end that does not continue the expression follows. Every literal refused before is still refused in the same words. Claude-Session: https://claude.ai/code/session_01L9qibykc1XS5J4B4J16dLb Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 3e8edf7 commit 291adbd

4 files changed

Lines changed: 210 additions & 16 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
'@objectstack/spec': patch
3+
---
4+
5+
fix(spec): a hook's or action's refusal code and reason are read as literal at save only when the quoted value closes the value
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
The save doors of `HookSchema` and `ActionSchema` read the refusal codes and reasons a body throws literally (`hookRefusalCodesThrownIn`, `hookRefusalReasonsThrownIn`) and refuse one that `refusals` does not declare. The four readers stopped at the closing quote, so a value built from a literal — `throw { code: 'OVER' + suffix }`, `e.code = 'OVER' + suffix`, `reason: 'too' + suffix`, `e.reason = 'too' + suffix`, or a member access such as `'OVER'.concat(suffix)` — was read as the literal `OVER` or `too` and refused at save, though the body would run correctly once the built value is declared. The published reading already left a computed value to the run-time refusal; the save now agrees with it.
11+
12+
A property value is literal now only when its closing quote is followed by `,` or `}`, after optional whitespace. An assigned value is literal only when its closing quote is followed by `;`, `,`, `}` or `)`, by the end of the source, or by a line end whose next line does not continue the expression (a next line opening with an operator, a member access, a bracket or a template continues it). Every literal code or reason the save refused before is still refused, in the same words: `throw { code: 'X' }`, `throw Object.assign(new Error(), { code: 'X' })`, `e.code = 'X';`, `e.code = 'X'` at a line end, and the same for a reason, a listed reason included. A value built from a literal is refused when the hook or action runs, naming the code or reason, as any other computed value is. No author action is needed.

‎packages/spec/src/data/hook-refusal-codes.ts‎

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,31 @@ export const HOOK_REFUSAL_PARAM_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*$/;
5959
/** A string literal in any of the three quote forms, holding a refusal reason. */
6060
const REASON_LITERAL = String.raw`(?<rquote>['"\x60])(?<reason>[a-z][a-z0-9]*(?:_[a-z0-9]+)*)\k<rquote>`;
6161

62-
/** `reason: 'x'` — the property in an object literal a `throw` builds. */
63-
const REASON_PROPERTY = new RegExp(String.raw`(?<![\w$.])reason\s*:\s*` + REASON_LITERAL, 'g');
62+
/**
63+
* [#36670] What must follow a property's quoted value for the value to be that
64+
* literal: `,` or `}`, after optional whitespace (a comment is blanked to
65+
* whitespace first, {@link maskedSource}). Anything else — `'X' + suffix`, a
66+
* member access such as `'X'.concat(suffix)` — builds a computed value, which
67+
* the run-time refusal decides.
68+
*/
69+
const PROPERTY_VALUE_END = String.raw`(?=\s*[,}])`;
6470

65-
/** `e.reason = 'x'` — the reason assigned onto a name the body later throws. */
71+
/**
72+
* [#36670] What must follow an assignment's quoted value for the value to be
73+
* that literal: `;`, `,`, `}` or `)` after optional whitespace, the source's
74+
* end, or a line end whose next line does not continue the expression. A next
75+
* line that opens with an operator, a member access, a call or index bracket
76+
* or a template (`+ suffix`, `.concat(suffix)`) continues it, as JavaScript
77+
* reads it, so the value is computed.
78+
*/
79+
const ASSIGNMENT_VALUE_END = String.raw`(?=\s*[;,})]|\s*$|[ \t]*\r?\n\s*[^\s+\-*/%.?[(&|^<>=,\x60])`;
80+
81+
/** `reason: 'x'` — the property in an object literal a `throw` builds, the literal closing the value. */
82+
const REASON_PROPERTY = new RegExp(String.raw`(?<![\w$.])reason\s*:\s*` + REASON_LITERAL + PROPERTY_VALUE_END, 'g');
83+
84+
/** `e.reason = 'x'` — the reason assigned onto a name the body later throws, the literal closing the value. */
6685
const REASON_ASSIGNMENT = new RegExp(
67-
String.raw`(?<![\w$.])(?<binding>[A-Za-z_$][\w$]*)\s*\.\s*reason\s*=(?!=)\s*` + REASON_LITERAL,
86+
String.raw`(?<![\w$.])(?<binding>[A-Za-z_$][\w$]*)\s*\.\s*reason\s*=(?!=)\s*` + REASON_LITERAL + ASSIGNMENT_VALUE_END,
6887
'g',
6988
);
7089

@@ -79,12 +98,12 @@ const REASONS_LIST_PROPERTY = /(?<![\w$.])reasons\s*:\s*\[/g;
7998
/** [#36877] `e.reasons = [` — the list assigned onto a name the body later throws. */
8099
const REASONS_LIST_ASSIGNMENT = /(?<![\w$.])(?<binding>[A-Za-z_$][\w$]*)\s*\.\s*reasons\s*=(?!=)\s*\[/g;
81100

82-
/** `code: 'X'` — the property in an object literal a `throw` builds. */
83-
const CODE_PROPERTY = new RegExp(String.raw`(?<![\w$.])code\s*:\s*` + CODE_LITERAL, 'g');
101+
/** `code: 'X'` — the property in an object literal a `throw` builds, the literal closing the value. */
102+
const CODE_PROPERTY = new RegExp(String.raw`(?<![\w$.])code\s*:\s*` + CODE_LITERAL + PROPERTY_VALUE_END, 'g');
84103

85-
/** `e.code = 'X'` — the code assigned onto a name the body later throws. */
104+
/** `e.code = 'X'` — the code assigned onto a name the body later throws, the literal closing the value. */
86105
const CODE_ASSIGNMENT = new RegExp(
87-
String.raw`(?<![\w$.])(?<binding>[A-Za-z_$][\w$]*)\s*\.\s*code\s*=(?!=)\s*` + CODE_LITERAL,
106+
String.raw`(?<![\w$.])(?<binding>[A-Za-z_$][\w$]*)\s*\.\s*code\s*=(?!=)\s*` + CODE_LITERAL + ASSIGNMENT_VALUE_END,
88107
'g',
89108
);
90109

@@ -253,11 +272,14 @@ function thrownBinding(statement: string): string | undefined {
253272
* `throw Object.assign(new Error(), { code: 'X' })`, `throw { code: 'X' }`;
254273
* - `e.code = 'X'` on a name some `throw` statement throws bare: `throw e`.
255274
*
256-
* A code that arrives any other way — a helper's parameter
257-
* (`refuse('…', code)`), a variable, a computed value — is left to the run-time
258-
* refusal, which judges the code the body actually threw. Only SCREAMING_SNAKE
259-
* literals are read ({@link HOOK_REFUSAL_CODE_PATTERN}), so a record field
260-
* named `code` written with a lower-case value is never mistaken for one.
275+
* Each literal closes its value ({@link PROPERTY_VALUE_END},
276+
* {@link ASSIGNMENT_VALUE_END}). A code that arrives any other way — a
277+
* helper's parameter (`refuse('…', code)`), a variable, a computed value, a
278+
* literal joined to something (`'OVER' + suffix`) included — is left to the
279+
* run-time refusal, which judges the code the body actually threw. Only
280+
* SCREAMING_SNAKE literals are read ({@link HOOK_REFUSAL_CODE_PATTERN}), so a
281+
* record field named `code` written with a lower-case value is never mistaken
282+
* for one.
261283
*/
262284
export function hookRefusalCodesThrownIn(source: string): string[] {
263285
const codes = new Set<string>();
@@ -299,9 +321,10 @@ export interface HookRefusalThrownReason {
299321
* with that name's literal `e.code = 'X'` — and, marked `listed`, each
300322
* `reason: 'x'` inside `e.reasons = [ … ]` on such a name.
301323
*
302-
* A reason with no literal code beside it, or one that arrives any other way
303-
* (a list built in a variable, a computed reason), is left to the run-time
304-
* refusal.
324+
* Each literal closes its value, as for a code. A reason with no literal code
325+
* beside it, or one that arrives any other way (a list built in a variable, a
326+
* computed reason, a literal joined to something such as `'too' + suffix`), is
327+
* left to the run-time refusal.
305328
*/
306329
export function hookRefusalReasonsThrownIn(source: string): HookRefusalThrownReason[] {
307330
const found: HookRefusalThrownReason[] = [];

‎packages/spec/src/data/hook-refusals.test.ts‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,3 +592,103 @@ describe('Hook.refusals — a listed reason is refused at save where the body na
592592
expect(r.success).toBe(true);
593593
});
594594
});
595+
596+
// A code or reason is literal only when its quoted value closes the value: a
597+
// property's quote is followed by `,` or `}`; an assignment's by `;`, `,`, `}`,
598+
// `)`, the source's end, or a line end whose next line does not continue the
599+
// expression. A value built from a literal (`'OVER' + suffix`, a member access,
600+
// a template hole) is computed, and the run-time refusal decides it, as the
601+
// `hookRefusalCodesThrownIn` docblock says.
602+
describe('Hook.refusals — a literal code or reason closes its value; one built from a literal is computed', () => {
603+
const refusals = {
604+
OVER_LIMIT: { message: 'Over the limit.', reasons: { too_high: { message: 'The amount is too high.' } } },
605+
};
606+
const codeSuffix = "const suffix = '_LIMIT';\n";
607+
const reasonSuffix = "const suffix = '_high';\n";
608+
609+
const LITERAL_CODE = [
610+
['a property closed by `}`', "throw { code: 'OVER' };"],
611+
['a property closed by `,`', "throw { code: 'OVER', reason: 'too_high' };"],
612+
['a property with a line break before the close', "throw {\n code: 'OVER'\n};"],
613+
['a property with a comment before the close', "throw { code: 'OVER' /* the cap */ };"],
614+
['a property in a backtick with no hole', 'throw { code: `OVER` };'],
615+
['a property in Object.assign', "throw Object.assign(new Error('x'), { code: 'OVER' });"],
616+
['an assignment closed by `;`', "const e = new Error('x'); e.code = 'OVER'; throw e;"],
617+
['an assignment closed by a line end', "const e = new Error('x')\ne.code = 'OVER'\nthrow e"],
618+
['an assignment closed by a comment and a line end', "const e = new Error('x');\ne.code = 'OVER' // the cap\nthrow e;"],
619+
['an assignment closed by `,`', "const e = new Error('x'); e.code = 'OVER', e.reason = 'too_high'; throw e;"],
620+
['an assignment closed by `}`', "const e = new Error('x'); if (ctx.input.x) { e.code = 'OVER' } throw e;"],
621+
['an assignment closed by `)`', "const e = new Error('x'); (e.code = 'OVER'); throw e;"],
622+
['an assignment at the source end', "const e = new Error('x'); if (ctx.input.x) throw e; e.code = 'OVER'"],
623+
] as const;
624+
625+
it.each(LITERAL_CODE)('lit: refuses an undeclared literal code written as %s, at body.source', (_shape, source) => {
626+
expect(hookRefusalCodesThrownIn(source)).toEqual(['OVER']);
627+
const r = HookSchema.safeParse({ ...base, refusals, body: body(source) });
628+
expect(r.success).toBe(false);
629+
const issue = r.error!.issues.find((i) => i.path.join('.') === 'body.source')!;
630+
expect(issue.message).toBe(undeclaredHookRefusalCodeAtSave('OVER', ['OVER_LIMIT']));
631+
});
632+
633+
const COMPUTED_CODE = [
634+
['a property concatenation', "throw { code: 'OVER' + suffix };"],
635+
['a property concatenation on the next line', "throw { code: 'OVER'\n + suffix };"],
636+
['a property backtick concatenation', 'throw { code: `OVER` + suffix };'],
637+
['a property member access', "throw { code: 'OVER'.concat(suffix) };"],
638+
['a property template hole', 'throw { code: `OVER${suffix}` };'],
639+
['an assignment concatenation', "const e = new Error('x'); e.code = 'OVER' + suffix; throw e;"],
640+
['an assignment concatenation on the next line', "const e = new Error('x');\ne.code = 'OVER'\n + suffix\nthrow e;"],
641+
['an assignment member access on the next line', "const e = new Error('x');\ne.code = 'OVER'\n .concat(suffix)\nthrow e;"],
642+
['an assignment backtick concatenation', "const e = new Error('x'); e.code = `OVER` + suffix; throw e;"],
643+
] as const;
644+
645+
it.each(COMPUTED_CODE)('dark: passes the save for a code built from a literal by %s (the run-time refusal decides it)', (_shape, source) => {
646+
expect(hookRefusalCodesThrownIn(source)).toEqual([]);
647+
expect(HookSchema.safeParse({ ...base, refusals, body: body(codeSuffix + source) }).success).toBe(true);
648+
});
649+
650+
const LITERAL_REASON = [
651+
['a property closed by `}`', "throw { code: 'OVER_LIMIT', reason: 'too' };"],
652+
['a property closed by `,`', "throw { code: 'OVER_LIMIT', reason: 'too', params: {} };"],
653+
['a property in a listed reason', "throw { code: 'OVER_LIMIT', reasons: [{ reason: 'too' }] };"],
654+
['an assignment closed by `;`', "const e = new Error('x'); e.code = 'OVER_LIMIT'; e.reason = 'too'; throw e;"],
655+
['an assignment closed by a line end', "const e = new Error('x')\ne.code = 'OVER_LIMIT'\ne.reason = 'too'\nthrow e"],
656+
] as const;
657+
658+
it.each(LITERAL_REASON)('lit: refuses an undeclared literal reason written as %s, at body.source', (_shape, source) => {
659+
expect(hookRefusalReasonsThrownIn(source).map((p) => p.reason)).toEqual(['too']);
660+
const r = HookSchema.safeParse({ ...base, refusals, body: body(source) });
661+
expect(r.success).toBe(false);
662+
const issue = r.error!.issues.find((i) => i.path.join('.') === 'body.source')!;
663+
expect(issue.message).toBe(undeclaredHookRefusalReasonAtSave('OVER_LIMIT', 'too', ['too_high'], 'body.source'));
664+
});
665+
666+
const COMPUTED_REASON = [
667+
['a property concatenation', "throw { code: 'OVER_LIMIT', reason: 'too' + suffix };"],
668+
['a property member access', "throw { code: 'OVER_LIMIT', reason: 'too'.concat(suffix) };"],
669+
['a listed property concatenation', "throw { code: 'OVER_LIMIT', reasons: [{ reason: 'too' + suffix }] };"],
670+
['an assignment concatenation', "const e = new Error('x'); e.code = 'OVER_LIMIT'; e.reason = 'too' + suffix; throw e;"],
671+
['an assignment concatenation on the next line', "const e = new Error('x');\ne.code = 'OVER_LIMIT';\ne.reason = 'too'\n + suffix\nthrow e;"],
672+
] as const;
673+
674+
it.each(COMPUTED_REASON)('dark: passes the save for a reason built from a literal by %s (the run-time refusal decides it)', (_shape, source) => {
675+
expect(hookRefusalReasonsThrownIn(source)).toEqual([]);
676+
expect(HookSchema.safeParse({ ...base, refusals, body: body(reasonSuffix + source) }).success).toBe(true);
677+
});
678+
679+
it('dark: an inline handler building its code by concatenation passes the save, at handler', () => {
680+
const handler = async () => {
681+
const suffix = '_LIMIT';
682+
throw Object.assign(new Error('x'), { code: 'OVER' + suffix });
683+
};
684+
expect(HookSchema.safeParse({ ...base, handler, refusals }).success).toBe(true);
685+
});
686+
687+
it('CONTROL: a literal code beside a computed one in the same body is still refused, once', () => {
688+
const source = `${codeSuffix}if (ctx.input.x) throw { code: 'OVER' + suffix };\nthrow { code: 'OVER' };`;
689+
expect(hookRefusalCodesThrownIn(source)).toEqual(['OVER']);
690+
const r = HookSchema.safeParse({ ...base, refusals, body: body(source) });
691+
expect(r.success).toBe(false);
692+
expect(r.error!.issues.filter((i) => i.path.join('.') === 'body.source')).toHaveLength(1);
693+
});
694+
});

‎packages/spec/src/ui/action-refusals.test.ts‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,3 +320,62 @@ describe('Action.refusals — a listed reason is refused at save where the body
320320
);
321321
});
322322
});
323+
324+
// The hook door's reading, at the action door: a code or reason is literal only
325+
// when its quoted value closes the value (a property's by `,` or `}`, an
326+
// assignment's by `;`, `,`, `}`, `)`, the source's end or a line end that does
327+
// not continue the expression); one built from a literal is computed, the
328+
// run-time refusal's.
329+
describe('Action.refusals — a literal code or reason closes its value; one built from a literal is computed', () => {
330+
const refusals = {
331+
OVER_LIMIT: { message: 'Over the limit.', reasons: { too_high: { message: 'The amount is too high.' } } },
332+
};
333+
const DECLARED = ['OVER_LIMIT'];
334+
335+
const LITERAL_CODE = [
336+
['a property closed by `}`', "throw { code: 'OVER' };"],
337+
['a property closed by `,`', "throw { code: 'OVER', reason: 'too_high' };"],
338+
['a property in Object.assign', "throw Object.assign(new Error('x'), { code: 'OVER' });"],
339+
['an assignment closed by `;`', "const e = new Error('x'); e.code = 'OVER'; throw e;"],
340+
['an assignment closed by a line end', "const e = new Error('x')\ne.code = 'OVER'\nthrow e"],
341+
] as const;
342+
343+
it.each(LITERAL_CODE)('lit: refuses an undeclared literal code written as %s, at body.source', (_shape, source) => {
344+
const r = ActionSchema.safeParse({ ...base, body: body(source), refusals });
345+
expect(r.success).toBe(false);
346+
const issue = issuesOf(r).find((i) => i.path.join('.') === 'body.source');
347+
expect(issue?.message).toBe(undeclaredActionRefusalCodeAtSave('OVER', DECLARED));
348+
});
349+
350+
const COMPUTED_CODE = [
351+
['a property concatenation', "throw { code: 'OVER' + suffix };"],
352+
['a property member access', "throw { code: 'OVER'.concat(suffix) };"],
353+
['an assignment concatenation', "const e = new Error('x'); e.code = 'OVER' + suffix; throw e;"],
354+
['an assignment concatenation on the next line', "const e = new Error('x');\ne.code = 'OVER'\n + suffix\nthrow e;"],
355+
] as const;
356+
357+
it.each(COMPUTED_CODE)('dark: passes the save for a code built from a literal by %s (the run-time refusal decides it)', (_shape, source) => {
358+
expect(ActionSchema.safeParse({ ...base, body: body(`const suffix = '_LIMIT';\n${source}`), refusals }).success).toBe(true);
359+
});
360+
361+
const LITERAL_REASON = [
362+
['a property closed by `}`', "throw { code: 'OVER_LIMIT', reason: 'too' };"],
363+
['an assignment closed by `;`', "const e = new Error('x'); e.code = 'OVER_LIMIT'; e.reason = 'too'; throw e;"],
364+
] as const;
365+
366+
it.each(LITERAL_REASON)('lit: refuses an undeclared literal reason written as %s, at body.source', (_shape, source) => {
367+
const r = ActionSchema.safeParse({ ...base, body: body(source), refusals });
368+
expect(r.success).toBe(false);
369+
const issue = issuesOf(r).find((i) => i.path.join('.') === 'body.source');
370+
expect(issue?.message).toBe(undeclaredActionRefusalReasonAtSave('OVER_LIMIT', 'too', ['too_high']));
371+
});
372+
373+
const COMPUTED_REASON = [
374+
['a property concatenation', "throw { code: 'OVER_LIMIT', reason: 'too' + suffix };"],
375+
['an assignment concatenation', "const e = new Error('x'); e.code = 'OVER_LIMIT'; e.reason = 'too' + suffix; throw e;"],
376+
] as const;
377+
378+
it.each(COMPUTED_REASON)('dark: passes the save for a reason built from a literal by %s (the run-time refusal decides it)', (_shape, source) => {
379+
expect(ActionSchema.safeParse({ ...base, body: body(`const suffix = '_high';\n${source}`), refusals }).success).toBe(true);
380+
});
381+
});

0 commit comments

Comments
 (0)