@travetto/compiler 2.1.4 → 2.2.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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@travetto/compiler",
3
3
  "displayName": "Compiler",
4
- "version": "2.1.4",
4
+ "version": "2.2.2",
5
5
  "description": "Node-integration of Typescript Compiler with advanced functionality for detecting changes in classes and methods.",
6
6
  "keywords": [
7
7
  "tsc",
@@ -29,9 +29,9 @@
29
29
  "directory": "module/compiler"
30
30
  },
31
31
  "dependencies": {
32
- "@travetto/base": "^2.1.3",
33
- "@travetto/transformer": "^2.1.4",
34
- "@travetto/watch": "^2.1.3"
32
+ "@travetto/base": "^2.2.2",
33
+ "@travetto/transformer": "^2.2.2",
34
+ "@travetto/watch": "^2.2.2"
35
35
  },
36
36
  "publishConfig": {
37
37
  "access": "public"
package/src/compiler.ts CHANGED
@@ -43,7 +43,7 @@ class $Compiler {
43
43
  }
44
44
  this.#program = ts.createProgram({
45
45
  rootNames: [...rootFiles],
46
- options: TranspileUtil.compilerOptions as ts.CompilerOptions,
46
+ options: TranspileUtil.compilerOptions,
47
47
  host: this.#host,
48
48
  oldProgram: this.#program
49
49
  });
