@travetto/transformer 3.0.2 → 3.1.0-rc.0

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/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  <!-- This file was generated by @travetto/doc and should not be modified directly -->
2
- <!-- Please modify https://github.com/travetto/travetto/tree/main/module/transformer/DOC.ts and execute "npx trv doc" to rebuild -->
2
+ <!-- Please modify https://github.com/travetto/travetto/tree/main/module/transformer/DOC.tsx and execute "npx trv doc" to rebuild -->
3
3
  # Transformation
4
+
4
5
  ## Functionality for AST transformations, with transformer registration, and general utils
5
6
 
6
7
  **Install: @travetto/transformer**
@@ -14,16 +15,15 @@ yarn add @travetto/transformer
14
15
 
15
16
  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.
16
17
 
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.
18
+ 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.
18
19
 
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.
20
+ Because working with the [Typescript](https://typescriptlang.org) API can be delicate (and open to breaking changes), creating new transformers should be done cautiously.
20
21
 
21
22
  ## Monorepos and Idempotency
22
23
  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
24
 
24
25
  ## Custom Transformer
25
-
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.
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.
27
27
 
28
28
  **Code: Sample Transformer - Upper case all declarations**
29
29
  ```typescript
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/transformer",
3
- "version": "3.0.2",
3
+ "version": "3.1.0-rc.0",
4
4
  "description": "Functionality for AST transformations, with transformer registration, and general utils",
5
5
  "keywords": [
6
6
  "typescript",
@@ -24,9 +24,9 @@
24
24
  "directory": "module/transformer"
25
25
  },
26
26
  "dependencies": {
27
- "@travetto/manifest": "^3.0.2",
27
+ "@travetto/manifest": "^3.1.0-rc.0",
28
28
  "tslib": "^2.5.0",
29
- "typescript": "^4.9.5"
29
+ "typescript": "^5.0.2"
30
30
  },
31
31
  "travetto": {
32
32
  "displayName": "Transformation",
@@ -1,6 +1,6 @@
1
1
  import ts from 'typescript';
2
2
 
3
- import { ManifestIndex, path } from '@travetto/manifest';
3
+ import { ManifestIndex, ManifestModuleUtil, path } from '@travetto/manifest';
4
4
 
5
5
  import type { AnyType, TransformResolver } from './types';
6
6
  import { TypeCategorize, TypeBuilder } from './builder';
@@ -34,16 +34,18 @@ export class SimpleResolver implements TransformResolver {
34
34
  getFileImportName(file: string, removeExt?: boolean): string {
35
35
  let sourceFile = path.toPosix(file);
36
36
 
37
- if (!sourceFile.endsWith('.js') && !sourceFile.endsWith('.ts')) {
37
+ const type = ManifestModuleUtil.getFileType(file);
38
+
39
+ if (type !== 'js' && type !== 'ts') {
38
40
  sourceFile = `${sourceFile}.ts`;
39
41
  }
40
42
 
41
43
  const imp =
42
- this.#manifestIndex.getEntry(/[.]ts$/.test(sourceFile) ? sourceFile : `${sourceFile}.js`)?.import ??
43
- this.#manifestIndex.getFromImport(sourceFile.replace(/^.*node_modules\//, '').replace(/[.]ts$/, ''))?.import ??
44
+ this.#manifestIndex.getEntry(ManifestModuleUtil.getFileType(sourceFile) === 'ts' ? sourceFile : `${sourceFile}.js`)?.import ??
45
+ this.#manifestIndex.getFromImport(ManifestModuleUtil.sourceToBlankExt(sourceFile).replace(/^.*node_modules\//, ''))?.import ??
44
46
  file;
45
47
 
46
- return removeExt ? imp.replace(/[.]js$/, '') : imp;
48
+ return removeExt ? ManifestModuleUtil.sourceToBlankExt(imp) : imp;
47
49
  }
48
50
 
49
51
  /**
package/src/util/doc.ts CHANGED
@@ -12,7 +12,7 @@ export class DocUtil {
12
12
  * See if node has js docs
13
13
  */
14
14
  static hasJSDoc(o: ts.Node): o is (ts.Node & { jsDoc: ts.JSDoc[] }) {
15
- return 'jsDoc' in o;
15
+ return 'jsDoc' in o && o.jsDoc !== null && o.jsDoc !== undefined && Array.isArray(o.jsDoc) && o.jsDoc.length > 0;
16
16
  }
17
17
 
18
18
  /**
@@ -1,6 +1,6 @@
1
1
  import ts from 'typescript';
2
2
 
3
- import { PackageUtil, path } from '@travetto/manifest';
3
+ import { ManifestModuleUtil, PackageUtil, path } from '@travetto/manifest';
4
4
 
5
5
  import { Import } from '../types/shared';
6
6
 
@@ -12,7 +12,7 @@ export class ImportUtil {
12
12
  * useful for handling failed imports, but still transpiling
13
13
  */
14
14
  static optionalResolve(file: string, base?: string): string {
15
- if (base?.endsWith('.ts')) {
15
+ if (base && ManifestModuleUtil.getFileType(base) === 'ts') {
16
16
  base = path.dirname(base);
17
17
  }
18
18
  if (base && file.startsWith('.')) {
package/src/visitor.ts CHANGED
@@ -2,6 +2,7 @@ import ts from 'typescript';
2
2
 
3
3
  import { DecoratorMeta, TransformerType, NodeTransformer, TransformerSet, State, TransformPhase } from './types/visitor';
4
4
  import { CoreUtil } from './util/core';
5
+ import { ManifestModuleUtil } from '@travetto/manifest';
5
6
 
6
7
  /**
7
8
  * AST Visitor Factory, combines all active transformers into a single pass transformer for the ts compiler
@@ -77,7 +78,8 @@ export class VisitorFactory<S extends State = State> {
77
78
  */
78
79
  visitor(): ts.TransformerFactory<ts.SourceFile> {
79
80
  return (context: ts.TransformationContext) => (file: ts.SourceFile): ts.SourceFile => {
80
- if (!file.fileName.endsWith('.ts')) { // Skip all non-ts files
81
+ const type = ManifestModuleUtil.getFileType(file.fileName);
82
+ if (type !== 'ts') { // Skip all non-ts files
81
83
  return file;
82
84
  }
83
85