ts-const-value-transformer 0.8.0 → 0.8.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/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # Changelog
2
2
 
3
+ ## v0.8.2
4
+
5
+ - Fix to use internal functions to reduce memory usage
6
+
7
+ ## v0.8.1
8
+
9
+ - Fix to parenthesize expression with multi-line
10
+ - Reduce mappings to only have position changings
11
+ - Minor changes in development environment
12
+
3
13
  ## v0.8.0
4
14
 
5
15
  - Add `cacheResult` option (enable by default but disable by default for webpack loader)
@@ -174,10 +174,17 @@ function visitNodeAndReplaceIfNeeded(node, parent, sourceFile, program, options,
174
174
  else {
175
175
  return node;
176
176
  }
177
- const result = ts.addSyntheticTrailingComment(newNode, ts.SyntaxKind.MultiLineCommentTrivia, ` ${node.getText(sourceFile)} `);
177
+ const originalSource = node.getText(sourceFile);
178
+ const comment = ` ${originalSource.replace(/\/\*/g, ' *').replace(/\*\//g, '* ')} `;
179
+ let result = ts.addSyntheticTrailingComment(newNode, ts.SyntaxKind.MultiLineCommentTrivia, comment);
180
+ newSource = `${newSource} /*${comment}*/`;
181
+ if (/[\r\n]/m.test(originalSource)) {
182
+ result = ts.factory.createParenthesizedExpression(result);
183
+ newSource = `(${newSource})`;
184
+ }
178
185
  ts.setTextRange(result, node);
179
186
  result[SYMBOL_ORIGINAL_NODE_DATA] = [
180
- node.getText(sourceFile),
187
+ originalSource,
181
188
  newSource,
182
189
  node.pos,
183
190
  node.end,
@@ -288,24 +295,6 @@ function getNameFromElementAccessExpression(node, typeChecker) {
288
295
  }
289
296
  return null;
290
297
  }
291
- function getMemberName(m, tsInstance) {
292
- if (!m || !m.name) {
293
- return '';
294
- }
295
- const name = m.name;
296
- if (tsInstance.isIdentifier(name)) {
297
- return name.escapedText;
298
- }
299
- else if (tsInstance.isPrivateIdentifier(name)) {
300
- return name.escapedText;
301
- }
302
- else if (tsInstance.isStringLiteral(name)) {
303
- return name.text;
304
- }
305
- else {
306
- return '';
307
- }
308
- }
309
298
  function isReadonlyPropertyAccess(a, typeChecker, tsInstance) {
310
299
  const ts = tsInstance;
311
300
  const type = typeChecker.getTypeAtLocation(a.expression);
@@ -316,32 +305,36 @@ function isReadonlyPropertyAccess(a, typeChecker, tsInstance) {
316
305
  return false;
317
306
  }
318
307
  if (type.getFlags() & ts.TypeFlags.Object) {
319
- const dummyTypeNode = typeChecker.typeToTypeNode(type, a, ts.NodeBuilderFlags.NoTruncation);
320
- if (dummyTypeNode && ts.isTypeLiteralNode(dummyTypeNode)) {
321
- for (let i = 0; i < dummyTypeNode.members.length; ++i) {
322
- const m = dummyTypeNode.members[i];
323
- if (m &&
324
- getMemberName(m, ts) === memberName &&
325
- ts.isPropertySignature(m)) {
326
- if (m.modifiers?.some((m) => m.kind === ts.SyntaxKind.ReadonlyKeyword)) {
327
- return true;
328
- }
308
+ const prop = type.getProperty(memberName);
309
+ if (prop) {
310
+ // Use internal but exported function to improve memory performance
311
+ if ('getCheckFlags' in ts &&
312
+ 'CheckFlags' in ts &&
313
+ ts.CheckFlags.Readonly != null) {
314
+ const checkFlags = ts.getCheckFlags(prop);
315
+ if (checkFlags & ts.CheckFlags.Readonly) {
316
+ return true;
329
317
  }
330
318
  }
331
- }
332
- const prop = type.getProperty(memberName);
333
- if (prop && prop.declarations && prop.declarations.length > 0) {
334
- const decl = prop.declarations[0];
335
- if (ts.isPropertySignature(decl) &&
336
- decl.modifiers?.some((m) => m.kind === ts.SyntaxKind.ReadonlyKeyword)) {
337
- return true;
319
+ if ('getDeclarationModifierFlagsFromSymbol' in ts) {
320
+ const modifierFlags = ts.getDeclarationModifierFlagsFromSymbol(prop);
321
+ if (modifierFlags & ts.ModifierFlags.Readonly) {
322
+ return true;
323
+ }
338
324
  }
339
- if (ts.isVariableDeclaration(decl) &&
340
- // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
341
- decl.parent &&
342
- ts.isVariableDeclarationList(decl.parent) &&
343
- decl.parent.flags & ts.NodeFlags.Const) {
344
- return true;
325
+ if (prop.declarations && prop.declarations.length > 0) {
326
+ const decl = prop.declarations[0];
327
+ if (ts.isPropertySignature(decl) &&
328
+ decl.modifiers?.some((m) => m.kind === ts.SyntaxKind.ReadonlyKeyword)) {
329
+ return true;
330
+ }
331
+ if (ts.isVariableDeclaration(decl) &&
332
+ // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
333
+ decl.parent &&
334
+ ts.isVariableDeclarationList(decl.parent) &&
335
+ decl.parent.flags & ts.NodeFlags.Const) {
336
+ return true;
337
+ }
345
338
  }
346
339
  }
347
340
  }
@@ -438,13 +431,7 @@ function printSourceImpl(tsInstance, sourceFile, originalSourceName, mapGenerato
438
431
  function printNode(tsInstance, baseSource, sourceFile, node, posContext, originalSourceName, mapGenerator) {
439
432
  const originalNodeData = node[SYMBOL_ORIGINAL_NODE_DATA];
440
433
  if (originalNodeData) {
441
- let result = originalNodeData[1];
442
- const comments = tsInstance.getSyntheticTrailingComments(node);
443
- if (comments) {
444
- for (const comment of comments) {
445
- result += ` /*${comment.text}*/`;
446
- }
447
- }
434
+ const result = originalNodeData[1];
448
435
  const old = originalNodeData[0];
449
436
  const oldFull = baseSource.substring(originalNodeData[2], originalNodeData[3]);
450
437
  const i = oldFull.lastIndexOf(old);
@@ -452,22 +439,12 @@ function printNode(tsInstance, baseSource, sourceFile, node, posContext, origina
452
439
  const newText = i < 0
453
440
  ? result
454
441
  : oldFull.substring(0, i) + result + oldFull.substring(i + old.length);
455
- if (mapGenerator) {
456
- if (posContext.pos < node.pos) {
457
- addMappingForCurrent();
458
- }
459
- posContext.pos = node.pos;
460
- if (leadingUnchanged > 0) {
461
- addMappingForCurrent();
462
- }
463
- posContext.pos = node.pos + leadingUnchanged;
464
- addMappingForCurrent(old);
465
- }
442
+ posContext.pos = node.pos + leadingUnchanged;
443
+ addMappingForCurrent(old);
466
444
  posContext.diff += result.length - old.length;
467
445
  posContext.pos += old.length;
468
446
  addMappingForCurrent();
469
447
  posContext.pos = node.end;
470
- addMappingForCurrent();
471
448
  return newText;
472
449
  }
473
450
  let output = '';
@@ -479,14 +456,12 @@ function printNode(tsInstance, baseSource, sourceFile, node, posContext, origina
479
456
  if (child.pos > node.pos) {
480
457
  const text = baseSource.substring(node.pos, child.pos);
481
458
  output += text;
482
- addMappingForCurrent();
483
459
  posContext.pos = child.pos;
484
460
  }
485
461
  }
486
462
  else if (child.pos > lastChildPos) {
487
463
  const text = baseSource.substring(lastChildPos, child.pos);
488
464
  output += text;
489
- addMappingForCurrent();
490
465
  posContext.pos = child.pos;
491
466
  }
492
467
  output += printNode(tsInstance, baseSource, sourceFile, child, posContext, originalSourceName, mapGenerator);
@@ -495,29 +470,19 @@ function printNode(tsInstance, baseSource, sourceFile, node, posContext, origina
495
470
  }, void 0);
496
471
  if (!headPrinted) {
497
472
  output = baseSource.substring(node.pos, node.end);
498
- addMappingForCurrent();
499
473
  posContext.pos = node.end;
500
474
  }
501
475
  else if (lastChildPos < node.end) {
502
476
  const text = baseSource.substring(lastChildPos, node.end);
503
477
  output += text;
504
- addMappingForCurrent();
505
478
  posContext.pos = node.end;
506
479
  }
507
- addMappingForCurrent();
508
480
  return output;
509
481
  function addMappingForCurrent(name) {
510
482
  const original = positionToLineAndColumn(sourceFile, posContext.pos, 0);
511
483
  if (original.line !== posContext.lastLine) {
512
484
  posContext.diff = 0;
513
485
  posContext.lastLine = original.line;
514
- if (mapGenerator && original.column > 0) {
515
- mapGenerator.addMapping({
516
- original: { line: original.line, column: 0 },
517
- generated: { line: original.line, column: 0 },
518
- source: originalSourceName,
519
- });
520
- }
521
486
  }
522
487
  if (mapGenerator) {
523
488
  mapGenerator.addMapping({
@@ -1,2 +1,2 @@
1
- declare const _default: "0.8.0";
1
+ declare const _default: "0.8.2";
2
2
  export default _default;
package/dist/version.mjs CHANGED
@@ -1 +1 @@
1
- export default '0.8.0';
1
+ export default '0.8.2';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-const-value-transformer",
3
- "version": "0.8.0",
3
+ "version": "0.8.2",
4
4
  "engines": {
5
5
  "node": ">=20.19.3"
6
6
  },
@@ -90,10 +90,13 @@
90
90
  "version": "node ./tools/updateVersion.mjs ./src/main/version.mts && git add -A ./src/main/version.mts"
91
91
  },
92
92
  "devDependencies": {
93
+ "@babel/preset-typescript": "^7.28.5",
94
+ "@swc/core": "^1.15.10",
93
95
  "@types/jest": "^30.0.0",
94
96
  "@types/node": "~20.19.22",
95
97
  "@typescript-eslint/eslint-plugin": "^8.48.0",
96
98
  "@typescript-eslint/parser": "^8.48.0",
99
+ "babel-loader": "^10.0.0",
97
100
  "eslint": "^9.39.1",
98
101
  "eslint-config-prettier": "^10.1.8",
99
102
  "eslint-plugin-import-x": "^4.16.1",
@@ -103,6 +106,7 @@
103
106
  "memfs": "^4.51.0",
104
107
  "neostandard": "^0.12.2",
105
108
  "prettier": "^3.6.2",
109
+ "swc-loader": "^0.2.7",
106
110
  "ts-jest": "^29.4.5",
107
111
  "ts-loader": "^9.5.4",
108
112
  "ts-patch": "^3.3.0",