@triflare/fx 2.2.0 → 2.3.0-alpha.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/package.json +1 -1
- package/product.yml +1 -1
- package/src/cli/commands/feature.js +90 -19
- package/src/cli/templates/index.js +2 -1
- package/src/comments.js +287 -0
- package/src/compiler.js +97 -33
- package/src/config-edit.js +341 -1
- package/src/config.js +139 -7
- package/src/feature-registry.js +85 -0
- package/src/features.js +219 -111
- package/src/layout.js +402 -0
- package/src/lint/config-file.js +26 -5
- package/src/merge.js +5 -1
- package/src/parser.js +96 -50
- package/vendor/pm-packager-dist/.fx-build.json +1 -1
- package/vendor/pm-vm-dist/.fx-build.json +1 -1
package/package.json
CHANGED
package/product.yml
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import path from "node:path";
|
|
2
|
+
import { findConfigPath } from "../../config.js";
|
|
2
3
|
import {
|
|
3
4
|
disableFeature,
|
|
4
5
|
enableFeature,
|
|
5
6
|
featureEnabled,
|
|
7
|
+
featureNames,
|
|
6
8
|
getFeature,
|
|
9
|
+
migrateFeatureConfig,
|
|
7
10
|
updateFeature,
|
|
8
11
|
} from "../../features.js";
|
|
9
12
|
import { loadVendorConfig, requireConfigPath } from "./vendor/helpers.js";
|
|
@@ -11,46 +14,115 @@ import { UsageError } from "../errors.js";
|
|
|
11
14
|
|
|
12
15
|
const ACTIONS = ["enable", "disable", "update"];
|
|
13
16
|
|
|
17
|
+
function featureSetting(feature) {
|
|
18
|
+
return `fx.features.${feature.name}`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function describeStatus(feature, enabled) {
|
|
22
|
+
if (enabled)
|
|
23
|
+
return feature.command
|
|
24
|
+
? `Run "fx ${feature.command}" to use this feature.`
|
|
25
|
+
: `This feature is active in builds.`;
|
|
26
|
+
return `Run "fx feature ${feature.name} enable" to enable it.`;
|
|
27
|
+
}
|
|
28
|
+
|
|
14
29
|
export const featureCommand = {
|
|
15
30
|
name: "feature",
|
|
16
31
|
group: "Core Commands:",
|
|
17
|
-
summary: "
|
|
18
|
-
description: `
|
|
19
|
-
|
|
32
|
+
summary: "List, install, update or remove an optional feature",
|
|
33
|
+
description: `Every optional feature is registered once and switched under
|
|
34
|
+
the "fx.features" key in fx.yml. "enable" turns a feature on (copying its
|
|
35
|
+
extension into the project when it has one); "update" replaces that copy
|
|
36
|
+
with the one bundled in this Fx version; and "disable" turns it off.
|
|
37
|
+
"fx feature migrate" moves older fx.tests, fx.run, fx.partialSprites, and
|
|
38
|
+
top-level features: settings under fx.features.`,
|
|
39
|
+
arguments: ["[name]", "[action]"],
|
|
20
40
|
options: [
|
|
21
41
|
{
|
|
22
42
|
flags: "--force",
|
|
23
43
|
description: "Replace an installed copy that was edited (enable)",
|
|
24
44
|
},
|
|
25
|
-
{ flags: "--dry-run", description: "Preview update
|
|
45
|
+
{ flags: "--dry-run", description: "Preview update or migration" },
|
|
26
46
|
],
|
|
27
47
|
examples: [
|
|
28
|
-
"fx feature
|
|
48
|
+
"fx feature list",
|
|
49
|
+
"fx feature tests enable",
|
|
50
|
+
"fx feature partialSprites enable",
|
|
29
51
|
"fx feature temporaryvariables update --dry-run",
|
|
30
|
-
"fx feature
|
|
52
|
+
"fx feature migrate",
|
|
31
53
|
],
|
|
32
54
|
async run({ args, options, cwd, reporter, prompts, yes, product }) {
|
|
33
55
|
const [name, action] = args;
|
|
56
|
+
|
|
57
|
+
if (name == null || name === "list") {
|
|
58
|
+
const configPath = findConfigPath(cwd);
|
|
59
|
+
const rows = featureNames().map((featureName) => {
|
|
60
|
+
const feature = getFeature(featureName);
|
|
61
|
+
const enabled = featureEnabled({ configPath, name: featureName });
|
|
62
|
+
reporter.item(featureName, enabled ? "enabled" : "disabled", {
|
|
63
|
+
width: 20,
|
|
64
|
+
suffix: feature.title,
|
|
65
|
+
});
|
|
66
|
+
return {
|
|
67
|
+
name: featureName,
|
|
68
|
+
title: feature.title,
|
|
69
|
+
enabled,
|
|
70
|
+
setting: featureSetting(feature),
|
|
71
|
+
};
|
|
72
|
+
});
|
|
73
|
+
return { features: rows };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (name === "migrate") {
|
|
77
|
+
if (action != null)
|
|
78
|
+
throw new UsageError(`"fx feature migrate" does not take an action.`, {
|
|
79
|
+
hint: `use "fx feature <name> <action>" for a single feature`,
|
|
80
|
+
});
|
|
81
|
+
const configPath = requireConfigPath(cwd, product);
|
|
82
|
+
const result = migrateFeatureConfig({
|
|
83
|
+
configPath,
|
|
84
|
+
dryRun: Boolean(options.dryRun),
|
|
85
|
+
});
|
|
86
|
+
for (const name of result.conflicts)
|
|
87
|
+
reporter.warn(
|
|
88
|
+
`Left top-level features.${name} in place: ${name} is a core feature, so it cannot move under fx.features.`,
|
|
89
|
+
);
|
|
90
|
+
if (!result.changed) {
|
|
91
|
+
reporter.info("No legacy feature settings to migrate");
|
|
92
|
+
return {
|
|
93
|
+
action: "migrate",
|
|
94
|
+
migrated: [],
|
|
95
|
+
conflicts: result.conflicts,
|
|
96
|
+
changed: false,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
reporter.text(result.diff);
|
|
100
|
+
if (options.dryRun) reporter.info("Dry run: fx.yml would be migrated");
|
|
101
|
+
else
|
|
102
|
+
reporter.success(
|
|
103
|
+
`Migrated ${result.migrated.length} feature setting(s); backup: ${path.basename(
|
|
104
|
+
result.backup,
|
|
105
|
+
)}`,
|
|
106
|
+
);
|
|
107
|
+
return {
|
|
108
|
+
action: "migrate",
|
|
109
|
+
migrated: result.migrated,
|
|
110
|
+
conflicts: result.conflicts,
|
|
111
|
+
changed: true,
|
|
112
|
+
dryRun: Boolean(options.dryRun),
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
34
116
|
const configPath = requireConfigPath(cwd, product);
|
|
35
117
|
const configDir = path.dirname(configPath);
|
|
36
118
|
const feature = getFeature(name);
|
|
37
119
|
|
|
38
120
|
if (action == null) {
|
|
39
|
-
if (!feature.core)
|
|
40
|
-
throw new UsageError(`Missing an action for ${JSON.stringify(name)}.`, {
|
|
41
|
-
hint: `use one of: ${ACTIONS.join(", ")}`,
|
|
42
|
-
});
|
|
43
121
|
const enabled = featureEnabled({ configPath, name: feature.name });
|
|
44
122
|
reporter.detail("Feature", feature.title);
|
|
45
123
|
reporter.detail("Status", enabled ? "enabled" : "disabled");
|
|
46
|
-
reporter.detail("Setting",
|
|
47
|
-
reporter.info(
|
|
48
|
-
enabled
|
|
49
|
-
? feature.command === "run"
|
|
50
|
-
? `Run "fx run" to run the project's entry point.`
|
|
51
|
-
: `Run "fx ${feature.command ?? "test"}" to use this feature.`
|
|
52
|
-
: `Run "fx feature ${feature.name} enable" to enable it.`,
|
|
53
|
-
);
|
|
124
|
+
reporter.detail("Setting", featureSetting(feature));
|
|
125
|
+
reporter.info(describeStatus(feature, enabled));
|
|
54
126
|
return {
|
|
55
127
|
feature: feature.name,
|
|
56
128
|
action: "status",
|
|
@@ -103,7 +175,6 @@ export const featureCommand = {
|
|
|
103
175
|
};
|
|
104
176
|
}
|
|
105
177
|
reporter.success(`Disabled ${result.feature.title}`);
|
|
106
|
-
if (result.warning) reporter.warn(result.warning);
|
|
107
178
|
return {
|
|
108
179
|
feature: result.name,
|
|
109
180
|
action,
|
|
@@ -25,7 +25,8 @@ function configText({ name, width, height, target }) {
|
|
|
25
25
|
" turbo: false",
|
|
26
26
|
" interpolation: true",
|
|
27
27
|
"fx:",
|
|
28
|
-
"
|
|
28
|
+
" features:",
|
|
29
|
+
" partialSprites: false",
|
|
29
30
|
];
|
|
30
31
|
if (target !== "none") {
|
|
31
32
|
lines.push("package:", ` target: ${target}`);
|
package/src/comments.js
ADDED
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Attach source comments to the AST.
|
|
3
|
+
*
|
|
4
|
+
* Comments are not part of the token stream. After parsing, each comment is
|
|
5
|
+
* resolved to the block-bearing declaration or statement it belongs to:
|
|
6
|
+
*
|
|
7
|
+
* - a comment before an item attaches to that item (`comments` on the node),
|
|
8
|
+
* - a trailing comment on an item's own line attaches to that item,
|
|
9
|
+
* - a comment inside a block body with nothing after it attaches to the
|
|
10
|
+
* enclosing block,
|
|
11
|
+
* - comments that cannot sit on a block (before a sprite, variable, list or
|
|
12
|
+
* property declaration, or in an empty sprite) become standalone workspace
|
|
13
|
+
* comments on the target they belong to.
|
|
14
|
+
*
|
|
15
|
+
* Comments inside `test` declarations are dropped: test files are never part of
|
|
16
|
+
* a built project.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/** AST node types that compile to at least one block. */
|
|
20
|
+
const BLOCK_OWNERS = new Set([
|
|
21
|
+
"Assign",
|
|
22
|
+
"BlockCall",
|
|
23
|
+
"Forever",
|
|
24
|
+
"FunctionDecl",
|
|
25
|
+
"HatDecl",
|
|
26
|
+
"If",
|
|
27
|
+
"Repeat",
|
|
28
|
+
"Return",
|
|
29
|
+
"TempVarDecl",
|
|
30
|
+
"While",
|
|
31
|
+
]);
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Turn a raw `//` or block comment into the text Scratch should display.
|
|
35
|
+
* Block-comment asterisk decorations are removed line by line.
|
|
36
|
+
* @param {string} raw
|
|
37
|
+
* @returns {string}
|
|
38
|
+
*/
|
|
39
|
+
export function commentText(raw) {
|
|
40
|
+
const source = raw.replace(/\r\n?/g, "\n");
|
|
41
|
+
if (source.startsWith("//")) return source.slice(2).trim();
|
|
42
|
+
if (source.startsWith("/*") && source.endsWith("*/")) {
|
|
43
|
+
const lines = source
|
|
44
|
+
.slice(2, -2)
|
|
45
|
+
.split("\n")
|
|
46
|
+
.map((line) => line.replace(/^\s*\*?[ \t]?/, "").trimEnd());
|
|
47
|
+
while (lines.length && !lines[0].trim()) lines.shift();
|
|
48
|
+
while (lines.length && !lines[lines.length - 1].trim()) lines.pop();
|
|
49
|
+
return lines.join("\n").trim();
|
|
50
|
+
}
|
|
51
|
+
return source.trim();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Resolve comment placement for a freshly parsed program.
|
|
56
|
+
* @param {object} ast program node (mutated with attachments)
|
|
57
|
+
* @param {Array} comments lexer comments
|
|
58
|
+
* @param {Array} tokens lexer tokens
|
|
59
|
+
* @param {WeakMap<object, object>} spans body brace tokens per node
|
|
60
|
+
*/
|
|
61
|
+
export function attachComments(ast, comments, tokens, spans) {
|
|
62
|
+
const standalone = [];
|
|
63
|
+
if (comments.length > 0) {
|
|
64
|
+
const indexes = new Map();
|
|
65
|
+
tokens.forEach((token, index) => indexes.set(token, index));
|
|
66
|
+
|
|
67
|
+
const programItems = [
|
|
68
|
+
...(ast.stage ? [ast.stage] : []),
|
|
69
|
+
...ast.sprites,
|
|
70
|
+
...ast.topItems,
|
|
71
|
+
...(ast.tests || []),
|
|
72
|
+
].filter((item) => indexes.has(item.loc));
|
|
73
|
+
|
|
74
|
+
const scopes = [
|
|
75
|
+
{
|
|
76
|
+
items: programItems,
|
|
77
|
+
open: -1,
|
|
78
|
+
close: tokens.length - 1,
|
|
79
|
+
owner: null,
|
|
80
|
+
target: "Stage",
|
|
81
|
+
stage: true,
|
|
82
|
+
},
|
|
83
|
+
];
|
|
84
|
+
for (const item of programItems)
|
|
85
|
+
collectScopes(item, scopes, "Stage", true, indexes, spans);
|
|
86
|
+
|
|
87
|
+
for (const comment of comments) {
|
|
88
|
+
const text = commentText(comment.text);
|
|
89
|
+
if (!text) continue;
|
|
90
|
+
placeComment(
|
|
91
|
+
ast,
|
|
92
|
+
innermostScope(scopes, comment.after),
|
|
93
|
+
comment,
|
|
94
|
+
text,
|
|
95
|
+
indexes,
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
ast.standaloneComments = standalone;
|
|
100
|
+
|
|
101
|
+
function addStandalone(text, target, stage) {
|
|
102
|
+
// `""` is a valid sprite name, so only a missing target falls back to the
|
|
103
|
+
// stage in the compiler; an empty name must still resolve through byName.
|
|
104
|
+
if (target != null) standalone.push({ text, target, stage });
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Sibling items ordered by source position, built once per scope. */
|
|
108
|
+
function orderedItems(scope, indexes) {
|
|
109
|
+
if (scope.entries) return scope.entries;
|
|
110
|
+
const entries = scope.items
|
|
111
|
+
.map((item) => ({ item, index: indexes.get(item.loc) }))
|
|
112
|
+
.filter((entry) => entry.index !== undefined)
|
|
113
|
+
.sort((a, b) => a.index - b.index);
|
|
114
|
+
// An item ends at the last token before the next item (or the scope's
|
|
115
|
+
// closing brace), so a trailing comment on a multiline item is still on
|
|
116
|
+
// that item's line.
|
|
117
|
+
for (let i = 0; i < entries.length; i++) {
|
|
118
|
+
const end = i + 1 < entries.length ? entries[i + 1].index : scope.close;
|
|
119
|
+
const token = tokens[Math.max(0, end - 1)];
|
|
120
|
+
entries[i].endLine = token ? token.line : entries[i].item.loc.line;
|
|
121
|
+
}
|
|
122
|
+
scope.entries = entries;
|
|
123
|
+
return entries;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** First position in the ordered entries whose token index is >= `index`. */
|
|
127
|
+
function lowerBound(entries, index) {
|
|
128
|
+
let low = 0;
|
|
129
|
+
let high = entries.length;
|
|
130
|
+
while (low < high) {
|
|
131
|
+
const mid = (low + high) >> 1;
|
|
132
|
+
if (entries[mid].index < index) low = mid + 1;
|
|
133
|
+
else high = mid;
|
|
134
|
+
}
|
|
135
|
+
return low;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function placeComment(program, scope, comment, text, indexes) {
|
|
139
|
+
// Comments inside a test body are dropped with the rest of the test.
|
|
140
|
+
if (scope.drop) return;
|
|
141
|
+
const entries = orderedItems(scope, indexes);
|
|
142
|
+
const position = lowerBound(entries, comment.after);
|
|
143
|
+
|
|
144
|
+
// A comment after code on the same line belongs to that code, including
|
|
145
|
+
// when the code spans several lines.
|
|
146
|
+
for (let i = position - 1; i >= 0; i--) {
|
|
147
|
+
const entry = entries[i];
|
|
148
|
+
if (entry.endLine !== comment.line) break;
|
|
149
|
+
return place(program, scope, entry.item, text);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const next = entries[position];
|
|
153
|
+
if (next) return place(program, scope, next.item, text);
|
|
154
|
+
|
|
155
|
+
// Nothing follows in a block body: the comment describes the block.
|
|
156
|
+
if (scope.owner && BLOCK_OWNERS.has(scope.owner.type))
|
|
157
|
+
return place(program, scope, scope.owner, text);
|
|
158
|
+
|
|
159
|
+
const previous = entries[position - 1];
|
|
160
|
+
if (previous) return place(program, scope, previous.item, text);
|
|
161
|
+
|
|
162
|
+
// An empty body: the comment describes the block or sprite that owns it.
|
|
163
|
+
if (scope.owner) return place(program, scope, scope.owner, text);
|
|
164
|
+
addStandalone(text, scope.target, scope.stage);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function place(program, scope, item, text) {
|
|
168
|
+
if (item.type === "TestDecl") return;
|
|
169
|
+
if (BLOCK_OWNERS.has(item.type)) {
|
|
170
|
+
item.comments = item.comments || [];
|
|
171
|
+
item.comments.push(text);
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
if (item.type === "SpriteDecl")
|
|
175
|
+
return addStandalone(text, item.name, false);
|
|
176
|
+
if (item.type === "StageDecl") return addStandalone(text, "Stage", true);
|
|
177
|
+
addStandalone(text, scope.target, scope.stage);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function innermostScope(scopes, index) {
|
|
182
|
+
let best = scopes[0];
|
|
183
|
+
for (const scope of scopes) {
|
|
184
|
+
if (index <= scope.open || index > scope.close) continue;
|
|
185
|
+
if (scope.open > best.open) best = scope;
|
|
186
|
+
}
|
|
187
|
+
return best;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function collectScopes(item, scopes, target, stage, indexes, spans) {
|
|
191
|
+
if (!item) return;
|
|
192
|
+
|
|
193
|
+
const span = spans.get(item) || {};
|
|
194
|
+
const addScope = (
|
|
195
|
+
body,
|
|
196
|
+
open,
|
|
197
|
+
close,
|
|
198
|
+
owner,
|
|
199
|
+
scopeTarget,
|
|
200
|
+
scopeStage,
|
|
201
|
+
drop = false,
|
|
202
|
+
) => {
|
|
203
|
+
if (!body || !open || !close) return;
|
|
204
|
+
scopes.push({
|
|
205
|
+
items: body,
|
|
206
|
+
open: indexes.get(open) ?? -1,
|
|
207
|
+
close: indexes.get(close) ?? -1,
|
|
208
|
+
owner,
|
|
209
|
+
target: scopeTarget,
|
|
210
|
+
stage: scopeStage,
|
|
211
|
+
drop,
|
|
212
|
+
});
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
// Test files never reach a built project, so their comments are dropped.
|
|
216
|
+
// Their braces are still registered as a scope so comments inside a test
|
|
217
|
+
// body cannot attach to declarations around it.
|
|
218
|
+
if (item.type === "TestDecl") {
|
|
219
|
+
addScope(
|
|
220
|
+
item.body,
|
|
221
|
+
span.bodyOpen,
|
|
222
|
+
span.bodyClose,
|
|
223
|
+
item,
|
|
224
|
+
target,
|
|
225
|
+
stage,
|
|
226
|
+
true,
|
|
227
|
+
);
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
switch (item.type) {
|
|
232
|
+
case "SpriteDecl":
|
|
233
|
+
addScope(
|
|
234
|
+
item.body,
|
|
235
|
+
span.bodyOpen,
|
|
236
|
+
span.bodyClose,
|
|
237
|
+
item,
|
|
238
|
+
item.name,
|
|
239
|
+
false,
|
|
240
|
+
);
|
|
241
|
+
for (const child of item.body)
|
|
242
|
+
collectScopes(child, scopes, item.name, false, indexes, spans);
|
|
243
|
+
break;
|
|
244
|
+
case "StageDecl":
|
|
245
|
+
addScope(item.body, span.bodyOpen, span.bodyClose, item, "Stage", true);
|
|
246
|
+
for (const child of item.body)
|
|
247
|
+
collectScopes(child, scopes, "Stage", true, indexes, spans);
|
|
248
|
+
break;
|
|
249
|
+
case "HatDecl":
|
|
250
|
+
case "FunctionDecl":
|
|
251
|
+
addScope(item.body, span.bodyOpen, span.bodyClose, item, target, stage);
|
|
252
|
+
for (const child of item.body)
|
|
253
|
+
collectScopes(child, scopes, target, stage, indexes, spans);
|
|
254
|
+
break;
|
|
255
|
+
case "If":
|
|
256
|
+
addScope(item.then, span.bodyOpen, span.bodyClose, item, target, stage);
|
|
257
|
+
for (const child of item.then)
|
|
258
|
+
collectScopes(child, scopes, target, stage, indexes, spans);
|
|
259
|
+
if (span.elseOpen) {
|
|
260
|
+
addScope(item.else, span.elseOpen, span.elseClose, item, target, stage);
|
|
261
|
+
}
|
|
262
|
+
for (const child of item.else || [])
|
|
263
|
+
collectScopes(child, scopes, target, stage, indexes, spans);
|
|
264
|
+
break;
|
|
265
|
+
case "Repeat":
|
|
266
|
+
case "While":
|
|
267
|
+
case "Forever":
|
|
268
|
+
addScope(item.body, span.bodyOpen, span.bodyClose, item, target, stage);
|
|
269
|
+
for (const child of item.body)
|
|
270
|
+
collectScopes(child, scopes, target, stage, indexes, spans);
|
|
271
|
+
break;
|
|
272
|
+
case "BlockCall":
|
|
273
|
+
if (item.body) {
|
|
274
|
+
addScope(item.body, span.bodyOpen, span.bodyClose, item, target, stage);
|
|
275
|
+
for (const child of item.body)
|
|
276
|
+
collectScopes(child, scopes, target, stage, indexes, spans);
|
|
277
|
+
}
|
|
278
|
+
if (item.else) {
|
|
279
|
+
addScope(item.else, span.elseOpen, span.elseClose, item, target, stage);
|
|
280
|
+
for (const child of item.else)
|
|
281
|
+
collectScopes(child, scopes, target, stage, indexes, spans);
|
|
282
|
+
}
|
|
283
|
+
break;
|
|
284
|
+
default:
|
|
285
|
+
break;
|
|
286
|
+
}
|
|
287
|
+
}
|
package/src/compiler.js
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
import { CATALOG, LIST_OPS, extensionForOpcode } from "./catalog.js";
|
|
9
9
|
import { CompileError } from "./errors.js";
|
|
10
10
|
import { configComment } from "./config.js";
|
|
11
|
+
import { COMMENT_HEIGHT, COMMENT_WIDTH, layoutTarget } from "./layout.js";
|
|
11
12
|
import { md5hex, imageSize, wavInfo, detectFormat } from "./sb3.js";
|
|
12
13
|
|
|
13
14
|
const HAT_ALIASES = {
|
|
@@ -84,19 +85,6 @@ class Context {
|
|
|
84
85
|
this.lists = new Map();
|
|
85
86
|
this.funcs = new Map();
|
|
86
87
|
this.paramScopes = [];
|
|
87
|
-
this.scriptX = 48;
|
|
88
|
-
this.scriptY = 48;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
nextScriptPosition() {
|
|
92
|
-
const x = this.scriptX;
|
|
93
|
-
const y = this.scriptY;
|
|
94
|
-
this.scriptY += 420;
|
|
95
|
-
if (this.scriptY > 3600) {
|
|
96
|
-
this.scriptY = 48;
|
|
97
|
-
this.scriptX += 500;
|
|
98
|
-
}
|
|
99
|
-
return { x, y };
|
|
100
88
|
}
|
|
101
89
|
|
|
102
90
|
resolveValue(name) {
|
|
@@ -261,6 +249,22 @@ export class Compiler {
|
|
|
261
249
|
// can only be given their mutation once every stack is complete.
|
|
262
250
|
for (const ctx of contexts) this.applyDynamicNextMutations(ctx);
|
|
263
251
|
|
|
252
|
+
// Source comments that could not sit on a block become workspace comments.
|
|
253
|
+
this.emitStandaloneComments(ast, contexts);
|
|
254
|
+
|
|
255
|
+
// The config comment is a workspace comment too. Emit it before layout so
|
|
256
|
+
// it is positioned with the rest instead of floating at its old spot.
|
|
257
|
+
const configText = configComment(this.projectConfig);
|
|
258
|
+
if (configText)
|
|
259
|
+
this.addWorkspaceComment(this.stageCtx, configText, {
|
|
260
|
+
width: 350,
|
|
261
|
+
height: 170,
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
// Pass 4: pack the scripts like the editor's clean up tool. Sizes depend on
|
|
265
|
+
// the complete block tree, so this has to run after every stack exists.
|
|
266
|
+
for (const ctx of contexts) layoutTarget(ctx.target, this.catalog);
|
|
267
|
+
|
|
264
268
|
this.validateRequiredExtensions();
|
|
265
269
|
|
|
266
270
|
return { project: this.assembleProject(contexts), assets: this.assets };
|
|
@@ -601,7 +605,6 @@ export class Compiler {
|
|
|
601
605
|
if (!entry || entry.kind !== "hat")
|
|
602
606
|
this.fail(`Unknown event ${JSON.stringify(short)}`, decl.loc);
|
|
603
607
|
|
|
604
|
-
const pos = ctx.nextScriptPosition();
|
|
605
608
|
const fields = {};
|
|
606
609
|
const inputs = {};
|
|
607
610
|
const argSpecs = entry.args || [];
|
|
@@ -637,9 +640,8 @@ export class Compiler {
|
|
|
637
640
|
fields,
|
|
638
641
|
inputs,
|
|
639
642
|
topLevel: true,
|
|
640
|
-
x: pos.x,
|
|
641
|
-
y: pos.y,
|
|
642
643
|
});
|
|
644
|
+
this.attachNodeComments(ctx, decl, id);
|
|
643
645
|
// re-parent any inputs that referenced this hat
|
|
644
646
|
this.reparentInputs(ctx, id, inputs);
|
|
645
647
|
const first = this.newStack(ctx, decl.body, id);
|
|
@@ -649,15 +651,13 @@ export class Compiler {
|
|
|
649
651
|
|
|
650
652
|
emitFunction(ctx, decl) {
|
|
651
653
|
const fn = ctx.funcs.get(decl.name);
|
|
652
|
-
const pos = ctx.nextScriptPosition();
|
|
653
654
|
const defId = this.addBlock(ctx, {
|
|
654
655
|
opcode: fn.returns
|
|
655
656
|
? "procedures_definition_return"
|
|
656
657
|
: "procedures_definition",
|
|
657
658
|
topLevel: true,
|
|
658
|
-
x: pos.x,
|
|
659
|
-
y: pos.y,
|
|
660
659
|
});
|
|
660
|
+
this.attachNodeComments(ctx, decl, defId);
|
|
661
661
|
|
|
662
662
|
const protoId = uid("proto");
|
|
663
663
|
const protoInputs = {};
|
|
@@ -704,6 +704,76 @@ export class Compiler {
|
|
|
704
704
|
if (first) ctx.target.blocks[defId].next = first;
|
|
705
705
|
}
|
|
706
706
|
|
|
707
|
+
// ------------------------------------------------------------- comments
|
|
708
|
+
|
|
709
|
+
/**
|
|
710
|
+
* Emit the comments attached to one AST node as a single Scratch block
|
|
711
|
+
* comment. Scratch links a comment from both ends (`block.comment` and
|
|
712
|
+
* `comment.blockId`), so both are written here.
|
|
713
|
+
* @param {Context} ctx
|
|
714
|
+
* @param {{comments?: string[]}} node
|
|
715
|
+
* @param {string} blockId
|
|
716
|
+
*/
|
|
717
|
+
attachNodeComments(ctx, node, blockId) {
|
|
718
|
+
if (!node.comments || node.comments.length === 0) return;
|
|
719
|
+
const block = ctx.target.blocks[blockId];
|
|
720
|
+
if (!block) return;
|
|
721
|
+
const id = uid("c");
|
|
722
|
+
block.comment = id;
|
|
723
|
+
ctx.target.comments[id] = {
|
|
724
|
+
blockId,
|
|
725
|
+
// The editor positions attached comments next to their block when both
|
|
726
|
+
// coordinates are missing (`ScratchBlockComment.autoPosition_`).
|
|
727
|
+
x: null,
|
|
728
|
+
y: null,
|
|
729
|
+
width: COMMENT_WIDTH,
|
|
730
|
+
height: COMMENT_HEIGHT,
|
|
731
|
+
minimized: false,
|
|
732
|
+
text: node.comments.join("\n"),
|
|
733
|
+
};
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
/**
|
|
737
|
+
* Add a comment that floats on the target's workspace instead of a block.
|
|
738
|
+
* @param {Context} ctx
|
|
739
|
+
* @param {string} text
|
|
740
|
+
* @param {{width?: number, height?: number}} [size]
|
|
741
|
+
*/
|
|
742
|
+
addWorkspaceComment(
|
|
743
|
+
ctx,
|
|
744
|
+
text,
|
|
745
|
+
{ width = COMMENT_WIDTH, height = COMMENT_HEIGHT } = {},
|
|
746
|
+
) {
|
|
747
|
+
const id = uid("c");
|
|
748
|
+
ctx.target.comments[id] = {
|
|
749
|
+
blockId: null,
|
|
750
|
+
x: null,
|
|
751
|
+
y: null,
|
|
752
|
+
width,
|
|
753
|
+
height,
|
|
754
|
+
minimized: false,
|
|
755
|
+
text,
|
|
756
|
+
};
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
/** Place standalone comments in the targets they were declared in. */
|
|
760
|
+
emitStandaloneComments(ast, contexts) {
|
|
761
|
+
const standalone = ast.standaloneComments || [];
|
|
762
|
+
if (standalone.length === 0) return;
|
|
763
|
+
// Sprite comments are routed by name; stage comments carry an explicit
|
|
764
|
+
// flag so a sprite named "Stage" cannot steal them.
|
|
765
|
+
const byName = new Map();
|
|
766
|
+
for (const ctx of contexts) {
|
|
767
|
+
if (!ctx.target.isStage) byName.set(ctx.target.name, ctx);
|
|
768
|
+
}
|
|
769
|
+
for (const comment of standalone) {
|
|
770
|
+
const ctx = comment.stage
|
|
771
|
+
? this.stageCtx
|
|
772
|
+
: byName.get(comment.target) || this.stageCtx;
|
|
773
|
+
this.addWorkspaceComment(ctx, comment.text);
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
|
|
707
777
|
// --------------------------------------------------------- block utils
|
|
708
778
|
|
|
709
779
|
reparentInputs(ctx, parentId, inputs) {
|
|
@@ -750,7 +820,14 @@ export class Compiler {
|
|
|
750
820
|
);
|
|
751
821
|
}
|
|
752
822
|
const id = this.compileStatement(ctx, statement);
|
|
753
|
-
if (id == null)
|
|
823
|
+
if (id == null) {
|
|
824
|
+
// A statement stripped in build mode (run-only block) still carries
|
|
825
|
+
// its notes: keep them on the workspace instead of dropping them.
|
|
826
|
+
if (statement.comments && statement.comments.length)
|
|
827
|
+
this.addWorkspaceComment(ctx, statement.comments.join("\n"));
|
|
828
|
+
continue;
|
|
829
|
+
}
|
|
830
|
+
this.attachNodeComments(ctx, statement, id);
|
|
754
831
|
if (first == null) {
|
|
755
832
|
first = id;
|
|
756
833
|
ctx.target.blocks[id].parent = parentId;
|
|
@@ -1797,19 +1874,6 @@ export class Compiler {
|
|
|
1797
1874
|
// broadcasts live on the stage
|
|
1798
1875
|
for (const [name, id] of this.broadcasts) target.broadcasts[id] = name;
|
|
1799
1876
|
this.ensureStageAssets(target);
|
|
1800
|
-
const comment = configComment(this.projectConfig);
|
|
1801
|
-
if (comment) {
|
|
1802
|
-
const commentId = uid("c");
|
|
1803
|
-
target.comments[commentId] = {
|
|
1804
|
-
blockId: null,
|
|
1805
|
-
x: 50,
|
|
1806
|
-
y: 50,
|
|
1807
|
-
width: 350,
|
|
1808
|
-
height: 170,
|
|
1809
|
-
minimized: false,
|
|
1810
|
-
text: comment,
|
|
1811
|
-
};
|
|
1812
|
-
}
|
|
1813
1877
|
} else {
|
|
1814
1878
|
this.ensureSpriteAssets(target);
|
|
1815
1879
|
}
|