@@ -55,7 +55,7 @@ class $Compiler {
55
55
  /**
56
56
  * Perform actual transpilation
57
57
  */
58
- #transpile(filename: string, force = false) {
58
+ #transpile(filename: string, force = false): string {
59
59
  if (force || !AppCache.hasEntry(filename)) {
60
60
  console.debug('Emitting', { filename: filename.replace(PathUtil.cwd, '.') });
61
61
 
@@ -70,8 +70,11 @@ class $Compiler {
70
70
  this.#transformerManager.getTransformers()
71
71
  );
72
72
 
73
- TranspileUtil.checkTranspileErrors(filename, result.diagnostics as []);
73
+ TranspileUtil.checkTranspileErrors(filename, result.diagnostics);
74
74
  } catch (err) {
75
+ if (!(err instanceof Error)) {
76
+ throw err;
77
+ }
75
78
  const errContent = TranspileUtil.transpileError(filename, err);
76
79
  this.#host.contents.set(filename, errContent);
77
80
  }
@@ -87,14 +90,14 @@ class $Compiler {
87
90
  * Get program
88
91
  * @private
89
92
  */
90
- getProgram() {
93
+ getProgram(): ts.Program {
91
94
  return this.#getProgram();
92
95
  }
93
96
 
94
97
  /**
95
98
  * Initialize the compiler
96
99
  */
97
- async init() {
100
+ async init(): Promise<void> {
98
101
  if (this.active) {
99
102
  return;
100
103
  }
@@ -110,7 +113,7 @@ class $Compiler {
110
113
 
111
114
  ModuleManager.onUnload((f, unlink) => this.#host.unload(f, unlink)); // Remove source
112
115
 
113
- // Update source map support to read from tranpsiler cache
116
+ // Update source map support to read from transpiler cache
114
117
  sourceMapSupport.install({
115
118
  retrieveFile: p => this.#host.contents.get(PathUtil.toUnixTs(p))!
116
119
  });
@@ -121,7 +124,7 @@ class $Compiler {
121
124
  /**
122
125
  * Reset the compiler
123
126
  */
124
- reset() {
127
+ reset(): void {
125
128
  if (!EnvUtil.isReadonly()) {
126
129
  this.#transformerManager.reset();
127
130
  this.#host.reset();
@@ -135,7 +138,7 @@ class $Compiler {
135
138
  /**
136
139
  * Notify of an add/remove/change event
137
140
  */
138
- notify(type: EventType, filename: string) {
141
+ notify(type: EventType, filename: string): void {
139
142
  console.debug('File Event', { type, filename: filename.replace(PathUtil.cwd, '.') });
140
143
  this.#emitter.emit(type, filename);
141
144
  }
@@ -143,7 +146,7 @@ class $Compiler {
143
146
  /**
144
147
  * Listen for events
145
148
  */
146
- on(type: EventType, handler: FileListener) {
149
+ on(type: EventType, handler: FileListener): this {
147
150
  this.#emitter.on(type, handler);
148
151
  return this;
149
152
  }
@@ -151,7 +154,7 @@ class $Compiler {
151
154
  /**
152
155
  * Unload if file is known
153
156
  */
154
- added(filename: string) {
157
+ added(filename: string): void {
155
158
  if (filename in require.cache) { // if already loaded
156
159
  ModuleManager.unload(filename);
157
160
  }
@@ -163,7 +166,7 @@ class $Compiler {
163
166
  /**
164
167
  * Handle when a file is removed during watch
165
168
  */
166
- removed(filename: string) {
169
+ removed(filename: string): void {
167
170
  ModuleManager.unload(filename, true);
168
171
  this.notify('removed', filename);
169
172
  }
@@ -171,7 +174,7 @@ class $Compiler {
171
174
  /**
172
175
  * When a file changes during watch
173
176
  */
174
- changed(filename: string) {
177
+ changed(filename: string): void {
175
178
  if (this.#host.hashChanged(filename)) {
176
179
  ModuleManager.unload(filename);
177
180
  // Load Synchronously
package/src/host.ts CHANGED
@@ -19,24 +19,24 @@ export class SourceHost implements ts.CompilerHost {
19
19
  #sources = new Map<string, ts.SourceFile>();
20
20
  readonly contents = new Map<string, string>();
21
21
 
22
- #trackFile(filename: string, content: string) {
22
+ #trackFile(filename: string, content: string): void {
23
23
  this.contents.set(filename, content);
24
24
  this.#hashes.set(filename, SystemUtil.naiveHash(readFileSync(filename, 'utf8'))); // Get og content for hashing
25
25
  }
26
26
 
27
- getCanonicalFileName = (f: string) => f;
28
- getCurrentDirectory = () => PathUtil.cwd;
29
- getDefaultLibFileName = (opts: ts.CompilerOptions) => ts.getDefaultLibFileName(opts);
30
- getNewLine = () => ts.sys.newLine;
31
- useCaseSensitiveFileNames = () => ts.sys.useCaseSensitiveFileNames;
32
- getDefaultLibLocation() {
33
- return path.dirname(ts.getDefaultLibFilePath(TranspileUtil.compilerOptions as ts.CompilerOptions));
27
+ getCanonicalFileName: (file: string) => string = (f: string) => f;
28
+ getCurrentDirectory: () => string = () => PathUtil.cwd;
29
+ getDefaultLibFileName: (opts: ts.CompilerOptions) => string = (opts: ts.CompilerOptions) => ts.getDefaultLibFileName(opts);
30
+ getNewLine: () => string = () => ts.sys.newLine;
31
+ useCaseSensitiveFileNames: () => boolean = () => ts.sys.useCaseSensitiveFileNames;
32
+ getDefaultLibLocation(): string {
33
+ return path.dirname(ts.getDefaultLibFilePath(TranspileUtil.compilerOptions));
34
34
  }
35
35
 
36
36
  /**
37
37
  * Get root files
38
38
  */
39
- getRootFiles() {
39
+ getRootFiles(): Set<string> {
40
40
  if (!this.#rootFiles.size) {
41
41
  // Only needed for compilation
42
42
  this.#rootFiles = new Set(SourceIndex.findByFolders(AppManifest.source, 'required').map(x => x.file));
@@ -47,7 +47,7 @@ export class SourceHost implements ts.CompilerHost {
47
47
  /**
48
48
  * Read file from disk, using the transpile pre-processor on .ts files
49
49
  */
50
- readFile(filename: string) {
50
+ readFile(filename: string): string {
51
51
  filename = PathUtil.toUnixTs(filename);
52
52
  let content = ts.sys.readFile(filename);
53
53
  if (content === undefined) {
@@ -62,7 +62,7 @@ export class SourceHost implements ts.CompilerHost {
62
62
  /**
63
63
  * Write file to disk, and set value in cache as well
64
64
  */
65
- writeFile(filename: string, content: string) {
65
+ writeFile(filename: string, content: string): void {
66
66
  filename = PathUtil.toUnixTs(filename);
67
67
  this.#trackFile(filename, content);
68
68
  AppCache.writeEntry(filename, content);
@@ -71,7 +71,7 @@ export class SourceHost implements ts.CompilerHost {
71
71
  /**
72
72
  * Fetch file
73
73
  */
74
- fetchFile(filename: string) {
74
+ fetchFile(filename: string): void {
75
75
  filename = PathUtil.toUnixTs(filename);
76
76
  const cached = AppCache.readEntry(filename);
77
77
  this.#trackFile(filename, cached);
@@ -81,20 +81,21 @@ export class SourceHost implements ts.CompilerHost {
81
81
  * Get a source file on demand
82
82
  * @returns
83
83
  */
84
- getSourceFile(filename: string, __tgt: unknown, __onErr: unknown, force?: boolean) {
84
+ getSourceFile(filename: string, __tgt: unknown, __onErr: unknown, force?: boolean): ts.SourceFile {
85
85
  if (!this.#sources.has(filename) || force) {
86
86
  const content = this.readFile(filename)!;
87
87
  this.#sources.set(filename, ts.createSourceFile(filename, content ?? '',
88
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
88
89
  (TranspileUtil.compilerOptions as ts.CompilerOptions).target!
89
90
  ));
90
91
  }
91
- return this.#sources.get(filename);
92
+ return this.#sources.get(filename)!;
92
93
  }
93
94
 
94
95
  /**
95
96
  * See if a file exists
96
97
  */
97
- fileExists(filename: string) {
98
+ fileExists(filename: string): boolean {
98
99
  filename = PathUtil.toUnixTs(filename);
99
100
  return this.contents.has(filename) || ts.sys.fileExists(filename);
100
101
  }
@@ -102,7 +103,7 @@ export class SourceHost implements ts.CompilerHost {
102
103
  /**
103
104
  * See if a file's hash code has changed
104
105
  */
105
- hashChanged(filename: string, content?: string) {
106
+ hashChanged(filename: string, content?: string): boolean {
106
107
  content ??= readFileSync(filename, 'utf8');
107
108
  // Let's see if they are really different
108
109
  const hash = SystemUtil.naiveHash(content);
@@ -116,7 +117,7 @@ export class SourceHost implements ts.CompilerHost {
116
117
  /**
117
118
  * Unload a file from the transpiler
118
119
  */
119
- unload(filename: string, unlink = true) {
120
+ unload(filename: string, unlink = true): void {
120
121
  if (this.contents.has(filename)) {
121
122
  AppCache.removeExpiredEntry(filename, unlink);
122
123
 
@@ -132,7 +133,7 @@ export class SourceHost implements ts.CompilerHost {
132
133
  /**
133
134
  * Reset the transpiler
134
135
  */
135
- reset() {
136
+ reset(): void {
136
137
  this.contents.clear();
137
138
  this.#rootFiles.clear();
138
139
  this.#hashes.clear();
@@ -6,18 +6,21 @@ import {
6
6
  NodeTransformer, VisitorFactory, TransformerState, getAllTransformers
7
7
  } from '@travetto/transformer'; // Narrow import to minimize scope
8
8
 
9
+ type TransformerList = { before: ts.TransformerFactory<ts.SourceFile>[] };
10
+
11
+
9
12
  /**
10
13
  * Manages the typescript transformers
11
14
  */
12
15
  export class TransformerManager {
13
16
 
14
- #cached: { before: ts.TransformerFactory<ts.SourceFile>[] } | undefined;
17
+ #cached: TransformerList | undefined;
15
18
  #transformers: NodeTransformer<TransformerState>[] = [];
16
19
 
17
20
  /**
18
21
  * Read all transformers from disk under the pattern support/transformer.*
19
22
  */
20
- async init() {
23
+ async init(): Promise<void> {
21
24
  if (this.#cached) {
22
25
  return;
23
26
  }
@@ -43,7 +46,7 @@ export class TransformerManager {
43
46
  // Prepare a new visitor factory with a given type checker
44
47
  }
45
48
 
46
- build(checker: ts.TypeChecker) {
49
+ build(checker: ts.TypeChecker): void {
47
50
  const visitor = new VisitorFactory(
48
51
  (ctx, src) => new TransformerState(src, ctx.factory, checker),
49
52
  this.#transformers
@@ -58,14 +61,14 @@ export class TransformerManager {
58
61
  /**
59
62
  * Get typescript transformer object
60
63
  */
61
- getTransformers() {
64
+ getTransformers(): TransformerList | undefined {
62
65
  return this.#cached!;
63
66
  }
64
67
 
65
68
  /**
66
69
  * Reset state
67
70
  */
68
- reset() {
71
+ reset(): void {
69
72
  this.#transformers = [];
70
73
  this.#cached = undefined;
71
74
  }
@@ -11,7 +11,7 @@ import { Compiler } from '../src/compiler';
11
11
  /**
12
12
  * Wraps the compiler supporting real-time changes to files
13
13
  */
14
- export function init($Compiler: Class<typeof Compiler>) {
14
+ export function init($Compiler: Class<typeof Compiler>): typeof $Compiler {
15
15
  /**
16
16
  * Extending the $Compiler class to add some functionality
17
17
  */
@@ -62,7 +62,7 @@ export function init($Compiler: Class<typeof Compiler>) {
62
62
  });
63
63
  }
64
64
 
65
- reset() {
65
+ reset(): void {
66
66
  super.reset();
67
67
  this.#modules.clear();
68
68
  }
@@ -5,7 +5,7 @@ export const init = {
5
5
  key: '@trv:compiler/init',
6
6
  after: ['@trv:base/init'],
7
7
  before: ['@trv:base/transpile'],
8
- action: async () => {
8
+ action: async (): Promise<void> => {
9
9
  // Overrides the require behavior
10
10
  const { Compiler } = await import('../src/compiler');
11
11
  await Compiler.init();
@@ -3,7 +3,7 @@
3
3
  */
4
4
  export const init = {
5
5
  key: '@trv:compiler/reset',
6
- action: async () => {
6
+ action: async (): Promise<void> => {
7
7
  const { Compiler } = await import('../src/compiler');
8
8
  Compiler.reset();
9
9
  }