@travetto/transformer 3.4.0-rc.2 → 3.4.1

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2020 ArcSine Technologies
3
+ Copyright (c) 2023 ArcSine Technologies
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/transformer",
3
- "version": "3.4.0-rc.2",
3
+ "version": "3.4.1",
4
4
  "description": "Functionality for AST transformations, with transformer registration, and general utils",
5
5
  "keywords": [
6
6
  "typescript",
@@ -24,7 +24,7 @@
24
24
  "directory": "module/transformer"
25
25
  },
26
26
  "dependencies": {
27
- "@travetto/manifest": "^3.4.0-rc.3",
27
+ "@travetto/manifest": "^3.4.0",
28
28
  "tslib": "^2.6.2",
29
29
  "typescript": "^5.2.2"
30
30
  },
package/src/state.ts CHANGED
@@ -296,28 +296,43 @@ export class TransformerState implements State {
296
296
  */
297
297
  generateUniqueIdentifier(node: ts.Node, type: AnyType): string {
298
298
  let unique: string | undefined;
299
+ let name = type.name && !type.name.startsWith('_') ? type.name : '';
300
+ if (!name && hasEscapedName(node)) {
301
+ name = `${node.name.escapedText}`;
302
+ }
303
+ name ||= 'Shape';
304
+
299
305
  try {
300
306
  // Tie to source location if possible
301
307
  const tgt = DeclarationUtil.getPrimaryDeclarationNode(type.original!);
302
308
  const fileName = tgt.getSourceFile().fileName;
303
309
 
304
310
  if (fileName === this.source.fileName) { // if in same file suffix with location
305
- unique = `${ts.getLineAndCharacterOfPosition(tgt.getSourceFile(), tgt.getStart()).line}_${tgt.getEnd() - tgt.getStart()}`;
311
+ const route: string[] = [];
312
+ let child = node;
313
+ while (child && !ts.isSourceFile(child)) {
314
+ if (ts.isFunctionDeclaration(child) || ts.isMethodDeclaration(child) || ts.isClassDeclaration(child)) {
315
+ if (child.name) {
316
+ route.push(child.name.getText());
317
+ }
318
+ }
319
+ child = child.parent;
320
+ }
321
+ if (!route.length) { // Only filename
322
+ route.push(ts.getLineAndCharacterOfPosition(tgt.getSourceFile(), tgt.getStart()).line.toString());
323
+ }
324
+ route.unshift(fileName);
325
+ unique = SystemUtil.naiveHashString(route.join(':'), 12);
306
326
  } else {
307
327
  // Otherwise treat it as external and add nothing to it
308
328
  }
309
329
  } catch {
310
330
  // Determine type unique ident
311
331
  const imp = this.#resolver.getFileImportName(this.source.fileName);
312
- unique = `${SystemUtil.naiveHash(`${imp}${type.name ?? 'unknown'}`)}`;
332
+ unique = SystemUtil.naiveHashString(`${imp}${type.name ?? 'unknown'}`, 12);
313
333
  }
314
334
  // Otherwise read name with uuid
315
- let name = type.name && !type.name.startsWith('_') ? type.name : '';
316
- if (!name && hasEscapedName(node)) {
317
- name = `${node.name.escapedText}`;
318
- }
319
- name ||= 'Shape';
320
- return unique ? `${name}_${unique}` : name;
335
+ return unique ? `${name}__${unique}` : name;
321
336
  }
322
337
 
323
338
  /**
@@ -28,4 +28,8 @@ export class SystemUtil {
28
28
 
29
29
  return Math.abs(hash);
30
30
  }
31
+
32
+ static naiveHashString(text: string, length: number): string {
33
+ return this.naiveHash(text).toString().padStart(length, '0').substring(0, length);
34
+ }
31
35
  }