pi-pocket 0.26.0 → 0.27.0-beta.2
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/CONTRIBUTING.md +1 -1
- package/docs/design/design-system.md +165 -0
- package/docs/design/ds-notes-b.md +61 -0
- package/docs/design/ds-notes-g.md +170 -0
- package/docs/design/ds-notes-t.md +303 -0
- package/docs/design/ds-notes-u.md +220 -0
- package/docs/design/ds-notes-v.md +186 -0
- package/docs/design/ds-rounds.md +160 -0
- package/docs/design/ds-u-bridge.patch +732 -0
- package/docs/examples/design-system/README.md +15 -0
- package/docs/examples/design-system/pocket/.pi/design/components/badge.html +112 -0
- package/docs/examples/design-system/pocket/.pi/design/components/button.html +203 -0
- package/docs/examples/design-system/pocket/.pi/design/components/card.html +125 -0
- package/docs/examples/design-system/pocket/.pi/design/components/code-block.html +155 -0
- package/docs/examples/design-system/pocket/.pi/design/components/input.html +156 -0
- package/docs/examples/design-system/pocket/.pi/design/components/menu-row.html +167 -0
- package/docs/examples/design-system/pocket/.pi/design/components/notice.html +171 -0
- package/docs/examples/design-system/pocket/.pi/design/components/segmented.html +120 -0
- package/docs/examples/design-system/pocket/.pi/design/components/switch.html +117 -0
- package/docs/examples/design-system/pocket/.pi/design/components/tool-step.html +163 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/components.css +730 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/names.md +495 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tailwind.css +1132 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tokens.css +1126 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tokens.dark.resolved.json +2022 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tokens.high-contrast.resolved.json +2022 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tokens.light.resolved.json +2022 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tokens.resolved.json +371 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tokens.studio.json +5915 -0
- package/docs/examples/design-system/pocket/.pi/design/guidelines.md +81 -0
- package/docs/examples/design-system/pocket/.pi/design/tokens.dark.json +182 -0
- package/docs/examples/design-system/pocket/.pi/design/tokens.high-contrast.json +185 -0
- package/docs/examples/design-system/pocket/.pi/design/tokens.json +2034 -0
- package/docs/features.md +6 -2
- package/docs/index.md +1 -0
- package/docs/map.md +34 -25
- package/docs/release-notes.md +1 -1
- package/docs/releases.md +45 -0
- package/package.json +2 -1
- package/src/server/design/check-cli.ts +283 -0
- package/src/server/design/color.ts +654 -0
- package/src/server/design/components.ts +328 -0
- package/src/server/design/docsite-assets.ts +29 -0
- package/src/server/design/docsite-color-section.ts +366 -0
- package/src/server/design/docsite-color.ts +303 -0
- package/src/server/design/docsite-components.ts +89 -0
- package/src/server/design/docsite-core.ts +634 -0
- package/src/server/design/docsite-css-base.ts +397 -0
- package/src/server/design/docsite-css-content.ts +477 -0
- package/src/server/design/docsite-css-phone.ts +251 -0
- package/src/server/design/docsite-css-reference.ts +374 -0
- package/src/server/design/docsite-css-specimens.ts +465 -0
- package/src/server/design/docsite-js-code.ts +96 -0
- package/src/server/design/docsite-js-core.ts +72 -0
- package/src/server/design/docsite-js-demos.ts +24 -0
- package/src/server/design/docsite-js-links.ts +25 -0
- package/src/server/design/docsite-js-nav.ts +73 -0
- package/src/server/design/docsite-js-stages.ts +81 -0
- package/src/server/design/docsite-js-table.ts +81 -0
- package/src/server/design/docsite-markdown.ts +221 -0
- package/src/server/design/docsite-motion.ts +110 -0
- package/src/server/design/docsite-reference.ts +269 -0
- package/src/server/design/docsite-sanitize.ts +193 -0
- package/src/server/design/docsite-shadows.ts +45 -0
- package/src/server/design/docsite-space.ts +289 -0
- package/src/server/design/docsite-type.ts +167 -0
- package/src/server/design/docsite.ts +508 -0
- package/src/server/design/export.ts +86 -9
- package/src/server/design/fix.ts +666 -0
- package/src/server/design/guide.ts +137 -22
- package/src/server/design/index.ts +368 -15
- package/src/server/design/lint-color.ts +293 -0
- package/src/server/design/lint-page-checks.ts +204 -0
- package/src/server/design/lint-page-elements.ts +252 -0
- package/src/server/design/lint-page-focus.ts +165 -0
- package/src/server/design/lint-page-literals.ts +293 -0
- package/src/server/design/lint-page-setup.ts +102 -0
- package/src/server/design/lint-page-states.ts +121 -0
- package/src/server/design/lint-page-values.ts +329 -0
- package/src/server/design/lint-page.ts +101 -0
- package/src/server/design/lint-suggest.ts +340 -0
- package/src/server/design/lint.ts +1136 -0
- package/src/server/design/render.ts +369 -18
- package/src/server/design/scan-braces.ts +115 -0
- package/src/server/design/scan-categorize.ts +187 -0
- package/src/server/design/scan-channels.ts +113 -0
- package/src/server/design/scan-color-base.ts +209 -0
- package/src/server/design/scan-color-names.ts +403 -0
- package/src/server/design/scan-color-palette.ts +91 -0
- package/src/server/design/scan-color-roles.ts +565 -0
- package/src/server/design/scan-colors.ts +22 -0
- package/src/server/design/scan-facts-antd.ts +171 -0
- package/src/server/design/scan-facts-files.ts +300 -0
- package/src/server/design/scan-facts-lists.ts +538 -0
- package/src/server/design/scan-facts-sass.ts +113 -0
- package/src/server/design/scan-facts-theme.ts +400 -0
- package/src/server/design/scan-facts-use.ts +674 -0
- package/src/server/design/scan-facts-wordpress.ts +374 -0
- package/src/server/design/scan-facts.ts +285 -0
- package/src/server/design/scan-files.ts +384 -0
- package/src/server/design/scan-import-convert.ts +277 -0
- package/src/server/design/scan-import-eval.ts +583 -0
- package/src/server/design/scan-import-material.ts +745 -0
- package/src/server/design/scan-import-sd-config.ts +370 -0
- package/src/server/design/scan-import-sd.ts +381 -0
- package/src/server/design/scan-import-studio.ts +242 -0
- package/src/server/design/scan-import.ts +336 -0
- package/src/server/design/scan-infer.ts +313 -0
- package/src/server/design/scan-js-eval.ts +1267 -0
- package/src/server/design/scan-palette.ts +485 -0
- package/src/server/design/scan-parse.ts +1134 -0
- package/src/server/design/scan-prepare.ts +402 -0
- package/src/server/design/scan-render-pages.ts +367 -0
- package/src/server/design/scan-render-probe.ts +229 -0
- package/src/server/design/scan-render.ts +524 -0
- package/src/server/design/scan-review.ts +54 -0
- package/src/server/design/scan-roles.ts +259 -0
- package/src/server/design/scan-sass-colors.ts +275 -0
- package/src/server/design/scan-sass-eval.ts +269 -0
- package/src/server/design/scan-sass-expr.ts +911 -0
- package/src/server/design/scan-sass-modules.ts +353 -0
- package/src/server/design/scan-sass-run.ts +1003 -0
- package/src/server/design/scan-sass-stmt.ts +256 -0
- package/src/server/design/scan-sass-values.ts +886 -0
- package/src/server/design/scan-scales.ts +983 -0
- package/src/server/design/scan-tailwind.ts +34 -0
- package/src/server/design/scan-theme-shapes.ts +579 -0
- package/src/server/design/scan-tree.ts +290 -0
- package/src/server/design/scan-util.ts +44 -0
- package/src/server/design/scan-variables.ts +470 -0
- package/src/server/design/scan.ts +703 -0
- package/src/server/design/system-schema.ts +68 -0
- package/src/server/design/system-types.ts +279 -0
- package/src/server/design/system.ts +510 -82
- package/src/server/design/tokens-base.ts +123 -0
- package/src/server/design/tokens-compile.ts +440 -0
- package/src/server/design/tokens-dtcg.ts +354 -0
- package/src/server/design/tokens-io.ts +417 -0
- package/src/server/design/tokens-model.ts +1163 -0
- package/src/server/design/tokens-names.ts +112 -0
- package/src/server/design/tokens-ramps.ts +112 -0
- package/src/server/design/tokens-summary.ts +317 -0
- package/src/server/design/tokens-tailwind.ts +324 -0
- package/src/server/design/tokens-values.ts +921 -0
- package/src/server/design/tokens.ts +533 -0
- package/src/server/docs.ts +8 -0
- package/src/server/extensions/artifacts.ts +87 -2
- package/src/server/extensions/design/app.js +280 -0
- package/src/server/extensions/design/canvas.js +142 -17
- package/src/server/extensions/design/check.js +656 -0
- package/src/server/extensions/design/color.js +383 -0
- package/src/server/extensions/design/data.js +102 -7
- package/src/server/extensions/design/devices.js +26 -0
- package/src/server/extensions/design/docks.js +117 -3
- package/src/server/extensions/design/index.js +171 -219
- package/src/server/extensions/design/messages.js +49 -0
- package/src/server/extensions/design/parts.js +15 -1
- package/src/server/extensions/design/paths.js +4 -0
- package/src/server/extensions/design/picker.js +123 -0
- package/src/server/extensions/design/specimen.js +55 -0
- package/src/server/extensions/design/state.js +72 -7
- package/src/server/extensions/design/style.css +546 -0
- package/src/server/extensions/design/system-actions.js +214 -0
- package/src/server/extensions/design/system-api.ts +62 -0
- package/src/server/extensions/design/system-data.js +315 -0
- package/src/server/extensions/design/system-preview.js +41 -0
- package/src/server/extensions/design/system-routes.ts +1122 -0
- package/src/server/extensions/design/system-sections.js +1344 -0
- package/src/server/extensions/design/system-ui.js +32 -0
- package/src/server/extensions/design/system-view.js +1196 -0
- package/src/server/extensions/design/system-widgets.js +942 -0
- package/src/server/extensions/design/system.css +2313 -0
- package/src/server/extensions/design/system.js +1479 -0
- package/src/server/extensions/design/unsaved.js +20 -0
- package/src/server/extensions/design/views.js +2 -2
- package/src/server/extensions/design/workspace.js +200 -105
- package/src/server/extensions/design.ts +89 -6
- package/src/server/extensions/updates/index.js +31 -9
- package/src/server/extensions/updates/style.css +11 -0
- package/src/server/extensions/updates.ts +29 -1
- package/src/server/host.ts +4 -1
- package/src/server/http/artifact-inspect.ts +338 -24
- package/src/server/updates.ts +235 -53
- package/src/server/versions.ts +71 -0
- package/web/artifact-bridge.js +34 -1
- package/npm-shrinkwrap.json +0 -3162
|
@@ -0,0 +1,1003 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A run of one stylesheet (stream T): its statements, `@each` and `@for` loops, `@if`, `@function`, `@mixin`, maps, and color
|
|
3
|
+
* functions, written as the stylesheet that Sass would make, on the same lines as the source: the variables as they end
|
|
4
|
+
* (`$palette: (primary-500: #3b82f6, …)`), the custom properties a loop makes (`--color-brand-300: #93c5fd`), and the rules
|
|
5
|
+
* and declarations with their values worked out. Whatever cannot be evaluated is left as it was written. The code runs in
|
|
6
|
+
* the module (`scan-sass-modules.ts`) of the stylesheet; `@use`, `@forward`, and `@import` load the others through the
|
|
7
|
+
* project (`scan-sass-eval.ts`).
|
|
8
|
+
*/
|
|
9
|
+
import { Unsupported, evaluate, parseExpression, type Env } from "./scan-sass-expr.ts";
|
|
10
|
+
import {
|
|
11
|
+
type Fn,
|
|
12
|
+
Module,
|
|
13
|
+
defaultNamespace,
|
|
14
|
+
parseModuleRule,
|
|
15
|
+
resolveModule,
|
|
16
|
+
} from "./scan-sass-modules.ts";
|
|
17
|
+
import type { Project } from "./scan-sass-eval.ts";
|
|
18
|
+
import { LIMITS, splitTop, type Stmt } from "./scan-sass-stmt.ts";
|
|
19
|
+
import {
|
|
20
|
+
NULL,
|
|
21
|
+
builtinConstant,
|
|
22
|
+
declText,
|
|
23
|
+
hasBuiltin,
|
|
24
|
+
itemsOf,
|
|
25
|
+
list,
|
|
26
|
+
num,
|
|
27
|
+
text,
|
|
28
|
+
truthy,
|
|
29
|
+
type Value,
|
|
30
|
+
} from "./scan-sass-values.ts";
|
|
31
|
+
|
|
32
|
+
/** The Sass modules that come with it: a namespace of one of them is a call to a built-in function. */
|
|
33
|
+
const BUILTIN_MODULES = new Set(["color", "math", "map", "list", "string", "meta", "selector"]);
|
|
34
|
+
|
|
35
|
+
class Scope {
|
|
36
|
+
vars = new Map<string, Value>();
|
|
37
|
+
parent: Scope | undefined;
|
|
38
|
+
/** Control flow (`@each`, `@if`) does not make a variable of its own: an assignment reaches the scope outside it. */
|
|
39
|
+
flow: boolean;
|
|
40
|
+
|
|
41
|
+
constructor(parent: Scope | undefined, flow: boolean) {
|
|
42
|
+
this.parent = parent;
|
|
43
|
+
this.flow = flow;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
get(name: string, module: Module, flat = true): Value | undefined {
|
|
47
|
+
for (let scope: Scope | undefined = this; scope !== undefined; scope = scope.parent) {
|
|
48
|
+
const found = scope.vars.get(name);
|
|
49
|
+
|
|
50
|
+
if (found !== undefined) {
|
|
51
|
+
return found;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return module.lookup<Value>("vars", name, flat);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Sets a variable and says whether it is the module's own (a global one). */
|
|
59
|
+
set(name: string, value: Value, module: Module, forceGlobal: boolean): boolean {
|
|
60
|
+
if (forceGlobal || this.parent === undefined) {
|
|
61
|
+
module.setVar(name, value);
|
|
62
|
+
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// An assignment reaches a variable that exists in this scope or in the control flow around it.
|
|
67
|
+
let scope: Scope = this;
|
|
68
|
+
|
|
69
|
+
for (;;) {
|
|
70
|
+
if (scope.vars.has(name)) {
|
|
71
|
+
scope.vars.set(name, value);
|
|
72
|
+
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (!scope.flow || scope.parent === undefined) {
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
scope = scope.parent;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Control flow at the top level assigns the project's variable when there is one; else the variable is its own.
|
|
84
|
+
if (scope.parent === undefined && (module.vars.has(name) || module.flat.vars.has(name))) {
|
|
85
|
+
module.setVar(name, value);
|
|
86
|
+
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
this.vars.set(name, value);
|
|
91
|
+
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** What a run of one stylesheet makes. */
|
|
97
|
+
export class Run implements Env {
|
|
98
|
+
project: Project;
|
|
99
|
+
/** The module whose code runs: a function from another module runs in its own. */
|
|
100
|
+
module: Module;
|
|
101
|
+
scope: Scope;
|
|
102
|
+
ticks = 0;
|
|
103
|
+
iterations = 0;
|
|
104
|
+
out: string[] = [];
|
|
105
|
+
/** The line the output has reached: a statement is written on the line it had in the source. */
|
|
106
|
+
outLine = 1;
|
|
107
|
+
/** How deep the statements are in control flow that repeats: a variable set there is written when the flow ends. */
|
|
108
|
+
repeating = 0;
|
|
109
|
+
pending = new Map<string, Value>();
|
|
110
|
+
size = 0;
|
|
111
|
+
depth = 0;
|
|
112
|
+
returned: Value | undefined;
|
|
113
|
+
returnFlag = false;
|
|
114
|
+
content: Stmt[] | undefined;
|
|
115
|
+
contentScope: Scope | undefined;
|
|
116
|
+
contentModule: Module | undefined;
|
|
117
|
+
|
|
118
|
+
constructor(project: Project, module: Module) {
|
|
119
|
+
this.project = project;
|
|
120
|
+
this.module = module;
|
|
121
|
+
this.scope = new Scope(undefined, false);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
tick(): void {
|
|
125
|
+
if (++this.ticks > LIMITS.ticks) {
|
|
126
|
+
throw new Unsupported("too much work");
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if ((this.ticks & 255) === 0 && Date.now() > this.project.deadline) {
|
|
130
|
+
throw new Unsupported("out of time");
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
get(name: string, namespace?: string): Value | undefined {
|
|
135
|
+
if (namespace !== undefined) {
|
|
136
|
+
const target = this.module.namespace(namespace);
|
|
137
|
+
|
|
138
|
+
if (target instanceof Module) {
|
|
139
|
+
return target.member<Value>("vars", name);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (typeof target === "string") {
|
|
143
|
+
return builtinConstant(target, name);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return this.scope.get(name, this.module);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** The built-in name of a call: `c.adjust` for `@use "sass:color" as c`, `div` for `@use "sass:math" as *`. */
|
|
151
|
+
builtin(name: string): string {
|
|
152
|
+
const dot = name.indexOf(".");
|
|
153
|
+
|
|
154
|
+
if (dot > 0) {
|
|
155
|
+
const target = this.module.namespace(name.slice(0, dot));
|
|
156
|
+
|
|
157
|
+
return typeof target === "string" ? `${target}.${name.slice(dot + 1)}` : name;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
for (const library of this.module.openBuiltins) {
|
|
161
|
+
if (hasBuiltin(`${library}.${name}`)) {
|
|
162
|
+
return `${library}.${name}`;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return name;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** A function or a mixin: by its bare name, or through a namespace (`p.fn`); a name through a built-in namespace is none. */
|
|
170
|
+
find(kind: "functions" | "mixins", name: string): Fn | undefined {
|
|
171
|
+
const dot = name.indexOf(".");
|
|
172
|
+
|
|
173
|
+
if (dot <= 0) {
|
|
174
|
+
return this.module.lookup<Fn>(kind, name);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const target = this.module.namespace(name.slice(0, dot));
|
|
178
|
+
const bare = name.slice(dot + 1);
|
|
179
|
+
|
|
180
|
+
if (target instanceof Module) {
|
|
181
|
+
return target.member<Fn>(kind, bare);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// A namespace that no module explains is the old reading: the name without it (but not a built-in module's).
|
|
185
|
+
return typeof target === "string" || BUILTIN_MODULES.has(name.slice(0, dot))
|
|
186
|
+
? undefined
|
|
187
|
+
: this.module.lookup<Fn>(kind, bare);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
call(name: string, positional: Value[], named: Map<string, Value>): Value | undefined {
|
|
191
|
+
const fn = this.find("functions", name);
|
|
192
|
+
|
|
193
|
+
if (fn === undefined) {
|
|
194
|
+
return undefined;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (this.depth > 20) {
|
|
198
|
+
throw new Unsupported("functions nest too deep");
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const saved = [this.scope, this.returned, this.returnFlag, this.module] as const;
|
|
202
|
+
const away = fn.module !== undefined && fn.module !== this.module;
|
|
203
|
+
|
|
204
|
+
// The body sees the members of the module that declared it.
|
|
205
|
+
if (away) {
|
|
206
|
+
this.module = fn.module!;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
this.scope = new Scope(
|
|
210
|
+
away
|
|
211
|
+
? new Scope(undefined, false)
|
|
212
|
+
: this.scope.parent === undefined
|
|
213
|
+
? this.scope
|
|
214
|
+
: this.rootScope(),
|
|
215
|
+
false,
|
|
216
|
+
);
|
|
217
|
+
this.bind(fn, positional, named);
|
|
218
|
+
this.depth += 1;
|
|
219
|
+
this.returned = undefined;
|
|
220
|
+
this.returnFlag = false;
|
|
221
|
+
|
|
222
|
+
try {
|
|
223
|
+
this.block(fn.body, false);
|
|
224
|
+
|
|
225
|
+
return this.returned;
|
|
226
|
+
} finally {
|
|
227
|
+
this.depth -= 1;
|
|
228
|
+
[this.scope, this.returned, this.returnFlag, this.module] = saved;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** The outermost scope (variables of a function body do not see the caller's locals). */
|
|
233
|
+
rootScope(): Scope {
|
|
234
|
+
let scope = this.scope;
|
|
235
|
+
|
|
236
|
+
while (scope.parent !== undefined) {
|
|
237
|
+
scope = scope.parent;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return scope;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
bind(fn: Fn, positional: Value[], named: Map<string, Value>): void {
|
|
244
|
+
fn.params.forEach((param, at) => {
|
|
245
|
+
if (param.rest) {
|
|
246
|
+
this.scope.vars.set(param.name, list(positional.slice(at)));
|
|
247
|
+
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const given = named.get(param.name) ?? positional[at];
|
|
252
|
+
|
|
253
|
+
if (given !== undefined) {
|
|
254
|
+
this.scope.vars.set(param.name, given);
|
|
255
|
+
} else if (param.fallback !== undefined) {
|
|
256
|
+
this.scope.vars.set(param.name, evaluate(parseExpression(param.fallback), this));
|
|
257
|
+
} else {
|
|
258
|
+
throw new Unsupported(`missing ${param.name}`);
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/** Writes text on the line the statement had. */
|
|
264
|
+
write(textOut: string, line: number): void {
|
|
265
|
+
if (this.repeating > 0 && line < this.outLine) {
|
|
266
|
+
line = this.outLine;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const pad = line > this.outLine ? "\n".repeat(Math.min(line - this.outLine, 40)) : " ";
|
|
270
|
+
|
|
271
|
+
this.out.push(pad, textOut);
|
|
272
|
+
this.size += textOut.length;
|
|
273
|
+
this.outLine = Math.max(this.outLine, line);
|
|
274
|
+
|
|
275
|
+
if (this.size > LIMITS.output) {
|
|
276
|
+
throw new Unsupported("too much output");
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** `#{…}` in a piece of text, evaluated. */
|
|
281
|
+
interpolated(source: string): string {
|
|
282
|
+
let out = "";
|
|
283
|
+
let at = 0;
|
|
284
|
+
|
|
285
|
+
while (at < source.length) {
|
|
286
|
+
const open = source.indexOf("#{", at);
|
|
287
|
+
|
|
288
|
+
if (open === -1) {
|
|
289
|
+
out += source.slice(at);
|
|
290
|
+
|
|
291
|
+
break;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
let level = 0;
|
|
295
|
+
let close = open + 1;
|
|
296
|
+
|
|
297
|
+
for (; close < source.length; close++) {
|
|
298
|
+
level += source[close] === "{" ? 1 : source[close] === "}" ? -1 : 0;
|
|
299
|
+
|
|
300
|
+
if (level === 0) {
|
|
301
|
+
break;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
out += source.slice(at, open);
|
|
306
|
+
|
|
307
|
+
const value = evaluate(parseExpression(source.slice(open + 2, close)), this);
|
|
308
|
+
|
|
309
|
+
out += value.t === "str" ? value.s : text(value);
|
|
310
|
+
at = close + 1;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
return out;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/** Runs the statements of a block. `emit` says whether rules and declarations are written. */
|
|
317
|
+
block(stmts: Stmt[], emit: boolean): void {
|
|
318
|
+
for (let at = 0; at < stmts.length; at++) {
|
|
319
|
+
if (this.returnFlag) {
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
const stmt = stmts[at]!;
|
|
324
|
+
|
|
325
|
+
// `@if` and the `@else`s after it are one statement.
|
|
326
|
+
if (/^@if\b/.test(stmt.head)) {
|
|
327
|
+
let end = at + 1;
|
|
328
|
+
|
|
329
|
+
while (end < stmts.length && /^@else\b/.test(stmts[end]!.head)) {
|
|
330
|
+
end += 1;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
const chain = stmts.slice(at, end);
|
|
334
|
+
|
|
335
|
+
try {
|
|
336
|
+
this.conditional(chain, emit);
|
|
337
|
+
} catch (error) {
|
|
338
|
+
if (!(error instanceof Unsupported)) {
|
|
339
|
+
throw error;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
chain.forEach((each) => this.fallback(each, emit));
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
at = end - 1;
|
|
346
|
+
|
|
347
|
+
continue;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
try {
|
|
351
|
+
this.statement(stmt, emit);
|
|
352
|
+
} catch (error) {
|
|
353
|
+
if (!(error instanceof Unsupported) && !(error instanceof RangeError)) {
|
|
354
|
+
throw error;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
this.fallback(stmt, emit);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/** A statement that cannot be run is written as it was, with the variables it loses forgotten. */
|
|
363
|
+
fallback(stmt: Stmt, emit: boolean): void {
|
|
364
|
+
const assign = /^\$([\w-]+)\s*:/.exec(stmt.head);
|
|
365
|
+
|
|
366
|
+
if (assign !== null) {
|
|
367
|
+
this.scope.vars.delete(assign[1]!);
|
|
368
|
+
this.module.deleteVar(assign[1]!);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
if (
|
|
372
|
+
emit &&
|
|
373
|
+
stmt.raw !== "" &&
|
|
374
|
+
!/^@(?:use|forward|import|function|mixin|return|include|content)\b/.test(stmt.head)
|
|
375
|
+
) {
|
|
376
|
+
this.write(
|
|
377
|
+
stmt.raw.endsWith("}") || stmt.raw.endsWith(";") ? stmt.raw : `${stmt.raw};`,
|
|
378
|
+
stmt.line,
|
|
379
|
+
);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
statement(stmt: Stmt, emit: boolean): void {
|
|
384
|
+
this.tick();
|
|
385
|
+
|
|
386
|
+
const head = stmt.head;
|
|
387
|
+
|
|
388
|
+
// A variable: `$name: expression [!default] [!global];`
|
|
389
|
+
const assign = /^\$([\w-]+)\s*:\s*([\s\S]*)$/.exec(head);
|
|
390
|
+
|
|
391
|
+
if (assign !== null && stmt.body === undefined) {
|
|
392
|
+
this.assign(stmt, assign[1]!, assign[2]!, emit);
|
|
393
|
+
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
if (head.startsWith("@")) {
|
|
398
|
+
this.atRule(stmt, emit);
|
|
399
|
+
|
|
400
|
+
return;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
if (stmt.body !== undefined) {
|
|
404
|
+
this.rule(stmt, emit);
|
|
405
|
+
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
if (emit) {
|
|
410
|
+
this.declaration(stmt);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
assign(stmt: Stmt, name: string, rawValue: string, emit: boolean): void {
|
|
415
|
+
const flags = /\s*!(default|global)\b/g;
|
|
416
|
+
const isDefault = /!default\b/.test(rawValue);
|
|
417
|
+
const isGlobal = /!global\b/.test(rawValue);
|
|
418
|
+
const source = rawValue.replace(flags, "").trim();
|
|
419
|
+
|
|
420
|
+
// A `!default` variable of a module that `@use … with (…)` configured takes the configured value.
|
|
421
|
+
const configured =
|
|
422
|
+
isDefault && this.scope.parent === undefined && !this.module.applied.has(name)
|
|
423
|
+
? this.module.config?.get(name)
|
|
424
|
+
: undefined;
|
|
425
|
+
|
|
426
|
+
if (configured !== undefined) {
|
|
427
|
+
this.module.applied.add(name);
|
|
428
|
+
} else if (
|
|
429
|
+
isDefault &&
|
|
430
|
+
this.scope.get(name, this.module, false) !== undefined &&
|
|
431
|
+
this.scope.get(name, this.module, false)!.t !== "null"
|
|
432
|
+
) {
|
|
433
|
+
// `!default`: a variable that this stylesheet or what it opens already has stays. The project's flat environment
|
|
434
|
+
// does not count: other stylesheets are compiled apart.
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
let value: Value;
|
|
439
|
+
|
|
440
|
+
try {
|
|
441
|
+
value = configured ?? evaluate(parseExpression(source), this);
|
|
442
|
+
} catch (error) {
|
|
443
|
+
if (!(error instanceof Unsupported)) {
|
|
444
|
+
throw error;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// Left as it was written: the value of the variable is unknown here, but the text may still be read.
|
|
448
|
+
if (emit) {
|
|
449
|
+
this.write(`$${name}: ${this.interpolatedSafe(source)};`, stmt.line);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
this.scope.vars.delete(name);
|
|
453
|
+
this.module.deleteVar(name);
|
|
454
|
+
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
const global = this.scope.set(name, value, this.module, isGlobal);
|
|
459
|
+
|
|
460
|
+
// A variable that a loop or a function keeps for itself is not the stylesheet's: only globals and a rule's own are written.
|
|
461
|
+
if (emit && value.t !== "null" && (global || !this.scope.flow)) {
|
|
462
|
+
// An alias (`$a: $b`) or a plain value keeps its text: the scan follows an alias by itself.
|
|
463
|
+
const plain =
|
|
464
|
+
/^\$[\w-]+$/.test(source) ||
|
|
465
|
+
(!/[\w-]\(|[+*/%]|#\{/.test(source) && !/\$/.test(source));
|
|
466
|
+
|
|
467
|
+
if (this.repeating > 0 && (value.t === "map" || value.t === "list")) {
|
|
468
|
+
this.pending.set(name, value);
|
|
469
|
+
} else {
|
|
470
|
+
this.write(`$${name}: ${plain ? source : declText(value)};`, stmt.line);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
interpolatedSafe(source: string): string {
|
|
476
|
+
try {
|
|
477
|
+
return this.interpolated(source);
|
|
478
|
+
} catch {
|
|
479
|
+
return source;
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
rule(stmt: Stmt, emit: boolean): void {
|
|
484
|
+
const selector = this.interpolatedSafe(stmt.head).replace(/\s+/g, " ");
|
|
485
|
+
const before = this.scope;
|
|
486
|
+
|
|
487
|
+
this.scope = new Scope(this.scope, false);
|
|
488
|
+
|
|
489
|
+
try {
|
|
490
|
+
this.write(`${selector} {`, stmt.line);
|
|
491
|
+
this.block(stmt.body!, emit);
|
|
492
|
+
this.write("}", this.outLine);
|
|
493
|
+
} finally {
|
|
494
|
+
this.scope = before;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
declaration(stmt: Stmt): void {
|
|
499
|
+
const colon = stmt.head.indexOf(":");
|
|
500
|
+
|
|
501
|
+
if (colon === -1) {
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
const name = this.interpolatedSafe(stmt.head.slice(0, colon).trim());
|
|
506
|
+
const value = stmt.head.slice(colon + 1).trim();
|
|
507
|
+
|
|
508
|
+
// A custom property is text: only `#{}` is evaluated in it. Any other property is an expression.
|
|
509
|
+
if (name.startsWith("--") || !/[$()]|#\{/.test(value)) {
|
|
510
|
+
this.write(`${name}: ${this.interpolatedSafe(value)};`, stmt.line);
|
|
511
|
+
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
// A value with variables and plain CSS in it (`1px solid $border`) keeps its text: the scan follows the variables. A
|
|
516
|
+
// value that calculates (`math.div($a, 2)`, `$a * 2`) is worked out.
|
|
517
|
+
if (!/[\w-]\(|[+*/%]|#\{/.test(value.replace(/\$[\w-]+/g, ""))) {
|
|
518
|
+
this.write(`${name}: ${value};`, stmt.line);
|
|
519
|
+
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
const important = /\s*!important\s*$/i.test(value);
|
|
524
|
+
const source = value.replace(/\s*!important\s*$/i, "");
|
|
525
|
+
|
|
526
|
+
try {
|
|
527
|
+
const result = evaluate(parseExpression(source), this);
|
|
528
|
+
|
|
529
|
+
this.write(`${name}: ${text(result)}${important ? " !important" : ""};`, stmt.line);
|
|
530
|
+
} catch (error) {
|
|
531
|
+
if (!(error instanceof Unsupported)) {
|
|
532
|
+
throw error;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
this.write(`${name}: ${this.interpolatedSafe(value)};`, stmt.line);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
atRule(stmt: Stmt, emit: boolean): void {
|
|
540
|
+
const match = /^@([\w-]+)\s*([\s\S]*)$/.exec(stmt.head)!;
|
|
541
|
+
const name = match[1]!;
|
|
542
|
+
const rest = match[2]!.trim();
|
|
543
|
+
|
|
544
|
+
switch (name) {
|
|
545
|
+
case "use":
|
|
546
|
+
this.useRule(rest);
|
|
547
|
+
|
|
548
|
+
return;
|
|
549
|
+
|
|
550
|
+
case "forward":
|
|
551
|
+
this.forwardRule(rest);
|
|
552
|
+
|
|
553
|
+
return;
|
|
554
|
+
|
|
555
|
+
case "import":
|
|
556
|
+
this.importRule(rest);
|
|
557
|
+
|
|
558
|
+
return;
|
|
559
|
+
|
|
560
|
+
case "charset":
|
|
561
|
+
case "debug":
|
|
562
|
+
case "warn":
|
|
563
|
+
case "error":
|
|
564
|
+
case "extend":
|
|
565
|
+
return;
|
|
566
|
+
|
|
567
|
+
case "function":
|
|
568
|
+
case "mixin":
|
|
569
|
+
this.define(name, rest, stmt);
|
|
570
|
+
|
|
571
|
+
return;
|
|
572
|
+
|
|
573
|
+
case "return": {
|
|
574
|
+
this.returned = evaluate(parseExpression(rest), this);
|
|
575
|
+
this.returnFlag = true;
|
|
576
|
+
|
|
577
|
+
return;
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
case "include":
|
|
581
|
+
this.include(rest, stmt, emit);
|
|
582
|
+
|
|
583
|
+
return;
|
|
584
|
+
|
|
585
|
+
case "content":
|
|
586
|
+
if (this.content !== undefined && this.contentScope !== undefined) {
|
|
587
|
+
const saved = [this.scope, this.module] as const;
|
|
588
|
+
|
|
589
|
+
this.scope = this.contentScope;
|
|
590
|
+
this.module = this.contentModule ?? this.module;
|
|
591
|
+
|
|
592
|
+
try {
|
|
593
|
+
this.block(this.content, emit);
|
|
594
|
+
} finally {
|
|
595
|
+
[this.scope, this.module] = saved;
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
return;
|
|
600
|
+
|
|
601
|
+
case "each":
|
|
602
|
+
this.each(stmt, rest, emit);
|
|
603
|
+
|
|
604
|
+
return;
|
|
605
|
+
|
|
606
|
+
case "for":
|
|
607
|
+
this.forLoop(stmt, rest, emit);
|
|
608
|
+
|
|
609
|
+
return;
|
|
610
|
+
|
|
611
|
+
case "while":
|
|
612
|
+
this.whileLoop(stmt, rest, emit);
|
|
613
|
+
|
|
614
|
+
return;
|
|
615
|
+
|
|
616
|
+
default:
|
|
617
|
+
break;
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
// Another at-rule (`@media`, `@supports`, `@layer`, `@keyframes`, `@font-face`) is written with its block.
|
|
621
|
+
if (stmt.body !== undefined) {
|
|
622
|
+
const before = this.scope;
|
|
623
|
+
|
|
624
|
+
this.scope = new Scope(this.scope, true);
|
|
625
|
+
|
|
626
|
+
try {
|
|
627
|
+
if (emit) {
|
|
628
|
+
this.write(
|
|
629
|
+
`@${name} ${this.interpolatedSafe(rest)} {`.replace(/\s+\{/, " {"),
|
|
630
|
+
stmt.line,
|
|
631
|
+
);
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
this.block(stmt.body, emit);
|
|
635
|
+
|
|
636
|
+
if (emit) {
|
|
637
|
+
this.write("}", this.outLine);
|
|
638
|
+
}
|
|
639
|
+
} finally {
|
|
640
|
+
this.scope = before;
|
|
641
|
+
}
|
|
642
|
+
} else if (emit) {
|
|
643
|
+
this.write(`${stmt.head};`, stmt.line);
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
/** The values of `with ($a: 1, $b: red !default)`, evaluated here; a `!default` entry may be overridden by an outer configuration. */
|
|
648
|
+
configOf(entries: string[], outer?: Map<string, Value>): Map<string, Value> | undefined {
|
|
649
|
+
const config = new Map<string, Value>(outer ?? []);
|
|
650
|
+
|
|
651
|
+
for (const entry of entries.slice(0, 60)) {
|
|
652
|
+
const match = /^\$([\w-]+)\s*:\s*([\s\S]+?)\s*(!default)?$/.exec(entry);
|
|
653
|
+
|
|
654
|
+
if (match === null) {
|
|
655
|
+
continue;
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
try {
|
|
659
|
+
if (match[3] === undefined || !config.has(match[1]!)) {
|
|
660
|
+
config.set(match[1]!, evaluate(parseExpression(match[2]!), this));
|
|
661
|
+
}
|
|
662
|
+
} catch (error) {
|
|
663
|
+
if (!(error instanceof Unsupported)) {
|
|
664
|
+
throw error;
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
return config.size === 0 ? undefined : config;
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
/** `@use "path" as ns with (…)`: the module under its namespace. A path that is no file of the project is read as it was. */
|
|
673
|
+
useRule(rest: string): void {
|
|
674
|
+
const rule = parseModuleRule(rest);
|
|
675
|
+
|
|
676
|
+
if (rule === undefined) {
|
|
677
|
+
return;
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
if (rule.path.startsWith("sass:")) {
|
|
681
|
+
const library = rule.path.slice(5);
|
|
682
|
+
|
|
683
|
+
if (rule.namespace === "*") {
|
|
684
|
+
this.module.openBuiltins.push(library);
|
|
685
|
+
} else {
|
|
686
|
+
this.module.uses.set(rule.namespace ?? library, library);
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
return;
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
const rel = resolveModule(this.module.rel, rule.path, this.project.files.keys());
|
|
693
|
+
const loaded =
|
|
694
|
+
rel === undefined ? undefined : this.project.load(rel, this.configOf(rule.config));
|
|
695
|
+
|
|
696
|
+
if (loaded === undefined) {
|
|
697
|
+
return;
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
if (rule.namespace === "*") {
|
|
701
|
+
this.module.opened.push(loaded);
|
|
702
|
+
} else {
|
|
703
|
+
this.module.uses.set(rule.namespace ?? defaultNamespace(rule.path), loaded);
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
/** `@forward "path" as list-* show a, $b`: the module's members, handed on to whoever `@use`s this one. */
|
|
708
|
+
forwardRule(rest: string): void {
|
|
709
|
+
const rule = parseModuleRule(rest);
|
|
710
|
+
|
|
711
|
+
if (rule === undefined || rule.path.startsWith("sass:")) {
|
|
712
|
+
return;
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
const rel = resolveModule(this.module.rel, rule.path, this.project.files.keys());
|
|
716
|
+
const loaded =
|
|
717
|
+
rel === undefined
|
|
718
|
+
? undefined
|
|
719
|
+
: this.project.load(rel, this.configOf(rule.config, this.module.config));
|
|
720
|
+
|
|
721
|
+
if (loaded === undefined) {
|
|
722
|
+
return;
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
this.module.forwards.push({
|
|
726
|
+
module: loaded,
|
|
727
|
+
prefix: rule.prefix,
|
|
728
|
+
show: rule.show === undefined ? undefined : new Set(rule.show),
|
|
729
|
+
hide: rule.hide === undefined ? undefined : new Set(rule.hide),
|
|
730
|
+
});
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
/** `@import "a", "b"`: the files' members are the importer's own. */
|
|
734
|
+
importRule(rest: string): void {
|
|
735
|
+
for (const match of rest.matchAll(/["']([^"'\n]{1,300})["']/g)) {
|
|
736
|
+
const rel = resolveModule(this.module.rel, match[1]!, this.project.files.keys());
|
|
737
|
+
const loaded =
|
|
738
|
+
rel === undefined || rel === this.module.rel ? undefined : this.project.load(rel);
|
|
739
|
+
|
|
740
|
+
if (
|
|
741
|
+
loaded !== undefined &&
|
|
742
|
+
loaded !== this.module &&
|
|
743
|
+
!this.module.opened.includes(loaded)
|
|
744
|
+
) {
|
|
745
|
+
this.module.opened.push(loaded);
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
define(kind: "function" | "mixin", rest: string, stmt: Stmt): void {
|
|
751
|
+
const open = rest.indexOf("(");
|
|
752
|
+
const name = (open === -1 ? rest : rest.slice(0, open)).trim();
|
|
753
|
+
const params: Fn["params"] = [];
|
|
754
|
+
|
|
755
|
+
if (open !== -1) {
|
|
756
|
+
const close = rest.lastIndexOf(")");
|
|
757
|
+
|
|
758
|
+
for (const part of splitTop(rest.slice(open + 1, close), ",")) {
|
|
759
|
+
const match = /^\$([\w-]+)(\.\.\.)?\s*(?::\s*([\s\S]+))?$/.exec(part.trim());
|
|
760
|
+
|
|
761
|
+
if (match !== null) {
|
|
762
|
+
params.push({
|
|
763
|
+
name: match[1]!,
|
|
764
|
+
fallback: match[3]?.trim(),
|
|
765
|
+
rest: match[2] !== undefined,
|
|
766
|
+
});
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
const fn: Fn = {
|
|
772
|
+
params,
|
|
773
|
+
body: stmt.body ?? [],
|
|
774
|
+
layout: /@(?:media|at-root|supports|container)\b/.test(stmt.raw),
|
|
775
|
+
module: this.module,
|
|
776
|
+
};
|
|
777
|
+
const own = kind === "function" ? this.module.functions : this.module.mixins;
|
|
778
|
+
|
|
779
|
+
own.set(name, fn);
|
|
780
|
+
(kind === "function" ? this.project.functions : this.project.mixins).set(name, fn);
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
include(rest: string, stmt: Stmt, emit: boolean): void {
|
|
784
|
+
const match = /^([\w.-]+)\s*(?:\(([\s\S]*)\))?\s*(?:using\s*\([^)]*\))?$/.exec(rest);
|
|
785
|
+
const mixin = match?.[1] === undefined ? undefined : this.find("mixins", match[1]);
|
|
786
|
+
|
|
787
|
+
if (match === null || mixin === undefined || mixin.layout) {
|
|
788
|
+
// A mixin that is not here: its content block is kept, and the call is written as it was.
|
|
789
|
+
if (emit && stmt.body !== undefined) {
|
|
790
|
+
this.write(`@include ${this.interpolatedSafe(rest)} {`, stmt.line);
|
|
791
|
+
this.block(stmt.body, emit);
|
|
792
|
+
this.write("}", this.outLine);
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
return;
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
const positional: Value[] = [];
|
|
799
|
+
const named = new Map<string, Value>();
|
|
800
|
+
|
|
801
|
+
for (const part of splitTop(match[2] ?? "", ",")) {
|
|
802
|
+
const trimmed = part.trim();
|
|
803
|
+
|
|
804
|
+
if (trimmed === "") {
|
|
805
|
+
continue;
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
const keyword = /^\$([\w-]+)\s*:\s*([\s\S]+)$/.exec(trimmed);
|
|
809
|
+
const spread = /^([\s\S]+?)\.\.\.$/.exec(trimmed);
|
|
810
|
+
|
|
811
|
+
if (spread !== null) {
|
|
812
|
+
const value = evaluate(parseExpression(spread[1]!), this);
|
|
813
|
+
|
|
814
|
+
if (value.t === "map") {
|
|
815
|
+
value.entries.forEach(([key, item]) => named.set(text(key), item));
|
|
816
|
+
} else {
|
|
817
|
+
positional.push(...itemsOf(value));
|
|
818
|
+
}
|
|
819
|
+
} else if (keyword !== null) {
|
|
820
|
+
named.set(keyword[1]!, evaluate(parseExpression(keyword[2]!), this));
|
|
821
|
+
} else {
|
|
822
|
+
positional.push(evaluate(parseExpression(trimmed), this));
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
if (this.depth > 12) {
|
|
827
|
+
throw new Unsupported("mixins nest too deep");
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
const saved = [
|
|
831
|
+
this.scope,
|
|
832
|
+
this.content,
|
|
833
|
+
this.contentScope,
|
|
834
|
+
this.contentModule,
|
|
835
|
+
this.module,
|
|
836
|
+
] as const;
|
|
837
|
+
const away = mixin.module !== undefined && mixin.module !== this.module;
|
|
838
|
+
|
|
839
|
+
this.content = stmt.body;
|
|
840
|
+
this.contentScope = this.scope;
|
|
841
|
+
this.contentModule = this.module;
|
|
842
|
+
|
|
843
|
+
// The body sees the members of the module that declared the mixin.
|
|
844
|
+
if (away) {
|
|
845
|
+
this.module = mixin.module!;
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
this.scope = new Scope(away ? new Scope(undefined, false) : this.rootScope(), false);
|
|
849
|
+
this.bind(mixin, positional, named);
|
|
850
|
+
this.depth += 1;
|
|
851
|
+
|
|
852
|
+
try {
|
|
853
|
+
this.block(mixin.body, emit);
|
|
854
|
+
} finally {
|
|
855
|
+
this.depth -= 1;
|
|
856
|
+
[this.scope, this.content, this.contentScope, this.contentModule, this.module] = saved;
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
/** `@if`, `@else if`, `@else`: the body of the first whose condition holds. */
|
|
861
|
+
conditional(chain: Stmt[], emit: boolean): void {
|
|
862
|
+
for (const stmt of chain) {
|
|
863
|
+
const head = stmt.head.replace(/^@(?:else\s+)?(?:if)?\s*/, "");
|
|
864
|
+
const isElse = /^@else\b/.test(stmt.head) && !/^@else\s+if\b/.test(stmt.head);
|
|
865
|
+
|
|
866
|
+
if (stmt.body === undefined) {
|
|
867
|
+
throw new Unsupported("@if without a block");
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
if (isElse || truthy(evaluate(parseExpression(head), this))) {
|
|
871
|
+
const before = this.scope;
|
|
872
|
+
|
|
873
|
+
this.scope = new Scope(before, true);
|
|
874
|
+
|
|
875
|
+
try {
|
|
876
|
+
this.block(stmt.body, emit);
|
|
877
|
+
} finally {
|
|
878
|
+
this.scope = before;
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
return;
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
each(stmt: Stmt, rest: string, emit: boolean): void {
|
|
887
|
+
const match = /^((?:\$[\w-]+\s*,\s*)*\$[\w-]+)\s+in\s+([\s\S]+)$/.exec(rest);
|
|
888
|
+
|
|
889
|
+
if (match === null || stmt.body === undefined) {
|
|
890
|
+
throw new Unsupported("@each");
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
const names = match[1]!.split(",").map((each) => each.trim().replace(/^\$/, ""));
|
|
894
|
+
const items = itemsOf(evaluate(parseExpression(match[2]!), this));
|
|
895
|
+
|
|
896
|
+
this.loop(
|
|
897
|
+
stmt,
|
|
898
|
+
items.map((item) => (scope: Scope) => {
|
|
899
|
+
const parts = names.length === 1 ? [item] : itemsOf(item);
|
|
900
|
+
|
|
901
|
+
names.forEach((name, at) => scope.vars.set(name, parts[at] ?? NULL));
|
|
902
|
+
}),
|
|
903
|
+
emit,
|
|
904
|
+
);
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
forLoop(stmt: Stmt, rest: string, emit: boolean): void {
|
|
908
|
+
const match = /^\$([\w-]+)\s+from\s+([\s\S]+?)\s+(through|to)\s+([\s\S]+)$/.exec(rest);
|
|
909
|
+
|
|
910
|
+
if (match === null || stmt.body === undefined) {
|
|
911
|
+
throw new Unsupported("@for");
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
const from = evaluate(parseExpression(match[2]!), this);
|
|
915
|
+
const to = evaluate(parseExpression(match[4]!), this);
|
|
916
|
+
|
|
917
|
+
if (from.t !== "num" || to.t !== "num") {
|
|
918
|
+
throw new Unsupported("@for bounds");
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
const steps: ((scope: Scope) => void)[] = [];
|
|
922
|
+
const last = match[3] === "through" ? to.n : from.n <= to.n ? to.n - 1 : to.n + 1;
|
|
923
|
+
const up = from.n <= last;
|
|
924
|
+
|
|
925
|
+
for (let n = from.n; up ? n <= last : n >= last; n += up ? 1 : -1) {
|
|
926
|
+
if (steps.length > LIMITS.iterations) {
|
|
927
|
+
throw new Unsupported("@for too long");
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
steps.push((scope) => scope.vars.set(match[1]!, num(n, from.unit)));
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
this.loop(stmt, steps, emit);
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
whileLoop(stmt: Stmt, rest: string, emit: boolean): void {
|
|
937
|
+
if (stmt.body === undefined) {
|
|
938
|
+
throw new Unsupported("@while");
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
const condition = parseExpression(rest);
|
|
942
|
+
const before = this.scope;
|
|
943
|
+
let count = 0;
|
|
944
|
+
|
|
945
|
+
this.repeating += 1;
|
|
946
|
+
|
|
947
|
+
try {
|
|
948
|
+
while (truthy(evaluate(condition, this))) {
|
|
949
|
+
if (++count > 400 || ++this.iterations > LIMITS.iterations) {
|
|
950
|
+
throw new Unsupported("@while too long");
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
this.scope = new Scope(before, true);
|
|
954
|
+
this.block(stmt.body, emit);
|
|
955
|
+
this.scope = before;
|
|
956
|
+
}
|
|
957
|
+
} finally {
|
|
958
|
+
this.scope = before;
|
|
959
|
+
this.repeating -= 1;
|
|
960
|
+
this.flush(stmt);
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
/** The body of a loop once for each step, in a scope of control flow. */
|
|
965
|
+
loop(stmt: Stmt, steps: ((scope: Scope) => void)[], emit: boolean): void {
|
|
966
|
+
const before = this.scope;
|
|
967
|
+
|
|
968
|
+
this.repeating += 1;
|
|
969
|
+
|
|
970
|
+
try {
|
|
971
|
+
for (const bind of steps) {
|
|
972
|
+
if (++this.iterations > LIMITS.iterations) {
|
|
973
|
+
throw new Unsupported("too many iterations");
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
this.scope = new Scope(before, true);
|
|
977
|
+
bind(this.scope);
|
|
978
|
+
this.block(stmt.body!, emit);
|
|
979
|
+
|
|
980
|
+
if (this.returnFlag) {
|
|
981
|
+
break;
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
} finally {
|
|
985
|
+
this.scope = before;
|
|
986
|
+
this.repeating -= 1;
|
|
987
|
+
this.flush(stmt);
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
/** The maps and lists that a loop built, written once when the loop ends. */
|
|
992
|
+
flush(stmt: Stmt): void {
|
|
993
|
+
if (this.repeating > 0) {
|
|
994
|
+
return;
|
|
995
|
+
}
|
|
996
|
+
|
|
997
|
+
for (const [name, value] of this.pending) {
|
|
998
|
+
this.write(`$${name}: ${declText(value)};`, stmt.line);
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
this.pending.clear();
|
|
1002
|
+
}
|
|
1003
|
+
}
|