@abaplint/transpiler-cli 2.14.31 → 2.14.32

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.
Files changed (2) hide show
  1. package/build/bundle.js +233 -66
  2. package/package.json +2 -2
package/build/bundle.js CHANGED
@@ -116978,43 +116978,92 @@ class Chunk {
116978
116978
  if (search === "") {
116979
116979
  throw new Error("Chunk.replaceAll, search string must not be empty");
116980
116980
  }
116981
- const positions = [];
116981
+ const escaped = search.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
116982
+ return this.replaceMatches(new RegExp(escaped, "g"), () => replacement);
116983
+ }
116984
+ /** Replace every match of a global pattern in one forward pass. The replacer
116985
+ * returns the new text, or undefined to keep the match. Source-map positions
116986
+ * are moved once at the end, so the cost is linear in the code size plus
116987
+ * mappings * log(replacements), however many matches there are. */
116988
+ replaceMatches(pattern, replacer) {
116989
+ if (!pattern.global) {
116990
+ throw new Error("Chunk.replaceMatches, pattern must be global");
116991
+ }
116992
+ const advance = (position, text) => {
116993
+ const lastNewline = text.lastIndexOf("\n");
116994
+ if (lastNewline < 0) {
116995
+ return { line: position.line, column: position.column + text.length };
116996
+ }
116997
+ let lines = 0;
116998
+ for (let index = text.indexOf("\n"); index >= 0; index = text.indexOf("\n", index + 1)) {
116999
+ lines++;
117000
+ }
117001
+ return { line: position.line + lines, column: text.length - lastNewline - 1 };
117002
+ };
117003
+ const edits = [];
117004
+ const output = [];
117005
+ let oldPosition = { line: 1, column: 0 };
117006
+ let newPosition = { line: 1, column: 0 };
116982
117007
  let cursor = 0;
116983
- while (true) {
116984
- const index = this.raw.indexOf(search, cursor);
116985
- if (index < 0) {
116986
- break;
117008
+ for (const match of this.raw.matchAll(pattern)) {
117009
+ const replacement = replacer(match);
117010
+ if (replacement === undefined || replacement === match[0]) {
117011
+ continue;
116987
117012
  }
116988
- positions.push(index);
116989
- cursor = index + search.length;
116990
- }
116991
- const replacementLines = replacement.split("\n");
116992
- const lineDelta = replacementLines.length - 1;
116993
- const replacementLastLineLength = replacementLines[replacementLines.length - 1].length;
116994
- for (const index of positions.reverse()) {
116995
- const before = this.raw.substring(0, index);
116996
- const startLine = before.split("\n").length;
116997
- const startColumn = before.length - before.lastIndexOf("\n") - 1;
116998
- const endColumn = startColumn + search.length;
116999
- for (const mapping of this.mappings) {
117000
- if (mapping.generated.line > startLine) {
117001
- mapping.generated.line += lineDelta;
117002
- }
117003
- else if (mapping.generated.line === startLine && mapping.generated.column >= endColumn) {
117004
- if (lineDelta === 0) {
117005
- mapping.generated.column += replacement.length - search.length;
117006
- }
117007
- else {
117008
- mapping.generated.line += lineDelta;
117009
- mapping.generated.column = replacementLastLineLength + mapping.generated.column - endColumn;
117010
- }
117013
+ const between = this.raw.substring(cursor, match.index);
117014
+ output.push(between, replacement);
117015
+ const oldStart = advance(oldPosition, between);
117016
+ const newStart = advance(newPosition, between);
117017
+ oldPosition = advance(oldStart, match[0]);
117018
+ newPosition = advance(newStart, replacement);
117019
+ edits.push({ oldStart, oldEnd: oldPosition, newStart, newEnd: newPosition });
117020
+ cursor = match.index + match[0].length;
117021
+ }
117022
+ if (edits.length === 0) {
117023
+ return this;
117024
+ }
117025
+ output.push(this.raw.substring(cursor));
117026
+ const before = (a, b) => a.line < b.line || (a.line === b.line && a.column < b.column);
117027
+ for (const mapping of this.mappings) {
117028
+ const position = mapping.generated;
117029
+ // last edit starting at or before this mapping
117030
+ let low = 0;
117031
+ let high = edits.length - 1;
117032
+ let found = -1;
117033
+ while (low <= high) {
117034
+ const middle = Math.floor((low + high) / 2);
117035
+ if (before(position, edits[middle].oldStart)) {
117036
+ high = middle - 1;
117011
117037
  }
117038
+ else {
117039
+ found = middle;
117040
+ low = middle + 1;
117041
+ }
117042
+ }
117043
+ if (found < 0) {
117044
+ continue;
117045
+ }
117046
+ const edit = edits[found];
117047
+ if (before(position, edit.oldEnd) && position.line === edit.oldStart.line) {
117048
+ // inside the replaced text, keep the offset from its start
117049
+ position.column = edit.newStart.column + position.column - edit.oldStart.column;
117050
+ position.line = edit.newStart.line;
117051
+ }
117052
+ else if (before(position, edit.oldEnd)) {
117053
+ position.line += edit.newStart.line - edit.oldStart.line;
117054
+ }
117055
+ else if (position.line === edit.oldEnd.line) {
117056
+ position.column = edit.newEnd.column + position.column - edit.oldEnd.column;
117057
+ position.line = edit.newEnd.line;
117058
+ }
117059
+ else {
117060
+ position.line += edit.newEnd.line - edit.oldEnd.line;
117012
117061
  }
117013
- this.raw = this.raw.substring(0, index) + replacement + this.raw.substring(index + search.length);
117014
117062
  }
117015
- const lastNewline = this.raw.lastIndexOf("\n");
117016
- this.lineCount = this.raw.split("\n").length;
117017
- this.lastLineLength = this.raw.length - lastNewline - 1;
117063
+ this.raw = output.join("");
117064
+ const end = advance(newPosition, output[output.length - 1]);
117065
+ this.lineCount = end.line;
117066
+ this.lastLineLength = end.column;
117018
117067
  return this;
117019
117068
  }
117020
117069
  stripLastNewline() {
@@ -143224,49 +143273,167 @@ class TypeFactoryRegistry {
143224
143273
  this.inProgress.delete(type);
143225
143274
  }
143226
143275
  }
143276
+ /** Matches every call to a factory of this module, the name is the match minus "()" */
143277
+ callPattern() {
143278
+ const prefix = ("$t_" + this.modulePrefix + "_").replace(/[$]/g, "\\$");
143279
+ return new RegExp(prefix + "\\d+\\(\\)", "g");
143280
+ }
143281
+ countCalls(text, into) {
143282
+ for (const match of text.matchAll(this.callPattern())) {
143283
+ const name = match[0].slice(0, -2);
143284
+ into.set(name, (into.get(name) ?? 0) + 1);
143285
+ }
143286
+ }
143227
143287
  /** Inline factories whose declaration and calls cost more than their use-site
143228
- * expressions. This keeps opting in safe for small, mostly unique modules. */
143288
+ * expressions. This keeps opting in safe for small, mostly unique modules.
143289
+ *
143290
+ * Factories are considered in registration order and the first one worth
143291
+ * inlining is inlined, until none is left. Inlining a factory only changes the
143292
+ * use counts of the factories it calls and the sizes of the factories calling
143293
+ * it, so these are updated in place instead of recounting all text, and the
143294
+ * text itself is rewritten once at the end. */
143229
143295
  finalize(body) {
143230
- let changed = true;
143231
- while (changed) {
143232
- changed = false;
143233
- for (const [key, factory] of this.factories) {
143234
- const call = factory.name + "()";
143235
- let uses = this.count(body.getCode(), call);
143236
- for (const other of this.factories.values()) {
143237
- if (other !== factory) {
143238
- uses += this.count(other.expression, call);
143239
- }
143296
+ const states = new Map();
143297
+ const ordered = [];
143298
+ for (const factory of this.factories.values()) {
143299
+ const state = {
143300
+ factory,
143301
+ order: ordered.length,
143302
+ uses: 0,
143303
+ expressionBytes: this.utf8Bytes(factory.expression),
143304
+ callBytes: this.utf8Bytes(factory.name + "()"),
143305
+ declarationBytes: this.utf8Bytes("function " + factory.name + "() { return ; }\n"),
143306
+ children: new Map(),
143307
+ parents: new Set(),
143308
+ inlined: false,
143309
+ };
143310
+ states.set(factory.name, state);
143311
+ ordered.push(state);
143312
+ }
143313
+ const bodyCalls = new Map();
143314
+ this.countCalls(body.getCode(), bodyCalls);
143315
+ for (const [name, count] of bodyCalls) {
143316
+ const state = states.get(name);
143317
+ if (state !== undefined) {
143318
+ state.uses += count;
143319
+ }
143320
+ }
143321
+ for (const state of ordered) {
143322
+ const calls = new Map();
143323
+ this.countCalls(state.factory.expression, calls);
143324
+ for (const [name, count] of calls) {
143325
+ const child = states.get(name);
143326
+ if (child !== undefined && child !== state) {
143327
+ state.children.set(name, count);
143328
+ child.parents.add(state.factory.name);
143329
+ child.uses += count;
143330
+ }
143331
+ }
143332
+ }
143333
+ const shouldInline = (state) => {
143334
+ const inlineBytes = state.expressionBytes * state.uses;
143335
+ const helperBytes = state.declarationBytes + state.expressionBytes + state.callBytes * state.uses;
143336
+ return state.uses === 0 || helperBytes >= inlineBytes;
143337
+ };
143338
+ // min-heap of registration order, entries are validated when popped
143339
+ const heap = [];
143340
+ const push = (value) => {
143341
+ heap.push(value);
143342
+ let index = heap.length - 1;
143343
+ while (index > 0) {
143344
+ const parent = Math.floor((index - 1) / 2);
143345
+ if (heap[parent] <= value) {
143346
+ break;
143240
143347
  }
143241
- const inlineBytes = this.utf8Bytes(factory.expression) * uses;
143242
- const helperBytes = this.utf8Bytes("function " + factory.name + "() { return " + factory.expression + "; }\n")
143243
- + this.utf8Bytes(call) * uses;
143244
- if (uses === 0 || helperBytes >= inlineBytes) {
143245
- body.replaceAll(call, factory.expression);
143246
- for (const other of this.factories.values()) {
143247
- if (other !== factory) {
143248
- other.expression = other.expression.split(call).join(factory.expression);
143249
- }
143348
+ heap[index] = heap[parent];
143349
+ index = parent;
143350
+ }
143351
+ heap[index] = value;
143352
+ };
143353
+ const pop = () => {
143354
+ const top = heap[0];
143355
+ const last = heap.pop();
143356
+ if (heap.length > 0) {
143357
+ let index = 0;
143358
+ while (true) {
143359
+ let child = index * 2 + 1;
143360
+ if (child >= heap.length) {
143361
+ break;
143250
143362
  }
143251
- this.factories.delete(key);
143252
- changed = true;
143253
- break;
143363
+ if (child + 1 < heap.length && heap[child + 1] < heap[child]) {
143364
+ child++;
143365
+ }
143366
+ if (heap[child] >= last) {
143367
+ break;
143368
+ }
143369
+ heap[index] = heap[child];
143370
+ index = child;
143254
143371
  }
143372
+ heap[index] = last;
143373
+ }
143374
+ return top;
143375
+ };
143376
+ for (const state of ordered) {
143377
+ if (shouldInline(state)) {
143378
+ push(state.order);
143255
143379
  }
143256
143380
  }
143257
- return this.render();
143258
- }
143259
- count(text, search) {
143260
- let count = 0;
143261
- let cursor = 0;
143262
- while (true) {
143263
- const index = text.indexOf(search, cursor);
143264
- if (index < 0) {
143265
- return count;
143381
+ while (heap.length > 0) {
143382
+ const state = ordered[pop()];
143383
+ if (state.inlined || !shouldInline(state)) {
143384
+ continue;
143385
+ }
143386
+ state.inlined = true;
143387
+ const name = state.factory.name;
143388
+ for (const parentName of state.parents) {
143389
+ const parent = states.get(parentName);
143390
+ const occurrences = parent.children.get(name);
143391
+ parent.children.delete(name);
143392
+ parent.expressionBytes += occurrences * (state.expressionBytes - state.callBytes);
143393
+ for (const [childName, count] of state.children) {
143394
+ parent.children.set(childName, (parent.children.get(childName) ?? 0) + occurrences * count);
143395
+ states.get(childName).parents.add(parentName);
143396
+ }
143397
+ }
143398
+ for (const [childName, count] of state.children) {
143399
+ const child = states.get(childName);
143400
+ child.uses += count * (state.uses - 1);
143401
+ child.parents.delete(name);
143402
+ }
143403
+ for (const affected of [...state.parents, ...state.children.keys()]) {
143404
+ const other = states.get(affected);
143405
+ if (shouldInline(other)) {
143406
+ push(other.order);
143407
+ }
143408
+ }
143409
+ }
143410
+ // rewrite all text once, expanding inlined calls recursively
143411
+ const expanded = new Map();
143412
+ const expand = (text) => text.replace(this.callPattern(), call => {
143413
+ const state = states.get(call.slice(0, -2));
143414
+ if (state === undefined || state.inlined === false) {
143415
+ return call;
143416
+ }
143417
+ let result = expanded.get(state.factory.name);
143418
+ if (result === undefined) {
143419
+ result = expand(state.factory.expression);
143420
+ expanded.set(state.factory.name, result);
143421
+ }
143422
+ return result;
143423
+ });
143424
+ body.replaceMatches(this.callPattern(), match => {
143425
+ const state = states.get(match[0].slice(0, -2));
143426
+ return state?.inlined === true ? expand(match[0]) : undefined;
143427
+ });
143428
+ for (const [key, factory] of this.factories) {
143429
+ if (states.get(factory.name).inlined) {
143430
+ this.factories.delete(key);
143431
+ }
143432
+ else {
143433
+ factory.expression = expand(factory.expression);
143266
143434
  }
143267
- count++;
143268
- cursor = index + search.length;
143269
143435
  }
143436
+ return this.render();
143270
143437
  }
143271
143438
  utf8Bytes(input) {
143272
143439
  let bytes = 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abaplint/transpiler-cli",
3
- "version": "2.14.31",
3
+ "version": "2.14.32",
4
4
  "description": "Transpiler - Command Line Interface",
5
5
  "funding": "https://github.com/sponsors/larshp",
6
6
  "bin": {
@@ -28,7 +28,7 @@
28
28
  "license": "MIT",
29
29
  "devDependencies": {
30
30
  "@abaplint/core": "^2.120.105",
31
- "@abaplint/transpiler": "^2.14.31",
31
+ "@abaplint/transpiler": "^2.14.32",
32
32
  "@types/glob": "^9.0.0",
33
33
  "@types/node": "^26.6.6",
34
34
  "@types/progress": "^2.0.7",