@travetto/transformer 3.0.0-rc.21 → 3.0.0-rc.22

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/README.md +48 -4
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -6,14 +6,21 @@
6
6
  **Install: @travetto/transformer**
7
7
  ```bash
8
8
  npm install @travetto/transformer
9
+
10
+ # or
11
+
12
+ yarn add @travetto/transformer
9
13
  ```
10
14
 
11
15
  This module provides support for enhanced AST transformations, and declarative transformer registration, with common patterns to support all the transformers used throughout the framework. Transformations are located by `support/transformer.<name>.ts` as the filename.
12
16
 
13
- The module is primarily aimed at extremely advanced usages for things that cannot be detected at runtime. The [Registry](https://github.com/travetto/travetto/tree/main/module/registry#readme "Patterns and utilities for handling registration of metadata and functionality for run-time use") module already has knowledge of all `class`es and `field`s, and is able to listen to changes there. Many of the modules build upon work by some of the foundational transformers defined in [Registry](https://github.com/travetto/travetto/tree/main/module/registry#readme "Patterns and utilities for handling registration of metadata and functionality for run-time use"), [Schema](https://github.com/travetto/travetto/tree/main/module/schema#readme "Data type registry for runtime validation, reflection and binding.") and [Dependency Injection](https://github.com/travetto/travetto/tree/main/module/di#readme "Dependency registration/management and injection support."). These all center around defining a registry of classes, and associated type information.
17
+ The module is primarily aimed at extremely advanced usages for things that cannot be detected at runtime. The [Registry](https://github.com/travetto/travetto/tree/main/module/registry#readme "Patterns and utilities for handling registration of metadata and functionality for run-time use") module already has knowledge of all `class`es and `field`s, and is able to listen to changes there. Many of the modules build upon work by some of the foundational transformers defined in [Manifest](https://github.com/travetto/travetto/tree/main/module/manifest#readme "Support for project indexing, manifesting, along with file watching"), [Registry](https://github.com/travetto/travetto/tree/main/module/registry#readme "Patterns and utilities for handling registration of metadata and functionality for run-time use"), [Schema](https://github.com/travetto/travetto/tree/main/module/schema#readme "Data type registry for runtime validation, reflection and binding.") and [Dependency Injection](https://github.com/travetto/travetto/tree/main/module/di#readme "Dependency registration/management and injection support."). These all center around defining a registry of classes, and associated type information.
14
18
 
15
19
  Because working with the [Typescript](https://typescriptlang.org) API can be delicate (and open to breaking changes), creating new transformers should be done cautiously.
16
20
 
21
+ ## Monorepos and Idempotency
22
+ Within the framework, any build or compile step will target the entire workspace, and for mono-repo projects, will include all modules. The optimization this provides is great, but comes with a strict requirement that all compilation processes need to be idempotent. This means that compiling a module directly, versus as a dependency should always produce the same output. This produces a requirement that all transformers are opt-in by the source code, and which transformers are needed in a file should be code-evident. This also means that no transformers are optional, as that could produce different output depending on the dependency graph for a given module.
23
+
17
24
  ## Custom Transformer
18
25
 
19
26
  Below is an example of a transformer that upper cases all `class`, `method` and `param` declarations. This will break any code that depends upon it as we are redefining all the identifiers at compile time.
@@ -28,7 +35,7 @@ export class MakeUpper {
28
35
 
29
36
  @OnProperty()
30
37
  static handleProperty(state: TransformerState, node: ts.PropertyDeclaration): ts.PropertyDeclaration {
31
- if (!state.file.includes('doc/src')) {
38
+ if (!state.importName.startsWith('@travetto/transformer/doc/upper')) {
32
39
  return node;
33
40
  }
34
41
  return state.factory.updatePropertyDeclaration(
@@ -43,7 +50,7 @@ export class MakeUpper {
43
50
 
44
51
  @OnClass()
45
52
  static handleClass(state: TransformerState, node: ts.ClassDeclaration): ts.ClassDeclaration {
46
- if (!state.file.includes('doc/src')) {
53
+ if (!state.importName.startsWith('@travetto/transformer/doc/upper')) {
47
54
  return node;
48
55
  }
49
56
  return state.factory.updateClassDeclaration(
@@ -58,7 +65,7 @@ export class MakeUpper {
58
65
 
59
66
  @OnMethod()
60
67
  static handleMethod(state: TransformerState, node: ts.MethodDeclaration): ts.MethodDeclaration {
61
- if (!state.file.includes('doc/src')) {
68
+ if (!state.importName.startsWith('@travetto/transformer/doc/upper')) {
62
69
  return node;
63
70
  }
64
71
  return state.factory.updateMethodDeclaration(
@@ -77,3 +84,40 @@ export class MakeUpper {
77
84
  ```
78
85
 
79
86
  **Note**: This should be a strong indicator that it is very easy to break code in unexpected ways.
87
+
88
+ **Code: Sample Input**
89
+ ```typescript
90
+ export class Test {
91
+ name: string;
92
+ age: number;
93
+ dob: Date;
94
+
95
+ computeAge(): void {
96
+ this['age'] = (Date.now() - this.dob.getTime());
97
+ }
98
+ }
99
+ ```
100
+
101
+ **Code: Sample Output**
102
+ ```javascript
103
+ "use strict";
104
+ Object.defineProperty(exports, "__esModule", { value: true });
105
+ exports.TEST = void 0;
106
+ const tslib_1 = require("tslib");
107
+ const Ⲑ_root_index_1 = tslib_1.__importStar(require("@travetto/manifest/src/root-index.js"));
108
+ const Ⲑ_decorator_1 = tslib_1.__importStar(require("@travetto/registry/src/decorator.js"));
109
+ var ᚕf = "@travetto/transformer/doc/upper.js";
110
+ let TEST = class TEST {
111
+ static Ⲑinit = Ⲑ_root_index_1.RootIndex.registerFunction(TEST, ᚕf, 649563175, { COMPUTEAGE: { hash: 1286718349 } }, false, false);
112
+ NAME;
113
+ AGE;
114
+ DOB;
115
+ COMPUTEAGE() {
116
+ this['AGE'] = (Date.now() - this.DOB.getTime());
117
+ }
118
+ };
119
+ TEST = tslib_1.__decorate([
120
+ Ⲑ_decorator_1.Register()
121
+ ], TEST);
122
+ exports.TEST = TEST;
123
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/transformer",
3
- "version": "3.0.0-rc.21",
3
+ "version": "3.0.0-rc.22",
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.0.0-rc.17",
27
+ "@travetto/manifest": "^3.0.0-rc.18",
28
28
  "tslib": "^2.5.0",
29
29
  "typescript": "^4.9.5"
30
30
  },