UNPKG

9.8 kBTypeScriptView Raw
1import { Nullable, PresentArray, WireFormat, Dict, SerializedTemplateBlock, TemplateJavascript } from "@glimmer/interfaces";
2import { PrecompileOptions, PrecompileOptionsWithLexicalScope, TemplateIdFn } from "@glimmer/syntax";
3type BuilderParams = BuilderExpression[];
4type BuilderHash = Nullable<Dict<BuilderExpression>>;
5type BuilderBlockHash = BuilderHash | {
6 as: string | string[];
7};
8type BuilderBlocks = Dict<BuilderBlock>;
9type BuilderAttrs = Dict<BuilderAttr>;
10type NormalizedParams = NormalizedExpression[];
11type NormalizedHash = Dict<NormalizedExpression>;
12type NormalizedBlock = NormalizedStatement[];
13type NormalizedBlocks = Dict<NormalizedBlock>;
14type NormalizedAttrs = Dict<NormalizedAttr>;
15type NormalizedAttr = HeadKind.Splat | NormalizedExpression;
16declare enum HeadKind {
17 Block = "Block",
18 Call = "Call",
19 Element = "Element",
20 AppendPath = "AppendPath",
21 AppendExpr = "AppendExpr",
22 Literal = "Literal",
23 Modifier = "Modifier",
24 DynamicComponent = "DynamicComponent",
25 Comment = "Comment",
26 Splat = "Splat",
27 Keyword = "Keyword"
28}
29declare enum VariableKind {
30 Local = "Local",
31 Free = "Free",
32 Arg = "Arg",
33 Block = "Block",
34 This = "This"
35}
36interface Variable {
37 kind: VariableKind;
38 name: string;
39 /**
40 * Differences:
41 *
42 * - strict mode variables always refer to in-scope variables
43 * - loose mode variables use this algorithm:
44 * 1. otherwise, fall back to `this.<name>`
45 */
46 mode: "loose" | "strict";
47}
48interface Path {
49 head: Variable;
50 tail: PresentArray<string>;
51}
52interface AppendExpr {
53 kind: HeadKind.AppendExpr;
54 expr: NormalizedExpression;
55 trusted: boolean;
56}
57interface AppendPath {
58 kind: HeadKind.AppendPath;
59 path: NormalizedPath;
60 trusted: boolean;
61}
62interface NormalizedKeywordStatement {
63 kind: HeadKind.Keyword;
64 name: string;
65 params: Nullable<NormalizedParams>;
66 hash: Nullable<NormalizedHash>;
67 blockParams: Nullable<string[]>;
68 blocks: NormalizedBlocks;
69}
70type NormalizedStatement = {
71 kind: HeadKind.Call;
72 head: NormalizedHead;
73 params: Nullable<NormalizedParams>;
74 hash: Nullable<NormalizedHash>;
75 trusted: boolean;
76} | {
77 kind: HeadKind.Block;
78 head: NormalizedHead;
79 params: Nullable<NormalizedParams>;
80 hash: Nullable<NormalizedHash>;
81 blockParams: Nullable<string[]>;
82 blocks: NormalizedBlocks;
83} | NormalizedKeywordStatement | {
84 kind: HeadKind.Element;
85 name: string;
86 attrs: NormalizedAttrs;
87 block: NormalizedBlock;
88} | {
89 kind: HeadKind.Comment;
90 value: string;
91} | {
92 kind: HeadKind.Literal;
93 value: string;
94} | AppendPath | AppendExpr | {
95 kind: HeadKind.Modifier;
96 params: NormalizedParams;
97 hash: Nullable<NormalizedHash>;
98} | {
99 kind: HeadKind.DynamicComponent;
100 expr: NormalizedExpression;
101 hash: Nullable<NormalizedHash>;
102 block: NormalizedBlock;
103};
104type SugaryArrayStatement = BuilderCallExpression | BuilderElement | BuilderBlockStatement;
105type BuilderBlockStatement = [
106 string,
107 BuilderBlock | BuilderBlocks
108] | [
109 string,
110 BuilderParams | BuilderBlockHash,
111 BuilderBlock | BuilderBlocks
112] | [
113 string,
114 BuilderParams,
115 BuilderBlockHash,
116 BuilderBlock | BuilderBlocks
117];
118type BuilderElement = [
119 string
120] | [
121 string,
122 BuilderAttrs,
123 BuilderBlock
124] | [
125 string,
126 BuilderBlock
127] | [
128 string,
129 BuilderAttrs
130];
131type BuilderComment = [
132 Builder.Comment,
133 string
134];
135declare enum Builder {
136 Literal = 0,
137 Comment = 1,
138 Append = 2,
139 Modifier = 3,
140 DynamicComponent = 4,
141 Get = 5,
142 Concat = 6,
143 HasBlock = 7,
144 HasBlockParams = 8
145}
146type VerboseStatement = [
147 Builder.Literal,
148 string
149] | [
150 Builder.Comment,
151 string
152] | [
153 Builder.Append,
154 BuilderExpression,
155 true
156] | [
157 Builder.Append,
158 BuilderExpression
159] | [
160 Builder.Modifier,
161 Params,
162 Hash
163] | [
164 Builder.DynamicComponent,
165 BuilderExpression,
166 Hash,
167 BuilderBlock
168];
169type BuilderStatement = VerboseStatement | SugaryArrayStatement | TupleBuilderExpression | string;
170type BuilderAttr = "splat" | BuilderExpression;
171type TupleBuilderExpression = [
172 Builder.Literal,
173 string | boolean | null | undefined
174] | [
175 Builder.Get,
176 string
177] | [
178 Builder.Get,
179 string,
180 string[]
181] | [
182 Builder.Concat,
183 ...BuilderExpression[]
184] | [
185 Builder.HasBlock,
186 string
187] | [
188 Builder.HasBlockParams,
189 string
190] | BuilderCallExpression;
191type Params = BuilderParams;
192type Hash = Dict<BuilderExpression>;
193declare enum ExpressionKind {
194 Literal = "Literal",
195 Call = "Call",
196 GetPath = "GetPath",
197 GetVar = "GetVar",
198 Concat = "Concat",
199 HasBlock = "HasBlock",
200 HasBlockParams = "HasBlockParams"
201}
202interface NormalizedCallExpression {
203 type: ExpressionKind.Call;
204 head: NormalizedHead;
205 params: Nullable<NormalizedParams>;
206 hash: Nullable<NormalizedHash>;
207}
208interface NormalizedPath {
209 type: ExpressionKind.GetPath;
210 path: Path;
211}
212interface NormalizedVar {
213 type: ExpressionKind.GetVar;
214 variable: Variable;
215}
216type NormalizedHead = NormalizedPath | NormalizedVar;
217interface NormalizedConcat {
218 type: ExpressionKind.Concat;
219 params: [
220 NormalizedExpression,
221 ...NormalizedExpression[]
222 ];
223}
224type NormalizedExpression = {
225 type: ExpressionKind.Literal;
226 value: null | undefined | boolean | string | number;
227} | NormalizedCallExpression | NormalizedPath | NormalizedVar | NormalizedConcat | {
228 type: ExpressionKind.HasBlock;
229 name: string;
230} | {
231 type: ExpressionKind.HasBlockParams;
232 name: string;
233};
234// | [Builder.Get, string]
235// | [Builder.Get, string, string[]]
236// | [Builder.Concat, Params]
237// | [Builder.HasBlock, string]
238// | [Builder.HasBlockParams, string]
239type BuilderExpression = TupleBuilderExpression | BuilderCallExpression | null | undefined | boolean | string | number;
240type MiniBuilderBlock = BuilderStatement[];
241type BuilderBlock = MiniBuilderBlock;
242type BuilderCallExpression = [
243 string
244] | [
245 string,
246 Params | Hash
247] | [
248 string,
249 Params,
250 Hash
251];
252interface Symbols {
253 top: ProgramSymbols;
254 freeVar(name: string): number;
255 arg(name: string): number;
256 block(name: string): number;
257 local(name: string): number;
258 this(): number;
259 hasLocal(name: string): boolean;
260 child(params: string[]): LocalSymbols;
261}
262declare class ProgramSymbols implements Symbols {
263 _freeVariables: string[];
264 _symbols: string[];
265 top: this;
266 toSymbols(): string[];
267 toUpvars(): string[];
268 freeVar(name: string): number;
269 block(name: string): number;
270 arg(name: string): number;
271 local(name: string): never;
272 this(): number;
273 hasLocal(_name: string): false;
274 // any symbol
275 symbol(name: string): number;
276 child(locals: string[]): LocalSymbols;
277}
278declare class LocalSymbols implements Symbols {
279 private parent;
280 private locals;
281 constructor(parent: Symbols, locals: string[]);
282 get paramSymbols(): number[];
283 get top(): ProgramSymbols;
284 freeVar(name: string): number;
285 arg(name: string): number;
286 block(name: string): number;
287 local(name: string): number;
288 this(): number;
289 hasLocal(name: string): boolean;
290 child(locals: string[]): LocalSymbols;
291}
292declare function buildStatements(statements: BuilderStatement[], symbols: Symbols): WireFormat.Statement[];
293declare function buildStatement(normalized: NormalizedStatement, symbols?: Symbols): WireFormat.Statement[];
294declare function s(arr: TemplateStringsArray, ...interpolated: unknown[]): [
295 Builder.Literal,
296 string
297];
298declare function c(arr: TemplateStringsArray, ...interpolated: unknown[]): BuilderComment;
299declare function unicode(charCode: string): string;
300declare const NEWLINE = "\n";
301declare const defaultId: TemplateIdFn;
302/*
303* Compile a string into a template javascript string.
304*
305* Example usage:
306* import { precompile } from '@glimmer/compiler';
307* import { templateFactory } from 'glimmer-runtime';
308* let templateJs = precompile("Howdy {{name}}");
309* let factory = templateFactory(new Function("return " + templateJs)());
310* let template = factory.create(env);
311*
312* @method precompile
313* @param {string} string a Glimmer template string
314* @return {string} a template javascript string
315*/
316declare function precompileJSON(string: Nullable<string>, options?: PrecompileOptions | PrecompileOptionsWithLexicalScope): [
317 block: SerializedTemplateBlock,
318 usedLocals: string[]
319];
320/*
321* Compile a string into a template javascript string.
322*
323* Example usage:
324* import { precompile } from '@glimmer/compiler';
325* import { templateFactory } from 'glimmer-runtime';
326* let templateJs = precompile("Howdy {{name}}");
327* let factory = templateFactory(new Function("return " + templateJs)());
328* let template = factory.create(env);
329*
330* @method precompile
331* @param {string} string a Glimmer template string
332* @return {string} a template javascript string
333*/
334declare function precompile(source: string, options?: PrecompileOptions | PrecompileOptionsWithLexicalScope): TemplateJavascript;
335declare class WireFormatDebugger {
336 private upvars;
337 private symbols;
338 constructor([_statements, symbols, _hasEval, upvars]: SerializedTemplateBlock);
339 format(program: SerializedTemplateBlock): unknown;
340 formatOpcode(opcode: WireFormat.Syntax): unknown;
341 private formatCurryType;
342 private formatElementParams;
343 private formatParams;
344 private formatHash;
345 private formatBlocks;
346 private formatBlock;
347}
348export { buildStatement, buildStatements, c, NEWLINE, ProgramSymbols, s, unicode, Builder, BuilderStatement, defaultId, precompile, precompileJSON, PrecompileOptions, WireFormatDebugger };
349//# sourceMappingURL=index.d.ts.map
\No newline at end of file