Skip to content

Commit 9c1b62b

Browse files
committed
(third) Rename Part → PatternElement
1 parent c4f6d51 commit 9c1b62b

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

experiments/stasm/third/impl/model.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ export interface Selector {
1818

1919
export interface Variant {
2020
keys: Array<StringLiteral>;
21-
value: Array<Part>;
21+
value: Array<PatternElement>;
2222
}
2323

24-
export type Part = StringLiteral | VariableReference | FunctionCall;
24+
export type PatternElement = StringLiteral | VariableReference | FunctionCall;
2525

2626
export interface FunctionCall {
2727
type: "FunctionCall";

experiments/stasm/third/impl/runtime.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
import {Message, Parameter, Part, Phrase, Selector, StringLiteral, Variant} from "./model.js";
1+
import {
2+
Message,
3+
Parameter,
4+
PatternElement,
5+
Phrase,
6+
Selector,
7+
StringLiteral,
8+
Variant,
9+
} from "./model.js";
210
import {REGISTRY} from "./registry.js";
311

412
// A value passed in as a variable to format() or to which literals are resolved
@@ -51,7 +59,7 @@ export class FormattingContext {
5159
locale: string;
5260
message: Message;
5361
vars: Record<string, RuntimeValue<unknown>>;
54-
visited: WeakSet<Array<Part>>;
62+
visited: WeakSet<Array<PatternElement>>;
5563
// TODO(stasm): expose cached formatters, etc.
5664

5765
constructor(locale: string, message: Message, vars: Record<string, RuntimeValue<unknown>>) {
@@ -66,33 +74,33 @@ export class FormattingContext {
6674
return this.formatPattern(variant.value);
6775
}
6876

69-
formatPattern(parts: Array<Part>): string {
70-
if (this.visited.has(parts)) {
77+
formatPattern(pattern: Array<PatternElement>): string {
78+
if (this.visited.has(pattern)) {
7179
throw new RangeError("Recursive reference to a variant value.");
7280
}
7381

74-
this.visited.add(parts);
82+
this.visited.add(pattern);
7583
let result = "";
76-
for (let part of parts) {
77-
switch (part.type) {
84+
for (let element of pattern) {
85+
switch (element.type) {
7886
case "StringLiteral":
79-
result += part.value;
87+
result += element.value;
8088
continue;
8189
case "VariableReference": {
82-
let value = this.vars[part.name];
90+
let value = this.vars[element.name];
8391
result += value.format(this);
8492
continue;
8593
}
8694
case "FunctionCall": {
87-
let callable = REGISTRY[part.name];
88-
let value = callable(this, part.args, part.opts);
95+
let callable = REGISTRY[element.name];
96+
let value = callable(this, element.args, element.opts);
8997
result += value.format(this);
9098
continue;
9199
}
92100
}
93101
}
94102

95-
this.visited.delete(parts);
103+
this.visited.delete(pattern);
96104
return result;
97105
}
98106

0 commit comments

Comments
 (0)